3 namespace Drupal\Tests\Core\TempStore;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\TempStore\PrivateTempStore;
7 use Drupal\Core\TempStore\TempStoreException;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\RequestStack;
12 * @coversDefaultClass \Drupal\Core\TempStore\PrivateTempStore
15 class PrivateTempStoreTest extends UnitTestCase {
18 * The mock key value expirable backend.
20 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
25 * The mock lock backend.
27 * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
34 * @var \Drupal\Core\TempStore\PrivateTempStore
41 * @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject
43 protected $currentUser;
48 * @var \Symfony\Component\HttpFoundation\RequestStack
50 protected $requestStack;
53 * A tempstore object belonging to the owner.
60 * A tempstore object not belonging to the owner.
64 protected $otherObject;
69 protected function setUp() {
72 $this->keyValue = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
73 $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
74 $this->currentUser = $this->getMock('Drupal\Core\Session\AccountProxyInterface');
75 $this->currentUser->expects($this->any())
79 $this->requestStack = new RequestStack();
80 $request = Request::createFromGlobals();
81 $this->requestStack->push($request);
83 $this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
85 $this->ownObject = (object) [
86 'data' => 'test_data',
87 'owner' => $this->currentUser->id(),
88 'updated' => (int) $request->server->get('REQUEST_TIME'),
91 // Clone the object but change the owner.
92 $this->otherObject = clone $this->ownObject;
93 $this->otherObject->owner = 2;
98 * Tests the get() method.
102 public function testGet() {
103 $this->keyValue->expects($this->at(0))
106 ->will($this->returnValue(FALSE));
107 $this->keyValue->expects($this->at(1))
110 ->will($this->returnValue($this->ownObject));
111 $this->keyValue->expects($this->at(2))
114 ->will($this->returnValue($this->otherObject));
116 $this->assertNull($this->tempStore->get('test_2'));
117 $this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
118 $this->assertNull($this->tempStore->get('test'));
122 * Tests the set() method with no lock available.
126 public function testSetWithNoLockAvailable() {
127 $this->lock->expects($this->at(0))
130 ->will($this->returnValue(FALSE));
131 $this->lock->expects($this->at(1))
134 $this->lock->expects($this->at(2))
137 ->will($this->returnValue(FALSE));
139 $this->keyValue->expects($this->once())
140 ->method('getCollectionName');
142 $this->setExpectedException(TempStoreException::class);
143 $this->tempStore->set('test', 'value');
147 * Tests a successful set() call.
151 public function testSet() {
152 $this->lock->expects($this->once())
155 ->will($this->returnValue(TRUE));
156 $this->lock->expects($this->never())
158 $this->lock->expects($this->once())
162 $this->keyValue->expects($this->once())
163 ->method('setWithExpire')
164 ->with('1:test', $this->ownObject, 604800);
166 $this->tempStore->set('test', 'test_data');
170 * Tests the getMetadata() method.
172 * @covers ::getMetadata
174 public function testGetMetadata() {
175 $this->keyValue->expects($this->at(0))
178 ->will($this->returnValue($this->ownObject));
180 $this->keyValue->expects($this->at(1))
183 ->will($this->returnValue(FALSE));
185 $metadata = $this->tempStore->getMetadata('test');
186 $this->assertObjectHasAttribute('owner', $metadata);
187 // Data should get removed.
188 $this->assertObjectNotHasAttribute('data', $metadata);
190 $this->assertNull($this->tempStore->getMetadata('test'));
194 * Tests the locking in the delete() method.
198 public function testDeleteLocking() {
199 $this->keyValue->expects($this->once())
202 ->will($this->returnValue($this->ownObject));
203 $this->lock->expects($this->once())
206 ->will($this->returnValue(TRUE));
207 $this->lock->expects($this->never())
209 $this->lock->expects($this->once())
213 $this->keyValue->expects($this->once())
217 $this->assertTrue($this->tempStore->delete('test'));
221 * Tests the delete() method with no lock available.
225 public function testDeleteWithNoLockAvailable() {
226 $this->keyValue->expects($this->once())
229 ->will($this->returnValue($this->ownObject));
230 $this->lock->expects($this->at(0))
233 ->will($this->returnValue(FALSE));
234 $this->lock->expects($this->at(1))
237 $this->lock->expects($this->at(2))
240 ->will($this->returnValue(FALSE));
242 $this->keyValue->expects($this->once())
243 ->method('getCollectionName');
245 $this->setExpectedException(TempStoreException::class);
246 $this->tempStore->delete('test');
250 * Tests the delete() method.
254 public function testDelete() {
255 $this->lock->expects($this->once())
258 ->will($this->returnValue(TRUE));
260 $this->keyValue->expects($this->at(0))
263 ->will($this->returnValue(FALSE));
264 $this->keyValue->expects($this->at(1))
267 ->will($this->returnValue($this->ownObject));
268 $this->keyValue->expects($this->at(2))
271 $this->keyValue->expects($this->at(3))
274 ->will($this->returnValue($this->otherObject));
276 $this->assertTrue($this->tempStore->delete('test_1'));
277 $this->assertTrue($this->tempStore->delete('test_2'));
278 $this->assertFalse($this->tempStore->delete('test_3'));