| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Category\Category; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use MediaWiki\Title\Title; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class CategoryTest extends MediaWikiIntegrationTestCase { |
| 13 |
protected function setUp(): void { |
| 14 |
parent::setUp(); |
| 15 |
|
| 16 |
$this->setUserLang( 'en' ); |
| 17 |
|
| 18 |
$this->overrideConfigValues( [ |
| 19 |
MainConfigNames::AllowUserJs => false, |
| 20 |
MainConfigNames::DefaultLanguageVariant => false, |
| 21 |
MainConfigNames::MetaNamespace => 'Project', |
| 22 |
MainConfigNames::LanguageCode => 'en', |
| 23 |
] ); |
| 24 |
} |
| 25 |
|
| 26 |
public function addDBData() { |
| 27 |
$this->getDb()->newInsertQueryBuilder() |
| 28 |
->insertInto( 'category' ) |
| 29 |
->ignore() |
| 30 |
->row( [ |
| 31 |
'cat_id' => 1, |
| 32 |
'cat_title' => 'Example', |
| 33 |
'cat_pages' => 3, |
| 34 |
'cat_subcats' => 4, |
| 35 |
'cat_files' => 5 |
| 36 |
] ) |
| 37 |
->caller( __METHOD__ ) |
| 38 |
->execute(); |
| 39 |
} |
| 40 |
|
| 41 |
public function testInitialize_idNotExist() { |
| 42 |
$category = Category::newFromID( -1 ); |
| 43 |
$this->assertFalse( $category->getName() ); |
| 44 |
} |
| 45 |
|
| 46 |
public static function provideInitializeVariants() { |
| 47 |
return [ |
| 48 |
|
| 49 |
[ 'newFromName', 'Example', 'getID', 1 ], |
| 50 |
[ 'newFromName', 'Example', 'getName', 'Example' ], |
| 51 |
[ 'newFromName', 'Example', 'getMemberCount', 3 ], |
| 52 |
[ 'newFromName', 'Example', 'getSubcatCount', 4 ], |
| 53 |
[ 'newFromName', 'Example', 'getFileCount', 5 ], |
| 54 |
|
| 55 |
|
| 56 |
[ 'newFromName', 'NoExample', 'getID', 0 ], |
| 57 |
[ 'newFromName', 'NoExample', 'getName', 'NoExample' ], |
| 58 |
[ 'newFromName', 'NoExample', 'getMemberCount', 0 ], |
| 59 |
[ 'newFromName', 'NoExample', 'getSubcatCount', 0 ], |
| 60 |
[ 'newFromName', 'NoExample', 'getFileCount', 0 ], |
| 61 |
|
| 62 |
|
| 63 |
[ 'newFromID', 1, 'getID', 1 ], |
| 64 |
[ 'newFromID', 1, 'getName', 'Example' ], |
| 65 |
[ 'newFromID', 1, 'getMemberCount', 3 ], |
| 66 |
[ 'newFromID', 1, 'getSubcatCount', 4 ], |
| 67 |
[ 'newFromID', 1, 'getFileCount', 5 ] |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function testInitialize( $createFunction, $createParam, $testFunction, $expected ) { |
| 75 |
$category = Category::{$createFunction}( $createParam ); |
| 76 |
$this->assertEquals( $expected, $category->{$testFunction}() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function testNewFromName_validTitle() { |
| 80 |
$category = Category::newFromName( 'Example' ); |
| 81 |
$this->assertSame( 'Example', $category->getName() ); |
| 82 |
} |
| 83 |
|
| 84 |
public function testNewFromName_invalidTitle() { |
| 85 |
$this->assertFalse( Category::newFromName( '#' ) ); |
| 86 |
} |
| 87 |
|
| 88 |
public function testNewFromTitle() { |
| 89 |
$title = Title::makeTitle( NS_CATEGORY, 'Example' ); |
| 90 |
$category = Category::newFromTitle( $title ); |
| 91 |
$this->assertSame( 'Example', $category->getName() ); |
| 92 |
$this->assertTrue( $title->isSamePageAs( $category->getPage() ) ); |
| 93 |
$this->assertTrue( $title->isSamePageAs( $category->getTitle() ) ); |
| 94 |
} |
| 95 |
|
| 96 |
public function testNewFromID() { |
| 97 |
$category = Category::newFromID( 5 ); |
| 98 |
$this->assertSame( 5, $category->getID() ); |
| 99 |
} |
| 100 |
|
| 101 |
public function testNewFromRow_found() { |
| 102 |
$category = Category::newFromRow( $this->getDb()->newSelectQueryBuilder() |
| 103 |
->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] ) |
| 104 |
->from( 'category' ) |
| 105 |
->where( [ 'cat_id' => 1 ] ) |
| 106 |
->caller( __METHOD__ )->fetchRow() |
| 107 |
); |
| 108 |
|
| 109 |
$this->assertSame( '1', $category->getID() ); |
| 110 |
} |
| 111 |
|
| 112 |
public function testNewFromRow_notFoundWithoutTitle() { |
| 113 |
$row = $this->getDb()->newSelectQueryBuilder() |
| 114 |
->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] ) |
| 115 |
->from( 'category' ) |
| 116 |
->where( [ 'cat_id' => 1 ] ) |
| 117 |
->caller( __METHOD__ )->fetchRow(); |
| 118 |
$row->cat_title = null; |
| 119 |
|
| 120 |
$this->assertFalse( Category::newFromRow( $row ) ); |
| 121 |
} |
| 122 |
|
| 123 |
public function testNewFromRow_notFoundWithTitle() { |
| 124 |
$row = $this->getDb()->newSelectQueryBuilder() |
| 125 |
->select( [ 'cat_id', 'cat_title', 'cat_pages', 'cat_subcats', 'cat_files' ] ) |
| 126 |
->from( 'category' ) |
| 127 |
->where( [ 'cat_id' => 1 ] ) |
| 128 |
->caller( __METHOD__ )->fetchRow(); |
| 129 |
$row->cat_title = null; |
| 130 |
|
| 131 |
$category = Category::newFromRow( |
| 132 |
$row, |
| 133 |
Title::makeTitle( NS_CATEGORY, 'Example' ) |
| 134 |
); |
| 135 |
|
| 136 |
$this->assertFalse( $category->getID() ); |
| 137 |
} |
| 138 |
|
| 139 |
public function testGetCounts() { |
| 140 |
|
| 141 |
$category = Category::newFromID( 1 ); |
| 142 |
$this->assertEquals( 3, $category->getMemberCount() ); |
| 143 |
$this->assertEquals( 4, $category->getSubcatCount() ); |
| 144 |
$this->assertEquals( 5, $category->getFileCount() ); |
| 145 |
} |
| 146 |
} |