d7ad1d1ed655c80058ee29c14f6fc0d0cd4b91b9
[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->getMockBuilder( 'DeferrableUpdate' )
17 ->getMock();
18 $update->expects( $this->never() )
19 ->method( 'doUpdate' );
20
21 DeferredUpdates::addUpdate( $update, $pre );
22 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $pre ) );
23 $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $post ) );
24 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
25 $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
26 DeferredUpdates::clearPendingUpdates();
27 $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
28
29 DeferredUpdates::addUpdate( $update, $post );
30 $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $pre ) );
31 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $post ) );
32 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
33 $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
34 DeferredUpdates::clearPendingUpdates();
35 $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
36 }
37
38 public function testDoUpdatesWeb() {
39 $this->setMwGlobals( 'wgCommandLineMode', false );
40
41 $updates = [
42 '1' => "deferred update 1;\n",
43 '2' => "deferred update 2;\n",
44 '2-1' => "deferred update 1 within deferred update 2;\n",
45 '2-2' => "deferred update 2 within deferred update 2;\n",
46 '3' => "deferred update 3;\n",
47 '3-1' => "deferred update 1 within deferred update 3;\n",
48 '3-2' => "deferred update 2 within deferred update 3;\n",
49 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
50 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
51 ];
52 DeferredUpdates::addCallableUpdate(
53 function () use ( $updates ) {
54 echo $updates['1'];
55 }
56 );
57 DeferredUpdates::addCallableUpdate(
58 function () use ( $updates ) {
59 echo $updates['2'];
60 DeferredUpdates::addCallableUpdate(
61 function () use ( $updates ) {
62 echo $updates['2-1'];
63 }
64 );
65 DeferredUpdates::addCallableUpdate(
66 function () use ( $updates ) {
67 echo $updates['2-2'];
68 }
69 );
70 }
71 );
72 DeferredUpdates::addCallableUpdate(
73 function () use ( $updates ) {
74 echo $updates['3'];
75 DeferredUpdates::addCallableUpdate(
76 function () use ( $updates ) {
77 echo $updates['3-1'];
78 DeferredUpdates::addCallableUpdate(
79 function () use ( $updates ) {
80 echo $updates['3-1-1'];
81 }
82 );
83 }
84 );
85 DeferredUpdates::addCallableUpdate(
86 function () use ( $updates ) {
87 echo $updates['3-2'];
88 DeferredUpdates::addCallableUpdate(
89 function () use ( $updates ) {
90 echo $updates['3-2-1'];
91 }
92 );
93 }
94 );
95 }
96 );
97
98 $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() );
99
100 $this->expectOutputString( implode( '', $updates ) );
101
102 DeferredUpdates::doUpdates();
103
104 $x = null;
105 $y = null;
106 DeferredUpdates::addCallableUpdate(
107 function () use ( &$x ) {
108 $x = 'Sherity';
109 },
110 DeferredUpdates::PRESEND
111 );
112 DeferredUpdates::addCallableUpdate(
113 function () use ( &$y ) {
114 $y = 'Marychu';
115 },
116 DeferredUpdates::POSTSEND
117 );
118
119 $this->assertNull( $x, "Update not run yet" );
120 $this->assertNull( $y, "Update not run yet" );
121
122 DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND );
123 $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
124 $this->assertNull( $y, "POSTSEND update not run yet" );
125
126 DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND );
127 $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
128 }
129
130 public function testDoUpdatesCLI() {
131 $this->setMwGlobals( 'wgCommandLineMode', true );
132 $updates = [
133 '1' => "deferred update 1;\n",
134 '2' => "deferred update 2;\n",
135 '2-1' => "deferred update 1 within deferred update 2;\n",
136 '2-2' => "deferred update 2 within deferred update 2;\n",
137 '3' => "deferred update 3;\n",
138 '3-1' => "deferred update 1 within deferred update 3;\n",
139 '3-2' => "deferred update 2 within deferred update 3;\n",
140 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
141 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
142 ];
143
144 wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything
145
146 DeferredUpdates::addCallableUpdate(
147 function () use ( $updates ) {
148 echo $updates['1'];
149 }
150 );
151 DeferredUpdates::addCallableUpdate(
152 function () use ( $updates ) {
153 echo $updates['2'];
154 DeferredUpdates::addCallableUpdate(
155 function () use ( $updates ) {
156 echo $updates['2-1'];
157 }
158 );
159 DeferredUpdates::addCallableUpdate(
160 function () use ( $updates ) {
161 echo $updates['2-2'];
162 }
163 );
164 }
165 );
166 DeferredUpdates::addCallableUpdate(
167 function () use ( $updates ) {
168 echo $updates['3'];
169 DeferredUpdates::addCallableUpdate(
170 function () use ( $updates ) {
171 echo $updates['3-1'];
172 DeferredUpdates::addCallableUpdate(
173 function () use ( $updates ) {
174 echo $updates['3-1-1'];
175 }
176 );
177 }
178 );
179 DeferredUpdates::addCallableUpdate(
180 function () use ( $updates ) {
181 echo $updates['3-2'];
182 DeferredUpdates::addCallableUpdate(
183 function () use ( $updates ) {
184 echo $updates['3-2-1'];
185 }
186 );
187 }
188 );
189 }
190 );
191
192 $this->expectOutputString( implode( '', $updates ) );
193
194 DeferredUpdates::doUpdates();
195 }
196 }