diff options
author | Aaron Schulz <aschulz@wikimedia.org> | 2019-03-29 23:07:48 -0700 |
---|---|---|
committer | Aaron Schulz <aschulz@wikimedia.org> | 2019-04-08 11:05:23 -0700 |
commit | fc5d51f12936edb4c7d067e298b5ec3a7c09a8fa (patch) | |
tree | 65dcb1a353a266340b064397cd973aba9054dde2 /tests/phpunit/includes/jobqueue/JobTest.php | |
parent | 54a8b9a14f42b063a143a981067ba564860ec556 (diff) | |
download | mediawikicore-fc5d51f12936edb4c7d067e298b5ec3a7c09a8fa.tar.gz mediawikicore-fc5d51f12936edb4c7d067e298b5ec3a7c09a8fa.zip |
jobqueue: add GenericParameterJob and RunnableJob interface
Simplify the code of jobs that do not care about titles and removes
the direct Title dependency from JobQueue. Remove getTitle() from
IJobSpecification itself. Move all the Job::factory calls into a
single JobQueue::factoryJob() method.
Depends-on: Iee78f4baeca0c0b4d6db073f2fbcc56855114ab0
Change-Id: I9c9d0726d4066bb0aa937665847ad6042ade13ec
Diffstat (limited to 'tests/phpunit/includes/jobqueue/JobTest.php')
-rw-r--r-- | tests/phpunit/includes/jobqueue/JobTest.php | 106 |
1 files changed, 94 insertions, 12 deletions
diff --git a/tests/phpunit/includes/jobqueue/JobTest.php b/tests/phpunit/includes/jobqueue/JobTest.php index 769b1930fe78..9fe3e3d1a4f6 100644 --- a/tests/phpunit/includes/jobqueue/JobTest.php +++ b/tests/phpunit/includes/jobqueue/JobTest.php @@ -29,31 +29,31 @@ class JobTest extends MediaWikiTestCase { return [ [ $this->getMockJob( false ), - 'someCommand ' . $requestId + 'someCommand Special: ' . $requestId ], [ $this->getMockJob( [ 'key' => 'val' ] ), - 'someCommand key=val ' . $requestId + 'someCommand Special: key=val ' . $requestId ], [ $this->getMockJob( [ 'key' => [ 'inkey' => 'inval' ] ] ), - 'someCommand key={"inkey":"inval"} ' . $requestId + 'someCommand Special: key={"inkey":"inval"} ' . $requestId ], [ $this->getMockJob( [ 'val1' ] ), - 'someCommand 0=val1 ' . $requestId + 'someCommand Special: 0=val1 ' . $requestId ], [ $this->getMockJob( [ 'val1', 'val2' ] ), - 'someCommand 0=val1 1=val2 ' . $requestId + 'someCommand Special: 0=val1 1=val2 ' . $requestId ], [ $this->getMockJob( [ new stdClass() ] ), - 'someCommand 0=object(stdClass) ' . $requestId + 'someCommand Special: 0=object(stdClass) ' . $requestId ], [ $this->getMockJob( [ $mockToStringObj ] ), - 'someCommand 0={STRING_OBJ_VAL} ' . $requestId + 'someCommand Special: 0={STRING_OBJ_VAL} ' . $requestId ], [ $this->getMockJob( [ @@ -72,7 +72,7 @@ class JobTest extends MediaWikiTestCase { ], "triggeredRecursive" => true ] ), - 'someCommand pages={"932737":[0,"Robert_James_Waller"]} ' . + 'someCommand Special: pages={"932737":[0,"Robert_James_Waller"]} ' . 'rootJobSignature=45868e99bba89064e4483743ebb9b682ef95c1a7 ' . 'rootJobTimestamp=20160309110158 masterPos=' . '{"file":"db1023-bin.001288","pos":"308257743","asOfTime":' . @@ -85,11 +85,13 @@ class JobTest extends MediaWikiTestCase { } public function getMockJob( $params ) { + $title = new Title(); $mock = $this->getMockForAbstractClass( Job::class, - [ 'someCommand', new Title(), $params ], + [ 'someCommand', $title, $params ], 'SomeJob' ); + return $mock; } @@ -115,7 +117,7 @@ class JobTest extends MediaWikiTestCase { return [ 'class name' => [ 'NullJob' ], 'closure' => [ function ( Title $title, array $params ) { - return new NullJob( $title, $params ); + return Job::factory( 'null', $title, $params ); } ], 'function' => [ [ $this, 'newNullJob' ] ], 'static function' => [ self::class . '::staticNullJob' ] @@ -123,11 +125,91 @@ class JobTest extends MediaWikiTestCase { } public function newNullJob( Title $title, array $params ) { - return new NullJob( $title, $params ); + return Job::factory( 'null', $title, $params ); } public static function staticNullJob( Title $title, array $params ) { - return new NullJob( $title, $params ); + return Job::factory( 'null', $title, $params ); + } + + /** + * @covers Job::factory + * @covers Job::__construct() + */ + public function testJobSignatureGeneric() { + $testPage = Title::makeTitle( NS_PROJECT, 'x' ); + $blankTitle = Title::makeTitle( NS_SPECIAL, '' ); + $params = [ 'z' => 1, 'lives' => 1, 'usleep' => 0 ]; + $paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ]; + + $job = new NullJob( [ 'namespace' => NS_PROJECT, 'title' => 'x' ] + $params ); + $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $paramsWithTitle ); + + $job = Job::factory( 'null', $testPage, $params ); + $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $params ); + + $job = Job::factory( 'null', $paramsWithTitle ); + $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $paramsWithTitle ); + + $job = Job::factory( 'null', $params ); + $this->assertTrue( $blankTitle->equals( $job->getTitle() ) ); + $this->assertJobParamsMatch( $job, $params ); + } + + /** + * @covers Job::factory + * @covers Job::__construct() + */ + public function testJobSignatureTitleBased() { + $testPage = Title::makeTitle( NS_PROJECT, 'x' ); + $blankTitle = Title::makeTitle( NS_SPECIAL, '' ); + $params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ]; + $paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ]; + + $job = new RefreshLinksJob( $testPage, $params ); + $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertSame( $testPage, $job->getTitle() ); + $this->assertJobParamsMatch( $job, $paramsWithTitle ); + $this->assertSame( $testPage, $job->getTitle() ); + + $job = Job::factory( 'refreshLinks', $testPage, $params ); + $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $paramsWithTitle ); + + $job = Job::factory( 'refreshLinks', $paramsWithTitle ); + $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $paramsWithTitle ); + + $job = Job::factory( 'refreshLinks', $params ); + $this->assertTrue( $blankTitle->equals( $job->getTitle() ) ); + $this->assertJobParamsMatch( $job, $params ); + } + + /** + * @covers Job::factory + * @covers Job::__construct() + */ + public function testJobSignatureTitleBasedIncomplete() { + $testPage = Title::makeTitle( NS_PROJECT, 'x' ); + $blankTitle = Title::makeTitle( NS_SPECIAL, '' ); + $params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ]; + + $job = new RefreshLinksJob( $testPage, $params + [ 'namespace' => 0 ] ); + $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $params + [ 'namespace' => 0 ] ); + + $job = new RefreshLinksJob( $testPage, $params + [ 'title' => 'x' ] ); + $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() ); + $this->assertJobParamsMatch( $job, $params + [ 'title' => 'x' ] ); } + private function assertJobParamsMatch( IJobSpecification $job, array $params ) { + $actual = $job->getParams(); + unset( $actual['requestId'] ); + + $this->assertEquals( $actual, $params ); + } } |