summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/ExternalStoreTest.php
blob: 07c2957c3a8ba4901555f50c9d9cc8f8cc96e560 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
 * External Store tests
 */

class ExternalStoreTest extends MediaWikiTestCase {

	/**
	 * @covers ExternalStore::fetchFromURL
	 */
	public function testExternalFetchFromURL() {
		$this->setMwGlobals( 'wgExternalStores', false );

		$this->assertFalse(
			ExternalStore::fetchFromURL( 'FOO://cluster1/200' ),
			'Deny if wgExternalStores is not set to a non-empty array'
		);

		$this->setMwGlobals( 'wgExternalStores', array( 'FOO' ) );

		$this->assertEquals(
			ExternalStore::fetchFromURL( 'FOO://cluster1/200' ),
			'Hello',
			'Allow FOO://cluster1/200'
		);
		$this->assertEquals(
			ExternalStore::fetchFromURL( 'FOO://cluster1/300/0' ),
			'Hello',
			'Allow FOO://cluster1/300/0'
		);
		# Assertions for r68900
		$this->assertFalse(
			ExternalStore::fetchFromURL( 'ftp.example.org' ),
			'Deny domain ftp.example.org'
		);
		$this->assertFalse(
			ExternalStore::fetchFromURL( '/example.txt' ),
			'Deny path /example.txt'
		);
		$this->assertFalse(
			ExternalStore::fetchFromURL( 'http://' ),
			'Deny protocol http://'
		);
	}
}

class ExternalStoreFOO {

	protected $data = array(
		'cluster1' => array(
			'200' => 'Hello',
			'300' => array(
				'Hello', 'World',
			),
		),
	);

	/**
	 * Fetch data from given URL
	 * @param string $url An url of the form FOO://cluster/id or FOO://cluster/id/itemid.
	 * @return mixed
	 */
	function fetchFromURL( $url ) {
		// Based on ExternalStoreDB
		$path = explode( '/', $url );
		$cluster = $path[2];
		$id = $path[3];
		if ( isset( $path[4] ) ) {
			$itemID = $path[4];
		} else {
			$itemID = false;
		}

		if ( !isset( $this->data[$cluster][$id] ) ) {
			return null;
		}

		if ( $itemID !== false
			&& is_array( $this->data[$cluster][$id] )
			&& isset( $this->data[$cluster][$id][$itemID] )
		) {
			return $this->data[$cluster][$id][$itemID];
		}

		return $this->data[$cluster][$id];
	}
}