MediaWiki  1.30.0
DBSiteStoreTest.php
Go to the documentation of this file.
1 <?php
2 
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  return new DBSiteStore( wfGetLB() );
41  }
42 
46  public function testGetSites() {
47  $expectedSites = TestSites::getSites();
49 
50  $store = $this->newDBSiteStore();
51 
52  $sites = $store->getSites();
53 
54  $this->assertInstanceOf( 'SiteList', $sites );
55 
59  foreach ( $sites as $site ) {
60  $this->assertInstanceOf( 'Site', $site );
61  }
62 
63  foreach ( $expectedSites as $site ) {
64  if ( $site->getGlobalId() !== null ) {
65  $this->assertTrue( $sites->hasSite( $site->getGlobalId() ) );
66  }
67  }
68  }
69 
73  public function testSaveSites() {
74  $store = $this->newDBSiteStore();
75 
76  $sites = [];
77 
78  $site = new Site();
79  $site->setGlobalId( 'ertrywuutr' );
80  $site->setLanguageCode( 'en' );
81  $sites[] = $site;
82 
83  $site = new MediaWikiSite();
84  $site->setGlobalId( 'sdfhxujgkfpth' );
85  $site->setLanguageCode( 'nl' );
86  $sites[] = $site;
87 
88  $this->assertTrue( $store->saveSites( $sites ) );
89 
90  $site = $store->getSite( 'ertrywuutr' );
91  $this->assertInstanceOf( 'Site', $site );
92  $this->assertEquals( 'en', $site->getLanguageCode() );
93  $this->assertTrue( is_integer( $site->getInternalId() ) );
94  $this->assertTrue( $site->getInternalId() >= 0 );
95 
96  $site = $store->getSite( 'sdfhxujgkfpth' );
97  $this->assertInstanceOf( 'Site', $site );
98  $this->assertEquals( 'nl', $site->getLanguageCode() );
99  $this->assertTrue( is_integer( $site->getInternalId() ) );
100  $this->assertTrue( $site->getInternalId() >= 0 );
101  }
102 
106  public function testReset() {
107  $store1 = $this->newDBSiteStore();
108  $store2 = $this->newDBSiteStore();
109 
110  // initialize internal cache
111  $this->assertGreaterThan( 0, $store1->getSites()->count() );
112  $this->assertGreaterThan( 0, $store2->getSites()->count() );
113 
114  // Clear actual data. Will purge the external cache and reset the internal
115  // cache in $store1, but not the internal cache in store2.
116  $this->assertTrue( $store1->clear() );
117 
118  // sanity check: $store2 should have a stale cache now
119  $this->assertNotNull( $store2->getSite( 'enwiki' ) );
120 
121  // purge cache
122  $store2->reset();
123 
124  // ...now the internal cache of $store2 should be updated and thus empty.
125  $site = $store2->getSite( 'enwiki' );
126  $this->assertNull( $site );
127  }
128 
132  public function testClear() {
133  $store = $this->newDBSiteStore();
134  $this->assertTrue( $store->clear() );
135 
136  $site = $store->getSite( 'enwiki' );
137  $this->assertNull( $site );
138 
139  $sites = $store->getSites();
140  $this->assertEquals( 0, $sites->count() );
141  }
142 
146  public function testGetSitesDefaultOrder() {
147  $store = $this->newDBSiteStore();
148  $siteB = new Site();
149  $siteB->setGlobalId( 'B' );
150  $siteA = new Site();
151  $siteA->setGlobalId( 'A' );
152  $store->saveSites( [ $siteB, $siteA ] );
153 
154  $sites = $store->getSites();
155  $siteIdentifiers = [];
157  foreach ( $sites as $site ) {
158  $siteIdentifiers[] = $site->getGlobalId();
159  }
160  $this->assertSame( [ 'A', 'B' ], $siteIdentifiers );
161 
162  // Note: SiteList::getGlobalIdentifiers uses an other internal state. Iteration must be
163  // tested separately.
164  $this->assertSame( [ 'A', 'B' ], $sites->getGlobalIdentifiers() );
165  }
166 }
TestSites\getSites
static getSites()
Definition: TestSites.php:38
wfGetLB
wfGetLB( $wiki=false)
Get a load balancer object.
Definition: GlobalFunctions.php:2869
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:15
DBSiteStore
Definition: DBSiteStore.php:33
Site
Definition: Site.php:29
DBSiteStoreTest\testClear
testClear()
DBSiteStore::clear.
Definition: DBSiteStoreTest.php:132
DBSiteStoreTest\testGetSitesDefaultOrder
testGetSitesDefaultOrder()
DBSiteStore::getSites.
Definition: DBSiteStoreTest.php:146
DBSiteStoreTest\testGetSites
testGetSites()
DBSiteStore::getSites.
Definition: DBSiteStoreTest.php:46
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:106
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:109
DBSiteStoreTest\newDBSiteStore
newDBSiteStore()
Definition: DBSiteStoreTest.php:37
DBSiteStoreTest\testSaveSites
testSaveSites()
DBSiteStore::saveSites.
Definition: DBSiteStoreTest.php:73