Merge "Slight improvements to FormSpecialPage behavior."
[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 */
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 if ( $sites->isEmpty() ) {
82 $this->assertTrue( true );
83 } else {
84 /**
85 * @var Site $site
86 */
87 foreach ( $sites as $site ) {
88 $this->assertEquals( $site, $sites->getSite( $site->getGlobalId() ) );
89 }
90 }
91 }
92
93 /**
94 * @dataProvider siteListProvider
95 * @param SiteList $sites
96 */
97 public function testGetSiteByInternalId( $sites ) {
98 /**
99 * @var Site $site
100 */
101 foreach ( $sites as $site ) {
102 if ( is_integer( $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 */
114 public function testHasGlobalId( $sites ) {
115 $this->assertFalse( $sites->hasSite( 'non-existing-global-id' ) );
116 $this->assertFalse( $sites->hasInternalId( 720101010 ) );
117
118 if ( !$sites->isEmpty() ) {
119 /**
120 * @var Site $site
121 */
122 foreach ( $sites as $site ) {
123 $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
124 }
125 }
126 }
127
128 /**
129 * @dataProvider siteListProvider
130 * @param SiteList $sites
131 */
132 public function testHasInternallId( $sites ) {
133 /**
134 * @var Site $site
135 */
136 foreach ( $sites as $site ) {
137 if ( is_integer( $site->getInternalId() ) ) {
138 $this->assertTrue( $site, $sites->hasInternalId( $site->getInternalId() ) );
139 }
140 }
141
142 $this->assertFalse( $sites->hasInternalId( -1 ) );
143 }
144
145 /**
146 * @dataProvider siteListProvider
147 * @param SiteList $sites
148 */
149 public function testGetGlobalIdentifiers( SiteList $sites ) {
150 $identifiers = $sites->getGlobalIdentifiers();
151
152 $this->assertTrue( is_array( $identifiers ) );
153
154 $expected = array();
155
156 /**
157 * @var Site $site
158 */
159 foreach ( $sites as $site ) {
160 $expected[] = $site->getGlobalId();
161 }
162
163 $this->assertArrayEquals( $expected, $identifiers );
164 }
165
166 /**
167 * @dataProvider siteListProvider
168 *
169 * @since 1.21
170 *
171 * @param SiteList $list
172 */
173 public function testSerialization( SiteList $list ) {
174 $serialization = serialize( $list );
175 /**
176 * @var SiteArray $copy
177 */
178 $copy = unserialize( $serialization );
179
180 $this->assertArrayEquals( $list->getGlobalIdentifiers(), $copy->getGlobalIdentifiers() );
181
182 /**
183 * @var Site $site
184 */
185 foreach ( $list as $site ) {
186 $this->assertTrue( $copy->hasInternalId( $site->getInternalId() ) );
187 }
188 }
189 }