File: LoggedUpdateMaintenanceTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (112 lines) | stat: -rw-r--r-- 3,784 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace MediaWiki\Tests\Maintenance\Includes;

use MediaWiki\Maintenance\LoggedUpdateMaintenance;
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Wikimedia\TestingAccessWrapper;

/**
 * @covers \MediaWiki\Maintenance\LoggedUpdateMaintenance
 * @author Dreamy Jazz
 * @group Database
 */
class LoggedUpdateMaintenanceTest extends MaintenanceBaseTestCase {

	/** @var LoggedUpdateMaintenance|MockObject */
	protected $maintenance;

	protected function getMaintenanceClass() {
		return LoggedUpdateMaintenance::class;
	}

	protected function createMaintenance() {
		$obj = $this->getMockBuilder( $this->getMaintenanceClass() )
			->onlyMethods( [ 'getUpdateKey', 'doDBUpdates' ] )
			->getMockForAbstractClass();

		// We use TestingAccessWrapper in order to access protected internals
		// such as `output()`.
		return TestingAccessWrapper::newFromObject( $obj );
	}

	/** @dataProvider provideForcedValues */
	public function testSetForce( $value ) {
		$this->maintenance->setForce( $value );
		$this->assertSame( $value, $this->maintenance->getParameters()->getOption( 'force' ) );
	}

	public static function provideForcedValues() {
		return [
			'--force set' => [ true ],
			'--force not set' => [ null ],
		];
	}

	/** @dataProvider provideExecute */
	public function testExecute(
		$doDBUpdatesReturnValue, $markedAsCompleteBeforeRun, $shouldBeMarkedAsCompleteAfterExecution, $force,
		$expectedReturnValueFromExecute, $expectedOutputRegex = false
	) {
		// If marked as complete before the run, manually add the key into the updatelog table
		if ( $markedAsCompleteBeforeRun ) {
			$this->getDb()->newInsertQueryBuilder()
				->insertInto( 'updatelog' )
				->row( [ 'ul_key' => 'test' ] )
				->execute();
		}
		// Set if --force is specified and also mock the return value of ::doDBUpdates
		$this->maintenance->setForce( $force );
		$this->maintenance->method( 'doDBUpdates' )
			->willReturn( $doDBUpdatesReturnValue );
		$this->maintenance->method( 'getUpdateKey' )
			->willReturn( 'test' );
		// Run the maintenance script and then assert that the updatelog table is as expected
		$this->assertSame( $expectedReturnValueFromExecute, $this->maintenance->execute() );
		$this->newSelectQueryBuilder()
			->select( 'COUNT(*)' )
			->from( 'updatelog' )
			->where( [ 'ul_key' => 'test' ] )
			->assertFieldValue( (int)$shouldBeMarkedAsCompleteAfterExecution );
		if ( $expectedOutputRegex ) {
			$this->expectOutputRegex( $expectedOutputRegex );
		} else {
			$this->expectOutputString( '' );
		}
	}

	public static function provideExecute() {
		return [
			'Update has been run before' => [
				'doDBUpdatesReturnValue' => false,
				'markedAsCompleteBeforeRun' => true,
				'shouldBeMarkedAsCompleteAfterExecution' => true,
				'force' => null,
				'expectedReturnValueFromExecute' => true,
				'expectedOutputRegex' => "/Update 'test' already logged as completed/"
			],
			'Update has been run before, but force provided and update fails' => [
				'doDBUpdatesReturnValue' => false,
				'markedAsCompleteBeforeRun' => true,
				'shouldBeMarkedAsCompleteAfterExecution' => true,
				'force' => true,
				'expectedReturnValueFromExecute' => false,
			],
			'Update has never been run before and update succeeds' => [
				'doDBUpdatesReturnValue' => true,
				'markedAsCompleteBeforeRun' => false,
				'shouldBeMarkedAsCompleteAfterExecution' => true,
				'force' => null,
				'expectedReturnValueFromExecute' => true,
			],
			'Update has never been run before and update fails' => [
				'doDBUpdatesReturnValue' => false,
				'markedAsCompleteBeforeRun' => false,
				'shouldBeMarkedAsCompleteAfterExecution' => false,
				'force' => false,
				'expectedReturnValueFromExecute' => false,
			],
		];
	}
}