Debian Codegraph @

tests/phpunit/unit/includes/title/TitleValueTest.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 * @author Daniel Kinzler
20 */
21
22 use MediaWiki\Page\PageIdentity;
23 use MediaWiki\Page\PageReference;
24 use MediaWiki\Page\PageReferenceValue;
25 use MediaWiki\Title\Title;
26 use MediaWiki\Title\TitleValue;
27
28 /**
29 * @covers \MediaWiki\Title\TitleValue
30 *
31 * @group Title
32 */
33 class TitleValueTest extends \MediaWikiUnitTestCase {
34
35 public static function goodConstructorProvider() {
36 return [
37 [ NS_MAIN, '', 'fragment', '', true, false ],
38 [ NS_MAIN, '', '', 'interwiki', false, true ],
39 [ NS_MAIN, '', 'fragment', 'interwiki', true, true ],
40 [ NS_USER, 'TestThis', 'stuff', '', true, false ],
41 [ NS_USER, 'TestThis', '', 'baz', false, true ],
42 [ NS_MAIN, 'foo bar', '', '', false, false ],
43 [ NS_MAIN, 'foo_bar', '', '', false, false ],
44 [ NS_MAIN, '', '', '', false, false ],
45 ];
46 }
47
48 /**
49 * @dataProvider goodConstructorProvider
50 */
51 public function testConstruction( $ns, $text, $fragment, $interwiki, $hasFragment,
52 $hasInterwiki
53 ) {
54 $title = new TitleValue( $ns, $text, $fragment, $interwiki );
55
56 $this->assertEquals( $ns, $title->getNamespace() );
57 $this->assertTrue( $title->inNamespace( $ns ) );
58 $this->assertEquals( strtr( $text, ' ', '_' ), $title->getDBkey() );
59 $this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() );
60 $this->assertEquals( $fragment, $title->getFragment() );
61 $this->assertEquals( $hasFragment, $title->hasFragment() );
62 $this->assertEquals( $interwiki, $title->getInterwiki() );
63 $this->assertEquals( $hasInterwiki, $title->isExternal() );
64 }
65
66 /**
67 * @dataProvider goodConstructorProvider
68 */
69 public function testTryNew( $ns, $text, $fragment, $interwiki, $hasFragment,
70 $hasInterwiki
71 ) {
72 $title = TitleValue::tryNew( $ns, $text, $fragment, $interwiki );
73
74 $this->assertEquals( $ns, $title->getNamespace() );
75 $this->assertTrue( $title->inNamespace( $ns ) );
76 $this->assertEquals( strtr( $text, ' ', '_' ), $title->getDBkey() );
77 $this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() );
78 $this->assertEquals( $fragment, $title->getFragment() );
79 $this->assertEquals( $hasFragment, $title->hasFragment() );
80 $this->assertEquals( $interwiki, $title->getInterwiki() );
81 $this->assertEquals( $hasInterwiki, $title->isExternal() );
82 }
83
84 /**
85 * @dataProvider goodConstructorProvider
86 */
87 public function testAssertValidSpec( $ns, $text, $fragment, $interwiki ) {
88 TitleValue::assertValidSpec( $ns, $text, $fragment, $interwiki );
89 $this->assertTrue( true ); // we are just checking that no exception is thrown
90 }
91
92 public static function badConstructorNamespaceTypeProvider() {
93 return [
94 [ 'foo', 'title', 'fragment', '' ],
95 [ null, 'title', 'fragment', '' ],
96 [ 2.3, 'title', 'fragment', '' ],
97 ];
98 }
99
100 public static function badConstructorProvider() {
101 return [
102 [ NS_MAIN, 5, 'fragment', '' ],
103 [ NS_MAIN, null, 'fragment', '' ],
104 [ NS_USER, '', 'fragment', '' ],
105 [ NS_USER, '', '', 'interwiki' ],
106 [ NS_MAIN, 'bar_', '', '' ],
107 [ NS_MAIN, '_foo', '', '' ],
108 [ NS_MAIN, ' eek ', '', '' ],
109
110 [ NS_MAIN, 'title', 5, '' ],
111 [ NS_MAIN, 'title', null, '' ],
112 [ NS_MAIN, 'title', [], '' ],
113
114 [ NS_MAIN, 'title', '', 5 ],
115 [ NS_MAIN, 'title', null, 5 ],
116 [ NS_MAIN, 'title', [], 5 ],
117 ];
118 }
119
120 /**
121 * @dataProvider badConstructorNamespaceTypeProvider
122 * @dataProvider badConstructorProvider
123 */
124 public function testConstructionErrors( $ns, $text, $fragment, $interwiki ) {
125 $this->expectException( InvalidArgumentException::class );
126 new TitleValue( $ns, $text, $fragment, $interwiki );
127 }
128
129 /**
130 * @dataProvider badConstructorNamespaceTypeProvider
131 */
132 public function testTryNewErrors( $ns, $text, $fragment, $interwiki ) {
133 $this->expectException( InvalidArgumentException::class );
134 TitleValue::tryNew( $ns, $text, $fragment, $interwiki );
135 }
136
137 /**
138 * @dataProvider badConstructorProvider
139 */
140 public function testTryNewFailure( $ns, $text, $fragment, $interwiki ) {
141 $this->assertNull( TitleValue::tryNew( $ns, $text, $fragment, $interwiki ) );
142 }
143
144 /**
145 * @dataProvider badConstructorProvider
146 */
147 public function testAssertValidSpecErrors( $ns, $text, $fragment, $interwiki ) {
148 $this->expectException( InvalidArgumentException::class );
149 TitleValue::assertValidSpec( $ns, $text, $fragment, $interwiki );
150 }
151
152 public static function fragmentTitleProvider() {
153 return [
154 [ new TitleValue( NS_MAIN, 'Test' ), 'foo' ],
155 [ new TitleValue( NS_TALK, 'Test', 'foo' ), '' ],
156 [ new TitleValue( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
157 ];
158 }
159
160 /**
161 * @dataProvider fragmentTitleProvider
162 */
163 public function testCreateFragmentTitle( TitleValue $title, $fragment ) {
164 $fragmentTitle = $title->createFragmentTarget( $fragment );
165
166 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
167 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
168 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
169 }
170
171 public static function provideNewFromPage() {
172 yield [ new PageReferenceValue( NS_USER, 'Test', PageIdentity::LOCAL ) ];
173 yield [ new PageReferenceValue( NS_USER, 'Test', 'acme' ) ];
174 }
175
176 /**
177 * @dataProvider provideNewFromPage
178 *
179 * @param PageReference $page
180 */
181 public function testNewFromPage( PageReference $page ) {
182 $title = TitleValue::newFromPage( $page );
183
184 $this->assertSame( $page->getNamespace(), $title->getNamespace() );
185 $this->assertSame( $page->getDBkey(), $title->getDBkey() );
186 $this->assertSame( $page->getDBkey(), $title->getText() );
187 $this->assertSame( '', $title->getFragment() );
188 $this->assertSame( '', $title->getInterwiki() );
189 $this->assertFalse( $title->isExternal() );
190 $this->assertFalse( $title->hasFragment() );
191 }
192
193 /**
194 * @dataProvider provideNewFromPage
195 *
196 * @param PageReference $page
197 */
198 public function testCastPageToLinkTarget( PageReference $page ) {
199 $title = TitleValue::castPageToLinkTarget( $page );
200
201 $this->assertSame( $page->getNamespace(), $title->getNamespace() );
202 $this->assertSame( $page->getDBkey(), $title->getDBkey() );
203 $this->assertSame( $page->getDBkey(), $title->getText() );
204 $this->assertSame( '', $title->getFragment() );
205 $this->assertSame( '', $title->getInterwiki() );
206 $this->assertFalse( $title->isExternal() );
207 $this->assertFalse( $title->hasFragment() );
208 }
209
210 public function testCastTitleToLinkTarget() {
211 $page = Title::makeTitle( NS_MAIN, 'Test' );
212 $this->assertSame( $page, TitleValue::castPageToLinkTarget( $page ) );
213 }
214
215 public function testCastNullToLinkTarget() {
216 $this->assertNull( TitleValue::castPageToLinkTarget( null ) );
217 }
218
219 public static function getTextProvider() {
220 return [
221 [ 'Foo', 'Foo' ],
222 [ 'Foo_Bar', 'Foo Bar' ],
223 ];
224 }
225
226 /**
227 * @dataProvider getTextProvider
228 */
229 public function testGetText( $dbkey, $text ) {
230 $title = new TitleValue( NS_MAIN, $dbkey );
231
232 $this->assertEquals( $text, $title->getText() );
233 }
234
235 public static function provideTestToString() {
236 yield [
237 new TitleValue( 0, 'Foo' ),
238 '0:Foo'
239 ];
240 yield [
241 new TitleValue( 1, 'Bar_Baz' ),
242 '1:Bar_Baz'
243 ];
244 yield [
245 new TitleValue( 9, 'JoJo', 'Frag' ),
246 '9:JoJo#Frag'
247 ];
248 yield [
249 new TitleValue( 200, 'tea', 'Fragment', 'wikicode' ),
250 'wikicode:200:tea#Fragment'
251 ];
252 }
253
254 /**
255 * @dataProvider provideTestToString
256 */
257 public function testToString( TitleValue $value, $expected ) {
258 $this->assertSame(
259 $expected,
260 $value->__toString()
261 );
262 }
263
264 public static function provideIsSameLinkAs() {
265 yield [
266 new TitleValue( 0, 'Foo' ),
267 new TitleValue( 0, 'Foo' ),
268 true
269 ];
270 yield [
271 new TitleValue( 1, 'Bar_Baz' ),
272 new TitleValue( 1, 'Bar_Baz' ),
273 true
274 ];
275 yield [
276 new TitleValue( 0, 'Foo' ),
277 new TitleValue( 1, 'Foo' ),
278 false
279 ];
280 yield [
281 new TitleValue( 0, 'Foo' ),
282 new TitleValue( 0, 'Foozz' ),
283 false
284 ];
285 yield [
286 new TitleValue( 0, 'Foo', '' ),
287 new TitleValue( 0, 'Foo', 'Bar' ),
288 false
289 ];
290 yield [
291 new TitleValue( 0, 'Foo', '', 'bar' ),
292 new TitleValue( 0, 'Foo', '', '' ),
293 false
294 ];
295 }
296
297 /**
298 * @dataProvider provideIsSameLinkAs
299 */
300 public function testIsSameLinkAs( TitleValue $a, TitleValue $b, $expected ) {
301 $this->assertSame( $expected, $a->isSameLinkAs( $b ) );
302 $this->assertSame( $expected, $b->isSameLinkAs( $a ) );
303 }
304 }