2018-02-27 06:24:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-03-30 15:41:42 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2018-02-27 06:24:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ExternalStoreAccess
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class ExternalStoreAccessTest extends MediaWikiIntegrationTestCase {
|
2018-02-27 06:24:46 +00:00
|
|
|
|
|
|
|
|
public function testBasic() {
|
|
|
|
|
$active = [ 'memory' ];
|
|
|
|
|
$defaults = [ 'memory://cluster1', 'memory://cluster2' ];
|
|
|
|
|
$esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' );
|
|
|
|
|
$access = new ExternalStoreAccess( $esFactory );
|
|
|
|
|
|
2019-09-17 14:19:26 +00:00
|
|
|
$this->assertFalse( $access->isReadOnly() );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
|
|
|
|
/** @var ExternalStoreMemory $store */
|
|
|
|
|
$store = $esFactory->getStore( 'memory' );
|
|
|
|
|
$this->assertInstanceOf( ExternalStoreMemory::class, $store );
|
|
|
|
|
|
2020-03-30 15:41:42 +00:00
|
|
|
$location = $esFactory->getStoreLocationFromUrl( 'memory://cluster1' );
|
|
|
|
|
$this->assertFalse( $store->isReadOnly( $location ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ExternalStoreAccess::isReadOnly
|
|
|
|
|
*/
|
|
|
|
|
public function testReadOnly() {
|
|
|
|
|
/** @var ExternalStoreMedium|MockObject $medium */
|
2022-07-14 12:42:07 +00:00
|
|
|
$medium = $this->createMock( ExternalStoreMedium::class );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
2020-03-30 15:41:42 +00:00
|
|
|
$medium->method( 'isReadOnly' )->willReturn( true );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
2020-03-30 15:41:42 +00:00
|
|
|
/** @var ExternalStoreFactory|MockObject $esFactory */
|
2022-07-14 12:42:07 +00:00
|
|
|
$esFactory = $this->createMock( ExternalStoreFactory::class );
|
2020-03-30 15:41:42 +00:00
|
|
|
|
|
|
|
|
$esFactory->method( 'getWriteBaseUrls' )->willReturn( [ 'test:' ] );
|
|
|
|
|
$esFactory->method( 'getStoreForUrl' )->willReturn( $medium );
|
|
|
|
|
$esFactory->method( 'getStoreLocationFromUrl' )->willReturn( 'dummy' );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
|
|
|
|
$access = new ExternalStoreAccess( $esFactory );
|
2020-01-09 23:23:19 +00:00
|
|
|
$this->assertTrue( $access->isReadOnly() );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
2020-03-30 15:41:42 +00:00
|
|
|
$this->expectExceptionObject( new ReadOnlyError() );
|
|
|
|
|
$access->insert( 'Lorem Ipsum' );
|
2018-02-27 06:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ExternalStoreAccess::fetchFromURL
|
|
|
|
|
* @covers ExternalStoreAccess::fetchFromURLs
|
|
|
|
|
* @covers ExternalStoreAccess::insert
|
|
|
|
|
*/
|
|
|
|
|
public function testReadWrite() {
|
|
|
|
|
$active = [ 'memory' ]; // active store types
|
|
|
|
|
$defaults = [ 'memory://cluster1', 'memory://cluster2' ];
|
|
|
|
|
$esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' );
|
|
|
|
|
$access = new ExternalStoreAccess( $esFactory );
|
|
|
|
|
|
|
|
|
|
/** @var ExternalStoreMemory $storeLocal */
|
|
|
|
|
$storeLocal = $esFactory->getStore( 'memory' );
|
|
|
|
|
/** @var ExternalStoreMemory $storeOther */
|
|
|
|
|
$storeOther = $esFactory->getStore( 'memory', [ 'domain' => 'other' ] );
|
|
|
|
|
$this->assertInstanceOf( ExternalStoreMemory::class, $storeLocal );
|
|
|
|
|
$this->assertInstanceOf( ExternalStoreMemory::class, $storeOther );
|
|
|
|
|
|
|
|
|
|
$v1 = wfRandomString();
|
|
|
|
|
$v2 = wfRandomString();
|
|
|
|
|
$v3 = wfRandomString();
|
|
|
|
|
|
2019-09-17 14:19:26 +00:00
|
|
|
$this->assertFalse( $storeLocal->fetchFromURL( 'memory://cluster1/1' ) );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
|
|
|
|
$url1 = 'memory://cluster1/1';
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$url1,
|
|
|
|
|
$esFactory->getStoreForUrl( 'memory://cluster1' )
|
|
|
|
|
->store( $esFactory->getStoreLocationFromUrl( 'memory://cluster1' ), $v1 )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$v1,
|
|
|
|
|
$esFactory->getStoreForUrl( 'memory://cluster1/1' )
|
|
|
|
|
->fetchFromURL( 'memory://cluster1/1' )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals( $v1, $storeLocal->fetchFromURL( 'memory://cluster1/1' ) );
|
|
|
|
|
|
|
|
|
|
$url2 = $access->insert( $v2 );
|
|
|
|
|
$url3 = $access->insert( $v3, [ 'domain' => 'other' ] );
|
|
|
|
|
$this->assertNotFalse( $url2 );
|
|
|
|
|
$this->assertNotFalse( $url3 );
|
|
|
|
|
// There is only one active store type
|
|
|
|
|
$this->assertEquals( $v2, $storeLocal->fetchFromURL( $url2 ) );
|
|
|
|
|
$this->assertEquals( $v3, $storeOther->fetchFromURL( $url3 ) );
|
2019-09-17 14:19:26 +00:00
|
|
|
$this->assertFalse( $storeOther->fetchFromURL( $url2 ) );
|
|
|
|
|
$this->assertFalse( $storeLocal->fetchFromURL( $url3 ) );
|
2018-02-27 06:24:46 +00:00
|
|
|
|
|
|
|
|
$res = $access->fetchFromURLs( [ $url1, $url2, $url3 ] );
|
|
|
|
|
$this->assertEquals( [ $url1 => $v1, $url2 => $v2, $url3 => false ], $res, "Local-only" );
|
|
|
|
|
|
|
|
|
|
$storeLocal->clear();
|
|
|
|
|
$storeOther->clear();
|
|
|
|
|
}
|
|
|
|
|
}
|