Merge "DefaultSettings: Document wgTmpDirectory guarantees and expectations"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / title / TitleValueTest.php
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 /**
23 * @covers TitleValue
24 *
25 * @group Title
26 */
27 class TitleValueTest extends \MediaWikiUnitTestCase {
28
29 public function goodConstructorProvider() {
30 return [
31 [ NS_MAIN, '', 'fragment', '', true, false ],
32 [ NS_USER, 'TestThis', 'stuff', '', true, false ],
33 [ NS_USER, 'TestThis', '', 'baz', false, true ],
34 [ NS_MAIN, 'foo bar', '', '', false, false ],
35 [ NS_MAIN, 'foo_bar', '', '', false, false ],
36 ];
37 }
38
39 /**
40 * @dataProvider goodConstructorProvider
41 */
42 public function testConstruction( $ns, $text, $fragment, $interwiki, $hasFragment,
43 $hasInterwiki
44 ) {
45 $title = new TitleValue( $ns, $text, $fragment, $interwiki );
46
47 $this->assertEquals( $ns, $title->getNamespace() );
48 $this->assertTrue( $title->inNamespace( $ns ) );
49 $this->assertEquals( strtr( $text, ' ', '_' ), $title->getDbKey() );
50 $this->assertEquals( strtr( $text, '_', ' ' ), $title->getText() );
51 $this->assertEquals( $fragment, $title->getFragment() );
52 $this->assertEquals( $hasFragment, $title->hasFragment() );
53 $this->assertEquals( $interwiki, $title->getInterwiki() );
54 $this->assertEquals( $hasInterwiki, $title->isExternal() );
55 }
56
57 public function badConstructorProvider() {
58 return [
59 [ 'foo', 'title', 'fragment', '' ],
60 [ null, 'title', 'fragment', '' ],
61 [ 2.3, 'title', 'fragment', '' ],
62
63 [ NS_MAIN, 5, 'fragment', '' ],
64 [ NS_MAIN, null, 'fragment', '' ],
65 [ NS_USER, '', 'fragment', '' ],
66 [ NS_MAIN, 'bar_', '', '' ],
67 [ NS_MAIN, '_foo', '', '' ],
68 [ NS_MAIN, ' eek ', '', '' ],
69
70 [ NS_MAIN, 'title', 5, '' ],
71 [ NS_MAIN, 'title', null, '' ],
72 [ NS_MAIN, 'title', [], '' ],
73
74 [ NS_MAIN, 'title', '', 5 ],
75 [ NS_MAIN, 'title', null, 5 ],
76 [ NS_MAIN, 'title', [], 5 ],
77 ];
78 }
79
80 /**
81 * @dataProvider badConstructorProvider
82 */
83 public function testConstructionErrors( $ns, $text, $fragment, $interwiki ) {
84 $this->setExpectedException( InvalidArgumentException::class );
85 new TitleValue( $ns, $text, $fragment, $interwiki );
86 }
87
88 public function fragmentTitleProvider() {
89 return [
90 [ new TitleValue( NS_MAIN, 'Test' ), 'foo' ],
91 [ new TitleValue( NS_TALK, 'Test', 'foo' ), '' ],
92 [ new TitleValue( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
93 ];
94 }
95
96 /**
97 * @dataProvider fragmentTitleProvider
98 */
99 public function testCreateFragmentTitle( TitleValue $title, $fragment ) {
100 $fragmentTitle = $title->createFragmentTarget( $fragment );
101
102 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
103 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
104 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
105 }
106
107 public function getTextProvider() {
108 return [
109 [ 'Foo', 'Foo' ],
110 [ 'Foo_Bar', 'Foo Bar' ],
111 ];
112 }
113
114 /**
115 * @dataProvider getTextProvider
116 */
117 public function testGetText( $dbkey, $text ) {
118 $title = new TitleValue( NS_MAIN, $dbkey );
119
120 $this->assertEquals( $text, $title->getText() );
121 }
122
123 public function provideTestToString() {
124 yield [
125 new TitleValue( 0, 'Foo' ),
126 '0:Foo'
127 ];
128 yield [
129 new TitleValue( 1, 'Bar_Baz' ),
130 '1:Bar_Baz'
131 ];
132 yield [
133 new TitleValue( 9, 'JoJo', 'Frag' ),
134 '9:JoJo#Frag'
135 ];
136 yield [
137 new TitleValue( 200, 'tea', 'Fragment', 'wikicode' ),
138 'wikicode:200:tea#Fragment'
139 ];
140 }
141
142 /**
143 * @dataProvider provideTestToString
144 */
145 public function testToString( TitleValue $value, $expected ) {
146 $this->assertSame(
147 $expected,
148 $value->__toString()
149 );
150 }
151 }