Debian Codegraph @

tests/phpunit/maintenance/includes/LoggedUpdateMaintenanceTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance\Includes;
4
5 use MediaWiki\Maintenance\LoggedUpdateMaintenance;
6 use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
7 use PHPUnit\Framework\MockObject\MockObject;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers \MediaWiki\Maintenance\LoggedUpdateMaintenance
12 * @author Dreamy Jazz
13 * @group Database
14 */
15 class LoggedUpdateMaintenanceTest extends MaintenanceBaseTestCase {
16
17 /** @var LoggedUpdateMaintenance|MockObject */
18 protected $maintenance;
19
20 protected function getMaintenanceClass() {
21 return LoggedUpdateMaintenance::class;
22 }
23
24 protected function createMaintenance() {
25 $obj = $this->getMockBuilder( $this->getMaintenanceClass() )
26 ->onlyMethods( [ 'getUpdateKey', 'doDBUpdates' ] )
27 ->getMockForAbstractClass();
28
29 // We use TestingAccessWrapper in order to access protected internals
30 // such as `output()`.
31 return TestingAccessWrapper::newFromObject( $obj );
32 }
33
34 /** @dataProvider provideForcedValues */
35 public function testSetForce( $value ) {
36 $this->maintenance->setForce( $value );
37 $this->assertSame( $value, $this->maintenance->getParameters()->getOption( 'force' ) );
38 }
39
40 public static function provideForcedValues() {
41 return [
42 '--force set' => [ true ],
43 '--force not set' => [ null ],
44 ];
45 }
46
47 /** @dataProvider provideExecute */
48 public function testExecute(
49 $doDBUpdatesReturnValue, $markedAsCompleteBeforeRun, $shouldBeMarkedAsCompleteAfterExecution, $force,
50 $expectedReturnValueFromExecute, $expectedOutputRegex = false
51 ) {
52 // If marked as complete before the run, manually add the key into the updatelog table
53 if ( $markedAsCompleteBeforeRun ) {
54 $this->getDb()->newInsertQueryBuilder()
55 ->insertInto( 'updatelog' )
56 ->row( [ 'ul_key' => 'test' ] )
57 ->execute();
58 }
59 // Set if --force is specified and also mock the return value of ::doDBUpdates
60 $this->maintenance->setForce( $force );
61 $this->maintenance->method( 'doDBUpdates' )
62 ->willReturn( $doDBUpdatesReturnValue );
63 $this->maintenance->method( 'getUpdateKey' )
64 ->willReturn( 'test' );
65 // Run the maintenance script and then assert that the updatelog table is as expected
66 $this->assertSame( $expectedReturnValueFromExecute, $this->maintenance->execute() );
67 $this->newSelectQueryBuilder()
68 ->select( 'COUNT(*)' )
69 ->from( 'updatelog' )
70 ->where( [ 'ul_key' => 'test' ] )
71 ->assertFieldValue( (int)$shouldBeMarkedAsCompleteAfterExecution );
72 if ( $expectedOutputRegex ) {
73 $this->expectOutputRegex( $expectedOutputRegex );
74 } else {
75 $this->expectOutputString( '' );
76 }
77 }
78
79 public static function provideExecute() {
80 return [
81 'Update has been run before' => [
82 'doDBUpdatesReturnValue' => false,
83 'markedAsCompleteBeforeRun' => true,
84 'shouldBeMarkedAsCompleteAfterExecution' => true,
85 'force' => null,
86 'expectedReturnValueFromExecute' => true,
87 'expectedOutputRegex' => "/Update 'test' already logged as completed/"
88 ],
89 'Update has been run before, but force provided and update fails' => [
90 'doDBUpdatesReturnValue' => false,
91 'markedAsCompleteBeforeRun' => true,
92 'shouldBeMarkedAsCompleteAfterExecution' => true,
93 'force' => true,
94 'expectedReturnValueFromExecute' => false,
95 ],
96 'Update has never been run before and update succeeds' => [
97 'doDBUpdatesReturnValue' => true,
98 'markedAsCompleteBeforeRun' => false,
99 'shouldBeMarkedAsCompleteAfterExecution' => true,
100 'force' => null,
101 'expectedReturnValueFromExecute' => true,
102 ],
103 'Update has never been run before and update fails' => [
104 'doDBUpdatesReturnValue' => false,
105 'markedAsCompleteBeforeRun' => false,
106 'shouldBeMarkedAsCompleteAfterExecution' => false,
107 'force' => false,
108 'expectedReturnValueFromExecute' => false,
109 ],
110 ];
111 }
112 }