Debian Codegraph @

tests/phpunit/unit/includes/page/PageIdentityValueTest.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\PageIdentityValue;
24 use Wikimedia\Assert\ParameterAssertionException;
25 use Wikimedia\Assert\PreconditionException;
26
27 /**
28 * @covers \MediaWiki\Page\PageIdentityValue
29 *
30 * @group Title
31 */
32 class PageIdentityValueTest extends MediaWikiUnitTestCase {
33
34 public static function goodConstructorProvider() {
35 return [
36 [ 0, NS_MAIN, 'Test', false ],
37 [ 7, NS_MAIN, 'Test', false ],
38 [ 3, NS_USER, 'Test', 'h2g2' ],
39 ];
40 }
41
42 /**
43 * @dataProvider goodConstructorProvider
44 */
45 public function testConstruction( $pageId, $namespace, $dbKey, $wikiId ) {
46 $pageIdentity = new PageIdentityValue( $pageId, $namespace, $dbKey, $wikiId );
47
48 $this->assertSame( $wikiId, $pageIdentity->getWikiId() );
49 $this->assertSame( $pageId, $pageIdentity->getId( $wikiId ) );
50 $this->assertSame( $pageId > 0, $pageIdentity->exists() );
51 $this->assertSame( $namespace, $pageIdentity->getNamespace() );
52 $this->assertSame( $dbKey, $pageIdentity->getDBkey() );
53
54 $this->assertTrue( $pageIdentity->canExist() );
55 }
56
57 /**
58 * @dataProvider goodConstructorProvider
59 */
60 public function testTryNew( $pageId, $namespace, $dbKey, $wikiId ) {
61 $pageIdentity = PageIdentityValue::tryNew( $pageId, $namespace, $dbKey, $wikiId );
62
63 $this->assertSame( $wikiId, $pageIdentity->getWikiId() );
64 $this->assertSame( $pageId, $pageIdentity->getId( $wikiId ) );
65 $this->assertSame( $pageId > 0, $pageIdentity->exists() );
66 $this->assertSame( $namespace, $pageIdentity->getNamespace() );
67 $this->assertSame( $dbKey, $pageIdentity->getDBkey() );
68
69 $this->assertTrue( $pageIdentity->canExist() );
70 }
71
72 public function testGetIdFailsForForeignWiki() {
73 $pageIdentity = new PageIdentityValue( 7, NS_MAIN, 'Foo', 'h2g2' );
74
75 $this->expectException( PreconditionException::class );
76 $pageIdentity->getId();
77 }
78
79 public static function badConstructorProvider() {
80 return [
81 [ -1, NS_MAIN, 'Test', false ],
82 [ 0, NS_MAIN, 'Test', 2.3 ],
83 [ 0, NS_SPECIAL, 'Test', false ],
84 [ 0, NS_USER, '#1234', false ],
85 [ 0, NS_SPECIAL, 'foo|bar', false ],
86 ];
87 }
88
89 /**
90 * @dataProvider badConstructorProvider
91 */
92 public function testConstructionErrors( $pageId, $namespace, $dbKey, $wikiId ) {
93 $this->expectException( ParameterAssertionException::class );
94 new PageIdentityValue( $pageId, $namespace, $dbKey, $wikiId );
95 }
96
97 /**
98 * @dataProvider badConstructorProvider
99 */
100 public function testTryNewWithBadValue( $pageId, $namespace, $dbKey, $wikiId ) {
101 $this->assertNull( PageIdentityValue::tryNew( $pageId, $namespace, $dbKey, $wikiId ) );
102 }
103
104 public static function provideToString() {
105 yield [
106 new PageIdentityValue( 5, 0, 'Foo', PageIdentity::LOCAL ),
107 '[0:Foo]'
108 ];
109 yield [
110 new PageIdentityValue( 0, 1, 'Bar_Baz', PageIdentity::LOCAL ),
111 '[1:Bar_Baz]'
112 ];
113 yield [
114 new PageIdentityValue( 7, 200, 'tea', 'codewiki' ),
115 '[200:tea]@codewiki'
116 ];
117 }
118
119 /**
120 * @dataProvider provideToString
121 */
122 public function testToString( PageIdentityValue $value, $expected ) {
123 $this->assertSame(
124 $expected,
125 $value->__toString()
126 );
127 }
128
129 public static function provideIsSamePageAs() {
130 yield [
131 new PageIdentityValue( 1, 0, 'Foo', PageIdentity::LOCAL ),
132 new PageIdentityValue( 1, 0, 'Foo', PageIdentity::LOCAL ),
133 true
134 ];
135 yield [
136 new PageIdentityValue( 0, 1, 'Bar_Baz', PageIdentity::LOCAL ),
137 new PageIdentityValue( 0, 1, 'Bar_Baz', PageIdentity::LOCAL ),
138 true
139 ];
140 yield [
141 new PageIdentityValue( 0, 0, 'Foo', PageIdentity::LOCAL ),
142 new PageIdentityValue( 0, 0, 'Foozz', PageIdentity::LOCAL ),
143 false
144 ];
145 yield [
146 new PageIdentityValue( 0, 0, 'Foo', PageIdentity::LOCAL ),
147 new PageIdentityValue( 0, 1, 'Foo', PageIdentity::LOCAL ),
148 false
149 ];
150 yield [
151 new PageIdentityValue( 1, 0, 'Foo', '' ),
152 new PageIdentityValue( 1, 0, 'Foo', 'bar' ),
153 false
154 ];
155 yield [
156 new PageIdentityValue( 0, 0, 'Foo', '' ),
157 new PageIdentityValue( 0, 0, 'Foo', 'bar' ),
158 false
159 ];
160 yield [
161 new PageIdentityValue( 3, 0, 'Foo', 'bar' ),
162 new PageIdentityValue( 3, 0, 'Foo', 'bar' ),
163 true
164 ];
165 }
166
167 /**
168 * @dataProvider provideIsSamePageAs
169 */
170 public function testIsSamePageAs( PageIdentityValue $a, PageIdentityValue $b, $expected ) {
171 $this->assertSame( $expected, $a->isSamePageAs( $b ) );
172 $this->assertSame( $expected, $b->isSamePageAs( $a ) );
173 }
174
175 /**
176 * @covers \MediaWiki\Page\PageIdentityValue::localIdentity
177 */
178 public function testLocalIdentity() {
179 $page = PageIdentityValue::localIdentity( 1, NS_MAIN, __METHOD__ );
180 $this->assertSame( 1, $page->getId( PageIdentity::LOCAL ) );
181 $this->assertSame( NS_MAIN, $page->getNamespace() );
182 $this->assertSame( __METHOD__, $page->getDBkey() );
183 $this->assertSame( PageIdentity::LOCAL, $page->getWikiId() );
184 }
185 }