Make sure that SQLite uses no prefix
[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 *
29 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 */
32 class SiteObjectTest extends ORMRowTest {
33
34 /**
35 * @see ORMRowTest::getRowClass
36 * @since 1.21
37 * @return string
38 */
39 protected function getRowClass() {
40 return 'SiteObject';
41 }
42
43 /**
44 * @see ORMRowTest::getTableInstance
45 * @since 1.21
46 * @return IORMTable
47 */
48 protected function getTableInstance() {
49 return SitesTable::singleton();
50 }
51
52 /**
53 * @see ORMRowTest::constructorTestProvider
54 * @since 1.21
55 * @return array
56 */
57 public function constructorTestProvider() {
58 $argLists = array();
59
60 $argLists[] = array( 'global_key' => '42' );
61
62 $argLists[] = array( 'global_key' => '42', 'type' => Site::TYPE_MEDIAWIKI );
63
64 $constructorArgs = array();
65
66 foreach ( $argLists as $argList ) {
67 $constructorArgs[] = array( $argList, true );
68 }
69
70 return $constructorArgs;
71 }
72
73 /**
74 * @dataProvider instanceProvider
75 * @param Site $site
76 */
77 public function testGetInterwikiIds( Site $site ) {
78 $this->assertInternalType( 'array', $site->getInterwikiIds() );
79 }
80
81 /**
82 * @dataProvider instanceProvider
83 * @param Site $site
84 */
85 public function testGetNavigationIds( Site $site ) {
86 $this->assertInternalType( 'array', $site->getNavigationIds() );
87 }
88
89 /**
90 * @dataProvider instanceProvider
91 * @param Site $site
92 */
93 public function testAddNavigationId( Site $site ) {
94 $site->addNavigationId( 'foobar' );
95 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
96 }
97
98 /**
99 * @dataProvider instanceProvider
100 * @param Site $site
101 */
102 public function testAddInterwikiId( Site $site ) {
103 $site->addInterwikiId( 'foobar' );
104 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
105 }
106
107 /**
108 * @dataProvider instanceProvider
109 * @param Site $site
110 */
111 public function testGetLanguageCode( Site $site ) {
112 $this->assertTypeOrFalse( 'string', $site->getLanguageCode() );
113 }
114
115 /**
116 * @dataProvider instanceProvider
117 * @param Site $site
118 */
119 public function testSetLanguageCode( Site $site ) {
120 $site->setLanguageCode( 'en' );
121 $this->assertEquals( 'en', $site->getLanguageCode() );
122 }
123
124 /**
125 * @dataProvider instanceProvider
126 * @param Site $site
127 */
128 public function testNormalizePageName( Site $site ) {
129 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
130 }
131
132 /**
133 * @dataProvider instanceProvider
134 * @param Site $site
135 */
136 public function testGetGlobalId( Site $site ) {
137 $this->assertInternalType( 'string', $site->getGlobalId() );
138 }
139
140 /**
141 * @dataProvider instanceProvider
142 * @param Site $site
143 */
144 public function testSetGlobalId( Site $site ) {
145 $site->setGlobalId( 'foobar' );
146 $this->assertEquals( 'foobar', $site->getGlobalId() );
147 }
148
149 /**
150 * @dataProvider instanceProvider
151 * @param Site $site
152 */
153 public function testGetType( Site $site ) {
154 $this->assertInternalType( 'string', $site->getType() );
155 }
156
157 /**
158 * @dataProvider instanceProvider
159 * @param Site $site
160 */
161 public function testGetPath( Site $site ) {
162 $this->assertTypeOrFalse( 'string', $site->getPath( 'page_path' ) );
163 $this->assertTypeOrFalse( 'string', $site->getPath( 'file_path' ) );
164 $this->assertTypeOrFalse( 'string', $site->getPath( 'foobar' ) );
165 }
166
167 /**
168 * @dataProvider instanceProvider
169 * @param Site $site
170 */
171 public function testGetAllPaths( Site $site ) {
172 $this->assertInternalType( 'array', $site->getAllPaths() );
173 }
174
175 /**
176 * @dataProvider instanceProvider
177 * @param Site $site
178 */
179 public function testSetAndRemovePath( Site $site ) {
180 $count = count( $site->getAllPaths() );
181
182 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
183 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
184 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
185
186 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
187
188 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
189 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
190
191 $site->removePath( 'spam' );
192 $site->removePath( 'foobar' );
193
194 $this->assertEquals( $count, count( $site->getAllPaths() ) );
195
196 $this->assertFalse( $site->getPath( 'foobar' ) );
197 $this->assertFalse( $site->getPath( 'spam' ) );
198 }
199
200 public function testSetLinkPath() {
201 /* @var SiteObject $site */
202 $site = $this->getRowInstance( $this->getMockFields(), false );
203 $path = "TestPath/$1";
204
205 $site->setLinkPath( $path );
206 $this->assertEquals( $path, $site->getLinkPath() );
207 }
208
209 public function testGetLinkPathType() {
210 /* @var SiteObject $site */
211 $site = $this->getRowInstance( $this->getMockFields(), false );
212
213 $path = 'TestPath/$1';
214 $site->setLinkPath( $path );
215 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
216
217 $path = 'AnotherPath/$1';
218 $site->setPath( $site->getLinkPathType(), $path );
219 $this->assertEquals( $path, $site->getLinkPath() );
220 }
221
222 public function testSetPath() {
223 /* @var SiteObject $site */
224 $site = $this->getRowInstance( $this->getMockFields(), false );
225
226 $path = 'TestPath/$1';
227 $site->setPath( 'foo', $path );
228
229 $this->assertEquals( $path, $site->getPath( 'foo' ) );
230 }
231
232 public function provideGetPageUrl() {
233 //NOTE: the assumption that the URL is built by replacing $1
234 // with the urlencoded version of $page
235 // is true for SiteObject but not guaranteed for subclasses.
236 // Subclasses need to override this provider appropriately.
237
238 return array(
239 array( #0
240 'http://acme.test/TestPath/$1',
241 'Foo',
242 '/TestPath/Foo',
243 ),
244 array( #1
245 'http://acme.test/TestScript?x=$1&y=bla',
246 'Foo',
247 'TestScript?x=Foo&y=bla',
248 ),
249 array( #2
250 'http://acme.test/TestPath/$1',
251 'foo & bar/xyzzy (quux-shmoox?)',
252 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
253 ),
254 );
255 }
256
257 /**
258 * @dataProvider provideGetPageUrl
259 */
260 public function testGetPageUrl( $path, $page, $expected ) {
261 /* @var SiteObject $site */
262 $site = $this->getRowInstance( $this->getMockFields(), false );
263
264 //NOTE: the assumption that getPageUrl is based on getLinkPath
265 // is true for SiteObject but not guaranteed for subclasses.
266 // Subclasses need to override this test case appropriately.
267 $site->setLinkPath( $path );
268 $this->assertContains( $path, $site->getPageUrl() );
269
270 $this->assertContains( $expected, $site->getPageUrl( $page ) );
271 }
272
273 protected function assertTypeOrFalse( $type, $value ) {
274 if ( $value === false ) {
275 $this->assertTrue( true );
276 }
277 else {
278 $this->assertInternalType( $type, $value );
279 }
280 }
281
282 }