| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 4 |
use PHPUnit\Framework\MockObject\MockObject; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class ExternalStoreAccessTest extends MediaWikiIntegrationTestCase { |
| 10 |
use DummyServicesTrait; |
| 11 |
|
| 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 ); |
| 17 |
|
| 18 |
$this->assertFalse( $access->isReadOnly() ); |
| 19 |
|
| 20 |
|
| 21 |
$store = $esFactory->getStore( 'memory' ); |
| 22 |
$this->assertInstanceOf( ExternalStoreMemory::class, $store ); |
| 23 |
|
| 24 |
$location = $esFactory->getStoreLocationFromUrl( 'memory://cluster1' ); |
| 25 |
$this->assertFalse( $store->isReadOnly( $location ) ); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public function testReadOnly() { |
| 32 |
|
| 33 |
$medium = $this->createMock( ExternalStoreMedium::class ); |
| 34 |
|
| 35 |
$medium->method( 'isReadOnly' )->willReturn( true ); |
| 36 |
|
| 37 |
|
| 38 |
$esFactory = $this->createMock( ExternalStoreFactory::class ); |
| 39 |
|
| 40 |
$esFactory->method( 'getWriteBaseUrls' )->willReturn( [ 'test:' ] ); |
| 41 |
$esFactory->method( 'getStoreForUrl' )->willReturn( $medium ); |
| 42 |
$esFactory->method( 'getStoreLocationFromUrl' )->willReturn( 'dummy' ); |
| 43 |
|
| 44 |
$access = new ExternalStoreAccess( $esFactory ); |
| 45 |
$this->assertTrue( $access->isReadOnly() ); |
| 46 |
|
| 47 |
$this->setService( 'ReadOnlyMode', $this->getDummyReadOnlyMode( 'Some absurd reason' ) ); |
| 48 |
$this->expectException( ReadOnlyError::class ); |
| 49 |
$access->insert( 'Lorem Ipsum' ); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function testReadWrite() { |
| 58 |
$active = [ 'memory' ]; |
| 59 |
$defaults = [ 'memory://cluster1', 'memory://cluster2' ]; |
| 60 |
$esFactory = new ExternalStoreFactory( $active, $defaults, 'db-prefix' ); |
| 61 |
$access = new ExternalStoreAccess( $esFactory ); |
| 62 |
|
| 63 |
|
| 64 |
$storeLocal = $esFactory->getStore( 'memory' ); |
| 65 |
|
| 66 |
$storeOther = $esFactory->getStore( 'memory', [ 'domain' => 'other' ] ); |
| 67 |
$this->assertInstanceOf( ExternalStoreMemory::class, $storeLocal ); |
| 68 |
$this->assertInstanceOf( ExternalStoreMemory::class, $storeOther ); |
| 69 |
|
| 70 |
$v1 = wfRandomString(); |
| 71 |
$v2 = wfRandomString(); |
| 72 |
$v3 = wfRandomString(); |
| 73 |
|
| 74 |
$this->assertFalse( $storeLocal->fetchFromURL( 'memory://cluster1/1' ) ); |
| 75 |
|
| 76 |
$url1 = 'memory://cluster1/1'; |
| 77 |
$this->assertEquals( |
| 78 |
$url1, |
| 79 |
$esFactory->getStoreForUrl( 'memory://cluster1' ) |
| 80 |
->store( $esFactory->getStoreLocationFromUrl( 'memory://cluster1' ), $v1 ) |
| 81 |
); |
| 82 |
$this->assertEquals( |
| 83 |
$v1, |
| 84 |
$esFactory->getStoreForUrl( 'memory://cluster1/1' ) |
| 85 |
->fetchFromURL( 'memory://cluster1/1' ) |
| 86 |
); |
| 87 |
$this->assertEquals( $v1, $storeLocal->fetchFromURL( 'memory://cluster1/1' ) ); |
| 88 |
|
| 89 |
$url2 = $access->insert( $v2 ); |
| 90 |
$url3 = $access->insert( $v3, [ 'domain' => 'other' ] ); |
| 91 |
$this->assertNotFalse( $url2 ); |
| 92 |
$this->assertNotFalse( $url3 ); |
| 93 |
|
| 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 ) ); |
| 98 |
|
| 99 |
$res = $access->fetchFromURLs( [ $url1, $url2, $url3 ] ); |
| 100 |
$this->assertEquals( [ $url1 => $v1, $url2 => $v2, $url3 => false ], $res, "Local-only" ); |
| 101 |
|
| 102 |
$storeLocal->clear(); |
| 103 |
$storeOther->clear(); |
| 104 |
} |
| 105 |
} |