MediaWiki  1.33.0
DBSiteStoreTest.php
Go to the documentation of this file.
1 <?php
2 
4 
33 
37  private function newDBSiteStore() {
38  // NOTE: Use the real DB load balancer for now. Eventually, the test framework should
39  // provide a LoadBalancer that is safe to use in unit tests.
40  $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
41  return new DBSiteStore( $lb );
42  }
43 
47  public function testGetSites() {
48  $expectedSites = TestSites::getSites();
50 
51  $store = $this->newDBSiteStore();
52 
53  $sites = $store->getSites();
54 
55  $this->assertInstanceOf( SiteList::class, $sites );
56 
60  foreach ( $sites as $site ) {
61  $this->assertInstanceOf( Site::class, $site );
62  }
63 
64  foreach ( $expectedSites as $site ) {
65  if ( $site->getGlobalId() !== null ) {
66  $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
67  }
68  }
69  }
70 
74  public function testSaveSites() {
75  $store = $this->newDBSiteStore();
76 
77  $sites = [];
78 
79  $site = new Site();
80  $site->setGlobalId( 'ertrywuutr' );
81  $site->setLanguageCode( 'en' );
82  $sites[] = $site;
83 
84  $site = new MediaWikiSite();
85  $site->setGlobalId( 'sdfhxujgkfpth' );
86  $site->setLanguageCode( 'nl' );
87  $sites[] = $site;
88 
89  $this->assertTrue( $store->saveSites( $sites ) );
90 
91  $site = $store->getSite( 'ertrywuutr' );
92  $this->assertInstanceOf( Site::class, $site );
93  $this->assertEquals( 'en', $site->getLanguageCode() );
94  $this->assertTrue( is_int( $site->getInternalId() ) );
95  $this->assertTrue( $site->getInternalId() >= 0 );
96 
97  $site = $store->getSite( 'sdfhxujgkfpth' );
98  $this->assertInstanceOf( Site::class, $site );
99  $this->assertEquals( 'nl', $site->getLanguageCode() );
100  $this->assertTrue( is_int( $site->getInternalId() ) );
101  $this->assertTrue( $site->getInternalId() >= 0 );
102  }
103 
107  public function testReset() {
108  $store1 = $this->newDBSiteStore();
109  $store2 = $this->newDBSiteStore();
110 
111  // initialize internal cache
112  $this->assertGreaterThan( 0, $store1->getSites()->count() );
113  $this->assertGreaterThan( 0, $store2->getSites()->count() );
114 
115  // Clear actual data. Will purge the external cache and reset the internal
116  // cache in $store1, but not the internal cache in store2.
117  $this->assertTrue( $store1->clear() );
118 
119  // sanity check: $store2 should have a stale cache now
120  $this->assertNotNull( $store2->getSite( 'enwiki' ) );
121 
122  // purge cache
123  $store2->reset();
124 
125  // ...now the internal cache of $store2 should be updated and thus empty.
126  $site = $store2->getSite( 'enwiki' );
127  $this->assertNull( $site );
128  }
129 
133  public function testClear() {
134  $store = $this->newDBSiteStore();
135  $this->assertTrue( $store->clear() );
136 
137  $site = $store->getSite( 'enwiki' );
138  $this->assertNull( $site );
139 
140  $sites = $store->getSites();
141  $this->assertEquals( 0, $sites->count() );
142  }
143 
147  public function testGetSitesDefaultOrder() {
148  $store = $this->newDBSiteStore();
149  $siteB = new Site();
150  $siteB->setGlobalId( 'B' );
151  $siteA = new Site();
152  $siteA->setGlobalId( 'A' );
153  $store->saveSites( [ $siteB, $siteA ] );
154 
155  $sites = $store->getSites();
156  $siteIdentifiers = [];
158  foreach ( $sites as $site ) {
159  $siteIdentifiers[] = $site->getGlobalId();
160  }
161  $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
162 
163  // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
164  // tested separately.
165  $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );
166  }
167 }
TestSites\getSites
static getSites()
Definition: TestSites.php:36
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
DBSiteStoreTest
Definition: DBSiteStoreTest.php:32
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DBSiteStore
Definition: DBSiteStore.php:33
Site
Definition: Site.php:29
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
DBSiteStoreTest\testClear
testClear()
DBSiteStore::clear.
Definition: DBSiteStoreTest.php:133
DBSiteStoreTest\testGetSitesDefaultOrder
testGetSitesDefaultOrder()
DBSiteStore::getSites.
Definition: DBSiteStoreTest.php:147
DBSiteStoreTest\testGetSites
testGetSites()
DBSiteStore::getSites.
Definition: DBSiteStoreTest.php:47
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
DBSiteStoreTest\testReset
testReset()
DBSiteStore::reset.
Definition: DBSiteStoreTest.php:107
MediaWikiSite
Class representing a MediaWiki site.
Definition: MediaWikiSite.php:38
TestSites\insertIntoDb
static insertIntoDb()
Inserts sites into the database for the unit tests that need them.
Definition: TestSites.php:107
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
DBSiteStoreTest\newDBSiteStore
newDBSiteStore()
Definition: DBSiteStoreTest.php:37
DBSiteStoreTest\testSaveSites
testSaveSites()
DBSiteStore::saveSites.
Definition: DBSiteStoreTest.php:74