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