Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / CategoryTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Category
6 */
7 class CategoryTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
10
11 $this->setMwGlobals( [
12 'wgAllowUserJs' => false,
13 'wgDefaultLanguageVariant' => false,
14 'wgMetaNamespace' => 'Project',
15 ] );
16 $this->setUserLang( 'en' );
17 $this->setContentLang( 'en' );
18 }
19
20 /**
21 * @covers Category::initialize()
22 */
23 public function testInitialize_idNotExist() {
24 $category = Category::newFromID( -1 );
25 $this->assertFalse( $category->getName() );
26 }
27
28 public function provideInitializeVariants() {
29 return [
30 // Existing title
31 [ 'newFromName', 'Example', 'getID', 1 ],
32 [ 'newFromName', 'Example', 'getName', 'Example' ],
33 [ 'newFromName', 'Example', 'getPageCount', 3 ],
34 [ 'newFromName', 'Example', 'getSubcatCount', 4 ],
35 [ 'newFromName', 'Example', 'getFileCount', 5 ],
36
37 // Non-existing title
38 [ 'newFromName', 'NoExample', 'getID', 0 ],
39 [ 'newFromName', 'NoExample', 'getName', 'NoExample' ],
40 [ 'newFromName', 'NoExample', 'getPageCount', 0 ],
41 [ 'newFromName', 'NoExample', 'getSubcatCount', 0 ],
42 [ 'newFromName', 'NoExample', 'getFileCount', 0 ],
43
44 // Existing ID
45 [ 'newFromID', 1, 'getID', 1 ],
46 [ 'newFromID', 1, 'getName', 'Example' ],
47 [ 'newFromID', 1, 'getPageCount', 3 ],
48 [ 'newFromID', 1, 'getSubcatCount', 4 ],
49 [ 'newFromID', 1, 'getFileCount', 5 ]
50 ];
51 }
52
53 /**
54 * @covers Category::initialize()
55 * @dataProvider provideInitializeVariants
56 */
57 public function testInitialize( $createFunction, $createParam, $testFunction, $expected ) {
58 $dbw = wfGetDB( DB_MASTER );
59 $dbw->insert( 'category',
60 [
61 [
62 'cat_id' => 1,
63 'cat_title' => 'Example',
64 'cat_pages' => 3,
65 'cat_subcats' => 4,
66 'cat_files' => 5
67 ]
68 ],
69 __METHOD__,
70 [ 'IGNORE' ]
71 );
72
73 $category = Category::{$createFunction}( $createParam );
74 $this->assertEquals( $expected, $category->{$testFunction}() );
75
76 $dbw->delete( 'category', '*', __METHOD__ );
77 }
78
79 /**
80 * @covers Category::newFromName()
81 * @covers Category::getName()
82 */
83 public function testNewFromName_validTitle() {
84 $category = Category::newFromName( 'Example' );
85 $this->assertSame( 'Example', $category->getName() );
86 }
87
88 /**
89 * @covers Category::newFromName()
90 */
91 public function testNewFromName_invalidTitle() {
92 $this->assertFalse( Category::newFromName( '#' ) );
93 }
94
95 /**
96 * @covers Category::newFromTitle()
97 */
98 public function testNewFromTitle() {
99 $title = Title::newFromText( 'Category:Example' );
100 $category = Category::newFromTitle( $title );
101 $this->assertSame( 'Example', $category->getName() );
102 }
103
104 /**
105 * @covers Category::newFromID()
106 * @covers Category::getID()
107 */
108 public function testNewFromID() {
109 $category = Category::newFromID( 5 );
110 $this->assertSame( 5, $category->getID() );
111 }
112
113 /**
114 * @covers Category::newFromRow()
115 */
116 public function testNewFromRow_found() {
117 $dbw = wfGetDB( DB_MASTER );
118 $dbw->insert( 'category',
119 [
120 [
121 'cat_id' => 1,
122 'cat_title' => 'Example',
123 'cat_pages' => 3,
124 'cat_subcats' => 4,
125 'cat_files' => 5
126 ]
127 ],
128 __METHOD__,
129 [ 'IGNORE' ]
130 );
131
132 $category = Category::newFromRow( $dbw->selectRow(
133 'category',
134 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
135 [ 'cat_id' => 1 ],
136 __METHOD__
137 ) );
138
139 $this->assertEquals( 1, $category->getID() );
140
141 $dbw->delete( 'category', '*', __METHOD__ );
142 }
143
144 /**
145 * @covers Category::newFromRow()
146 */
147 public function testNewFromRow_notFoundWithoutTitle() {
148 $dbw = wfGetDB( DB_MASTER );
149 $dbw->insert( 'category',
150 [
151 [
152 'cat_id' => 1,
153 'cat_title' => 'Example',
154 'cat_pages' => 3,
155 'cat_subcats' => 4,
156 'cat_files' => 5
157 ]
158 ],
159 __METHOD__,
160 [ 'IGNORE' ]
161 );
162
163 $row = $dbw->selectRow(
164 'category',
165 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
166 [ 'cat_id' => 1 ],
167 __METHOD__
168 );
169 $row->cat_title = null;
170
171 $this->assertFalse( Category::newFromRow( $row ) );
172
173 $dbw->delete( 'category', '*', __METHOD__ );
174 }
175
176 /**
177 * @covers Category::newFromRow()
178 */
179 public function testNewFromRow_notFoundWithTitle() {
180 $dbw = wfGetDB( DB_MASTER );
181 $dbw->insert( 'category',
182 [
183 [
184 'cat_id' => 1,
185 'cat_title' => 'Example',
186 'cat_pages' => 3,
187 'cat_subcats' => 4,
188 'cat_files' => 5
189 ]
190 ],
191 __METHOD__,
192 [ 'IGNORE' ]
193 );
194
195 $row = $dbw->selectRow(
196 'category',
197 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
198 [ 'cat_id' => 1 ],
199 __METHOD__
200 );
201 $row->cat_title = null;
202
203 $category = Category::newFromRow(
204 $row,
205 Title::newFromText( NS_CATEGORY, 'Example' )
206 );
207
208 $this->assertFalse( $category->getID() );
209
210 $dbw->delete( 'category', '*', __METHOD__ );
211 }
212
213 /**
214 * @covers Category::getPageCount()
215 */
216 public function testGetPageCount() {
217 $dbw = wfGetDB( DB_MASTER );
218 $dbw->insert( 'category',
219 [
220 [
221 'cat_id' => 1,
222 'cat_title' => 'Example',
223 'cat_pages' => 3,
224 'cat_subcats' => 4,
225 'cat_files' => 5
226 ]
227 ],
228 __METHOD__,
229 [ 'IGNORE' ]
230 );
231
232 $category = Category::newFromID( 1 );
233 $this->assertEquals( 3, $category->getPageCount() );
234
235 $dbw->delete( 'category', '*', __METHOD__ );
236 }
237
238 /**
239 * @covers Category::getSubcatCount()
240 */
241 public function testGetSubcatCount() {
242 $dbw = wfGetDB( DB_MASTER );
243 $dbw->insert( 'category',
244 [
245 [
246 'cat_id' => 1,
247 'cat_title' => 'Example',
248 'cat_pages' => 3,
249 'cat_subcats' => 4,
250 'cat_files' => 5
251 ]
252 ],
253 __METHOD__,
254 [ 'IGNORE' ]
255 );
256
257 $category = Category::newFromID( 1 );
258 $this->assertEquals( 4, $category->getSubcatCount() );
259
260 $dbw->delete( 'category', '*', __METHOD__ );
261 }
262
263 /**
264 * @covers Category::getFileCount()
265 */
266 public function testGetFileCount() {
267 $dbw = wfGetDB( DB_MASTER );
268 $dbw->insert( 'category',
269 [
270 [
271 'cat_id' => 1,
272 'cat_title' => 'Example',
273 'cat_pages' => 3,
274 'cat_subcats' => 4,
275 'cat_files' => 5
276 ]
277 ],
278 __METHOD__,
279 [ 'IGNORE' ]
280 );
281
282 $category = Category::newFromID( 1 );
283 $this->assertEquals( 5, $category->getFileCount() );
284
285 $dbw->delete( 'category', '*', __METHOD__ );
286 }
287 }