MediaWiki  1.32.5
DeferredUpdatesTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
14  public function testAddAndRun() {
15  $update = $this->getMockBuilder( DeferrableUpdate::class )
16  ->setMethods( [ 'doUpdate' ] )->getMock();
17  $update->expects( $this->once() )->method( 'doUpdate' );
18 
19  DeferredUpdates::addUpdate( $update );
21  }
22 
27  public function testAddMergeable() {
28  $this->setMwGlobals( 'wgCommandLineMode', false );
29 
30  $update1 = $this->getMockBuilder( MergeableUpdate::class )
31  ->setMethods( [ 'merge', 'doUpdate' ] )->getMock();
32  $update1->expects( $this->once() )->method( 'merge' );
33  $update1->expects( $this->never() )->method( 'doUpdate' );
34 
35  $update2 = $this->getMockBuilder( MergeableUpdate::class )
36  ->setMethods( [ 'merge', 'doUpdate' ] )->getMock();
37  $update2->expects( $this->never() )->method( 'merge' );
38  $update2->expects( $this->never() )->method( 'doUpdate' );
39 
40  DeferredUpdates::addUpdate( $update1 );
41  DeferredUpdates::addUpdate( $update2 );
42  }
43 
48  public function testAddCallableUpdate() {
49  $this->setMwGlobals( 'wgCommandLineMode', true );
50 
51  $ran = 0;
52  DeferredUpdates::addCallableUpdate( function () use ( &$ran ) {
53  $ran++;
54  } );
56 
57  $this->assertSame( 1, $ran, 'Update ran' );
58  }
59 
64  public function testGetPendingUpdates() {
65  // Prevent updates from running
66  $this->setMwGlobals( 'wgCommandLineMode', false );
67 
70  $all = DeferredUpdates::ALL;
71 
72  $update = $this->getMock( DeferrableUpdate::class );
73  $update->expects( $this->never() )
74  ->method( 'doUpdate' );
75 
76  DeferredUpdates::addUpdate( $update, $pre );
77  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $pre ) );
78  $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $post ) );
79  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
80  $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
82  $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
83 
84  DeferredUpdates::addUpdate( $update, $post );
85  $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $pre ) );
86  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $post ) );
87  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
88  $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
90  $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
91  }
92 
98  public function testDoUpdatesWeb() {
99  $this->setMwGlobals( 'wgCommandLineMode', false );
100 
101  $updates = [
102  '1' => "deferred update 1;\n",
103  '2' => "deferred update 2;\n",
104  '2-1' => "deferred update 1 within deferred update 2;\n",
105  '2-2' => "deferred update 2 within deferred update 2;\n",
106  '3' => "deferred update 3;\n",
107  '3-1' => "deferred update 1 within deferred update 3;\n",
108  '3-2' => "deferred update 2 within deferred update 3;\n",
109  '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
110  '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
111  ];
113  function () use ( $updates ) {
114  echo $updates['1'];
115  }
116  );
118  function () use ( $updates ) {
119  echo $updates['2'];
121  function () use ( $updates ) {
122  echo $updates['2-1'];
123  }
124  );
126  function () use ( $updates ) {
127  echo $updates['2-2'];
128  }
129  );
130  }
131  );
133  function () use ( $updates ) {
134  echo $updates['3'];
136  function () use ( $updates ) {
137  echo $updates['3-1'];
139  function () use ( $updates ) {
140  echo $updates['3-1-1'];
141  }
142  );
143  }
144  );
146  function () use ( $updates ) {
147  echo $updates['3-2'];
149  function () use ( $updates ) {
150  echo $updates['3-2-1'];
151  }
152  );
153  }
154  );
155  }
156  );
157 
158  $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() );
159 
160  $this->expectOutputString( implode( '', $updates ) );
161 
163 
164  $x = null;
165  $y = null;
167  function () use ( &$x ) {
168  $x = 'Sherity';
169  },
171  );
173  function () use ( &$y ) {
174  $y = 'Marychu';
175  },
177  );
178 
179  $this->assertNull( $x, "Update not run yet" );
180  $this->assertNull( $y, "Update not run yet" );
181 
183  $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
184  $this->assertNull( $y, "POSTSEND update not run yet" );
185 
187  $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
188  }
189 
195  public function testDoUpdatesCLI() {
196  $this->setMwGlobals( 'wgCommandLineMode', true );
197  $updates = [
198  '1' => "deferred update 1;\n",
199  '2' => "deferred update 2;\n",
200  '2-1' => "deferred update 1 within deferred update 2;\n",
201  '2-2' => "deferred update 2 within deferred update 2;\n",
202  '3' => "deferred update 3;\n",
203  '3-1' => "deferred update 1 within deferred update 3;\n",
204  '3-2' => "deferred update 2 within deferred update 3;\n",
205  '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
206  '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
207  ];
208 
209  // clear anything
210  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
211  $lbFactory->commitMasterChanges( __METHOD__ );
212 
214  function () use ( $updates ) {
215  echo $updates['1'];
216  }
217  );
219  function () use ( $updates ) {
220  echo $updates['2'];
222  function () use ( $updates ) {
223  echo $updates['2-1'];
224  }
225  );
227  function () use ( $updates ) {
228  echo $updates['2-2'];
229  }
230  );
231  }
232  );
234  function () use ( $updates ) {
235  echo $updates['3'];
237  function () use ( $updates ) {
238  echo $updates['3-1'];
240  function () use ( $updates ) {
241  echo $updates['3-1-1'];
242  }
243  );
244  }
245  );
247  function () use ( $updates ) {
248  echo $updates['3-2'];
250  function () use ( $updates ) {
251  echo $updates['3-2-1'];
252  }
253  );
254  }
255  );
256  }
257  );
258 
259  $this->expectOutputString( implode( '', $updates ) );
260 
262  }
263 
269  public function testPresendAddOnPostsendRun() {
270  $this->setMwGlobals( 'wgCommandLineMode', true );
271 
272  $x = false;
273  $y = false;
274  // clear anything
275  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
276  $lbFactory->commitMasterChanges( __METHOD__ );
277 
279  function () use ( &$x, &$y ) {
280  $x = true;
282  function () use ( &$y ) {
283  $y = true;
284  },
286  );
287  },
289  );
290 
292 
293  $this->assertTrue( $x, "Outer POSTSEND update ran" );
294  $this->assertTrue( $y, "Nested PRESEND update ran" );
295  }
296 
300  public function testRunUpdateTransactionScope() {
301  $this->setMwGlobals( 'wgCommandLineMode', false );
302 
303  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
304  $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' );
305 
306  $ran = 0;
307  DeferredUpdates::addCallableUpdate( function () use ( &$ran, $lbFactory ) {
308  $ran++;
309  $this->assertTrue( $lbFactory->hasTransactionRound(), 'Has transaction' );
310  } );
312 
313  $this->assertSame( 1, $ran, 'Update ran' );
314  $this->assertFalse( $lbFactory->hasTransactionRound(), 'Final state' );
315  }
316 
321  public function testRunOuterScopeUpdate() {
322  $this->setMwGlobals( 'wgCommandLineMode', false );
323 
324  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
325  $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' );
326 
327  $ran = 0;
329  function () use ( &$ran, $lbFactory ) {
330  $ran++;
331  $this->assertFalse( $lbFactory->hasTransactionRound(), 'No transaction' );
332  } )
333  );
335 
336  $this->assertSame( 1, $ran, 'Update ran' );
337  }
338 
342  public function testTryOpportunisticExecute() {
343  $calls = [];
344  $callback1 = function () use ( &$calls ) {
345  $calls[] = 1;
346  };
347  $callback2 = function () use ( &$calls ) {
348  $calls[] = 2;
349  };
350 
351  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
352  $lbFactory->beginMasterChanges( __METHOD__ );
353 
355  $this->assertEquals( [], $calls );
356 
358  $this->assertEquals( [], $calls );
359 
360  $dbw = wfGetDB( DB_MASTER );
361  $dbw->onTransactionCommitOrIdle( function () use ( &$calls, $callback2 ) {
363  $this->assertEquals( [], $calls );
364  $calls[] = 'oti';
365  } );
366  $this->assertEquals( 1, $dbw->trxLevel() );
367  $this->assertEquals( [], $calls );
368 
369  $lbFactory->commitMasterChanges( __METHOD__ );
370 
371  $this->assertEquals( [ 'oti' ], $calls );
372 
374  $this->assertEquals( [ 'oti', 1, 2 ], $calls );
375  }
376 }
DeferredUpdates\ALL
const ALL
Definition: DeferredUpdates.php:62
$pre
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped $pre
Definition: hooks.txt:1515
DeferredUpdatesTest\testAddMergeable
testAddMergeable()
DeferredUpdates::addUpdate DeferredUpdates::push.
Definition: DeferredUpdatesTest.php:27
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Definition: DeferredUpdates.php:79
DeferredUpdates\clearPendingUpdates
static clearPendingUpdates()
Clear all pending updates without performing them.
Definition: DeferredUpdates.php:372
DeferredUpdatesTest\testAddCallableUpdate
testAddCallableUpdate()
DeferredUpdates::addCallableUpdate MWCallableUpdate::getOrigin.
Definition: DeferredUpdatesTest.php:48
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
DeferredUpdatesTest\testPresendAddOnPostsendRun
testPresendAddOnPostsendRun()
DeferredUpdates::doUpdates DeferredUpdates::execute DeferredUpdates::addUpdate.
Definition: DeferredUpdatesTest.php:269
DeferredUpdatesTest
Definition: DeferredUpdatesTest.php:5
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2693
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:706
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
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
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:64
DB_MASTER
const DB_MASTER
Definition: defines.php:26
DeferredUpdatesTest\testDoUpdatesWeb
testDoUpdatesWeb()
DeferredUpdates::doUpdates DeferredUpdates::execute DeferredUpdates::addUpdate.
Definition: DeferredUpdatesTest.php:98
DeferredUpdates\tryOpportunisticExecute
static tryOpportunisticExecute( $mode='run')
Run all deferred updates immediately if there are no DB writes active.
Definition: DeferredUpdates.php:301
DeferredUpdatesTest\testGetPendingUpdates
testGetPendingUpdates()
DeferredUpdates::getPendingUpdates DeferredUpdates::clearPendingUpdates.
Definition: DeferredUpdatesTest.php:64
DeferredUpdates\doUpdates
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
Definition: DeferredUpdates.php:130
DeferredUpdates\pendingUpdatesCount
static pendingUpdatesCount()
Definition: DeferredUpdates.php:348
DeferredUpdatesTest\testRunUpdateTransactionScope
testRunUpdateTransactionScope()
DeferredUpdates::runUpdate.
Definition: DeferredUpdatesTest.php:300
DeferredUpdates\PRESEND
const PRESEND
Definition: DeferredUpdates.php:63
DeferredUpdates\getPendingUpdates
static getPendingUpdates( $stage=self::ALL)
Definition: DeferredUpdates.php:357
DeferredUpdatesTest\testTryOpportunisticExecute
testTryOpportunisticExecute()
DeferredUpdates::tryOpportunisticExecute.
Definition: DeferredUpdatesTest.php:342
TransactionRoundDefiningUpdate
Deferrable update that must run outside of any explicit LBFactory transaction round.
Definition: TransactionRoundDefiningUpdate.php:8
DeferredUpdatesTest\testDoUpdatesCLI
testDoUpdatesCLI()
DeferredUpdates::doUpdates DeferredUpdates::execute DeferredUpdates::addUpdate.
Definition: DeferredUpdatesTest.php:195
DeferredUpdatesTest\testAddAndRun
testAddAndRun()
DeferredUpdates::addUpdate DeferredUpdates::push DeferredUpdates::doUpdates DeferredUpdates::execute ...
Definition: DeferredUpdatesTest.php:14
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
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:118
DeferredUpdatesTest\testRunOuterScopeUpdate
testRunOuterScopeUpdate()
DeferredUpdates::runUpdate TransactionRoundDefiningUpdate::getOrigin.
Definition: DeferredUpdatesTest.php:321