MediaWiki  1.30.0
DeferredUpdatesTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8  public function testGetPendingUpdates() {
9  # Prevent updates from running
10  $this->setMwGlobals( 'wgCommandLineMode', false );
11 
14  $all = DeferredUpdates::ALL;
15 
16  $update = $this->getMock( DeferrableUpdate::class );
17  $update->expects( $this->never() )
18  ->method( 'doUpdate' );
19 
20  DeferredUpdates::addUpdate( $update, $pre );
21  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $pre ) );
22  $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $post ) );
23  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
24  $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
26  $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
27 
28  DeferredUpdates::addUpdate( $update, $post );
29  $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $pre ) );
30  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $post ) );
31  $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
32  $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
34  $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
35  }
36 
37  public function testDoUpdatesWeb() {
38  $this->setMwGlobals( 'wgCommandLineMode', false );
39 
40  $updates = [
41  '1' => "deferred update 1;\n",
42  '2' => "deferred update 2;\n",
43  '2-1' => "deferred update 1 within deferred update 2;\n",
44  '2-2' => "deferred update 2 within deferred update 2;\n",
45  '3' => "deferred update 3;\n",
46  '3-1' => "deferred update 1 within deferred update 3;\n",
47  '3-2' => "deferred update 2 within deferred update 3;\n",
48  '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
49  '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
50  ];
52  function () use ( $updates ) {
53  echo $updates['1'];
54  }
55  );
57  function () use ( $updates ) {
58  echo $updates['2'];
60  function () use ( $updates ) {
61  echo $updates['2-1'];
62  }
63  );
65  function () use ( $updates ) {
66  echo $updates['2-2'];
67  }
68  );
69  }
70  );
72  function () use ( $updates ) {
73  echo $updates['3'];
75  function () use ( $updates ) {
76  echo $updates['3-1'];
78  function () use ( $updates ) {
79  echo $updates['3-1-1'];
80  }
81  );
82  }
83  );
85  function () use ( $updates ) {
86  echo $updates['3-2'];
88  function () use ( $updates ) {
89  echo $updates['3-2-1'];
90  }
91  );
92  }
93  );
94  }
95  );
96 
97  $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() );
98 
99  $this->expectOutputString( implode( '', $updates ) );
100 
102 
103  $x = null;
104  $y = null;
106  function () use ( &$x ) {
107  $x = 'Sherity';
108  },
110  );
112  function () use ( &$y ) {
113  $y = 'Marychu';
114  },
116  );
117 
118  $this->assertNull( $x, "Update not run yet" );
119  $this->assertNull( $y, "Update not run yet" );
120 
122  $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
123  $this->assertNull( $y, "POSTSEND update not run yet" );
124 
126  $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
127  }
128 
129  public function testDoUpdatesCLI() {
130  $this->setMwGlobals( 'wgCommandLineMode', true );
131  $updates = [
132  '1' => "deferred update 1;\n",
133  '2' => "deferred update 2;\n",
134  '2-1' => "deferred update 1 within deferred update 2;\n",
135  '2-2' => "deferred update 2 within deferred update 2;\n",
136  '3' => "deferred update 3;\n",
137  '3-1' => "deferred update 1 within deferred update 3;\n",
138  '3-2' => "deferred update 2 within deferred update 3;\n",
139  '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
140  '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
141  ];
142 
143  wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything
144 
146  function () use ( $updates ) {
147  echo $updates['1'];
148  }
149  );
151  function () use ( $updates ) {
152  echo $updates['2'];
154  function () use ( $updates ) {
155  echo $updates['2-1'];
156  }
157  );
159  function () use ( $updates ) {
160  echo $updates['2-2'];
161  }
162  );
163  }
164  );
166  function () use ( $updates ) {
167  echo $updates['3'];
169  function () use ( $updates ) {
170  echo $updates['3-1'];
172  function () use ( $updates ) {
173  echo $updates['3-1-1'];
174  }
175  );
176  }
177  );
179  function () use ( $updates ) {
180  echo $updates['3-2'];
182  function () use ( $updates ) {
183  echo $updates['3-2-1'];
184  }
185  );
186  }
187  );
188  }
189  );
190 
191  $this->expectOutputString( implode( '', $updates ) );
192 
194  }
195 
196  public function testPresendAddOnPostsendRun() {
197  $this->setMwGlobals( 'wgCommandLineMode', true );
198 
199  $x = false;
200  $y = false;
201  wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything
202 
204  function () use ( &$x, &$y ) {
205  $x = true;
207  function () use ( &$y ) {
208  $y = true;
209  },
211  );
212  },
214  );
215 
217 
218  $this->assertTrue( $x, "Outer POSTSEND update ran" );
219  $this->assertTrue( $y, "Nested PRESEND update ran" );
220  }
221 }
DeferredUpdates\ALL
const ALL
Definition: DeferredUpdates.php:59
$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:1472
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\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Definition: DeferredUpdates.php:76
DeferredUpdates\clearPendingUpdates
static clearPendingUpdates()
Clear all pending updates without performing them.
Definition: DeferredUpdates.php:352
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
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, IDatabase $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:111
DeferredUpdatesTest\testPresendAddOnPostsendRun
testPresendAddOnPostsendRun()
Definition: DeferredUpdatesTest.php:196
DeferredUpdatesTest
Definition: DeferredUpdatesTest.php:3
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:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:61
DeferredUpdatesTest\testDoUpdatesWeb
testDoUpdatesWeb()
Definition: DeferredUpdatesTest.php:37
DeferredUpdatesTest\testGetPendingUpdates
testGetPendingUpdates()
DeferredUpdates::getPendingUpdates.
Definition: DeferredUpdatesTest.php:8
wfGetLBFactory
wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:2885
DeferredUpdates\doUpdates
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
Definition: DeferredUpdates.php:123
DeferredUpdates\pendingUpdatesCount
static pendingUpdatesCount()
Definition: DeferredUpdates.php:328
DeferredUpdates\PRESEND
const PRESEND
Definition: DeferredUpdates.php:60
DeferredUpdates\getPendingUpdates
static getPendingUpdates( $stage=self::ALL)
Definition: DeferredUpdates.php:337
DeferredUpdatesTest\testDoUpdatesCLI
testDoUpdatesCLI()
Definition: DeferredUpdatesTest.php:129
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