class PrivateTempStoreTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/TempStore/PrivateTempStoreTest.php \Drupal\Tests\Core\TempStore\PrivateTempStoreTest
  2. 8.9.x core/modules/user/tests/src/Unit/PrivateTempStoreTest.php \Drupal\Tests\user\Unit\PrivateTempStoreTest
  3. 8.9.x core/tests/Drupal/Tests/Core/TempStore/PrivateTempStoreTest.php \Drupal\Tests\Core\TempStore\PrivateTempStoreTest
  4. 10 core/tests/Drupal/Tests/Core/TempStore/PrivateTempStoreTest.php \Drupal\Tests\Core\TempStore\PrivateTempStoreTest

@coversDefaultClass \Drupal\Core\TempStore\PrivateTempStore @group TempStore

Hierarchy

Expanded class hierarchy of PrivateTempStoreTest

File

core/tests/Drupal/Tests/Core/TempStore/PrivateTempStoreTest.php, line 18

Namespace

Drupal\Tests\Core\TempStore
View source
class PrivateTempStoreTest 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\PrivateTempStore
     */
    protected $tempStore;
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $currentUser;
    
    /**
     * 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->currentUser = $this->createMock('Drupal\\Core\\Session\\AccountProxyInterface');
        $this->currentUser
            ->expects($this->any())
            ->method('id')
            ->willReturn(1);
        $this->requestStack = new RequestStack();
        $request = Request::createFromGlobals();
        $this->requestStack
            ->push($request);
        $this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
        $this->ownObject = (object) [
            'data' => 'test_data',
            'owner' => $this->currentUser
                ->id(),
            'updated' => (int) $request->server
                ->get('REQUEST_TIME'),
        ];
        // Clone the object but change the owner.
        $this->otherObject = clone $this->ownObject;
        $this->otherObject->owner = 2;
    }
    
    /**
     * Tests the get() method.
     *
     * @covers ::get
     */
    public function testGet() : void {
        $calls = [
            '1:test_2',
            '1:test',
            '1:test',
        ];
        $this->keyValue
            ->expects($this->exactly(count($calls)))
            ->method('get')
            ->with($this->callback(function (string $key) use (&$calls) : bool {
            return array_shift($calls) == $key;
        }))
            ->willReturnOnConsecutiveCalls(FALSE, $this->ownObject, $this->otherObject);
        $this->assertNull($this->tempStore
            ->get('test_2'));
        $this->assertSame($this->ownObject->data, $this->tempStore
            ->get('test'));
        $this->assertNull($this->tempStore
            ->get('test'));
    }
    
    /**
     * Tests the set() method with no lock available.
     *
     * @covers ::set
     */
    public function testSetWithNoLockAvailable() : void {
        $this->lock
            ->expects($this->exactly(2))
            ->method('acquire')
            ->with('1:test')
            ->willReturn(FALSE);
        $this->lock
            ->expects($this->once())
            ->method('wait')
            ->with('1: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() : void {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('1:test')
            ->willReturn(TRUE);
        $this->lock
            ->expects($this->never())
            ->method('wait');
        $this->lock
            ->expects($this->once())
            ->method('release')
            ->with('1:test');
        $this->keyValue
            ->expects($this->once())
            ->method('setWithExpire')
            ->with('1:test', $this->ownObject, 604800);
        $this->tempStore
            ->set('test', 'test_data');
    }
    
    /**
     * Tests the getMetadata() method.
     *
     * @covers ::getMetadata
     */
    public function testGetMetadata() : void {
        $this->keyValue
            ->expects($this->exactly(2))
            ->method('get')
            ->with('1:test')
            ->willReturnOnConsecutiveCalls($this->ownObject, FALSE);
        $metadata = $this->tempStore
            ->getMetadata('test');
        $this->assertInstanceOf(Lock::class, $metadata);
        $this->assertObjectHasProperty('ownerId', $metadata);
        $this->assertObjectHasProperty('updated', $metadata);
        // Data should get removed.
        $this->assertObjectNotHasProperty('data', $metadata);
        $this->assertNull($this->tempStore
            ->getMetadata('test'));
    }
    
    /**
     * Tests the locking in the delete() method.
     *
     * @covers ::delete
     */
    public function testDeleteLocking() : void {
        $this->keyValue
            ->expects($this->once())
            ->method('get')
            ->with('1:test')
            ->willReturn($this->ownObject);
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('1:test')
            ->willReturn(TRUE);
        $this->lock
            ->expects($this->never())
            ->method('wait');
        $this->lock
            ->expects($this->once())
            ->method('release')
            ->with('1:test');
        $this->keyValue
            ->expects($this->once())
            ->method('delete')
            ->with('1:test');
        $this->assertTrue($this->tempStore
            ->delete('test'));
    }
    
    /**
     * Tests the delete() method with no lock available.
     *
     * @covers ::delete
     */
    public function testDeleteWithNoLockAvailable() : void {
        $this->keyValue
            ->expects($this->once())
            ->method('get')
            ->with('1:test')
            ->willReturn($this->ownObject);
        $this->lock
            ->expects($this->exactly(2))
            ->method('acquire')
            ->with('1:test')
            ->willReturn(FALSE);
        $this->lock
            ->expects($this->once())
            ->method('wait')
            ->with('1:test');
        $this->keyValue
            ->expects($this->once())
            ->method('getCollectionName');
        $this->expectException(TempStoreException::class);
        $this->tempStore
            ->delete('test');
    }
    
    /**
     * Tests the delete() method.
     *
     * @covers ::delete
     */
    public function testDelete() : void {
        $this->lock
            ->expects($this->once())
            ->method('acquire')
            ->with('1:test_2')
            ->willReturn(TRUE);
        $calls = [
            '1:test_1',
            '1:test_2',
            '1:test_3',
        ];
        $this->keyValue
            ->expects($this->exactly(count($calls)))
            ->method('get')
            ->with($this->callback(function (string $key) use (&$calls) : bool {
            return array_shift($calls) == $key;
        }))
            ->willReturnOnConsecutiveCalls(FALSE, $this->ownObject, $this->otherObject);
        $this->keyValue
            ->expects($this->once())
            ->method('delete')
            ->with('1:test_2');
        $this->assertTrue($this->tempStore
            ->delete('test_1'));
        $this->assertTrue($this->tempStore
            ->delete('test_2'));
        $this->assertFalse($this->tempStore
            ->delete('test_3'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
PrivateTempStoreTest::$currentUser protected property The current user.
PrivateTempStoreTest::$keyValue protected property The mock key value expirable backend.
PrivateTempStoreTest::$lock protected property The mock lock backend.
PrivateTempStoreTest::$otherObject protected property A tempstore object not belonging to the owner.
PrivateTempStoreTest::$ownObject protected property A tempstore object belonging to the owner.
PrivateTempStoreTest::$requestStack protected property The request stack.
PrivateTempStoreTest::$tempStore protected property The temp store.
PrivateTempStoreTest::setUp protected function Overrides UnitTestCase::setUp
PrivateTempStoreTest::testDelete public function Tests the delete() method.
PrivateTempStoreTest::testDeleteLocking public function Tests the locking in the delete() method.
PrivateTempStoreTest::testDeleteWithNoLockAvailable public function Tests the delete() method with no lock available.
PrivateTempStoreTest::testGet public function Tests the get() method.
PrivateTempStoreTest::testGetMetadata public function Tests the getMetadata() method.
PrivateTempStoreTest::testSet public function Tests a successful set() call.
PrivateTempStoreTest::testSetWithNoLockAvailable public function Tests the set() method with no lock available.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.