Debian Codegraph @

tests/phpunit/unit/includes/preferences/MultiTitleFilterTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
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 * @group Preferences
31 * @coversDefaultClass \MediaWiki\Preferences\MultiTitleFilter
32 */
33 class MultiTitleFilterTest extends MediaWikiUnitTestCase {
34
35 /**
36 * @covers ::__construct
37 */
38 public function testConstructNoArgs() {
39 $this->assertInstanceOf( MultiTitleFilter::class, new MultiTitleFilter() );
40 }
41
42 /**
43 * @covers ::__construct
44 */
45 public function testConstructTitleFactory() {
46 $this->assertInstanceOf(
47 MultiTitleFilter::class,
48 new MultiTitleFilter( new TitleFactory() )
49 );
50 }
51
52 /**
53 * @covers ::filterForForm
54 * @dataProvider filterForFormDataProvider
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 * @covers ::filterFromForm
112 * @dataProvider filterFromFormDataProvider
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 }