X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/tests/phpunit/includes/externalstore/ExternalStoreTest.php diff --git a/tests/phpunit/includes/externalstore/ExternalStoreTest.php b/tests/phpunit/includes/externalstore/ExternalStoreTest.php new file mode 100644 index 00000000..a365c4de --- /dev/null +++ b/tests/phpunit/includes/externalstore/ExternalStoreTest.php @@ -0,0 +1,87 @@ +setMwGlobals( 'wgExternalStores', false ); + + $this->assertFalse( + ExternalStore::fetchFromURL( 'FOO://cluster1/200' ), + 'Deny if wgExternalStores is not set to a non-empty array' + ); + + $this->setMwGlobals( 'wgExternalStores', [ '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 = [ + 'cluster1' => [ + '200' => 'Hello', + '300' => [ + '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]; + } +}