Merge "(bug 37755) Set robot meta tags for 'view source' pages"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteObjectTest.php
1 <?php
2
3 /**
4 * Tests for the SiteObject class.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @since 1.21
23 *
24 * @ingroup Site
25 * @ingroup Test
26 *
27 * @group Site
28 * @group Database
29 *
30 * @licence GNU GPL v2+
31 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 */
33 class SiteObjectTest extends ORMRowTest {
34
35 /**
36 * @see ORMRowTest::getRowClass
37 * @since 1.21
38 * @return string
39 */
40 protected function getRowClass() {
41 return 'SiteObject';
42 }
43
44 /**
45 * @see ORMRowTest::getTableInstance
46 * @since 1.21
47 * @return IORMTable
48 */
49 protected function getTableInstance() {
50 return SitesTable::singleton();
51 }
52
53 /**
54 * @see ORMRowTest::constructorTestProvider
55 * @since 1.21
56 * @return array
57 */
58 public function constructorTestProvider() {
59 $argLists = array();
60
61 $argLists[] = array( 'global_key' => 'foo' );
62
63 $argLists[] = array( 'global_key' => 'bar', 'type' => Site::TYPE_MEDIAWIKI );
64
65 $constructorArgs = array();
66
67 foreach ( $argLists as $argList ) {
68 $constructorArgs[] = array( $argList, true );
69 }
70
71 return $constructorArgs;
72 }
73
74 /**
75 * @dataProvider instanceProvider
76 * @param Site $site
77 */
78 public function testGetInterwikiIds( Site $site ) {
79 $this->assertInternalType( 'array', $site->getInterwikiIds() );
80 }
81
82 /**
83 * @dataProvider instanceProvider
84 * @param Site $site
85 */
86 public function testGetNavigationIds( Site $site ) {
87 $this->assertInternalType( 'array', $site->getNavigationIds() );
88 }
89
90 /**
91 * @dataProvider instanceProvider
92 * @param Site $site
93 */
94 public function testAddNavigationId( Site $site ) {
95 $site->addNavigationId( 'foobar' );
96 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
97 }
98
99 /**
100 * @dataProvider instanceProvider
101 * @param Site $site
102 */
103 public function testAddInterwikiId( Site $site ) {
104 $site->addInterwikiId( 'foobar' );
105 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
106 }
107
108 /**
109 * @dataProvider instanceProvider
110 * @param Site $site
111 */
112 public function testGetLanguageCode( Site $site ) {
113 $this->assertTypeOrFalse( 'string', $site->getLanguageCode() );
114 }
115
116 /**
117 * @dataProvider instanceProvider
118 * @param Site $site
119 */
120 public function testSetLanguageCode( Site $site ) {
121 $site->setLanguageCode( 'en' );
122 $this->assertEquals( 'en', $site->getLanguageCode() );
123 }
124
125 /**
126 * @dataProvider instanceProvider
127 * @param Site $site
128 */
129 public function testNormalizePageName( Site $site ) {
130 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
131 }
132
133 /**
134 * @dataProvider instanceProvider
135 * @param Site $site
136 */
137 public function testGetGlobalId( Site $site ) {
138 $this->assertInternalType( 'string', $site->getGlobalId() );
139 }
140
141 /**
142 * @dataProvider instanceProvider
143 * @param Site $site
144 */
145 public function testSetGlobalId( Site $site ) {
146 $site->setGlobalId( 'foobar' );
147 $this->assertEquals( 'foobar', $site->getGlobalId() );
148 }
149
150 /**
151 * @dataProvider instanceProvider
152 * @param Site $site
153 */
154 public function testGetType( Site $site ) {
155 $this->assertInternalType( 'string', $site->getType() );
156 }
157
158 /**
159 * @dataProvider instanceProvider
160 * @param Site $site
161 */
162 public function testGetPath( Site $site ) {
163 $this->assertTypeOrFalse( 'string', $site->getPath( 'page_path' ) );
164 $this->assertTypeOrFalse( 'string', $site->getPath( 'file_path' ) );
165 $this->assertTypeOrFalse( 'string', $site->getPath( 'foobar' ) );
166 }
167
168 /**
169 * @dataProvider instanceProvider
170 * @param Site $site
171 */
172 public function testGetAllPaths( Site $site ) {
173 $this->assertInternalType( 'array', $site->getAllPaths() );
174 }
175
176 /**
177 * @dataProvider instanceProvider
178 * @param Site $site
179 */
180 public function testSetAndRemovePath( Site $site ) {
181 $count = count( $site->getAllPaths() );
182
183 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
184 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
185 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
186
187 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
188
189 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
190 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
191
192 $site->removePath( 'spam' );
193 $site->removePath( 'foobar' );
194
195 $this->assertEquals( $count, count( $site->getAllPaths() ) );
196
197 $this->assertFalse( $site->getPath( 'foobar' ) );
198 $this->assertFalse( $site->getPath( 'spam' ) );
199 }
200
201 public function testSetLinkPath() {
202 /* @var SiteObject $site */
203 $site = $this->getRowInstance( $this->getMockFields(), false );
204 $path = "TestPath/$1";
205
206 $site->setLinkPath( $path );
207 $this->assertEquals( $path, $site->getLinkPath() );
208 }
209
210 public function testGetLinkPathType() {
211 /* @var SiteObject $site */
212 $site = $this->getRowInstance( $this->getMockFields(), false );
213
214 $path = 'TestPath/$1';
215 $site->setLinkPath( $path );
216 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
217
218 $path = 'AnotherPath/$1';
219 $site->setPath( $site->getLinkPathType(), $path );
220 $this->assertEquals( $path, $site->getLinkPath() );
221 }
222
223 public function testSetPath() {
224 /* @var SiteObject $site */
225 $site = $this->getRowInstance( $this->getMockFields(), false );
226
227 $path = 'TestPath/$1';
228 $site->setPath( 'foo', $path );
229
230 $this->assertEquals( $path, $site->getPath( 'foo' ) );
231 }
232
233 public function testProtocolRelativePath() {
234 /* @var SiteObject $site */
235 $site = $this->getRowInstance( $this->getMockFields(), false );
236
237 $type = $site->getLinkPathType();
238 $path = '//acme.com/'; // protocol-relative URL
239 $site->setPath( $type, $path );
240
241 $this->assertEquals( '', $site->getProtocol() );
242 }
243
244 public function provideGetPageUrl() {
245 //NOTE: the assumption that the URL is built by replacing $1
246 // with the urlencoded version of $page
247 // is true for SiteObject but not guaranteed for subclasses.
248 // Subclasses need to override this provider appropriately.
249
250 return array(
251 array( #0
252 'http://acme.test/TestPath/$1',
253 'Foo',
254 '/TestPath/Foo',
255 ),
256 array( #1
257 'http://acme.test/TestScript?x=$1&y=bla',
258 'Foo',
259 'TestScript?x=Foo&y=bla',
260 ),
261 array( #2
262 'http://acme.test/TestPath/$1',
263 'foo & bar/xyzzy (quux-shmoox?)',
264 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
265 ),
266 );
267 }
268
269 /**
270 * @dataProvider provideGetPageUrl
271 */
272 public function testGetPageUrl( $path, $page, $expected ) {
273 /* @var SiteObject $site */
274 $site = $this->getRowInstance( $this->getMockFields(), false );
275
276 //NOTE: the assumption that getPageUrl is based on getLinkPath
277 // is true for SiteObject but not guaranteed for subclasses.
278 // Subclasses need to override this test case appropriately.
279 $site->setLinkPath( $path );
280 $this->assertContains( $path, $site->getPageUrl() );
281
282 $this->assertContains( $expected, $site->getPageUrl( $page ) );
283 }
284
285 protected function assertTypeOrFalse( $type, $value ) {
286 if ( $value === false ) {
287 $this->assertTrue( true );
288 }
289 else {
290 $this->assertInternalType( $type, $value );
291 }
292 }
293
294 }