Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / MwSqlTest.php
blobaa8b45a5a62aae204a789a5505808bd6a31a9a06
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use MwSql;
7 /**
8 * @covers \MwSql
9 * @group Database
10 * @author Dreamy Jazz
12 class MwSqlTest extends MaintenanceBaseTestCase {
14 protected function getMaintenanceClass() {
15 return MwSql::class;
18 public function testExecuteForSelectQueryProvidedViaQueryOption() {
19 // Add a testing row to the updatelog table, which we will use the maintenance script to read back.
20 $this->getDb()->newInsertQueryBuilder()
21 ->insertInto( 'updatelog' )
22 ->row( [ 'ul_key' => 'testing', 'ul_value' => 'testing-value-12345' ] )
23 ->caller( __METHOD__ )
24 ->execute();
25 // Output JSON to make it easier to assert that the script worked.
26 $this->maintenance->setOption( 'json', 1 );
27 // Specify the query using the 'query' option to avoid needing to write to STDIN.
28 $this->maintenance->setOption(
29 'query',
30 $this->newSelectQueryBuilder()
31 ->field( 'ul_value' )
32 ->from( 'updatelog' )
33 ->where( [ 'ul_key' => 'testing' ] )
34 ->caller( __METHOD__ )
35 ->getSQL()
37 $this->maintenance->execute();
38 $this->assertArrayEquals(
39 [ [ 'ul_value' => 'testing-value-12345' ] ],
40 json_decode( $this->getActualOutputForAssertion(), true ),
41 true,
42 true,
43 'JSON output was not as expected.'
47 public function testExecuteForSelectQueryProvidedViaSQLFile() {
48 // Add a testing row to the updatelog table, which we will use the maintenance script to read back.
49 $this->getDb()->newInsertQueryBuilder()
50 ->insertInto( 'updatelog' )
51 ->row( [ 'ul_key' => 'testing', 'ul_value' => 'testing-value-12345' ] )
52 ->caller( __METHOD__ )
53 ->execute();
54 // Output JSON to make it easier to assert that the script worked.
55 $this->maintenance->setOption( 'json', 1 );
56 // Specify the query using the 'query' option to avoid needing to write to STDIN.
57 $file = $this->getNewTempFile();
58 file_put_contents(
59 $file,
60 $this->newSelectQueryBuilder()
61 ->field( 'ul_value' )
62 ->from( 'updatelog' )
63 ->where( [ 'ul_key' => 'testing' ] )
64 ->caller( __METHOD__ )
65 ->getSQL()
67 $this->maintenance->setArg( 0, $file );
68 $this->maintenance->execute();
69 $this->assertArrayEquals(
70 [ [ 'ul_value' => 'testing-value-12345' ] ],
71 json_decode( $this->getActualOutputForAssertion(), true ),
72 true,
73 true,
74 'JSON output was not as expected.'
78 public function testExecuteForUnconfiguredReplicaDB() {
79 $this->expectCallToFatalError();
80 $this->expectOutputRegex( '/No replica DB server.*abceftest/' );
81 // Specify the query using the 'query' option with a query that should fail.
82 $this->maintenance->setOption(
83 'query',
84 $this->newSelectQueryBuilder()
85 ->field( 'abcdef' )
86 ->from( 'updatelog' )
87 ->where( [ 'ul_key' => 'testing' ] )
88 ->caller( __METHOD__ )
89 ->getSQL()
91 $this->maintenance->setOption( 'replicadb', 'abceftest' );
92 $this->maintenance->execute();