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