MediaWiki  1.33.0
CategoryTest.php
Go to the documentation of this file.
1 <?php
2 
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 
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 
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 
83  public function testNewFromName_validTitle() {
84  $category = Category::newFromName( 'Example' );
85  $this->assertSame( 'Example', $category->getName() );
86  }
87 
91  public function testNewFromName_invalidTitle() {
92  $this->assertFalse( Category::newFromName( '#' ) );
93  }
94 
98  public function testNewFromTitle() {
99  $title = Title::newFromText( 'Category:Example' );
100  $category = Category::newFromTitle( $title );
101  $this->assertSame( 'Example', $category->getName() );
102  }
103 
108  public function testNewFromID() {
109  $category = Category::newFromID( 5 );
110  $this->assertSame( 5, $category->getID() );
111  }
112 
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 
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 
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 
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 
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 
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 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
CategoryTest\provideInitializeVariants
provideInitializeVariants()
Definition: CategoryTest.php:28
CategoryTest\testNewFromName_validTitle
testNewFromName_validTitle()
Category::newFromName() Category::getName()
Definition: CategoryTest.php:83
Category
Category objects are immutable, strictly speaking.
Definition: Category.php:31
CategoryTest\testNewFromRow_notFoundWithoutTitle
testNewFromRow_notFoundWithoutTitle()
Category::newFromRow()
Definition: CategoryTest.php:147
CategoryTest\testInitialize
testInitialize( $createFunction, $createParam, $testFunction, $expected)
Category::initialize() provideInitializeVariants.
Definition: CategoryTest.php:57
CategoryTest\testNewFromName_invalidTitle
testNewFromName_invalidTitle()
Category::newFromName()
Definition: CategoryTest.php:91
CategoryTest\testGetFileCount
testGetFileCount()
Category::getFileCount()
Definition: CategoryTest.php:266
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
CategoryTest\testGetSubcatCount
testGetSubcatCount()
Category::getSubcatCount()
Definition: CategoryTest.php:241
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
CategoryTest
Database Category.
Definition: CategoryTest.php:7
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
MediaWikiTestCase\setUserLang
setUserLang( $lang)
Definition: MediaWikiTestCase.php:1057
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Category\newFromRow
static newFromRow( $row, $title=null)
Factory function, for constructing a Category object from a result set.
Definition: Category.php:179
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:1066
Category\newFromTitle
static newFromTitle( $title)
Factory function.
Definition: Category.php:146
CategoryTest\testNewFromID
testNewFromID()
Category::newFromID() Category::getID()
Definition: CategoryTest.php:108
CategoryTest\testInitialize_idNotExist
testInitialize_idNotExist()
Category::initialize()
Definition: CategoryTest.php:23
Category\newFromID
static newFromID( $id)
Factory function.
Definition: Category.php:161
CategoryTest\testNewFromRow_found
testNewFromRow_found()
Category::newFromRow()
Definition: CategoryTest.php:116
Category\newFromName
static newFromName( $name)
Factory function.
Definition: Category.php:126
CategoryTest\setUp
setUp()
Definition: CategoryTest.php:8
CategoryTest\testGetPageCount
testGetPageCount()
Category::getPageCount()
Definition: CategoryTest.php:216
CategoryTest\testNewFromTitle
testNewFromTitle()
Category::newFromTitle()
Definition: CategoryTest.php:98
CategoryTest\testNewFromRow_notFoundWithTitle
testNewFromRow_notFoundWithTitle()
Category::newFromRow()
Definition: CategoryTest.php:179