4 * Tests for the SiteObject class.
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.
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.
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
30 * @licence GNU GPL v2+
31 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
33 class SiteObjectTest
extends ORMRowTest
{
36 * @see ORMRowTest::getRowClass
40 protected function getRowClass() {
45 * @see ORMRowTest::getTableInstance
49 protected function getTableInstance() {
50 return SitesTable
::singleton();
54 * @see ORMRowTest::constructorTestProvider
58 public function constructorTestProvider() {
61 $argLists[] = array( 'global_key' => 'foo' );
63 $argLists[] = array( 'global_key' => 'bar', 'type' => Site
::TYPE_MEDIAWIKI
);
65 $constructorArgs = array();
67 foreach ( $argLists as $argList ) {
68 $constructorArgs[] = array( $argList, true );
71 return $constructorArgs;
75 * @dataProvider instanceProvider
78 public function testGetInterwikiIds( Site
$site ) {
79 $this->assertInternalType( 'array', $site->getInterwikiIds() );
83 * @dataProvider instanceProvider
86 public function testGetNavigationIds( Site
$site ) {
87 $this->assertInternalType( 'array', $site->getNavigationIds() );
91 * @dataProvider instanceProvider
94 public function testAddNavigationId( Site
$site ) {
95 $site->addNavigationId( 'foobar' );
96 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
100 * @dataProvider instanceProvider
103 public function testAddInterwikiId( Site
$site ) {
104 $site->addInterwikiId( 'foobar' );
105 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
109 * @dataProvider instanceProvider
112 public function testGetLanguageCode( Site
$site ) {
113 $this->assertTypeOrFalse( 'string', $site->getLanguageCode() );
117 * @dataProvider instanceProvider
120 public function testSetLanguageCode( Site
$site ) {
121 $site->setLanguageCode( 'en' );
122 $this->assertEquals( 'en', $site->getLanguageCode() );
126 * @dataProvider instanceProvider
129 public function testNormalizePageName( Site
$site ) {
130 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
134 * @dataProvider instanceProvider
137 public function testGetGlobalId( Site
$site ) {
138 $this->assertInternalType( 'string', $site->getGlobalId() );
142 * @dataProvider instanceProvider
145 public function testSetGlobalId( Site
$site ) {
146 $site->setGlobalId( 'foobar' );
147 $this->assertEquals( 'foobar', $site->getGlobalId() );
151 * @dataProvider instanceProvider
154 public function testGetType( Site
$site ) {
155 $this->assertInternalType( 'string', $site->getType() );
159 * @dataProvider instanceProvider
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' ) );
169 * @dataProvider instanceProvider
172 public function testGetAllPaths( Site
$site ) {
173 $this->assertInternalType( 'array', $site->getAllPaths() );
177 * @dataProvider instanceProvider
180 public function testSetAndRemovePath( Site
$site ) {
181 $count = count( $site->getAllPaths() );
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' );
187 $this->assertEquals( $count +
2, count( $site->getAllPaths() ) );
189 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
190 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
192 $site->removePath( 'spam' );
193 $site->removePath( 'foobar' );
195 $this->assertEquals( $count, count( $site->getAllPaths() ) );
197 $this->assertFalse( $site->getPath( 'foobar' ) );
198 $this->assertFalse( $site->getPath( 'spam' ) );
201 public function testSetLinkPath() {
202 /* @var SiteObject $site */
203 $site = $this->getRowInstance( $this->getMockFields(), false );
204 $path = "TestPath/$1";
206 $site->setLinkPath( $path );
207 $this->assertEquals( $path, $site->getLinkPath() );
210 public function testGetLinkPathType() {
211 /* @var SiteObject $site */
212 $site = $this->getRowInstance( $this->getMockFields(), false );
214 $path = 'TestPath/$1';
215 $site->setLinkPath( $path );
216 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
218 $path = 'AnotherPath/$1';
219 $site->setPath( $site->getLinkPathType(), $path );
220 $this->assertEquals( $path, $site->getLinkPath() );
223 public function testSetPath() {
224 /* @var SiteObject $site */
225 $site = $this->getRowInstance( $this->getMockFields(), false );
227 $path = 'TestPath/$1';
228 $site->setPath( 'foo', $path );
230 $this->assertEquals( $path, $site->getPath( 'foo' ) );
233 public function testProtocolRelativePath() {
234 /* @var SiteObject $site */
235 $site = $this->getRowInstance( $this->getMockFields(), false );
237 $type = $site->getLinkPathType();
238 $path = '//acme.com/'; // protocol-relative URL
239 $site->setPath( $type, $path );
241 $this->assertEquals( '', $site->getProtocol() );
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.
252 'http://acme.test/TestPath/$1',
257 'http://acme.test/TestScript?x=$1&y=bla',
259 'TestScript?x=Foo&y=bla',
262 'http://acme.test/TestPath/$1',
263 'foo & bar/xyzzy (quux-shmoox?)',
264 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
270 * @dataProvider provideGetPageUrl
272 public function testGetPageUrl( $path, $page, $expected ) {
273 /* @var SiteObject $site */
274 $site = $this->getRowInstance( $this->getMockFields(), false );
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() );
282 $this->assertContains( $expected, $site->getPageUrl( $page ) );
285 protected function assertTypeOrFalse( $type, $value ) {
286 if ( $value === false ) {
287 $this->assertTrue( true );
290 $this->assertInternalType( $type, $value );