diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/externalstore')
3 files changed, 140 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreFactoryTest.php b/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreFactoryTest.php new file mode 100644 index 00000000..f7626938 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreFactoryTest.php @@ -0,0 +1,41 @@ +<?php + +/** + * @covers ExternalStoreFactory + */ +class ExternalStoreFactoryTest extends PHPUnit\Framework\TestCase { + + use MediaWikiCoversValidator; + + public function testExternalStoreFactory_noStores() { + $factory = new ExternalStoreFactory( [] ); + $this->assertFalse( $factory->getStoreObject( 'ForTesting' ) ); + $this->assertFalse( $factory->getStoreObject( 'foo' ) ); + } + + public function provideStoreNames() { + yield 'Same case as construction' => [ 'ForTesting' ]; + yield 'All lower case' => [ 'fortesting' ]; + yield 'All upper case' => [ 'FORTESTING' ]; + yield 'Mix of cases' => [ 'FOrTEsTInG' ]; + } + + /** + * @dataProvider provideStoreNames + */ + public function testExternalStoreFactory_someStore_protoMatch( $proto ) { + $factory = new ExternalStoreFactory( [ 'ForTesting' ] ); + $store = $factory->getStoreObject( $proto ); + $this->assertInstanceOf( ExternalStoreForTesting::class, $store ); + } + + /** + * @dataProvider provideStoreNames + */ + public function testExternalStoreFactory_someStore_noProtoMatch( $proto ) { + $factory = new ExternalStoreFactory( [ 'SomeOtherClassName' ] ); + $store = $factory->getStoreObject( $proto ); + $this->assertFalse( $store ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreForTesting.php b/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreForTesting.php new file mode 100644 index 00000000..50f1e523 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreForTesting.php @@ -0,0 +1,46 @@ +<?php + +class ExternalStoreForTesting { + + protected $data = [ + 'cluster1' => [ + '200' => 'Hello', + '300' => [ + 'Hello', 'World', + ], + // gzip string below generated with gzdeflate( 'AAAABBAAA' ) + '12345' => "sttttr\002\022\000", + ], + ]; + + /** + * Fetch data from given URL + * @param string $url An url of the form FOO://cluster/id or FOO://cluster/id/itemid. + * @return mixed + */ + public 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]; + } + +} diff --git a/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreTest.php b/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreTest.php new file mode 100644 index 00000000..7ca38749 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/externalstore/ExternalStoreTest.php @@ -0,0 +1,53 @@ +<?php + +class ExternalStoreTest extends MediaWikiTestCase { + + /** + * @covers ExternalStore::fetchFromURL + */ + public function testExternalFetchFromURL_noExternalStores() { + $this->setService( + 'ExternalStoreFactory', + new ExternalStoreFactory( [] ) + ); + + $this->assertFalse( + ExternalStore::fetchFromURL( 'ForTesting://cluster1/200' ), + 'Deny if wgExternalStores is not set to a non-empty array' + ); + } + + /** + * @covers ExternalStore::fetchFromURL + */ + public function testExternalFetchFromURL_someExternalStore() { + $this->setService( + 'ExternalStoreFactory', + new ExternalStoreFactory( [ 'ForTesting' ] ) + ); + + $this->assertEquals( + 'Hello', + ExternalStore::fetchFromURL( 'ForTesting://cluster1/200' ), + 'Allow FOO://cluster1/200' + ); + $this->assertEquals( + 'Hello', + ExternalStore::fetchFromURL( 'ForTesting://cluster1/300/0' ), + '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://' + ); + } +} |