Merge "Fix order of @var parameter in PHP"
[lhc/web/wiklou.git] / tests / phpunit / includes / jobqueue / JobTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 */
6 class JobTest extends MediaWikiTestCase {
7
8 /**
9 * @dataProvider provideTestToString
10 *
11 * @param Job $job
12 * @param string $expected
13 *
14 * @covers Job::toString
15 */
16 public function testToString( $job, $expected ) {
17 $this->assertEquals( $expected, $job->toString() );
18 }
19
20 public function provideTestToString() {
21 $mockToStringObj = $this->getMockBuilder( stdClass::class )
22 ->setMethods( [ '__toString' ] )->getMock();
23 $mockToStringObj->expects( $this->any() )
24 ->method( '__toString' )
25 ->will( $this->returnValue( '{STRING_OBJ_VAL}' ) );
26
27 $requestId = 'requestId=' . WebRequest::getRequestId();
28
29 return [
30 [
31 $this->getMockJob( false ),
32 'someCommand Special: ' . $requestId
33 ],
34 [
35 $this->getMockJob( [ 'key' => 'val' ] ),
36 'someCommand Special: key=val ' . $requestId
37 ],
38 [
39 $this->getMockJob( [ 'key' => [ 'inkey' => 'inval' ] ] ),
40 'someCommand Special: key={"inkey":"inval"} ' . $requestId
41 ],
42 [
43 $this->getMockJob( [ 'val1' ] ),
44 'someCommand Special: 0=val1 ' . $requestId
45 ],
46 [
47 $this->getMockJob( [ 'val1', 'val2' ] ),
48 'someCommand Special: 0=val1 1=val2 ' . $requestId
49 ],
50 [
51 $this->getMockJob( [ new stdClass() ] ),
52 'someCommand Special: 0=object(stdClass) ' . $requestId
53 ],
54 [
55 $this->getMockJob( [ $mockToStringObj ] ),
56 'someCommand Special: 0={STRING_OBJ_VAL} ' . $requestId
57 ],
58 [
59 $this->getMockJob( [
60 "pages" => [
61 "932737" => [
62 0,
63 "Robert_James_Waller"
64 ]
65 ],
66 "rootJobSignature" => "45868e99bba89064e4483743ebb9b682ef95c1a7",
67 "rootJobTimestamp" => "20160309110158",
68 "masterPos" => [
69 "file" => "db1023-bin.001288",
70 "pos" => "308257743",
71 "asOfTime" => 1457521464.3814
72 ],
73 "triggeredRecursive" => true
74 ] ),
75 'someCommand Special: pages={"932737":[0,"Robert_James_Waller"]} ' .
76 'rootJobSignature=45868e99bba89064e4483743ebb9b682ef95c1a7 ' .
77 'rootJobTimestamp=20160309110158 masterPos=' .
78 '{"file":"db1023-bin.001288","pos":"308257743","asOfTime":' .
79 // Embed dynamically because TestSetup sets serialize_precision=17
80 // which, in PHP 7.1 and 7.2, produces 1457521464.3814001 instead
81 json_encode( 1457521464.3814 ) . '} triggeredRecursive=1 ' .
82 $requestId
83 ],
84 ];
85 }
86
87 public function getMockJob( $params ) {
88 $title = new Title();
89 $mock = $this->getMockForAbstractClass(
90 Job::class,
91 [ 'someCommand', $title, $params ],
92 'SomeJob'
93 );
94
95 return $mock;
96 }
97
98 /**
99 * @dataProvider provideTestJobFactory
100 *
101 * @param mixed $handler
102 *
103 * @covers Job::factory
104 */
105 public function testJobFactory( $handler ) {
106 $this->mergeMwGlobalArrayValue( 'wgJobClasses', [ 'testdummy' => $handler ] );
107
108 $job = Job::factory( 'testdummy', Title::newMainPage(), [] );
109 $this->assertInstanceOf( NullJob::class, $job );
110
111 $job2 = Job::factory( 'testdummy', Title::newMainPage(), [] );
112 $this->assertInstanceOf( NullJob::class, $job2 );
113 $this->assertNotSame( $job, $job2, 'should not reuse instance' );
114 }
115
116 public function provideTestJobFactory() {
117 return [
118 'class name' => [ 'NullJob' ],
119 'closure' => [ function ( Title $title, array $params ) {
120 return Job::factory( 'null', $title, $params );
121 } ],
122 'function' => [ [ $this, 'newNullJob' ] ],
123 'static function' => [ self::class . '::staticNullJob' ]
124 ];
125 }
126
127 public function newNullJob( Title $title, array $params ) {
128 return Job::factory( 'null', $title, $params );
129 }
130
131 public static function staticNullJob( Title $title, array $params ) {
132 return Job::factory( 'null', $title, $params );
133 }
134
135 /**
136 * @covers Job::factory
137 * @covers Job::__construct()
138 */
139 public function testJobSignatureGeneric() {
140 $testPage = Title::makeTitle( NS_PROJECT, 'x' );
141 $blankTitle = Title::makeTitle( NS_SPECIAL, '' );
142 $params = [ 'z' => 1, 'lives' => 1, 'usleep' => 0 ];
143 $paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ];
144
145 $job = new NullJob( [ 'namespace' => NS_PROJECT, 'title' => 'x' ] + $params );
146 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
147 $this->assertJobParamsMatch( $job, $paramsWithTitle );
148
149 $job = Job::factory( 'null', $testPage, $params );
150 $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
151 $this->assertJobParamsMatch( $job, $params );
152
153 $job = Job::factory( 'null', $paramsWithTitle );
154 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
155 $this->assertJobParamsMatch( $job, $paramsWithTitle );
156
157 $job = Job::factory( 'null', $params );
158 $this->assertTrue( $blankTitle->equals( $job->getTitle() ) );
159 $this->assertJobParamsMatch( $job, $params );
160 }
161
162 /**
163 * @covers Job::factory
164 * @covers Job::__construct()
165 */
166 public function testJobSignatureTitleBased() {
167 $testPage = Title::makeTitle( NS_PROJECT, 'x' );
168 $blankTitle = Title::makeTitle( NS_SPECIAL, '' );
169 $params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
170 $paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ];
171
172 $job = new RefreshLinksJob( $testPage, $params );
173 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
174 $this->assertSame( $testPage, $job->getTitle() );
175 $this->assertJobParamsMatch( $job, $paramsWithTitle );
176 $this->assertSame( $testPage, $job->getTitle() );
177
178 $job = Job::factory( 'refreshLinks', $testPage, $params );
179 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
180 $this->assertJobParamsMatch( $job, $paramsWithTitle );
181
182 $job = Job::factory( 'refreshLinks', $paramsWithTitle );
183 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
184 $this->assertJobParamsMatch( $job, $paramsWithTitle );
185
186 $job = Job::factory( 'refreshLinks', $params );
187 $this->assertTrue( $blankTitle->equals( $job->getTitle() ) );
188 $this->assertJobParamsMatch( $job, $params );
189 }
190
191 /**
192 * @covers Job::factory
193 * @covers Job::__construct()
194 */
195 public function testJobSignatureTitleBasedIncomplete() {
196 $testPage = Title::makeTitle( NS_PROJECT, 'x' );
197 $blankTitle = Title::makeTitle( NS_SPECIAL, '' );
198 $params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
199
200 $job = new RefreshLinksJob( $testPage, $params + [ 'namespace' => 0 ] );
201 $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
202 $this->assertJobParamsMatch( $job, $params + [ 'namespace' => 0 ] );
203
204 $job = new RefreshLinksJob( $testPage, $params + [ 'title' => 'x' ] );
205 $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
206 $this->assertJobParamsMatch( $job, $params + [ 'title' => 'x' ] );
207 }
208
209 private function assertJobParamsMatch( IJobSpecification $job, array $params ) {
210 $actual = $job->getParams();
211 unset( $actual['requestId'] );
212
213 $this->assertEquals( $actual, $params );
214 }
215 }