7 class CategoryTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( [
12 'wgAllowUserJs' => false,
13 'wgDefaultLanguageVariant' => false,
14 'wgMetaNamespace' => 'Project',
16 $this->setUserLang( 'en' );
17 $this->setContentLang( 'en' );
21 * @covers Category::initialize()
23 public function testInitialize_idNotExist() {
24 $category = Category
::newFromID( -1 );
25 $this->assertFalse( $category->getName() );
28 public function provideInitializeVariants() {
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 ],
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 ],
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 ]
54 * @covers Category::initialize()
55 * @dataProvider provideInitializeVariants
57 public function testInitialize( $createFunction, $createParam, $testFunction, $expected ) {
58 $dbw = wfGetDB( DB_MASTER
);
59 $dbw->insert( 'category',
63 'cat_title' => 'Example',
73 $category = Category
::{$createFunction}( $createParam );
74 $this->assertEquals( $expected, $category->{$testFunction}() );
76 $dbw->delete( 'category', '*', __METHOD__
);
80 * @covers Category::newFromName()
81 * @covers Category::getName()
83 public function testNewFromName_validTitle() {
84 $category = Category
::newFromName( 'Example' );
85 $this->assertSame( 'Example', $category->getName() );
89 * @covers Category::newFromName()
91 public function testNewFromName_invalidTitle() {
92 $this->assertFalse( Category
::newFromName( '#' ) );
96 * @covers Category::newFromTitle()
98 public function testNewFromTitle() {
99 $title = Title
::newFromText( 'Category:Example' );
100 $category = Category
::newFromTitle( $title );
101 $this->assertSame( 'Example', $category->getName() );
105 * @covers Category::newFromID()
106 * @covers Category::getID()
108 public function testNewFromID() {
109 $category = Category
::newFromID( 5 );
110 $this->assertSame( 5, $category->getID() );
114 * @covers Category::newFromRow()
116 public function testNewFromRow_found() {
117 $dbw = wfGetDB( DB_MASTER
);
118 $dbw->insert( 'category',
122 'cat_title' => 'Example',
132 $category = Category
::newFromRow( $dbw->selectRow(
134 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
139 $this->assertEquals( 1, $category->getID() );
141 $dbw->delete( 'category', '*', __METHOD__
);
145 * @covers Category::newFromRow()
147 public function testNewFromRow_notFoundWithoutTitle() {
148 $dbw = wfGetDB( DB_MASTER
);
149 $dbw->insert( 'category',
153 'cat_title' => 'Example',
163 $row = $dbw->selectRow(
165 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
169 $row->cat_title
= null;
171 $this->assertFalse( Category
::newFromRow( $row ) );
173 $dbw->delete( 'category', '*', __METHOD__
);
177 * @covers Category::newFromRow()
179 public function testNewFromRow_notFoundWithTitle() {
180 $dbw = wfGetDB( DB_MASTER
);
181 $dbw->insert( 'category',
185 'cat_title' => 'Example',
195 $row = $dbw->selectRow(
197 [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ],
201 $row->cat_title
= null;
203 $category = Category
::newFromRow(
205 Title
::newFromText( NS_CATEGORY
, 'Example' )
208 $this->assertFalse( $category->getID() );
210 $dbw->delete( 'category', '*', __METHOD__
);
214 * @covers Category::getPageCount()
216 public function testGetPageCount() {
217 $dbw = wfGetDB( DB_MASTER
);
218 $dbw->insert( 'category',
222 'cat_title' => 'Example',
232 $category = Category
::newFromID( 1 );
233 $this->assertEquals( 3, $category->getPageCount() );
235 $dbw->delete( 'category', '*', __METHOD__
);
239 * @covers Category::getSubcatCount()
241 public function testGetSubcatCount() {
242 $dbw = wfGetDB( DB_MASTER
);
243 $dbw->insert( 'category',
247 'cat_title' => 'Example',
257 $category = Category
::newFromID( 1 );
258 $this->assertEquals( 4, $category->getSubcatCount() );
260 $dbw->delete( 'category', '*', __METHOD__
);
264 * @covers Category::getFileCount()
266 public function testGetFileCount() {
267 $dbw = wfGetDB( DB_MASTER
);
268 $dbw->insert( 'category',
272 'cat_title' => 'Example',
282 $category = Category
::newFromID( 1 );
283 $this->assertEquals( 5, $category->getFileCount() );
285 $dbw->delete( 'category', '*', __METHOD__
);