Debian Codegraph @

tests/phpunit/includes/site/SiteListTest.php

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