3 namespace Drupal\Tests\user\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\user\SharedTempStore;
7 use Drupal\user\TempStoreException;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\RequestStack;
12 * @coversDefaultClass \Drupal\user\SharedTempStore
15 class SharedTempStoreTest 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
32 * The user temp store.
34 * @var \Drupal\user\SharedTempStore
39 * The owner used in this test.
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->requestStack = new RequestStack();
75 $request = Request::createFromGlobals();
76 $this->requestStack->push($request);
78 $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800);
80 $this->ownObject = (object) [
81 'data' => 'test_data',
82 'owner' => $this->owner,
83 'updated' => (int) $request->server->get('REQUEST_TIME'),
86 // Clone the object but change the owner.
87 $this->otherObject = clone $this->ownObject;
88 $this->otherObject->owner = 2;
94 public function testGet() {
95 $this->keyValue->expects($this->at(0))
98 ->will($this->returnValue(FALSE));
99 $this->keyValue->expects($this->at(1))
102 ->will($this->returnValue($this->ownObject));
104 $this->assertNull($this->tempStore->get('test_2'));
105 $this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
109 * Tests the getIfOwner() method.
111 * @covers ::getIfOwner
113 public function testGetIfOwner() {
114 $this->keyValue->expects($this->at(0))
117 ->will($this->returnValue(FALSE));
118 $this->keyValue->expects($this->at(1))
121 ->will($this->returnValue($this->ownObject));
122 $this->keyValue->expects($this->at(2))
125 ->will($this->returnValue($this->otherObject));
127 $this->assertNull($this->tempStore->getIfOwner('test_2'));
128 $this->assertSame($this->ownObject->data, $this->tempStore->getIfOwner('test'));
129 $this->assertNull($this->tempStore->getIfOwner('test'));
133 * Tests the set() method with no lock available.
137 public function testSetWithNoLockAvailable() {
138 $this->lock->expects($this->at(0))
141 ->will($this->returnValue(FALSE));
142 $this->lock->expects($this->at(1))
145 $this->lock->expects($this->at(2))
148 ->will($this->returnValue(FALSE));
150 $this->keyValue->expects($this->once())
151 ->method('getCollectionName');
153 $this->setExpectedException(TempStoreException::class);
154 $this->tempStore->set('test', 'value');
158 * Tests a successful set() call.
162 public function testSet() {
163 $this->lock->expects($this->once())
166 ->will($this->returnValue(TRUE));
167 $this->lock->expects($this->never())
169 $this->lock->expects($this->once())
173 $this->keyValue->expects($this->once())
174 ->method('setWithExpire')
175 ->with('test', $this->ownObject, 604800);
177 $this->tempStore->set('test', 'test_data');
181 * Tests the setIfNotExists() methods.
183 * @covers ::setIfNotExists
185 public function testSetIfNotExists() {
186 $this->keyValue->expects($this->once())
187 ->method('setWithExpireIfNotExists')
188 ->with('test', $this->ownObject, 604800)
189 ->will($this->returnValue(TRUE));
191 $this->assertTrue($this->tempStore->setIfNotExists('test', 'test_data'));
195 * Tests the setIfOwner() method when no key exists.
197 * @covers ::setIfOwner
199 public function testSetIfOwnerWhenNotExists() {
200 $this->keyValue->expects($this->once())
201 ->method('setWithExpireIfNotExists')
202 ->will($this->returnValue(TRUE));
204 $this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
208 * Tests the setIfOwner() method when a key already exists but no object.
210 * @covers ::setIfOwner
212 public function testSetIfOwnerNoObject() {
213 $this->keyValue->expects($this->once())
214 ->method('setWithExpireIfNotExists')
215 ->will($this->returnValue(FALSE));
217 $this->keyValue->expects($this->once())
220 ->will($this->returnValue(FALSE));
222 $this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
226 * Tests the setIfOwner() method with matching and non matching owners.
228 * @covers ::setIfOwner
230 public function testSetIfOwner() {
231 $this->lock->expects($this->once())
234 ->will($this->returnValue(TRUE));
236 $this->keyValue->expects($this->exactly(2))
237 ->method('setWithExpireIfNotExists')
238 ->will($this->returnValue(FALSE));
240 $this->keyValue->expects($this->exactly(2))
243 ->will($this->onConsecutiveCalls($this->ownObject, $this->otherObject));
245 $this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
246 $this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
250 * Tests the getMetadata() method.
252 * @covers ::getMetadata
254 public function testGetMetadata() {
255 $this->keyValue->expects($this->at(0))
258 ->will($this->returnValue($this->ownObject));
260 $this->keyValue->expects($this->at(1))
263 ->will($this->returnValue(FALSE));
265 $metadata = $this->tempStore->getMetadata('test');
266 $this->assertObjectHasAttribute('owner', $metadata);
267 // Data should get removed.
268 $this->assertObjectNotHasAttribute('data', $metadata);
270 $this->assertNull($this->tempStore->getMetadata('test'));
274 * Tests the delete() method.
278 public function testDelete() {
279 $this->lock->expects($this->once())
282 ->will($this->returnValue(TRUE));
283 $this->lock->expects($this->never())
285 $this->lock->expects($this->once())
289 $this->keyValue->expects($this->once())
293 $this->tempStore->delete('test');
297 * Tests the delete() method with no lock available.
301 public function testDeleteWithNoLockAvailable() {
302 $this->lock->expects($this->at(0))
305 ->will($this->returnValue(FALSE));
306 $this->lock->expects($this->at(1))
309 $this->lock->expects($this->at(2))
312 ->will($this->returnValue(FALSE));
314 $this->keyValue->expects($this->once())
315 ->method('getCollectionName');
317 $this->setExpectedException(TempStoreException::class);
318 $this->tempStore->delete('test');
322 * Tests the deleteIfOwner() method.
324 * @covers ::deleteIfOwner
326 public function testDeleteIfOwner() {
327 $this->lock->expects($this->once())
330 ->will($this->returnValue(TRUE));
332 $this->keyValue->expects($this->at(0))
335 ->will($this->returnValue(FALSE));
336 $this->keyValue->expects($this->at(1))
339 ->will($this->returnValue($this->ownObject));
340 $this->keyValue->expects($this->at(2))
343 $this->keyValue->expects($this->at(3))
346 ->will($this->returnValue($this->otherObject));
348 $this->assertTrue($this->tempStore->deleteIfOwner('test_1'));
349 $this->assertTrue($this->tempStore->deleteIfOwner('test_2'));
350 $this->assertFalse($this->tempStore->deleteIfOwner('test_3'));