View source
<?php
namespace Drupal\Tests\Core\TempStore;
use Drupal\Core\TempStore\Lock;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\TempStore\SharedTempStore;
use Drupal\Core\TempStore\TempStoreException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class SharedTempStoreTest extends UnitTestCase {
protected $keyValue;
protected $lock;
protected $tempStore;
protected $owner = 1;
protected $requestStack;
protected $ownObject;
protected $otherObject;
protected function setUp() {
parent::setUp();
$this->keyValue = $this
->createMock('Drupal\\Core\\KeyValueStore\\KeyValueStoreExpirableInterface');
$this->lock = $this
->createMock('Drupal\\Core\\Lock\\LockBackendInterface');
$this->requestStack = new RequestStack();
$request = Request::createFromGlobals();
$this->requestStack
->push($request);
$this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800);
$this->ownObject = (object) [
'data' => 'test_data',
'owner' => $this->owner,
'updated' => (int) $request->server
->get('REQUEST_TIME'),
];
$this->otherObject = clone $this->ownObject;
$this->otherObject->owner = 2;
}
public function testGet() {
$this->keyValue
->expects($this
->at(0))
->method('get')
->with('test_2')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->at(1))
->method('get')
->with('test')
->will($this
->returnValue($this->ownObject));
$this
->assertNull($this->tempStore
->get('test_2'));
$this
->assertSame($this->ownObject->data, $this->tempStore
->get('test'));
}
public function testGetIfOwner() {
$this->keyValue
->expects($this
->at(0))
->method('get')
->with('test_2')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->at(1))
->method('get')
->with('test')
->will($this
->returnValue($this->ownObject));
$this->keyValue
->expects($this
->at(2))
->method('get')
->with('test')
->will($this
->returnValue($this->otherObject));
$this
->assertNull($this->tempStore
->getIfOwner('test_2'));
$this
->assertSame($this->ownObject->data, $this->tempStore
->getIfOwner('test'));
$this
->assertNull($this->tempStore
->getIfOwner('test'));
}
public function testSetWithNoLockAvailable() {
$this->lock
->expects($this
->at(0))
->method('acquire')
->with('test')
->will($this
->returnValue(FALSE));
$this->lock
->expects($this
->at(1))
->method('wait')
->with('test');
$this->lock
->expects($this
->at(2))
->method('acquire')
->with('test')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->once())
->method('getCollectionName');
$this
->expectException(TempStoreException::class);
$this->tempStore
->set('test', 'value');
}
public function testSet() {
$this->lock
->expects($this
->once())
->method('acquire')
->with('test')
->will($this
->returnValue(TRUE));
$this->lock
->expects($this
->never())
->method('wait');
$this->lock
->expects($this
->once())
->method('release')
->with('test');
$this->keyValue
->expects($this
->once())
->method('setWithExpire')
->with('test', $this->ownObject, 604800);
$this->tempStore
->set('test', 'test_data');
}
public function testSetIfNotExists() {
$this->keyValue
->expects($this
->once())
->method('setWithExpireIfNotExists')
->with('test', $this->ownObject, 604800)
->will($this
->returnValue(TRUE));
$this
->assertTrue($this->tempStore
->setIfNotExists('test', 'test_data'));
}
public function testSetIfOwnerWhenNotExists() {
$this->keyValue
->expects($this
->once())
->method('setWithExpireIfNotExists')
->will($this
->returnValue(TRUE));
$this
->assertTrue($this->tempStore
->setIfOwner('test', 'test_data'));
}
public function testSetIfOwnerNoObject() {
$this->keyValue
->expects($this
->once())
->method('setWithExpireIfNotExists')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->once())
->method('get')
->with('test')
->will($this
->returnValue(FALSE));
$this
->assertFalse($this->tempStore
->setIfOwner('test', 'test_data'));
}
public function testSetIfOwner() {
$this->lock
->expects($this
->once())
->method('acquire')
->with('test')
->will($this
->returnValue(TRUE));
$this->keyValue
->expects($this
->exactly(2))
->method('setWithExpireIfNotExists')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->exactly(2))
->method('get')
->with('test')
->will($this
->onConsecutiveCalls($this->ownObject, $this->otherObject));
$this
->assertTrue($this->tempStore
->setIfOwner('test', 'test_data'));
$this
->assertFalse($this->tempStore
->setIfOwner('test', 'test_data'));
}
public function testGetMetadata() {
$this->keyValue
->expects($this
->at(0))
->method('get')
->with('test')
->will($this
->returnValue($this->ownObject));
$this->keyValue
->expects($this
->at(1))
->method('get')
->with('test')
->will($this
->returnValue(FALSE));
$metadata = $this->tempStore
->getMetadata('test');
$this
->assertInstanceOf(Lock::class, $metadata);
$this
->assertObjectHasAttribute('updated', $metadata);
$this
->assertObjectNotHasAttribute('data', $metadata);
$this
->assertNull($this->tempStore
->getMetadata('test'));
}
public function testGetMetadataOwner() {
$this->keyValue
->expects($this
->once())
->method('get')
->with('test')
->will($this
->returnValue($this->ownObject));
$metadata = $this->tempStore
->getMetadata('test');
$this
->assertSame(1, $metadata->owner);
}
public function testGetMetadataUpdated() {
$this->keyValue
->expects($this
->once())
->method('get')
->with('test')
->will($this
->returnValue($this->ownObject));
$metadata = $this->tempStore
->getMetadata('test');
$this
->assertSame($metadata
->getUpdated(), $metadata->updated);
}
public function testDelete() {
$this->lock
->expects($this
->once())
->method('acquire')
->with('test')
->will($this
->returnValue(TRUE));
$this->lock
->expects($this
->never())
->method('wait');
$this->lock
->expects($this
->once())
->method('release')
->with('test');
$this->keyValue
->expects($this
->once())
->method('delete')
->with('test');
$this->tempStore
->delete('test');
}
public function testDeleteWithNoLockAvailable() {
$this->lock
->expects($this
->at(0))
->method('acquire')
->with('test')
->will($this
->returnValue(FALSE));
$this->lock
->expects($this
->at(1))
->method('wait')
->with('test');
$this->lock
->expects($this
->at(2))
->method('acquire')
->with('test')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->once())
->method('getCollectionName');
$this
->expectException(TempStoreException::class);
$this->tempStore
->delete('test');
}
public function testDeleteIfOwner() {
$this->lock
->expects($this
->once())
->method('acquire')
->with('test_2')
->will($this
->returnValue(TRUE));
$this->keyValue
->expects($this
->at(0))
->method('get')
->with('test_1')
->will($this
->returnValue(FALSE));
$this->keyValue
->expects($this
->at(1))
->method('get')
->with('test_2')
->will($this
->returnValue($this->ownObject));
$this->keyValue
->expects($this
->at(2))
->method('delete')
->with('test_2');
$this->keyValue
->expects($this
->at(3))
->method('get')
->with('test_3')
->will($this
->returnValue($this->otherObject));
$this
->assertTrue($this->tempStore
->deleteIfOwner('test_1'));
$this
->assertTrue($this->tempStore
->deleteIfOwner('test_2'));
$this
->assertFalse($this->tempStore
->deleteIfOwner('test_3'));
}
public function testSerialization() {
$request = $this
->prophesize(Request::class);
$request
->willImplement('\\Serializable');
$request
->serialize()
->willThrow(new \LogicException('Oops!'));
$unserializable_request = $request
->reveal();
$this->requestStack
->push($unserializable_request);
$this->requestStack->_serviceId = 'request_stack';
$container = $this
->prophesize(ContainerInterface::class);
$container
->get('request_stack')
->willReturn($this->requestStack);
$container
->has('request_stack')
->willReturn(TRUE);
\Drupal::setContainer($container
->reveal());
$store = unserialize(serialize($this->tempStore));
$this
->assertInstanceOf(SharedTempStore::class, $store);
$request_stack = $this
->getObjectAttribute($store, 'requestStack');
$this
->assertEquals($this->requestStack, $request_stack);
$this
->assertSame($unserializable_request, $request_stack
->pop());
}
}