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
22use MediaWiki\Page\PageReference;
23use MediaWiki\Page\PageReferenceValue;
24use Wikimedia\Assert\ParameterAssertionException;
25
26/**
27 * @covers MediaWiki\Page\PageReferenceValue
28 *
29 * @group Title
30 */
31class PageReferenceValueTest extends MediaWikiUnitTestCase {
32
33	public function goodConstructorProvider() {
34		return [
35			[ NS_MAIN, 'Test', false ],
36			[ NS_MAIN, 'Test', false ],
37			[ NS_USER, 'Test', 'h2g2' ],
38			[ NS_MEDIA, 'Test', false ],
39			[ NS_SPECIAL, 'Test', false ],
40		];
41	}
42
43	/**
44	 * @dataProvider goodConstructorProvider
45	 */
46	public function testConstruction( $namespace, $dbKey, $wikiId ) {
47		$pageReference = new PageReferenceValue( $namespace, $dbKey, $wikiId );
48
49		$this->assertSame( $wikiId, $pageReference->getWikiId() );
50		$this->assertSame( $namespace, $pageReference->getNamespace() );
51		$this->assertSame( $dbKey, $pageReference->getDBkey() );
52	}
53
54	public function badConstructorProvider() {
55		return [
56			[ NS_MAIN, 'Test', 2.3 ],
57		];
58	}
59
60	/**
61	 * @dataProvider badConstructorProvider
62	 */
63	public function testConstructionErrors( $namespace, $dbKey, $wikiId ) {
64		$this->expectException( ParameterAssertionException::class );
65		new PageReferenceValue( $namespace, $dbKey, $wikiId );
66	}
67
68	public function provideToString() {
69		yield [
70			new PageReferenceValue( 0, 'Foo', PageReference::LOCAL ),
71			'[0:Foo]'
72		];
73		yield [
74			new PageReferenceValue( 1, 'Bar_Baz', PageReference::LOCAL ),
75			'[1:Bar_Baz]'
76		];
77		yield [
78			new PageReferenceValue( 200, 'tea', 'codewiki' ),
79			'[200:tea]@codewiki'
80		];
81	}
82
83	/**
84	 * @dataProvider provideToString
85	 */
86	public function testToString( PageReferenceValue $value, $expected ) {
87		$this->assertSame(
88			$expected,
89			$value->__toString()
90		);
91	}
92
93	public function provideIsSamePageAs() {
94		yield [
95			new PageReferenceValue( 0, 'Foo', PageReference::LOCAL ),
96			new PageReferenceValue( 0, 'Foo', PageReference::LOCAL ),
97			true
98		];
99		yield [
100			new PageReferenceValue( 1, 'Bar_Baz', PageReference::LOCAL ),
101			new PageReferenceValue( 1, 'Bar_Baz', PageReference::LOCAL ),
102			true
103		];
104		yield [
105			new PageReferenceValue( 0, 'Foo', PageReference::LOCAL ),
106			new PageReferenceValue( 0, 'Foozz', PageReference::LOCAL ),
107			false
108		];
109		yield [
110			new PageReferenceValue( 0, 'Foo', PageReference::LOCAL ),
111			new PageReferenceValue( 1, 'Foo', PageReference::LOCAL ),
112			false
113		];
114		yield [
115			new PageReferenceValue( 0, 'Foo', '' ),
116			new PageReferenceValue( 0, 'Foo', 'bar' ),
117			false
118		];
119		yield [
120			new PageReferenceValue( 0, 'Foo', '' ),
121			new PageReferenceValue( 0, 'Foo', 'bar' ),
122			false
123		];
124		yield [
125			new PageReferenceValue( 0, 'Foo', 'bar' ),
126			new PageReferenceValue( 0, 'Foo', 'bar' ),
127			true
128		];
129	}
130
131	/**
132	 * @dataProvider provideIsSamePageAs
133	 */
134	public function testIsSamePageAs( PageReferenceValue $a, PageReferenceValue $b, $expected ) {
135		$this->assertSame( $expected, $a->isSamePageAs( $b ) );
136		$this->assertSame( $expected, $b->isSamePageAs( $a ) );
137	}
138
139	/**
140	 * @covers \MediaWiki\Page\PageReferenceValue::localReference
141	 */
142	public function testLocalIdentity() {
143		$page = PageReferenceValue::localReference( NS_MAIN, __METHOD__ );
144		$this->assertSame( NS_MAIN, $page->getNamespace() );
145		$this->assertSame( __METHOD__, $page->getDBkey() );
146		$this->assertSame( PageReference::LOCAL, $page->getWikiId() );
147	}
148}
149