3 use MediaWiki\Tests\Unit\DummyServicesTrait
;
4 use PHPUnit\Framework\MockObject\MockObject
;
7 * @covers \ExternalStoreAccess
9 class ExternalStoreAccessTest
extends MediaWikiIntegrationTestCase
{
10 use DummyServicesTrait
;
12 public function testBasic() {
13 $active = [ 'memory' ];
14 $defaults = [ 'memory://cluster1', 'memory://cluster2' ];
15 $esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' );
16 $access = new ExternalStoreAccess( $esFactory );
18 $this->assertFalse( $access->isReadOnly() );
20 /** @var ExternalStoreMemory $store */
21 $store = $esFactory->getStore( 'memory' );
22 $this->assertInstanceOf( ExternalStoreMemory
::class, $store );
24 $location = $esFactory->getStoreLocationFromUrl( 'memory://cluster1' );
25 $this->assertFalse( $store->isReadOnly( $location ) );
29 * @covers \ExternalStoreAccess::isReadOnly
31 public function testReadOnly() {
32 /** @var ExternalStoreMedium|MockObject $medium */
33 $medium = $this->createMock( ExternalStoreMedium
::class );
35 $medium->method( 'isReadOnly' )->willReturn( true );
37 /** @var ExternalStoreFactory|MockObject $esFactory */
38 $esFactory = $this->createMock( ExternalStoreFactory
::class );
40 $esFactory->method( 'getWriteBaseUrls' )->willReturn( [ 'test:' ] );
41 $esFactory->method( 'getStoreForUrl' )->willReturn( $medium );
42 $esFactory->method( 'getStoreLocationFromUrl' )->willReturn( 'dummy' );
44 $access = new ExternalStoreAccess( $esFactory );
45 $this->assertTrue( $access->isReadOnly() );
47 $this->setService( 'ReadOnlyMode', $this->getDummyReadOnlyMode( 'Some absurd reason' ) );
48 $this->expectException( ReadOnlyError
::class );
49 $access->insert( 'Lorem Ipsum' );
53 * @covers \ExternalStoreAccess::fetchFromURL
54 * @covers \ExternalStoreAccess::fetchFromURLs
55 * @covers \ExternalStoreAccess::insert
57 public function testReadWrite() {
58 $active = [ 'memory' ]; // active store types
59 $defaults = [ 'memory://cluster1', 'memory://cluster2' ];
60 $esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' );
61 $access = new ExternalStoreAccess( $esFactory );
63 /** @var ExternalStoreMemory $storeLocal */
64 $storeLocal = $esFactory->getStore( 'memory' );
65 /** @var ExternalStoreMemory $storeOther */
66 $storeOther = $esFactory->getStore( 'memory', [ 'domain' => 'other' ] );
67 $this->assertInstanceOf( ExternalStoreMemory
::class, $storeLocal );
68 $this->assertInstanceOf( ExternalStoreMemory
::class, $storeOther );
70 $v1 = wfRandomString();
71 $v2 = wfRandomString();
72 $v3 = wfRandomString();
74 $this->assertFalse( $storeLocal->fetchFromURL( 'memory://cluster1/1' ) );
76 $url1 = 'memory://cluster1/1';
79 $esFactory->getStoreForUrl( 'memory://cluster1' )
80 ->store( $esFactory->getStoreLocationFromUrl( 'memory://cluster1' ), $v1 )
84 $esFactory->getStoreForUrl( 'memory://cluster1/1' )
85 ->fetchFromURL( 'memory://cluster1/1' )
87 $this->assertEquals( $v1, $storeLocal->fetchFromURL( 'memory://cluster1/1' ) );
89 $url2 = $access->insert( $v2 );
90 $url3 = $access->insert( $v3, [ 'domain' => 'other' ] );
91 $this->assertNotFalse( $url2 );
92 $this->assertNotFalse( $url3 );
93 // There is only one active store type
94 $this->assertEquals( $v2, $storeLocal->fetchFromURL( $url2 ) );
95 $this->assertEquals( $v3, $storeOther->fetchFromURL( $url3 ) );
96 $this->assertFalse( $storeOther->fetchFromURL( $url2 ) );
97 $this->assertFalse( $storeLocal->fetchFromURL( $url3 ) );
99 $res = $access->fetchFromURLs( [ $url1, $url2, $url3 ] );
100 $this->assertEquals( [ $url1 => $v1, $url2 => $v2, $url3 => false ], $res, "Local-only" );
102 $storeLocal->clear();
103 $storeOther->clear();