MediaWiki  master
JobTest.php
Go to the documentation of this file.
1 <?php
2 
6 class JobTest extends MediaWikiTestCase {
7 
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( [ 'key' => 'val' ] ),
32  'someCommand Special: key=val ' . $requestId
33  ],
34  [
35  $this->getMockJob( [ 'key' => [ 'inkey' => 'inval' ] ] ),
36  'someCommand Special: key={"inkey":"inval"} ' . $requestId
37  ],
38  [
39  $this->getMockJob( [ 'val1' ] ),
40  'someCommand Special: 0=val1 ' . $requestId
41  ],
42  [
43  $this->getMockJob( [ 'val1', 'val2' ] ),
44  'someCommand Special: 0=val1 1=val2 ' . $requestId
45  ],
46  [
47  $this->getMockJob( [ new stdClass() ] ),
48  'someCommand Special: 0=object(stdClass) ' . $requestId
49  ],
50  [
51  $this->getMockJob( [ $mockToStringObj ] ),
52  'someCommand Special: 0={STRING_OBJ_VAL} ' . $requestId
53  ],
54  [
55  $this->getMockJob( [
56  "pages" => [
57  "932737" => [
58  0,
59  "Robert_James_Waller"
60  ]
61  ],
62  "rootJobSignature" => "45868e99bba89064e4483743ebb9b682ef95c1a7",
63  "rootJobTimestamp" => "20160309110158",
64  "masterPos" => [
65  "file" => "db1023-bin.001288",
66  "pos" => "308257743",
67  "asOfTime" => 1457521464.3814
68  ],
69  "triggeredRecursive" => true
70  ] ),
71  'someCommand Special: pages={"932737":[0,"Robert_James_Waller"]} ' .
72  'rootJobSignature=45868e99bba89064e4483743ebb9b682ef95c1a7 ' .
73  'rootJobTimestamp=20160309110158 masterPos=' .
74  '{"file":"db1023-bin.001288","pos":"308257743","asOfTime":' .
75  // Embed dynamically because TestSetup sets serialize_precision=17
76  // which, in PHP 7.1 and 7.2, produces 1457521464.3814001 instead
77  json_encode( 1457521464.3814 ) . '} triggeredRecursive=1 ' .
78  $requestId
79  ],
80  ];
81  }
82 
83  public function getMockJob( $params ) {
84  $mock = $this->getMockForAbstractClass(
85  Job::class,
86  [ 'someCommand', $params ],
87  'SomeJob'
88  );
89 
90  return $mock;
91  }
92 
96  public function testInvalidParamsArgument() {
97  $params = false;
98  $this->setExpectedException( InvalidArgumentException::class, '$params must be an array' );
99  $job = $this->getMockJob( $params );
100  }
101 
109  public function testJobFactory( $handler ) {
110  $this->mergeMwGlobalArrayValue( 'wgJobClasses', [ 'testdummy' => $handler ] );
111 
112  $job = Job::factory( 'testdummy', Title::newMainPage(), [] );
113  $this->assertInstanceOf( NullJob::class, $job );
114 
115  $job2 = Job::factory( 'testdummy', Title::newMainPage(), [] );
116  $this->assertInstanceOf( NullJob::class, $job2 );
117  $this->assertNotSame( $job, $job2, 'should not reuse instance' );
118  }
119 
120  public function provideTestJobFactory() {
121  return [
122  'class name' => [ 'NullJob' ],
123  'closure' => [ function ( Title $title, array $params ) {
124  return Job::factory( 'null', $title, $params );
125  } ],
126  'function' => [ [ $this, 'newNullJob' ] ],
127  'static function' => [ self::class . '::staticNullJob' ]
128  ];
129  }
130 
131  public function newNullJob( Title $title, array $params ) {
132  return Job::factory( 'null', $title, $params );
133  }
134 
135  public static function staticNullJob( Title $title, array $params ) {
136  return Job::factory( 'null', $title, $params );
137  }
138 
143  public function testJobSignatureGeneric() {
144  $testPage = Title::makeTitle( NS_PROJECT, 'x' );
145  $blankTitle = Title::makeTitle( NS_SPECIAL, '' );
146  $params = [ 'z' => 1, 'lives' => 1, 'usleep' => 0 ];
147  $paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ];
148 
149  $job = new NullJob( [ 'namespace' => NS_PROJECT, 'title' => 'x' ] + $params );
150  $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
151  $this->assertJobParamsMatch( $job, $paramsWithTitle );
152 
153  $job = Job::factory( 'null', $testPage, $params );
154  $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
155  $this->assertJobParamsMatch( $job, $params );
156 
157  $job = Job::factory( 'null', $paramsWithTitle );
158  $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
159  $this->assertJobParamsMatch( $job, $paramsWithTitle );
160 
161  $job = Job::factory( 'null', $params );
162  $this->assertTrue( $blankTitle->equals( $job->getTitle() ) );
163  $this->assertJobParamsMatch( $job, $params );
164  }
165 
170  public function testJobSignatureTitleBased() {
171  $testPage = Title::makeTitle( NS_PROJECT, 'x' );
172  $blankPage = Title::makeTitle( NS_SPECIAL, 'Blankpage' );
173  $params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
174  $paramsWithTitle = $params + [ 'namespace' => NS_PROJECT, 'title' => 'x' ];
175  $paramsWithBlankpage = $params + [ 'namespace' => NS_SPECIAL, 'title' => 'Blankpage' ];
176 
177  $job = new RefreshLinksJob( $testPage, $params );
178  $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
179  $this->assertTrue( $testPage->equals( $job->getTitle() ) );
180  $this->assertJobParamsMatch( $job, $paramsWithTitle );
181 
182  $job = Job::factory( 'refreshLinks', $testPage, $params );
183  $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
184  $this->assertJobParamsMatch( $job, $paramsWithTitle );
185 
186  $job = Job::factory( 'refreshLinks', $paramsWithTitle );
187  $this->assertEquals( $testPage->getPrefixedText(), $job->getTitle()->getPrefixedText() );
188  $this->assertJobParamsMatch( $job, $paramsWithTitle );
189 
190  $job = Job::factory( 'refreshLinks', $params );
191  $this->assertTrue( $blankPage->equals( $job->getTitle() ) );
192  $this->assertJobParamsMatch( $job, $paramsWithBlankpage );
193  }
194 
200  $testPage = Title::makeTitle( NS_PROJECT, 'x' );
201  $blankTitle = Title::makeTitle( NS_SPECIAL, '' );
202  $params = [ 'z' => 1, 'causeAction' => 'unknown', 'causeAgent' => 'unknown' ];
203 
204  $job = new RefreshLinksJob( $testPage, $params + [ 'namespace' => 0 ] );
205  $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
206  $this->assertJobParamsMatch( $job, $params + [ 'namespace' => 0 ] );
207 
208  $job = new RefreshLinksJob( $testPage, $params + [ 'title' => 'x' ] );
209  $this->assertEquals( $blankTitle->getPrefixedText(), $job->getTitle()->getPrefixedText() );
210  $this->assertJobParamsMatch( $job, $params + [ 'title' => 'x' ] );
211  }
212 
214  $actual = $job->getParams();
215  unset( $actual['requestId'] );
216 
217  $this->assertEquals( $actual, $params );
218  }
219 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
static getRequestId()
Get the unique request ID.
Definition: WebRequest.php:275
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:653
const NS_SPECIAL
Definition: Defines.php:49
assertJobParamsMatch(IJobSpecification $job, array $params)
Definition: JobTest.php:213
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
provideTestToString()
Definition: JobTest.php:20
provideTestJobFactory()
Definition: JobTest.php:120
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
const NS_PROJECT
Definition: Defines.php:64
testJobSignatureGeneric()
Job::factory Job::__construct()
Definition: JobTest.php:143
testJobSignatureTitleBased()
Job::factory Job::__construct()
Definition: JobTest.php:170
testInvalidParamsArgument()
Job::__construct()
Definition: JobTest.php:96
$params
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
static factory( $command, $params=[])
Create the appropriate object to handle a specific job.
Definition: Job.php:66
testToString( $job, $expected)
provideTestToString
Definition: JobTest.php:16
testJobFactory( $handler)
provideTestJobFactory
Definition: JobTest.php:109
Job to update link tables for pages.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:589
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static staticNullJob(Title $title, array $params)
Definition: JobTest.php:135
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Degenerate job that does nothing, but can optionally replace itself in the queue and/or sleep for a b...
Definition: NullJob.php:47
if(count( $args)< 1) $job
getMockJob( $params)
Definition: JobTest.php:83
Interface for serializable objects that describe a job queue task.
testJobSignatureTitleBasedIncomplete()
Job::factory Job::__construct()
Definition: JobTest.php:199
newNullJob(Title $title, array $params)
Definition: JobTest.php:131