Merge "Set relevant User on Special:Unblock"
[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 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 */
32 class SiteListTest extends MediaWikiTestCase {
33
34 /**
35 * Returns instances of SiteList implementing objects.
36 * @return array
37 */
38 public function siteListProvider() {
39 $sitesArrays = $this->siteArrayProvider();
40
41 $listInstances = array();
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 = array();
58
59 $siteArrays[] = $sites;
60
61 $siteArrays[] = array( array_shift( $sites ) );
62
63 $siteArrays[] = array( array_shift( $sites ), array_shift( $sites ) );
64
65 return $this->arrayWrap( $siteArrays );
66 }
67
68 /**
69 * @dataProvider siteListProvider
70 * @param SiteList $sites
71 * @covers SiteList::isEmpty
72 */
73 public function testIsEmpty( SiteList $sites ) {
74 $this->assertEquals( count( $sites ) === 0, $sites->isEmpty() );
75 }
76
77 /**
78 * @dataProvider siteListProvider
79 * @param SiteList $sites
80 * @covers SiteList::getSite
81 */
82 public function testGetSiteByGlobalId( SiteList $sites ) {
83 if ( $sites->isEmpty() ) {
84 $this->assertTrue( true );
85 } else {
86 /**
87 * @var Site $site
88 */
89 foreach ( $sites as $site ) {
90 $this->assertEquals( $site, $sites->getSite( $site->getGlobalId() ) );
91 }
92 }
93 }
94
95 /**
96 * @dataProvider siteListProvider
97 * @param SiteList $sites
98 * @covers SiteList::getSiteByInternalId
99 */
100 public function testGetSiteByInternalId( $sites ) {
101 /**
102 * @var Site $site
103 */
104 foreach ( $sites as $site ) {
105 if ( is_integer( $site->getInternalId() ) ) {
106 $this->assertEquals( $site, $sites->getSiteByInternalId( $site->getInternalId() ) );
107 }
108 }
109
110 $this->assertTrue( true );
111 }
112
113 /**
114 * @dataProvider siteListProvider
115 * @param SiteList $sites
116 * @covers SiteList::hasSite
117 */
118 public function testHasGlobalId( $sites ) {
119 $this->assertFalse( $sites->hasSite( 'non-existing-global-id' ) );
120 $this->assertFalse( $sites->hasInternalId( 720101010 ) );
121
122 if ( !$sites->isEmpty() ) {
123 /**
124 * @var Site $site
125 */
126 foreach ( $sites as $site ) {
127 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
128 }
129 }
130 }
131
132 /**
133 * @dataProvider siteListProvider
134 * @param SiteList $sites
135 * @covers SiteList::hasInternalId
136 */
137 public function testHasInternallId( $sites ) {
138 /**
139 * @var Site $site
140 */
141 foreach ( $sites as $site ) {
142 if ( is_integer( $site->getInternalId() ) ) {
143 $this->assertTrue( $site, $sites->hasInternalId( $site->getInternalId() ) );
144 }
145 }
146
147 $this->assertFalse( $sites->hasInternalId( -1 ) );
148 }
149
150 /**
151 * @dataProvider siteListProvider
152 * @param SiteList $sites
153 * @covers SiteList::getGlobalIdentifiers
154 */
155 public function testGetGlobalIdentifiers( SiteList $sites ) {
156 $identifiers = $sites->getGlobalIdentifiers();
157
158 $this->assertTrue( is_array( $identifiers ) );
159
160 $expected = array();
161
162 /**
163 * @var Site $site
164 */
165 foreach ( $sites as $site ) {
166 $expected[] = $site->getGlobalId();
167 }
168
169 $this->assertArrayEquals( $expected, $identifiers );
170 }
171
172 /**
173 * @dataProvider siteListProvider
174 *
175 * @since 1.21
176 *
177 * @param SiteList $list
178 * @covers SiteList::getSerializationData
179 * @covers SiteList::unserialize
180 */
181 public function testSerialization( SiteList $list ) {
182 $serialization = serialize( $list );
183 /**
184 * @var SiteArray $copy
185 */
186 $copy = unserialize( $serialization );
187
188 $this->assertArrayEquals( $list->getGlobalIdentifiers(), $copy->getGlobalIdentifiers() );
189
190 /**
191 * @var Site $site
192 */
193 foreach ( $list as $site ) {
194 $this->assertTrue( $copy->hasInternalId( $site->getInternalId() ) );
195 }
196 }
197 }