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 * @runTestsInSeparateProcesses
16 * @preserveGlobalState disabled
18 class SharedTempStoreTest extends UnitTestCase {
21 * The mock key value expirable backend.
23 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
28 * The mock lock backend.
30 * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
35 * The user temp store.
37 * @var \Drupal\user\SharedTempStore
42 * The owner used in this test.
51 * @var \Symfony\Component\HttpFoundation\RequestStack
53 protected $requestStack;
56 * A tempstore object belonging to the owner.
63 * A tempstore object not belonging to the owner.
67 protected $otherObject;
72 protected function setUp() {
75 $this->keyValue = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
76 $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
77 $this->requestStack = new RequestStack();
78 $request = Request::createFromGlobals();
79 $this->requestStack->push($request);
81 $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800);
83 $this->ownObject = (object) [
84 'data' => 'test_data',
85 'owner' => $this->owner,
86 'updated' => (int) $request->server->get('REQUEST_TIME'),
89 // Clone the object but change the owner.
90 $this->otherObject = clone $this->ownObject;
91 $this->otherObject->owner = 2;
96 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
98 public function testGet() {
99 $this->keyValue->expects($this->at(0))
102 ->will($this->returnValue(FALSE));
103 $this->keyValue->expects($this->at(1))
106 ->will($this->returnValue($this->ownObject));
108 $this->assertNull($this->tempStore->get('test_2'));
109 $this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
113 * Tests the getIfOwner() method.
115 * @covers ::getIfOwner
116 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
118 public function testGetIfOwner() {
119 $this->keyValue->expects($this->at(0))
122 ->will($this->returnValue(FALSE));
123 $this->keyValue->expects($this->at(1))
126 ->will($this->returnValue($this->ownObject));
127 $this->keyValue->expects($this->at(2))
130 ->will($this->returnValue($this->otherObject));
132 $this->assertNull($this->tempStore->getIfOwner('test_2'));
133 $this->assertSame($this->ownObject->data, $this->tempStore->getIfOwner('test'));
134 $this->assertNull($this->tempStore->getIfOwner('test'));
138 * Tests the set() method with no lock available.
141 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
143 public function testSetWithNoLockAvailable() {
144 $this->lock->expects($this->at(0))
147 ->will($this->returnValue(FALSE));
148 $this->lock->expects($this->at(1))
151 $this->lock->expects($this->at(2))
154 ->will($this->returnValue(FALSE));
156 $this->keyValue->expects($this->once())
157 ->method('getCollectionName');
159 $this->setExpectedException(TempStoreException::class);
160 $this->tempStore->set('test', 'value');
164 * Tests a successful set() call.
167 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
169 public function testSet() {
170 $this->lock->expects($this->once())
173 ->will($this->returnValue(TRUE));
174 $this->lock->expects($this->never())
176 $this->lock->expects($this->once())
180 $this->keyValue->expects($this->once())
181 ->method('setWithExpire')
182 ->with('test', $this->ownObject, 604800);
184 $this->tempStore->set('test', 'test_data');
188 * Tests the setIfNotExists() methods.
190 * @covers ::setIfNotExists
191 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
193 public function testSetIfNotExists() {
194 $this->keyValue->expects($this->once())
195 ->method('setWithExpireIfNotExists')
196 ->with('test', $this->ownObject, 604800)
197 ->will($this->returnValue(TRUE));
199 $this->assertTrue($this->tempStore->setIfNotExists('test', 'test_data'));
203 * Tests the setIfOwner() method when no key exists.
205 * @covers ::setIfOwner
206 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
208 public function testSetIfOwnerWhenNotExists() {
209 $this->keyValue->expects($this->once())
210 ->method('setWithExpireIfNotExists')
211 ->will($this->returnValue(TRUE));
213 $this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
217 * Tests the setIfOwner() method when a key already exists but no object.
219 * @covers ::setIfOwner
220 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
222 public function testSetIfOwnerNoObject() {
223 $this->keyValue->expects($this->once())
224 ->method('setWithExpireIfNotExists')
225 ->will($this->returnValue(FALSE));
227 $this->keyValue->expects($this->once())
230 ->will($this->returnValue(FALSE));
232 $this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
236 * Tests the setIfOwner() method with matching and non matching owners.
238 * @covers ::setIfOwner
239 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
241 public function testSetIfOwner() {
242 $this->lock->expects($this->once())
245 ->will($this->returnValue(TRUE));
247 $this->keyValue->expects($this->exactly(2))
248 ->method('setWithExpireIfNotExists')
249 ->will($this->returnValue(FALSE));
251 $this->keyValue->expects($this->exactly(2))
254 ->will($this->onConsecutiveCalls($this->ownObject, $this->otherObject));
256 $this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
257 $this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
261 * Tests the getMetadata() method.
263 * @covers ::getMetadata
264 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
266 public function testGetMetadata() {
267 $this->keyValue->expects($this->at(0))
270 ->will($this->returnValue($this->ownObject));
272 $this->keyValue->expects($this->at(1))
275 ->will($this->returnValue(FALSE));
277 $metadata = $this->tempStore->getMetadata('test');
278 $this->assertObjectHasAttribute('owner', $metadata);
279 // Data should get removed.
280 $this->assertObjectNotHasAttribute('data', $metadata);
282 $this->assertNull($this->tempStore->getMetadata('test'));
286 * Tests the delete() method.
289 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
291 public function testDelete() {
292 $this->lock->expects($this->once())
295 ->will($this->returnValue(TRUE));
296 $this->lock->expects($this->never())
298 $this->lock->expects($this->once())
302 $this->keyValue->expects($this->once())
306 $this->tempStore->delete('test');
310 * Tests the delete() method with no lock available.
313 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
315 public function testDeleteWithNoLockAvailable() {
316 $this->lock->expects($this->at(0))
319 ->will($this->returnValue(FALSE));
320 $this->lock->expects($this->at(1))
323 $this->lock->expects($this->at(2))
326 ->will($this->returnValue(FALSE));
328 $this->keyValue->expects($this->once())
329 ->method('getCollectionName');
331 $this->setExpectedException(TempStoreException::class);
332 $this->tempStore->delete('test');
336 * Tests the deleteIfOwner() method.
338 * @covers ::deleteIfOwner
339 * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639.
341 public function testDeleteIfOwner() {
342 $this->lock->expects($this->once())
345 ->will($this->returnValue(TRUE));
347 $this->keyValue->expects($this->at(0))
350 ->will($this->returnValue(FALSE));
351 $this->keyValue->expects($this->at(1))
354 ->will($this->returnValue($this->ownObject));
355 $this->keyValue->expects($this->at(2))
358 $this->keyValue->expects($this->at(3))
361 ->will($this->returnValue($this->otherObject));
363 $this->assertTrue($this->tempStore->deleteIfOwner('test_1'));
364 $this->assertTrue($this->tempStore->deleteIfOwner('test_2'));
365 $this->assertFalse($this->tempStore->deleteIfOwner('test_3'));