class SharedTempStoreTest
Same name in other branches
- 8.9.x core/modules/user/tests/src/Unit/SharedTempStoreTest.php \Drupal\Tests\user\Unit\SharedTempStoreTest
- 8.9.x core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest
- 10 core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest
- 11.x core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php \Drupal\Tests\Core\TempStore\SharedTempStoreTest
@coversDefaultClass \Drupal\Core\TempStore\SharedTempStore @group TempStore
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait
- class \Drupal\Tests\Core\TempStore\SharedTempStoreTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of SharedTempStoreTest
File
-
core/
tests/ Drupal/ Tests/ Core/ TempStore/ SharedTempStoreTest.php, line 22
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 object
*/
protected $ownObject;
/**
* A tempstore object not belonging to the owner.
*
* @var object
*/
protected $otherObject;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
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();
$session = $this->createMock(SessionInterface::class);
$request->setSession($session);
$this->requestStack
->push($request);
$current_user = $this->createMock(AccountProxyInterface::class);
$this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, $current_user, 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->exactly(2))
->method('get')
->withConsecutive([
'test_2',
], [
'test',
])
->willReturnOnConsecutiveCalls(FALSE, $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->exactly(3))
->method('get')
->withConsecutive([
'test_2',
], [
'test',
], [
'test',
])
->willReturnOnConsecutiveCalls(FALSE, $this->ownObject, $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->exactly(2))
->method('acquire')
->with('test')
->willReturn(FALSE);
$this->lock
->expects($this->once())
->method('wait')
->with('test');
$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')
->willReturn(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)
->willReturn(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')
->willReturn(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')
->willReturn(FALSE);
$this->keyValue
->expects($this->once())
->method('get')
->with('test')
->willReturn(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')
->willReturn(TRUE);
$this->keyValue
->expects($this->exactly(2))
->method('setWithExpireIfNotExists')
->willReturn(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->exactly(2))
->method('get')
->with('test')
->willReturnOnConsecutiveCalls($this->ownObject, 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'));
}
/**
* Tests the delete() method.
*
* @covers ::delete
*/
public function testDelete() {
$this->lock
->expects($this->once())
->method('acquire')
->with('test')
->willReturn(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->exactly(2))
->method('acquire')
->with('test')
->willReturn(FALSE);
$this->lock
->expects($this->once())
->method('wait')
->with('test');
$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')
->willReturn(TRUE);
$this->keyValue
->expects($this->exactly(3))
->method('get')
->withConsecutive([
'test_1',
], [
'test_2',
], [
'test_3',
])
->willReturnOnConsecutiveCalls(FALSE, $this->ownObject, $this->otherObject);
$this->keyValue
->expects($this->once())
->method('delete')
->with('test_2');
$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, an exception would be thrown
// when we try to serialize the tempstore.
$unserializable_request = new UnserializableRequest();
$this->requestStack
->push($unserializable_request);
$container = TestKernel::setContainerWithKernel();
$container->set('request_stack', $this->requestStack);
\Drupal::setContainer($container);
$store = unserialize(serialize($this->tempStore));
$this->assertInstanceOf(SharedTempStore::class, $store);
$reflected_request_stack = (new \ReflectionObject($store))->getProperty('requestStack');
$reflected_request_stack->setAccessible(TRUE);
$request_stack = $reflected_request_stack->getValue($store);
$this->assertEquals($this->requestStack, $request_stack);
$this->assertSame($unserializable_request, $request_stack->pop());
}
/**
* @group legacy
*/
public function testLegacyConstructor() {
$this->expectDeprecation('Calling Drupal\\Core\\TempStore\\SharedTempStore::__construct() without the $current_user argument is deprecated in drupal:9.2.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3006268');
$container = new ContainerBuilder();
$current_user = $this->createMock(AccountProxyInterface::class);
$container->set('current_user', $current_user);
\Drupal::setContainer($container);
$store = new SharedTempStore($this->keyValue, $this->lock, 2, $this->requestStack, 1000);
$reflection_class = new \ReflectionClass(SharedTempStore::class);
$current_user_property = $reflection_class->getProperty('currentUser');
$current_user_property->setAccessible(TRUE);
$this->assertSame($current_user, $current_user_property->getValue($store));
$expire_property = $reflection_class->getProperty('expire');
$expire_property->setAccessible(TRUE);
$this->assertSame(1000, $expire_property->getValue($store));
}
/**
* @group legacy
* @covers \Drupal\Core\TempStore\SharedTempStoreFactory::__construct
*/
public function testLegacyFactoryConstructor() {
$this->expectDeprecation('Calling Drupal\\Core\\TempStore\\SharedTempStoreFactory::__construct() without the $current_user argument is deprecated in drupal:9.2.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3006268');
$container = new ContainerBuilder();
$current_user = $this->createMock(AccountProxyInterface::class);
$container->set('current_user', $current_user);
\Drupal::setContainer($container);
$key_value_factory = $this->prophesize(KeyValueExpirableFactoryInterface::class);
$store = new SharedTempStoreFactory($key_value_factory->reveal(), $this->lock, $this->requestStack, 1000);
$reflection_class = new \ReflectionClass(SharedTempStoreFactory::class);
$current_user_property = $reflection_class->getProperty('currentUser');
$current_user_property->setAccessible(TRUE);
$this->assertSame($current_user, $current_user_property->getValue($store));
$expire_property = $reflection_class->getProperty('expire');
$expire_property->setAccessible(TRUE);
$this->assertSame(1000, $expire_property->getValue($store));
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
SharedTempStoreTest::$keyValue | protected | property | The mock key value expirable backend. | |||
SharedTempStoreTest::$lock | protected | property | The mock lock backend. | |||
SharedTempStoreTest::$otherObject | protected | property | A tempstore object not belonging to the owner. | |||
SharedTempStoreTest::$owner | protected | property | The owner used in this test. | |||
SharedTempStoreTest::$ownObject | protected | property | A tempstore object belonging to the owner. | |||
SharedTempStoreTest::$requestStack | protected | property | The request stack. | |||
SharedTempStoreTest::$tempStore | protected | property | The temp store. | |||
SharedTempStoreTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
SharedTempStoreTest::testDelete | public | function | Tests the delete() method. | |||
SharedTempStoreTest::testDeleteIfOwner | public | function | Tests the deleteIfOwner() method. | |||
SharedTempStoreTest::testDeleteWithNoLockAvailable | public | function | Tests the delete() method with no lock available. | |||
SharedTempStoreTest::testGet | public | function | @covers ::get | |||
SharedTempStoreTest::testGetIfOwner | public | function | Tests the getIfOwner() method. | |||
SharedTempStoreTest::testGetMetadata | public | function | Tests the getMetadata() method. | |||
SharedTempStoreTest::testLegacyConstructor | public | function | @group legacy | |||
SharedTempStoreTest::testLegacyFactoryConstructor | public | function | @group legacy @covers \Drupal\Core\TempStore\SharedTempStoreFactory::__construct |
|||
SharedTempStoreTest::testSerialization | public | function | Tests the serialization of a shared temp store. | |||
SharedTempStoreTest::testSet | public | function | Tests a successful set() call. | |||
SharedTempStoreTest::testSetIfNotExists | public | function | Tests the setIfNotExists() methods. | |||
SharedTempStoreTest::testSetIfOwner | public | function | Tests the setIfOwner() method with matching and non matching owners. | |||
SharedTempStoreTest::testSetIfOwnerNoObject | public | function | Tests the setIfOwner() method when a key already exists but no object. | |||
SharedTempStoreTest::testSetIfOwnerWhenNotExists | public | function | Tests the setIfOwner() method when no key exists. | |||
SharedTempStoreTest::testSetWithNoLockAvailable | public | function | Tests the set() method with no lock available. | |||
UnitTestCase::$randomGenerator | protected | property | The random generator. | |||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::assertArrayEquals | Deprecated | protected | function | Asserts if two arrays are equal by sorting them first. | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. | |||
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.