class SharedTempStoreTest in Drupal 8
Same name in this branch
- 8 core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest
- 8 core/modules/user/tests/src/Unit/SharedTempStoreTest.php \Drupal\Tests\user\Unit\SharedTempStoreTest
@coversDefaultClass \Drupal\Core\TempStore\SharedTempStore @group TempStore
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\TempStore\SharedTempStoreTest
Expanded class hierarchy of SharedTempStoreTest
File
- core/
tests/ Drupal/ Tests/ Core/ TempStore/ SharedTempStoreTest.php, line 17
Namespace
Drupal\Tests\Core\TempStoreView source
class SharedTempStoreTest extends UnitTestCase {
/**
* The mock key value expirable backend.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $keyValue;
/**
* The mock lock backend.
*
* @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $lock;
/**
* The temp store.
*
* @var \Drupal\Core\TempStore\SharedTempStore
*/
protected $tempStore;
/**
* The owner used in this test.
*
* @var int
*/
protected $owner = 1;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* A tempstore object belonging to the owner.
*
* @var \stdClass
*/
protected $ownObject;
/**
* A tempstore object not belonging to the owner.
*
* @var \stdClass
*/
protected $otherObject;
/**
* {@inheritdoc}
*/
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'),
];
// Clone the object but change the owner.
$this->otherObject = clone $this->ownObject;
$this->otherObject->owner = 2;
}
/**
* @covers ::get
*/
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'));
}
/**
* Tests the getIfOwner() method.
*
* @covers ::getIfOwner
*/
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'));
}
/**
* Tests the set() method with no lock available.
*
* @covers ::set
*/
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');
}
/**
* Tests a successful set() call.
*
* @covers ::set
*/
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');
}
/**
* Tests the setIfNotExists() methods.
*
* @covers ::setIfNotExists
*/
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'));
}
/**
* Tests the setIfOwner() method when no key exists.
*
* @covers ::setIfOwner
*/
public function testSetIfOwnerWhenNotExists() {
$this->keyValue
->expects($this
->once())
->method('setWithExpireIfNotExists')
->will($this
->returnValue(TRUE));
$this
->assertTrue($this->tempStore
->setIfOwner('test', 'test_data'));
}
/**
* Tests the setIfOwner() method when a key already exists but no object.
*
* @covers ::setIfOwner
*/
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'));
}
/**
* Tests the setIfOwner() method with matching and non matching owners.
*
* @covers ::setIfOwner
*/
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'));
}
/**
* Tests the getMetadata() method.
*
* @covers ::getMetadata
*/
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);
// Data should get removed.
$this
->assertObjectNotHasAttribute('data', $metadata);
$this
->assertNull($this->tempStore
->getMetadata('test'));
}
/**
* @covers ::getMetadata
* @expectedDeprecation Using the "owner" public property of a TempStore lock is deprecated in Drupal 8.7.0 and will not be allowed in Drupal 9.0.0. Use \Drupal\Core\TempStore\Lock::getOwnerId() instead. See https://www.drupal.org/node/3025869.
* @group legacy
*/
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);
}
/**
* @covers ::getMetadata
* @expectedDeprecation Using the "updated" public property of a TempStore lock is deprecated in Drupal 8.7.0 and will not be allowed in Drupal 9.0.0. Use \Drupal\Core\TempStore\Lock::getUpdated() instead. See https://www.drupal.org/node/3025869.
* @group legacy
*/
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);
}
/**
* Tests the delete() method.
*
* @covers ::delete
*/
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');
}
/**
* Tests the delete() method with no lock available.
*
* @covers ::delete
*/
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');
}
/**
* Tests the deleteIfOwner() method.
*
* @covers ::deleteIfOwner
*/
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'));
}
/**
* Tests the serialization of a shared temp store.
*/
public function testSerialization() {
// Add an unserializable request to the request stack. If the tempstore
// didn't use DependencySerializationTrait, the exception would be thrown
// when we try to serialize the tempstore.
$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());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
SharedTempStoreTest:: |
protected | property | The mock key value expirable backend. | |
SharedTempStoreTest:: |
protected | property | The mock lock backend. | |
SharedTempStoreTest:: |
protected | property | A tempstore object not belonging to the owner. | |
SharedTempStoreTest:: |
protected | property | The owner used in this test. | |
SharedTempStoreTest:: |
protected | property | A tempstore object belonging to the owner. | |
SharedTempStoreTest:: |
protected | property | The request stack. | |
SharedTempStoreTest:: |
protected | property | The temp store. | |
SharedTempStoreTest:: |
protected | function |
Overrides UnitTestCase:: |
|
SharedTempStoreTest:: |
public | function | Tests the delete() method. | |
SharedTempStoreTest:: |
public | function | Tests the deleteIfOwner() method. | |
SharedTempStoreTest:: |
public | function | Tests the delete() method with no lock available. | |
SharedTempStoreTest:: |
public | function | @covers ::get | |
SharedTempStoreTest:: |
public | function | Tests the getIfOwner() method. | |
SharedTempStoreTest:: |
public | function | Tests the getMetadata() method. | |
SharedTempStoreTest:: |
public | function | @covers ::getMetadata @expectedDeprecation Using the "owner" public property of a TempStore lock is deprecated in Drupal 8.7.0 and will not be allowed in Drupal 9.0.0. Use \Drupal\Core\TempStore\Lock::getOwnerId() instead. See… | |
SharedTempStoreTest:: |
public | function | @covers ::getMetadata @expectedDeprecation Using the "updated" public property of a TempStore lock is deprecated in Drupal 8.7.0 and will not be allowed in Drupal 9.0.0. Use \Drupal\Core\TempStore\Lock::getUpdated() instead. See… | |
SharedTempStoreTest:: |
public | function | Tests the serialization of a shared temp store. | |
SharedTempStoreTest:: |
public | function | Tests a successful set() call. | |
SharedTempStoreTest:: |
public | function | Tests the setIfNotExists() methods. | |
SharedTempStoreTest:: |
public | function | Tests the setIfOwner() method with matching and non matching owners. | |
SharedTempStoreTest:: |
public | function | Tests the setIfOwner() method when a key already exists but no object. | |
SharedTempStoreTest:: |
public | function | Tests the setIfOwner() method when no key exists. | |
SharedTempStoreTest:: |
public | function | Tests the set() method with no lock available. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |