wiki.techinc.nl/tests/phpunit/includes/jobqueue/JobTest.php

221 lines
7.1 KiB
PHP
Raw Normal View History

<?php
/**
* @author Addshore
*/
class JobTest extends MediaWikiTestCase {
/**
* @dataProvider provideTestToString
*
* @param Job $job
* @param string $expected
*
* @covers Job::toString
*/
public function testToString( $job, $expected ) {
$this->assertEquals( $expected, $job->toString() );
}
public function provideTestToString() {
$mockToStringObj = $this->getMockBuilder( stdClass::class )
->setMethods( [ '__toString' ] )->getMock();
$mockToStringObj->expects( $this->any() )
->method( '__toString' )
->will( $this->returnValue( '{STRING_OBJ_VAL}' ) );
$requestId = 'requestId=' . WebRequest::getRequestId();
return [
[
$this->getMockJob( [ 'key' => 'val' ] ),
'someCommand Special: key=val ' . $requestId
],
[
$this->getMockJob( [ 'key' => [ 'inkey' => 'inval' ] ] ),
'someCommand Special: key={"inkey":"inval"} ' . $requestId
],
[
$this->getMockJob( [ 'val1' ] ),
'someCommand Special: 0=val1 ' . $requestId
],
[
$this->getMockJob( [ 'val1', 'val2' ] ),
'someCommand Special: 0=val1 1=val2 ' . $requestId
],
[
$this->getMockJob( [ new stdClass() ] ),
'someCommand Special: 0=object(stdClass) ' . $requestId
],
[
$this->getMockJob( [ $mockToStringObj ] ),
'someCommand Special: 0={STRING_OBJ_VAL} ' . $requestId
],
[
$this->getMockJob( [
"pages" => [
"932737" => [
0,
"Robert_James_Waller"
]
],
"rootJobSignature" => "45868e99bba89064e4483743ebb9b682ef95c1a7",
"rootJobTimestamp" => "20160309110158",
"masterPos" => [
"file" => "db1023-bin.001288",
"pos" => "308257743",
"asOfTime" => 1457521464.3814
],
"triggeredRecursive" => true
] ),
'someCommand Special: pages={"932737":[0,"Robert_James_Waller"]} ' .
'rootJobSignature=45868e99bba89064e4483743ebb9b682ef95c1a7 ' .
'rootJobTimestamp=20160309110158 masterPos=' .
'{"file":"db1023-bin.001288","pos":"308257743","asOfTime":' .
// Embed dynamically because TestSetup sets serialize_precision=17
// which, in PHP 7.1 and 7.2, produces 1457521464.3814001 instead
json_encode( 1457521464.3814 ) . '} triggeredRecursive=1 ' .
$requestId
],
];
}
public function getMockJob( $params ) {
$mock = $this->getMockForAbstractClass(
Job::class,
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
[ 'someCommand', $params ],
'SomeJob'
);
return $mock;
}
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
/**
* @covers Job::__construct()
*/
public function testInvalidParamsArgument() {
$params = false;
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( '$params must be an array' );
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
$job = $this->getMockJob( $params );
}
/**
* @dataProvider provideTestJobFactory
*
* @param mixed $handler
*
* @covers Job::factory
*/
public function testJobFactory( $handler ) {
$this->mergeMwGlobalArrayValue( 'wgJobClasses', [ 'testdummy' => $handler ] );
$job = Job::factory( 'testdummy', Title::newMainPage(), [] );
$this->assertInstanceOf( NullJob::class, $job );
$job2 = Job::factory( 'testdummy', Title::newMainPage(), [] );
$this->assertInstanceOf( NullJob::class, $job2 );
$this->assertNotSame( $job, $job2, 'should not reuse instance' );
}
public function provideTestJobFactory() {
return [
'class name' => [ 'NullJob' ],
'closure' => [ function ( Title $title, array $params ) {
return Job::factory( 'null', $title, $params );
} ],
'function' => [ [ $this, 'newNullJob' ] ],
'static function' => [ self::class . '::staticNullJob' ]
];
}
public function newNullJob( Title $title, array $params ) {
return Job::factory( 'null', $title, $params );
}
public static function staticNullJob( Title $title, array $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' );
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
$blankPage = Title::makeTitle( NS_SPECIAL, 'Blankpage' );
$params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
$paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ];
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
$paramsWithBlankpage = $params + [ 'namespace' => NS_SPECIAL, 'title' => 'Blankpage' ];
$job = new RefreshLinksJob( $testPage, $params );
$this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
$this->assertTrue( $testPage->equals( $job->getTitle() ) );
$this->assertJobParamsMatch( $job, $paramsWithTitle );
$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 );
jobqueue: Follow-up for fc5d51f12936ed (added GenericParameterJob) * Remove duplicate $params check from Job::factory done in Job::__construct. * In Job::factory(), restore use of a valid title as default for passing as constructor arg to old job classes. Their constructor may expect it to be valid. Keep the invalid dummy in Job::__construct, and document why. * tests: Update test case for failure mode when using Job::factory with a class that requires a title. It asserted getting an invalid title. This now restores the behaviour prior to fc5d51f12936ed, which is that job classes that require a title, get a valid one. * tests: Remove test case for testToString that used an explicitly passed but invalid params value. I've converted that to expect the exception we now throw instead. * tests: Update getMockJob(), also used by testToString, which was relying on undocumented behaviour that 'new Title' is public and gets namespace=0 and title=''. Before fc5d51f12936ed, title params weren't in toString() and it asserted outputting three spaces (delimiter, empty string from formatted title, delimiter). In fc5d51f12936ed, this changed to asserting "Special:" which seems unintentional as we didn't pass it the internally reserved NS_SPECIAL/'' value, and yet was caught by the dbkey=='' check. Given this test case doesn't deal with titles, omit it for now. A job can either have a $title and title/namespace in params, or neither. This test was asserting an in-memory scenario where $title can be an object, but title/namespace absent from params. Bug: T221368 Depends-On: I89f6ad6967d6f82d87a62c15c0dded901c51b714 Change-Id: I2ec99a12ecc627359a2aae5153d5d7c54156ff46
2019-04-18 15:29:41 +00:00
$this->assertTrue( $blankPage->equals( $job->getTitle() ) );
$this->assertJobParamsMatch( $job, $paramsWithBlankpage );
}
/**
* @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 );
}
}