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
29 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 class SiteObjectTest
extends ORMRowTest
{
35 * @see ORMRowTest::getRowClass
39 protected function getRowClass() {
44 * @see ORMRowTest::getTableInstance
48 protected function getTableInstance() {
49 return SitesTable
::singleton();
53 * @see ORMRowTest::constructorTestProvider
57 public function constructorTestProvider() {
60 $argLists[] = array( 'global_key' => '42' );
62 $argLists[] = array( 'global_key' => '42', 'type' => Site
::TYPE_MEDIAWIKI
);
64 $constructorArgs = array();
66 foreach ( $argLists as $argList ) {
67 $constructorArgs[] = array( $argList, true );
70 return $constructorArgs;
74 * @dataProvider instanceProvider
77 public function testGetInterwikiIds( Site
$site ) {
78 $this->assertInternalType( 'array', $site->getInterwikiIds() );
82 * @dataProvider instanceProvider
85 public function testGetNavigationIds( Site
$site ) {
86 $this->assertInternalType( 'array', $site->getNavigationIds() );
90 * @dataProvider instanceProvider
93 public function testAddNavigationId( Site
$site ) {
94 $site->addNavigationId( 'foobar' );
95 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
99 * @dataProvider instanceProvider
102 public function testAddInterwikiId( Site
$site ) {
103 $site->addInterwikiId( 'foobar' );
104 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
108 * @dataProvider instanceProvider
111 public function testGetLanguageCode( Site
$site ) {
112 $this->assertTypeOrFalse( 'string', $site->getLanguageCode() );
116 * @dataProvider instanceProvider
119 public function testSetLanguageCode( Site
$site ) {
120 $site->setLanguageCode( 'en' );
121 $this->assertEquals( 'en', $site->getLanguageCode() );
125 * @dataProvider instanceProvider
128 public function testNormalizePageName( Site
$site ) {
129 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
133 * @dataProvider instanceProvider
136 public function testGetGlobalId( Site
$site ) {
137 $this->assertInternalType( 'string', $site->getGlobalId() );
141 * @dataProvider instanceProvider
144 public function testSetGlobalId( Site
$site ) {
145 $site->setGlobalId( 'foobar' );
146 $this->assertEquals( 'foobar', $site->getGlobalId() );
150 * @dataProvider instanceProvider
153 public function testGetType( Site
$site ) {
154 $this->assertInternalType( 'string', $site->getType() );
158 * @dataProvider instanceProvider
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' ) );
168 * @dataProvider instanceProvider
171 public function testGetAllPaths( Site
$site ) {
172 $this->assertInternalType( 'array', $site->getAllPaths() );
176 * @dataProvider instanceProvider
179 public function testSetAndRemovePath( Site
$site ) {
180 $count = count( $site->getAllPaths() );
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' );
186 $this->assertEquals( $count +
2, count( $site->getAllPaths() ) );
188 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
189 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
191 $site->removePath( 'spam' );
192 $site->removePath( 'foobar' );
194 $this->assertEquals( $count, count( $site->getAllPaths() ) );
196 $this->assertFalse( $site->getPath( 'foobar' ) );
197 $this->assertFalse( $site->getPath( 'spam' ) );
200 public function testSetLinkPath() {
201 /* @var SiteObject $site */
202 $site = $this->getRowInstance( $this->getMockFields(), false );
203 $path = "TestPath/$1";
205 $site->setLinkPath( $path );
206 $this->assertEquals( $path, $site->getLinkPath() );
209 public function testGetLinkPathType() {
210 /* @var SiteObject $site */
211 $site = $this->getRowInstance( $this->getMockFields(), false );
213 $path = 'TestPath/$1';
214 $site->setLinkPath( $path );
215 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
217 $path = 'AnotherPath/$1';
218 $site->setPath( $site->getLinkPathType(), $path );
219 $this->assertEquals( $path, $site->getLinkPath() );
222 public function testSetPath() {
223 /* @var SiteObject $site */
224 $site = $this->getRowInstance( $this->getMockFields(), false );
226 $path = 'TestPath/$1';
227 $site->setPath( 'foo', $path );
229 $this->assertEquals( $path, $site->getPath( 'foo' ) );
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.
240 'http://acme.test/TestPath/$1',
245 'http://acme.test/TestScript?x=$1&y=bla',
247 'TestScript?x=Foo&y=bla',
250 'http://acme.test/TestPath/$1',
251 'foo & bar/xyzzy (quux-shmoox?)',
252 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
258 * @dataProvider provideGetPageUrl
260 public function testGetPageUrl( $path, $page, $expected ) {
261 /* @var SiteObject $site */
262 $site = $this->getRowInstance( $this->getMockFields(), false );
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() );
270 $this->assertContains( $expected, $site->getPageUrl( $page ) );
273 protected function assertTypeOrFalse( $type, $value ) {
274 if ( $value === false ) {
275 $this->assertTrue( true );
278 $this->assertInternalType( $type, $value );