Newer
Older
<?php
Dries Buytaert
committed
namespace Drupal\Tests\user\Unit;
use Drupal\Tests\UnitTestCase;
Angie Byron
committed
use Drupal\user\PrivateTempStore;
Alex Pott
committed
use Drupal\user\TempStoreException;
use Symfony\Component\HttpFoundation\Request;
Angie Byron
committed
use Symfony\Component\HttpFoundation\RequestStack;
/**
Angie Byron
committed
* @coversDefaultClass \Drupal\user\PrivateTempStore
* @group user
Angie Byron
committed
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 user temp store.
*
Angie Byron
committed
* @var \Drupal\user\PrivateTempStore
*/
protected $tempStore;
/**
Angie Byron
committed
* The current user.
Angie Byron
committed
* @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
protected $currentUser;
/**
* 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->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
Angie Byron
committed
$this->currentUser = $this->getMock('Drupal\Core\Session\AccountProxyInterface');
$this->currentUser->expects($this->any())
->method('id')
->willReturn(1);
$this->requestStack = new RequestStack();
$request = Request::createFromGlobals();
$this->requestStack->push($request);
Angie Byron
committed
$this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
$this->ownObject = (object) [
'data' => 'test_data',
Angie Byron
committed
'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;
}
/**
Angie Byron
committed
* Tests the get() method.
Angie Byron
committed
* @covers ::get
Angie Byron
committed
public function testGet() {
$this->keyValue->expects($this->at(0))
->method('get')
Angie Byron
committed
->with('1:test_2')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
Angie Byron
committed
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(2))
->method('get')
Angie Byron
committed
->with('1:test')
->will($this->returnValue($this->otherObject));
Angie Byron
committed
$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() {
$this->lock->expects($this->at(0))
->method('acquire')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(FALSE));
$this->lock->expects($this->at(1))
->method('wait')
Angie Byron
committed
->with('1:test');
$this->lock->expects($this->at(2))
->method('acquire')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('getCollectionName');
Alex Pott
committed
$this->setExpectedException(TempStoreException::class);
$this->tempStore->set('test', 'value');
}
/**
* Tests a successful set() call.
*
* @covers ::set
*/
public function testSet() {
$this->lock->expects($this->once())
->method('acquire')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(TRUE));
$this->lock->expects($this->never())
->method('wait');
$this->lock->expects($this->once())
->method('release')
Angie Byron
committed
->with('1:test');
$this->keyValue->expects($this->once())
->method('setWithExpire')
Angie Byron
committed
->with('1:test', $this->ownObject, 604800);
$this->tempStore->set('test', 'test_data');
}
/**
* Tests the getMetadata() method.
*
* @covers ::getMetadata
*/
public function testGetMetadata() {
$this->keyValue->expects($this->at(0))
->method('get')
Angie Byron
committed
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(1))
->method('get')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(FALSE));
$metadata = $this->tempStore->getMetadata('test');
$this->assertObjectHasAttribute('owner', $metadata);
// Data should get removed.
$this->assertObjectNotHasAttribute('data', $metadata);
$this->assertNull($this->tempStore->getMetadata('test'));
}
/**
Angie Byron
committed
* Tests the locking in the delete() method.
* @covers ::delete
Angie Byron
committed
public function testDeleteLocking() {
$this->keyValue->expects($this->once())
->method('get')
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->lock->expects($this->once())
->method('acquire')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(TRUE));
$this->lock->expects($this->never())
->method('wait');
$this->lock->expects($this->once())
->method('release')
Angie Byron
committed
->with('1:test');
$this->keyValue->expects($this->once())
->method('delete')
Angie Byron
committed
->with('1:test');
Angie Byron
committed
$this->assertTrue($this->tempStore->delete('test'));
}
/**
* Tests the delete() method with no lock available.
*
* @covers ::delete
*/
public function testDeleteWithNoLockAvailable() {
Angie Byron
committed
$this->keyValue->expects($this->once())
->method('get')
->with('1:test')
->will($this->returnValue($this->ownObject));
$this->lock->expects($this->at(0))
->method('acquire')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(FALSE));
$this->lock->expects($this->at(1))
->method('wait')
Angie Byron
committed
->with('1:test');
$this->lock->expects($this->at(2))
->method('acquire')
Angie Byron
committed
->with('1:test')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->once())
->method('getCollectionName');
Alex Pott
committed
$this->setExpectedException(TempStoreException::class);
$this->tempStore->delete('test');
}
/**
Angie Byron
committed
* Tests the delete() method.
Angie Byron
committed
* @covers ::delete
Angie Byron
committed
public function testDelete() {
$this->lock->expects($this->once())
->method('acquire')
Angie Byron
committed
->with('1:test_2')
->will($this->returnValue(TRUE));
$this->keyValue->expects($this->at(0))
->method('get')
Angie Byron
committed
->with('1:test_1')
->will($this->returnValue(FALSE));
$this->keyValue->expects($this->at(1))
->method('get')
Angie Byron
committed
->with('1:test_2')
->will($this->returnValue($this->ownObject));
$this->keyValue->expects($this->at(2))
->method('delete')
Angie Byron
committed
->with('1:test_2');
$this->keyValue->expects($this->at(3))
->method('get')
Angie Byron
committed
->with('1:test_3')
->will($this->returnValue($this->otherObject));
Angie Byron
committed
$this->assertTrue($this->tempStore->delete('test_1'));
$this->assertTrue($this->tempStore->delete('test_2'));
$this->assertFalse($this->tempStore->delete('test_3'));