Debian Codegraph @

tests/phpunit/includes/jobqueue/JobTest.php

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