Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / jobqueue / JobTest.php
blobba8ff124d03b409a883b7037bc20634f5e4befe1
1 <?php
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Request\WebRequest;
5 use MediaWiki\Title\Title;
7 /**
8 * @author Addshore
9 * @covers \Job
11 class JobTest extends MediaWikiIntegrationTestCase {
13 /**
14 * @dataProvider provideTestToString
16 * @param Job $job
17 * @param string $expected
19 public function testToString( $job, $expected ) {
20 $this->overrideConfigValue( MainConfigNames::LanguageCode, 'en' );
21 $this->assertEquals( $expected, $job->toString() );
24 public function provideTestToString() {
25 $mockToStringObj = $this->getMockBuilder( stdClass::class )
26 ->addMethods( [ '__toString' ] )->getMock();
27 $mockToStringObj->method( '__toString' )
28 ->willReturn( '{STRING_OBJ_VAL}' );
30 $requestId = 'requestId=' . WebRequest::getRequestId();
32 return [
34 $this->getMockJob( [ 'key' => 'val' ] ),
35 'someCommand Special: key=val ' . $requestId
38 $this->getMockJob( [ 'key' => [ 'inkey' => 'inval' ] ] ),
39 'someCommand Special: key={"inkey":"inval"} ' . $requestId
42 $this->getMockJob( [ 'val1' ] ),
43 'someCommand Special: 0=val1 ' . $requestId
46 $this->getMockJob( [ 'val1', 'val2' ] ),
47 'someCommand Special: 0=val1 1=val2 ' . $requestId
50 $this->getMockJob( [ (object)[] ] ),
51 'someCommand Special: 0=stdClass ' . $requestId
54 $this->getMockJob( [ $mockToStringObj ] ),
55 'someCommand Special: 0={STRING_OBJ_VAL} ' . $requestId
58 $this->getMockJob( [
59 "pages" => [
60 "932737" => [
62 "Robert_James_Waller"
65 "rootJobSignature" => "45868e99bba89064e4483743ebb9b682ef95c1a7",
66 "rootJobTimestamp" => "20160309110158",
67 "masterPos" => [
68 "file" => "db1023-bin.001288",
69 "pos" => "308257743",
70 "asOfTime" => 1457521464.3814
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
84 public function getMockJob( $params ) {
85 $mock = $this->getMockForAbstractClass(
86 Job::class,
87 [ 'someCommand', $params ],
88 'SomeJob'
91 return $mock;
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 );
102 * @dataProvider provideTestJobFactory
104 public function testJobFactory( $handler, $expectedClass ) {
105 $this->overrideConfigValue( MainConfigNames::JobClasses, [ 'testdummy' => $handler ] );
107 $job = Job::factory( 'testdummy', Title::newMainPage(), [] );
108 $this->assertInstanceOf( $expectedClass, $job );
110 $job2 = Job::factory( 'testdummy', [] );
111 $this->assertInstanceOf( $expectedClass, $job2 );
112 $this->assertNotSame( $job, $job2, 'should not reuse instance' );
114 $job3 = Job::factory( 'testdummy', [ 'namespace' => NS_MAIN, 'title' => 'JobTestTitle' ] );
115 $this->assertInstanceOf( $expectedClass, $job3 );
116 $this->assertNotSame( $job, $job3, 'should not reuse instance' );
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' => [
131 'class' => ParsoidCachePrewarmJob::class,
132 'services' => [
133 'ParserOutputAccess',
134 'PageStore',
135 'RevisionLookup',
136 'ParsoidSiteConfig',
138 'needsPage' => false
140 ParsoidCachePrewarmJob::class
145 public function newNullJob( Title $title, array $params ) {
146 return new NullJob( $params );
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' ];
155 $job = new NullJob( [ 'namespace' => NS_PROJECT, 'title' => 'x' ] + $params );
156 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
157 $this->assertJobParamsMatch( $job, $paramsWithTitle );
159 $job = Job::factory( 'null', $testPage, $params );
160 $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
161 $this->assertJobParamsMatch( $job, $params );
163 $job = Job::factory( 'null', $paramsWithTitle );
164 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
165 $this->assertJobParamsMatch( $job, $paramsWithTitle );
167 $job = Job::factory( 'null', $params );
168 $this->assertTrue( $blankTitle->equals( $job->getTitle() ) );
169 $this->assertJobParamsMatch( $job, $params );
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' ];
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 );
184 $job = Job::factory( 'htmlCacheUpdate', $testPage, $params );
185 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
186 $this->assertJobParamsMatch( $job, $paramsWithTitle );
188 $job = Job::factory( 'htmlCacheUpdate', $paramsWithTitle );
189 $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
190 $this->assertJobParamsMatch( $job, $paramsWithTitle );
192 $job = Job::factory( 'htmlCacheUpdate', $params );
193 $this->assertTrue( $blankPage->equals( $job->getTitle() ) );
194 $this->assertJobParamsMatch( $job, $paramsWithBlankpage );
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' ];
202 $job = new RefreshLinksJob( $testPage, $params + [ 'namespace' => 0 ] );
203 $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
204 $this->assertJobParamsMatch( $job, $params + [ 'namespace' => 0 ] );
206 $job = new RefreshLinksJob( $testPage, $params + [ 'title' => 'x' ] );
207 $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
208 $this->assertJobParamsMatch( $job, $params + [ 'title' => 'x' ] );
211 private function assertJobParamsMatch( IJobSpecification $job, array $params ) {
212 $actual = $job->getParams();
213 unset( $actual['requestId'] );
215 $this->assertEquals( $actual, $params );