Merge "SpecialNewFiles: Swap from/to date serverside"
[lhc/web/wiklou.git] / tests / phpunit / includes / deferred / DeferredUpdatesTest.php
1 <?php
2
3 class DeferredUpdatesTest extends MediaWikiTestCase {
4
5 /**
6 * @covers DeferredUpdates::getPendingUpdates
7 */
8 public function testGetPendingUpdates() {
9 # Prevent updates from running
10 $this->setMwGlobals( 'wgCommandLineMode', false );
11
12 $pre = DeferredUpdates::PRESEND;
13 $post = DeferredUpdates::POSTSEND;
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() );
25 DeferredUpdates::clearPendingUpdates();
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() );
33 DeferredUpdates::clearPendingUpdates();
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 ];
51 DeferredUpdates::addCallableUpdate(
52 function () use ( $updates ) {
53 echo $updates['1'];
54 }
55 );
56 DeferredUpdates::addCallableUpdate(
57 function () use ( $updates ) {
58 echo $updates['2'];
59 DeferredUpdates::addCallableUpdate(
60 function () use ( $updates ) {
61 echo $updates['2-1'];
62 }
63 );
64 DeferredUpdates::addCallableUpdate(
65 function () use ( $updates ) {
66 echo $updates['2-2'];
67 }
68 );
69 }
70 );
71 DeferredUpdates::addCallableUpdate(
72 function () use ( $updates ) {
73 echo $updates['3'];
74 DeferredUpdates::addCallableUpdate(
75 function () use ( $updates ) {
76 echo $updates['3-1'];
77 DeferredUpdates::addCallableUpdate(
78 function () use ( $updates ) {
79 echo $updates['3-1-1'];
80 }
81 );
82 }
83 );
84 DeferredUpdates::addCallableUpdate(
85 function () use ( $updates ) {
86 echo $updates['3-2'];
87 DeferredUpdates::addCallableUpdate(
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
101 DeferredUpdates::doUpdates();
102
103 $x = null;
104 $y = null;
105 DeferredUpdates::addCallableUpdate(
106 function () use ( &$x ) {
107 $x = 'Sherity';
108 },
109 DeferredUpdates::PRESEND
110 );
111 DeferredUpdates::addCallableUpdate(
112 function () use ( &$y ) {
113 $y = 'Marychu';
114 },
115 DeferredUpdates::POSTSEND
116 );
117
118 $this->assertNull( $x, "Update not run yet" );
119 $this->assertNull( $y, "Update not run yet" );
120
121 DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND );
122 $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
123 $this->assertNull( $y, "POSTSEND update not run yet" );
124
125 DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND );
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
145 DeferredUpdates::addCallableUpdate(
146 function () use ( $updates ) {
147 echo $updates['1'];
148 }
149 );
150 DeferredUpdates::addCallableUpdate(
151 function () use ( $updates ) {
152 echo $updates['2'];
153 DeferredUpdates::addCallableUpdate(
154 function () use ( $updates ) {
155 echo $updates['2-1'];
156 }
157 );
158 DeferredUpdates::addCallableUpdate(
159 function () use ( $updates ) {
160 echo $updates['2-2'];
161 }
162 );
163 }
164 );
165 DeferredUpdates::addCallableUpdate(
166 function () use ( $updates ) {
167 echo $updates['3'];
168 DeferredUpdates::addCallableUpdate(
169 function () use ( $updates ) {
170 echo $updates['3-1'];
171 DeferredUpdates::addCallableUpdate(
172 function () use ( $updates ) {
173 echo $updates['3-1-1'];
174 }
175 );
176 }
177 );
178 DeferredUpdates::addCallableUpdate(
179 function () use ( $updates ) {
180 echo $updates['3-2'];
181 DeferredUpdates::addCallableUpdate(
182 function () use ( $updates ) {
183 echo $updates['3-2-1'];
184 }
185 );
186 }
187 );
188 }
189 );
190
191 $this->expectOutputString( implode( '', $updates ) );
192
193 DeferredUpdates::doUpdates();
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
203 DeferredUpdates::addCallableUpdate(
204 function () use ( &$x, &$y ) {
205 $x = true;
206 DeferredUpdates::addCallableUpdate(
207 function () use ( &$y ) {
208 $y = true;
209 },
210 DeferredUpdates::PRESEND
211 );
212 },
213 DeferredUpdates::POSTSEND
214 );
215
216 DeferredUpdates::doUpdates();
217
218 $this->assertTrue( $x, "Outer POSTSEND update ran" );
219 $this->assertTrue( $y, "Nested PRESEND update ran" );
220 }
221 }