| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Page\PageIdentityValue; |
| 22 |
use MediaWiki\Page\PageSelectQueryBuilder; |
| 23 |
use MediaWiki\Page\PageStore; |
| 24 |
use MediaWiki\Preferences\MultiTitleFilter; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use MediaWiki\Title\TitleFactory; |
| 27 |
use MediaWiki\Title\TitleFormatter; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class MultiTitleFilterTest extends MediaWikiUnitTestCase { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function testConstructNoArgs() { |
| 39 |
$this->assertInstanceOf( MultiTitleFilter::class, new MultiTitleFilter() ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testConstructTitleFactory() { |
| 46 |
$this->assertInstanceOf( |
| 47 |
MultiTitleFilter::class, |
| 48 |
new MultiTitleFilter( new TitleFactory() ) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function testFilterForForm( $expected, $inputValue, $newFromIDsReturnValue ) { |
| 57 |
$titleFormatter = $this->createMock( TitleFormatter::class ); |
| 58 |
$titleFormatter->method( 'getPrefixedText' ) |
| 59 |
->willReturnOnConsecutiveCalls( |
| 60 |
...array_map( |
| 61 |
static function ( $t ) { |
| 62 |
return $t->getPrefixedText(); |
| 63 |
}, |
| 64 |
$newFromIDsReturnValue |
| 65 |
) |
| 66 |
); |
| 67 |
$pageStore = $this->getPageStore( $newFromIDsReturnValue ); |
| 68 |
$multiTitleFilter = new MultiTitleFilter( null, $pageStore, $titleFormatter ); |
| 69 |
$this->assertSame( $expected, $multiTitleFilter->filterForForm( $inputValue ) ); |
| 70 |
} |
| 71 |
|
| 72 |
private function getPageStore( $mockFetchPageRecordsReturn ): PageStore { |
| 73 |
$pageSelectQueryBuilder = $this->getMockBuilder( PageSelectQueryBuilder::class ) |
| 74 |
->disableOriginalConstructor() |
| 75 |
->onlyMethods( [ 'fetchPageRecords' ] ) |
| 76 |
->getMock(); |
| 77 |
$pageSelectQueryBuilder->method( 'fetchPageRecords' ) |
| 78 |
->willReturn( new ArrayIterator( $mockFetchPageRecordsReturn ) ); |
| 79 |
|
| 80 |
$pageStore = $this->createMock( PageStore::class ); |
| 81 |
$pageStore->method( 'newSelectQueryBuilder' ) |
| 82 |
->willReturn( $pageSelectQueryBuilder ); |
| 83 |
|
| 84 |
return $pageStore; |
| 85 |
} |
| 86 |
|
| 87 |
public function filterForFormDataProvider(): array { |
| 88 |
return [ |
| 89 |
[ |
| 90 |
'', |
| 91 |
'', |
| 92 |
[] |
| 93 |
], |
| 94 |
[ |
| 95 |
'', |
| 96 |
"2\n\3\n\42", |
| 97 |
[] |
| 98 |
], |
| 99 |
[ |
| 100 |
"Foo\nBar", |
| 101 |
"2\n\3\n\42", |
| 102 |
[ |
| 103 |
$this->getMockTitle( 'Foo' ), |
| 104 |
$this->getMockTitle( 'Bar' ) |
| 105 |
] |
| 106 |
] |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function testFilterFromForm( $expected, $titles, $newFromTextValue ) { |
| 115 |
$pageStore = $this->createMock( PageStore::class ); |
| 116 |
$pageStore->method( 'getPageByText' ) |
| 117 |
->willReturnOnConsecutiveCalls( $newFromTextValue ); |
| 118 |
$multiTitleFilter = new MultiTitleFilter( null, $pageStore ); |
| 119 |
$this->assertSame( $expected, $multiTitleFilter->filterFromForm( $titles ) ); |
| 120 |
} |
| 121 |
|
| 122 |
public function filterFromFormDataProvider(): array { |
| 123 |
return [ |
| 124 |
[ |
| 125 |
null, |
| 126 |
'', |
| 127 |
$this->getMockPageIdentityValue( 0, 'Foo' ), |
| 128 |
], |
| 129 |
[ |
| 130 |
"42", |
| 131 |
"Foo", |
| 132 |
$this->getMockPageIdentityValue( 42, 'Foo' ) |
| 133 |
], |
| 134 |
[ |
| 135 |
"", |
| 136 |
"Bar", |
| 137 |
$this->getMockPageIdentityValue( 0, 'Bar' ) |
| 138 |
] |
| 139 |
|
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
private function getMockPageIdentityValue( int $pageId, string $dbKey ) { |
| 144 |
return new PageIdentityValue( $pageId, NS_MAIN, $dbKey, PageIdentityValue::LOCAL ); |
| 145 |
} |
| 146 |
|
| 147 |
private function getMockTitle( $getTextResult, $articleId = 0 ) { |
| 148 |
$title = $this->createMock( Title::class ); |
| 149 |
$title->method( 'getPrefixedText' )->willReturn( $getTextResult ); |
| 150 |
$title->method( 'getArticleID' )->willReturn( $articleId ); |
| 151 |
return $title; |
| 152 |
} |
| 153 |
|
| 154 |
} |