99544e7ed0fa7534e54f56ea133554f08649d776
[lhc/web/wiklou.git] / tests / phpunit / includes / ExternalStoreTest.php
1 <?php
2 /**
3 * External Store tests
4 */
5
6 class ExternalStoreTest extends MediaWikiTestCase {
7
8 function testExternalFetchFromURL() {
9 $this->setMwGlobals( 'wgExternalStores', false );
10
11 $this->assertFalse(
12 ExternalStore::fetchFromURL( 'FOO://cluster1/200' ),
13 'Deny if wgExternalStores is not set to a non-empty array'
14 );
15
16 $this->setMwGlobals( 'wgExternalStores', array( 'FOO' ) );
17
18 $this->assertEquals(
19 ExternalStore::fetchFromURL( 'FOO://cluster1/200' ),
20 'Hello',
21 'Allow FOO://cluster1/200'
22 );
23 $this->assertEquals(
24 ExternalStore::fetchFromURL( 'FOO://cluster1/300/0' ),
25 'Hello',
26 'Allow FOO://cluster1/300/0'
27 );
28 # Assertions for r68900
29 $this->assertFalse(
30 ExternalStore::fetchFromURL( 'ftp.example.org' ),
31 'Deny domain ftp.example.org'
32 );
33 $this->assertFalse(
34 ExternalStore::fetchFromURL( '/example.txt' ),
35 'Deny path /example.txt'
36 );
37 $this->assertFalse(
38 ExternalStore::fetchFromURL( 'http://' ),
39 'Deny protocol http://'
40 );
41 }
42 }
43
44 class ExternalStoreFOO {
45
46 protected $data = array(
47 'cluster1' => array(
48 '200' => 'Hello',
49 '300' => array(
50 'Hello', 'World',
51 ),
52 ),
53 );
54
55 /**
56 * Fetch data from given URL
57 * @param $url String: an url of the form FOO://cluster/id or FOO://cluster/id/itemid.
58 * @return mixed
59 */
60 function fetchFromURL( $url ) {
61 // Based on ExternalStoreDB
62 $path = explode( '/', $url );
63 $cluster = $path[2];
64 $id = $path[3];
65 if ( isset( $path[4] ) ) {
66 $itemID = $path[4];
67 } else {
68 $itemID = false;
69 }
70
71 if ( !isset( $this->data[$cluster][$id] ) ) {
72 return null;
73 }
74
75 if ( $itemID !== false && is_array( $this->data[$cluster][$id] ) && isset( $this->data[$cluster][$id][$itemID] ) ) {
76 return $this->data[$cluster][$id][$itemID];
77 }
78
79 return $this->data[$cluster][$id];
80 }
81 }