1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @since 1.21
21 *
22 * @ingroup Site
23 * @ingroup Test
24 *
25 * @group Site
26 *
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 */
29class SiteListTest extends MediaWikiIntegrationTestCase {
30
31	/**
32	 * Returns instances of SiteList implementing objects.
33	 * @return array
34	 */
35	public function siteListProvider() {
36		$sitesArrays = $this->siteArrayProvider();
37
38		$listInstances = [];
39
40		foreach ( $sitesArrays as $sitesArray ) {
41			$listInstances[] = new SiteList( $sitesArray[0] );
42		}
43
44		return $this->arrayWrap( $listInstances );
45	}
46
47	/**
48	 * Returns arrays with instances of Site implementing objects.
49	 * @return array
50	 */
51	public function siteArrayProvider() {
52		$sites = TestSites::getSites();
53
54		$siteArrays = [];
55
56		$siteArrays[] = $sites;
57
58		$siteArrays[] = [ array_shift( $sites ) ];
59
60		$siteArrays[] = [ array_shift( $sites ), array_shift( $sites ) ];
61
62		return $this->arrayWrap( $siteArrays );
63	}
64
65	/**
66	 * @dataProvider siteListProvider
67	 * @param SiteList $sites
68	 * @covers SiteList::isEmpty
69	 */
70	public function testIsEmpty( SiteList $sites ) {
71		$this->assertEquals( count( $sites ) === 0, $sites->isEmpty() );
72	}
73
74	/**
75	 * @dataProvider siteListProvider
76	 * @param SiteList $sites
77	 * @covers SiteList::getSite
78	 */
79	public function testGetSiteByGlobalId( SiteList $sites ) {
80		/**
81		 * @var Site $site
82		 */
83		foreach ( $sites as $site ) {
84			$this->assertEquals( $site, $sites->getSite( $site->getGlobalId() ) );
85		}
86
87		$this->assertTrue( true );
88	}
89
90	/**
91	 * @dataProvider siteListProvider
92	 * @param SiteList $sites
93	 * @covers SiteList::getSiteByInternalId
94	 */
95	public function testGetSiteByInternalId( $sites ) {
96		/**
97		 * @var Site $site
98		 */
99		foreach ( $sites as $site ) {
100			if ( is_int( $site->getInternalId() ) ) {
101				$this->assertEquals( $site, $sites->getSiteByInternalId( $site->getInternalId() ) );
102			}
103		}
104
105		$this->assertTrue( true );
106	}
107
108	/**
109	 * @dataProvider siteListProvider
110	 * @param SiteList $sites
111	 * @covers SiteList::getSiteByNavigationId
112	 */
113	public function testGetSiteByNavigationId( $sites ) {
114		/**
115		 * @var Site $site
116		 */
117		foreach ( $sites as $site ) {
118			$ids = $site->getNavigationIds();
119			foreach ( $ids as $navId ) {
120				$this->assertEquals( $site, $sites->getSiteByNavigationId( $navId ) );
121			}
122		}
123
124		$this->assertTrue( true );
125	}
126
127	/**
128	 * @dataProvider siteListProvider
129	 * @param SiteList $sites
130	 * @covers SiteList::hasSite
131	 */
132	public function testHasGlobalId( $sites ) {
133		$this->assertFalse( $sites->hasSite( 'non-existing-global-id' ) );
134		$this->assertFalse( $sites->hasInternalId( 720101010 ) );
135
136		if ( !$sites->isEmpty() ) {
137			/**
138			 * @var Site $site
139			 */
140			foreach ( $sites as $site ) {
141				$this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
142			}
143		}
144	}
145
146	/**
147	 * @dataProvider siteListProvider
148	 * @param SiteList $sites
149	 * @covers SiteList::hasInternalId
150	 */
151	public function testHasInternallId( $sites ) {
152		/**
153		 * @var Site $site
154		 */
155		foreach ( $sites as $site ) {
156			if ( is_int( $site->getInternalId() ) ) {
157				$this->assertTrue( $site, $sites->hasInternalId( $site->getInternalId() ) );
158			}
159		}
160
161		$this->assertFalse( $sites->hasInternalId( -1 ) );
162	}
163
164	/**
165	 * @dataProvider siteListProvider
166	 * @param SiteList $sites
167	 * @covers SiteList::hasNavigationId
168	 */
169	public function testHasNavigationId( $sites ) {
170		/**
171		 * @var Site $site
172		 */
173		foreach ( $sites as $site ) {
174			$ids = $site->getNavigationIds();
175			foreach ( $ids as $navId ) {
176				$this->assertTrue( $sites->hasNavigationId( $navId ) );
177			}
178		}
179
180		$this->assertFalse( $sites->hasNavigationId( 'non-existing-navigation-id' ) );
181	}
182
183	/**
184	 * @dataProvider siteListProvider
185	 * @param SiteList $sites
186	 * @covers SiteList::getGlobalIdentifiers
187	 */
188	public function testGetGlobalIdentifiers( SiteList $sites ) {
189		$identifiers = $sites->getGlobalIdentifiers();
190
191		$this->assertIsArray( $identifiers );
192
193		$expected = [];
194
195		/**
196		 * @var Site $site
197		 */
198		foreach ( $sites as $site ) {
199			$expected[] = $site->getGlobalId();
200		}
201
202		$this->assertArrayEquals( $expected, $identifiers );
203	}
204
205	/**
206	 * @dataProvider siteListProvider
207	 *
208	 * @since 1.21
209	 *
210	 * @param SiteList $list
211	 * @covers SiteList::getSerializationData
212	 * @covers SiteList::unserialize
213	 */
214	public function testSerialization( SiteList $list ) {
215		$serialization = serialize( $list );
216		/**
217		 * @var SiteList $copy
218		 */
219		$copy = unserialize( $serialization );
220
221		$this->assertArrayEquals( $list->getGlobalIdentifiers(), $copy->getGlobalIdentifiers() );
222
223		/**
224		 * @var Site $site
225		 */
226		foreach ( $list as $site ) {
227			$this->assertTrue( $copy->hasInternalId( $site->getInternalId() ) );
228
229			foreach ( $site->getNavigationIds() as $navId ) {
230				$this->assertTrue(
231					$copy->hasNavigationId( $navId ),
232					'unserialized data expects nav id ' . $navId . ' for site ' . $site->getGlobalId()
233				);
234			}
235		}
236	}
237}
238