summaryrefslogtreecommitdiff
path: root/tests/phpunit/maintenance/getSlaveServerTest.php
blob: 0b7c758c75ec3e9b58a8b17e11aee42de4783123 (plain)
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
<?php

require_once __DIR__ . "/../../../maintenance/getSlaveServer.php";

/**
 * Tests for getSlaveServer
 *
 * @group Database
 */
class GetSlaveServerTest extends MediaWikiTestCase {

	/**
	 * Yields a regular expression that matches a good DB server name
	 *
	 * It matches IPs or hostnames, both optionally followed by a
	 * port specification
	 *
	 * @return String the regular expression
	 */
	private function getServerRE() {
		if ( $this->db->getType() === 'sqlite' ) {
			// for SQLite, only the empty string is a good server name
			return '';
		}

		$octet = '([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])';
		$ip = "(($octet\.){3}$octet)";

		$label = '([a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)';
		$hostname = "($label(\.$label)*)";

		return "($ip|$hostname)(:[0-9]{1,5})?";
	}

	function testPlain() {
		$gss = new GetSlaveServer();
		$gss->execute();

		$this->expectOutputRegex( "/^" . self::getServerRE() . "\n$/D" );
	}

	function testXmlDumpsBackupUseCase() {
		global $wgDBprefix;

		global $argv;
		$argv = array( null, "--globals" );

		$gss = new GetSlaveServer();
		$gss->loadParamsAndArgs();
		$gss->execute();
		$gss->globals();

		// The main answer
		$output = $this->getActualOutput();
		$firstLineEndPos = strpos( $output,"\n");
		if ( $firstLineEndPos === FALSE ) {
			$this->fail( "Could not find end of first line of output" );
		}
		$firstLine = substr( $output, 0 , $firstLineEndPos );
		$this->assertRegExp( "/^" . self::getServerRE() . "$/D",
			$firstLine, "DB Server" );

		// xmldumps-backup relies on the wgDBprefix in the output.
		$this->expectOutputRegex( "/^[[:space:]]*\[wgDBprefix\][[:space:]]*=> "
			. $wgDBprefix . "$/m" );
	}


}