Merge "Skin: Make skins aware of their registered skin name"
[lhc/web/wiklou.git] / tests / phpunit / includes / deferred / DeferredUpdatesTest.php
1 <?php
2
3 class DeferredUpdatesTest extends MediaWikiTestCase {
4
5 /**
6 * @covers DeferredUpdates::addUpdate
7 * @covers DeferredUpdates::push
8 * @covers DeferredUpdates::doUpdates
9 * @covers DeferredUpdates::execute
10 * @covers DeferredUpdates::runUpdate
11 */
12 public function testAddAndRun() {
13 $update = $this->getMockBuilder( DeferrableUpdate::class )
14 ->setMethods( [ 'doUpdate' ] )->getMock();
15 $update->expects( $this->once() )->method( 'doUpdate' );
16
17 DeferredUpdates::addUpdate( $update );
18 DeferredUpdates::doUpdates();
19 }
20
21 /**
22 * @covers DeferredUpdates::addUpdate
23 * @covers DeferredUpdates::push
24 */
25 public function testAddMergeable() {
26 $this->setMwGlobals( 'wgCommandLineMode', false );
27
28 $update1 = $this->getMockBuilder( MergeableUpdate::class )
29 ->setMethods( [ 'merge', 'doUpdate' ] )->getMock();
30 $update1->expects( $this->once() )->method( 'merge' );
31 $update1->expects( $this->never() )->method( 'doUpdate' );
32
33 $update2 = $this->getMockBuilder( MergeableUpdate::class )
34 ->setMethods( [ 'merge', 'doUpdate' ] )->getMock();
35 $update2->expects( $this->never() )->method( 'merge' );
36 $update2->expects( $this->never() )->method( 'doUpdate' );
37
38 DeferredUpdates::addUpdate( $update1 );
39 DeferredUpdates::addUpdate( $update2 );
40 }
41
42 /**
43 * @covers DeferredUpdates::addCallableUpdate
44 * @covers MWCallableUpdate::getOrigin
45 */
46 public function testAddCallableUpdate() {
47 $this->setMwGlobals( 'wgCommandLineMode', true );
48
49 $ran = 0;
50 DeferredUpdates::addCallableUpdate( function () use ( &$ran ) {
51 $ran++;
52 } );
53 DeferredUpdates::doUpdates();
54
55 $this->assertSame( 1, $ran, 'Update ran' );
56 }
57
58 /**
59 * @covers DeferredUpdates::getPendingUpdates
60 * @covers DeferredUpdates::clearPendingUpdates
61 */
62 public function testGetPendingUpdates() {
63 // Prevent updates from running
64 $this->setMwGlobals( 'wgCommandLineMode', false );
65
66 $pre = DeferredUpdates::PRESEND;
67 $post = DeferredUpdates::POSTSEND;
68 $all = DeferredUpdates::ALL;
69
70 $update = $this->getMock( DeferrableUpdate::class );
71 $update->expects( $this->never() )
72 ->method( 'doUpdate' );
73
74 DeferredUpdates::addUpdate( $update, $pre );
75 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $pre ) );
76 $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $post ) );
77 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
78 $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
79 DeferredUpdates::clearPendingUpdates();
80 $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
81
82 DeferredUpdates::addUpdate( $update, $post );
83 $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $pre ) );
84 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $post ) );
85 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
86 $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
87 DeferredUpdates::clearPendingUpdates();
88 $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
89 }
90
91 /**
92 * @covers DeferredUpdates::doUpdates
93 * @covers DeferredUpdates::execute
94 * @covers DeferredUpdates::addUpdate
95 */
96 public function testDoUpdatesWeb() {
97 $this->setMwGlobals( 'wgCommandLineMode', false );
98
99 $updates = [
100 '1' => "deferred update 1;\n",
101 '2' => "deferred update 2;\n",
102 '2-1' => "deferred update 1 within deferred update 2;\n",
103 '2-2' => "deferred update 2 within deferred update 2;\n",
104 '3' => "deferred update 3;\n",
105 '3-1' => "deferred update 1 within deferred update 3;\n",
106 '3-2' => "deferred update 2 within deferred update 3;\n",
107 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
108 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
109 ];
110 DeferredUpdates::addCallableUpdate(
111 function () use ( $updates ) {
112 echo $updates['1'];
113 }
114 );
115 DeferredUpdates::addCallableUpdate(
116 function () use ( $updates ) {
117 echo $updates['2'];
118 DeferredUpdates::addCallableUpdate(
119 function () use ( $updates ) {
120 echo $updates['2-1'];
121 }
122 );
123 DeferredUpdates::addCallableUpdate(
124 function () use ( $updates ) {
125 echo $updates['2-2'];
126 }
127 );
128 }
129 );
130 DeferredUpdates::addCallableUpdate(
131 function () use ( $updates ) {
132 echo $updates['3'];
133 DeferredUpdates::addCallableUpdate(
134 function () use ( $updates ) {
135 echo $updates['3-1'];
136 DeferredUpdates::addCallableUpdate(
137 function () use ( $updates ) {
138 echo $updates['3-1-1'];
139 }
140 );
141 }
142 );
143 DeferredUpdates::addCallableUpdate(
144 function () use ( $updates ) {
145 echo $updates['3-2'];
146 DeferredUpdates::addCallableUpdate(
147 function () use ( $updates ) {
148 echo $updates['3-2-1'];
149 }
150 );
151 }
152 );
153 }
154 );
155
156 $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() );
157
158 $this->expectOutputString( implode( '', $updates ) );
159
160 DeferredUpdates::doUpdates();
161
162 $x = null;
163 $y = null;
164 DeferredUpdates::addCallableUpdate(
165 function () use ( &$x ) {
166 $x = 'Sherity';
167 },
168 DeferredUpdates::PRESEND
169 );
170 DeferredUpdates::addCallableUpdate(
171 function () use ( &$y ) {
172 $y = 'Marychu';
173 },
174 DeferredUpdates::POSTSEND
175 );
176
177 $this->assertNull( $x, "Update not run yet" );
178 $this->assertNull( $y, "Update not run yet" );
179
180 DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND );
181 $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
182 $this->assertNull( $y, "POSTSEND update not run yet" );
183
184 DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND );
185 $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
186 }
187
188 /**
189 * @covers DeferredUpdates::doUpdates
190 * @covers DeferredUpdates::execute
191 * @covers DeferredUpdates::addUpdate
192 */
193 public function testDoUpdatesCLI() {
194 $this->setMwGlobals( 'wgCommandLineMode', true );
195 $updates = [
196 '1' => "deferred update 1;\n",
197 '2' => "deferred update 2;\n",
198 '2-1' => "deferred update 1 within deferred update 2;\n",
199 '2-2' => "deferred update 2 within deferred update 2;\n",
200 '3' => "deferred update 3;\n",
201 '3-1' => "deferred update 1 within deferred update 3;\n",
202 '3-2' => "deferred update 2 within deferred update 3;\n",
203 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
204 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
205 ];
206
207 // clear anything
208 wfGetLBFactory()->commitMasterChanges( __METHOD__ );
209
210 DeferredUpdates::addCallableUpdate(
211 function () use ( $updates ) {
212 echo $updates['1'];
213 }
214 );
215 DeferredUpdates::addCallableUpdate(
216 function () use ( $updates ) {
217 echo $updates['2'];
218 DeferredUpdates::addCallableUpdate(
219 function () use ( $updates ) {
220 echo $updates['2-1'];
221 }
222 );
223 DeferredUpdates::addCallableUpdate(
224 function () use ( $updates ) {
225 echo $updates['2-2'];
226 }
227 );
228 }
229 );
230 DeferredUpdates::addCallableUpdate(
231 function () use ( $updates ) {
232 echo $updates['3'];
233 DeferredUpdates::addCallableUpdate(
234 function () use ( $updates ) {
235 echo $updates['3-1'];
236 DeferredUpdates::addCallableUpdate(
237 function () use ( $updates ) {
238 echo $updates['3-1-1'];
239 }
240 );
241 }
242 );
243 DeferredUpdates::addCallableUpdate(
244 function () use ( $updates ) {
245 echo $updates['3-2'];
246 DeferredUpdates::addCallableUpdate(
247 function () use ( $updates ) {
248 echo $updates['3-2-1'];
249 }
250 );
251 }
252 );
253 }
254 );
255
256 $this->expectOutputString( implode( '', $updates ) );
257
258 DeferredUpdates::doUpdates();
259 }
260
261 /**
262 * @covers DeferredUpdates::doUpdates
263 * @covers DeferredUpdates::execute
264 * @covers DeferredUpdates::addUpdate
265 */
266 public function testPresendAddOnPostsendRun() {
267 $this->setMwGlobals( 'wgCommandLineMode', true );
268
269 $x = false;
270 $y = false;
271 // clear anything
272 wfGetLBFactory()->commitMasterChanges( __METHOD__ );
273
274 DeferredUpdates::addCallableUpdate(
275 function () use ( &$x, &$y ) {
276 $x = true;
277 DeferredUpdates::addCallableUpdate(
278 function () use ( &$y ) {
279 $y = true;
280 },
281 DeferredUpdates::PRESEND
282 );
283 },
284 DeferredUpdates::POSTSEND
285 );
286
287 DeferredUpdates::doUpdates();
288
289 $this->assertTrue( $x, "Outer POSTSEND update ran" );
290 $this->assertTrue( $y, "Nested PRESEND update ran" );
291 }
292 }