Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteListTest.php
1 <?php
2
3 /**
4 * Tests for the SiteList 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 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 */
31 class SiteListTest extends MediaWikiTestCase {
32
33 /**
34 * Returns instances of SiteList implementing objects.
35 * @return array
36 */
37 public function siteListProvider() {
38 $sitesArrays = $this->siteArrayProvider();
39
40 $listInstances = [];
41
42 foreach ( $sitesArrays as $sitesArray ) {
43 $listInstances[] = new SiteList( $sitesArray[0] );
44 }
45
46 return $this->arrayWrap( $listInstances );
47 }
48
49 /**
50 * Returns arrays with instances of Site implementing objects.
51 * @return array
52 */
53 public function siteArrayProvider() {
54 $sites = TestSites::getSites();
55
56 $siteArrays = [];
57
58 $siteArrays[] = $sites;
59
60 $siteArrays[] = [ array_shift( $sites ) ];
61
62 $siteArrays[] = [ array_shift( $sites ), array_shift( $sites ) ];
63
64 return $this->arrayWrap( $siteArrays );
65 }
66
67 /**
68 * @dataProvider siteListProvider
69 * @param SiteList $sites
70 * @covers SiteList::isEmpty
71 */
72 public function testIsEmpty( SiteList $sites ) {
73 $this->assertEquals( count( $sites ) === 0, $sites->isEmpty() );
74 }
75
76 /**
77 * @dataProvider siteListProvider
78 * @param SiteList $sites
79 * @covers SiteList::getSite
80 */
81 public function testGetSiteByGlobalId( SiteList $sites ) {
82 /**
83 * @var Site $site
84 */
85 foreach ( $sites as $site ) {
86 $this->assertEquals( $site, $sites->getSite( $site->getGlobalId() ) );
87 }
88
89 $this->assertTrue( true );
90 }
91
92 /**
93 * @dataProvider siteListProvider
94 * @param SiteList $sites
95 * @covers SiteList::getSiteByInternalId
96 */
97 public function testGetSiteByInternalId( $sites ) {
98 /**
99 * @var Site $site
100 */
101 foreach ( $sites as $site ) {
102 if ( is_int( $site->getInternalId() ) ) {
103 $this->assertEquals( $site, $sites->getSiteByInternalId( $site->getInternalId() ) );
104 }
105 }
106
107 $this->assertTrue( true );
108 }
109
110 /**
111 * @dataProvider siteListProvider
112 * @param SiteList $sites
113 * @covers SiteList::getSiteByNavigationId
114 */
115 public function testGetSiteByNavigationId( $sites ) {
116 /**
117 * @var Site $site
118 */
119 foreach ( $sites as $site ) {
120 $ids = $site->getNavigationIds();
121 foreach ( $ids as $navId ) {
122 $this->assertEquals( $site, $sites->getSiteByNavigationId( $navId ) );
123 }
124 }
125
126 $this->assertTrue( true );
127 }
128
129 /**
130 * @dataProvider siteListProvider
131 * @param SiteList $sites
132 * @covers SiteList::hasSite
133 */
134 public function testHasGlobalId( $sites ) {
135 $this->assertFalse( $sites->hasSite( 'non-existing-global-id' ) );
136 $this->assertFalse( $sites->hasInternalId( 720101010 ) );
137
138 if ( !$sites->isEmpty() ) {
139 /**
140 * @var Site $site
141 */
142 foreach ( $sites as $site ) {
143 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
144 }
145 }
146 }
147
148 /**
149 * @dataProvider siteListProvider
150 * @param SiteList $sites
151 * @covers SiteList::hasInternalId
152 */
153 public function testHasInternallId( $sites ) {
154 /**
155 * @var Site $site
156 */
157 foreach ( $sites as $site ) {
158 if ( is_int( $site->getInternalId() ) ) {
159 $this->assertTrue( $site, $sites->hasInternalId( $site->getInternalId() ) );
160 }
161 }
162
163 $this->assertFalse( $sites->hasInternalId( -1 ) );
164 }
165
166 /**
167 * @dataProvider siteListProvider
168 * @param SiteList $sites
169 * @covers SiteList::hasNavigationId
170 */
171 public function testHasNavigationId( $sites ) {
172 /**
173 * @var Site $site
174 */
175 foreach ( $sites as $site ) {
176 $ids = $site->getNavigationIds();
177 foreach ( $ids as $navId ) {
178 $this->assertTrue( $sites->hasNavigationId( $navId ) );
179 }
180 }
181
182 $this->assertFalse( $sites->hasNavigationId( 'non-existing-navigation-id' ) );
183 }
184
185 /**
186 * @dataProvider siteListProvider
187 * @param SiteList $sites
188 * @covers SiteList::getGlobalIdentifiers
189 */
190 public function testGetGlobalIdentifiers( SiteList $sites ) {
191 $identifiers = $sites->getGlobalIdentifiers();
192
193 $this->assertTrue( is_array( $identifiers ) );
194
195 $expected = [];
196
197 /**
198 * @var Site $site
199 */
200 foreach ( $sites as $site ) {
201 $expected[] = $site->getGlobalId();
202 }
203
204 $this->assertArrayEquals( $expected, $identifiers );
205 }
206
207 /**
208 * @dataProvider siteListProvider
209 *
210 * @since 1.21
211 *
212 * @param SiteList $list
213 * @covers SiteList::getSerializationData
214 * @covers SiteList::unserialize
215 */
216 public function testSerialization( SiteList $list ) {
217 $serialization = serialize( $list );
218 /**
219 * @var SiteList $copy
220 */
221 $copy = unserialize( $serialization );
222
223 $this->assertArrayEquals( $list->getGlobalIdentifiers(), $copy->getGlobalIdentifiers() );
224
225 /**
226 * @var Site $site
227 */
228 foreach ( $list as $site ) {
229 $this->assertTrue( $copy->hasInternalId( $site->getInternalId() ) );
230
231 foreach ( $site->getNavigationIds() as $navId ) {
232 $this->assertTrue(
233 $copy->hasNavigationId( $navId ),
234 'unserialized data expects nav id ' . $navId . ' for site ' . $site->getGlobalId()
235 );
236 }
237 }
238 }
239 }