6b5d4419681b7f8e677bfddbe42a4a6b53862c47
[yaffs-website] / web / core / modules / user / tests / src / Unit / SharedTempStoreTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Unit;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\user\SharedTempStore
13  * @group user
14  * @group legacy
15  * @runTestsInSeparateProcesses
16  * @preserveGlobalState disabled
17  */
18 class SharedTempStoreTest extends UnitTestCase {
19
20   /**
21    * The mock key value expirable backend.
22    *
23    * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
24    */
25   protected $keyValue;
26
27   /**
28    * The mock lock backend.
29    *
30    * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
31    */
32   protected $lock;
33
34   /**
35    * The user temp store.
36    *
37    * @var \Drupal\user\SharedTempStore
38    */
39   protected $tempStore;
40
41   /**
42    * The owner used in this test.
43    *
44    * @var int
45    */
46   protected $owner = 1;
47
48   /**
49    * The request stack.
50    *
51    * @var \Symfony\Component\HttpFoundation\RequestStack
52    */
53   protected $requestStack;
54
55   /**
56    * A tempstore object belonging to the owner.
57    *
58    * @var \stdClass
59    */
60   protected $ownObject;
61
62   /**
63    * A tempstore object not belonging to the owner.
64    *
65    * @var \stdClass
66    */
67   protected $otherObject;
68
69   /**
70    * {@inheritdoc}
71    */
72   protected function setUp() {
73     parent::setUp();
74
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);
80
81     $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800);
82
83     $this->ownObject = (object) [
84       'data' => 'test_data',
85       'owner' => $this->owner,
86       'updated' => (int) $request->server->get('REQUEST_TIME'),
87     ];
88
89     // Clone the object but change the owner.
90     $this->otherObject = clone $this->ownObject;
91     $this->otherObject->owner = 2;
92   }
93
94   /**
95    * @covers ::get
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.
97    */
98   public function testGet() {
99     $this->keyValue->expects($this->at(0))
100       ->method('get')
101       ->with('test_2')
102       ->will($this->returnValue(FALSE));
103     $this->keyValue->expects($this->at(1))
104       ->method('get')
105       ->with('test')
106       ->will($this->returnValue($this->ownObject));
107
108     $this->assertNull($this->tempStore->get('test_2'));
109     $this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
110   }
111
112   /**
113    * Tests the getIfOwner() method.
114    *
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.
117    */
118   public function testGetIfOwner() {
119     $this->keyValue->expects($this->at(0))
120       ->method('get')
121       ->with('test_2')
122       ->will($this->returnValue(FALSE));
123     $this->keyValue->expects($this->at(1))
124       ->method('get')
125       ->with('test')
126       ->will($this->returnValue($this->ownObject));
127     $this->keyValue->expects($this->at(2))
128       ->method('get')
129       ->with('test')
130       ->will($this->returnValue($this->otherObject));
131
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'));
135   }
136
137   /**
138    * Tests the set() method with no lock available.
139    *
140    * @covers ::set
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.
142    */
143   public function testSetWithNoLockAvailable() {
144     $this->lock->expects($this->at(0))
145       ->method('acquire')
146       ->with('test')
147       ->will($this->returnValue(FALSE));
148     $this->lock->expects($this->at(1))
149       ->method('wait')
150       ->with('test');
151     $this->lock->expects($this->at(2))
152       ->method('acquire')
153       ->with('test')
154       ->will($this->returnValue(FALSE));
155
156     $this->keyValue->expects($this->once())
157       ->method('getCollectionName');
158
159     $this->setExpectedException(TempStoreException::class);
160     $this->tempStore->set('test', 'value');
161   }
162
163   /**
164    * Tests a successful set() call.
165    *
166    * @covers ::set
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.
168    */
169   public function testSet() {
170     $this->lock->expects($this->once())
171       ->method('acquire')
172       ->with('test')
173       ->will($this->returnValue(TRUE));
174     $this->lock->expects($this->never())
175       ->method('wait');
176     $this->lock->expects($this->once())
177       ->method('release')
178       ->with('test');
179
180     $this->keyValue->expects($this->once())
181       ->method('setWithExpire')
182       ->with('test', $this->ownObject, 604800);
183
184     $this->tempStore->set('test', 'test_data');
185   }
186
187   /**
188    * Tests the setIfNotExists() methods.
189    *
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.
192    */
193   public function testSetIfNotExists() {
194     $this->keyValue->expects($this->once())
195       ->method('setWithExpireIfNotExists')
196       ->with('test', $this->ownObject, 604800)
197       ->will($this->returnValue(TRUE));
198
199     $this->assertTrue($this->tempStore->setIfNotExists('test', 'test_data'));
200   }
201
202   /**
203    * Tests the setIfOwner() method when no key exists.
204    *
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.
207    */
208   public function testSetIfOwnerWhenNotExists() {
209     $this->keyValue->expects($this->once())
210       ->method('setWithExpireIfNotExists')
211       ->will($this->returnValue(TRUE));
212
213     $this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
214   }
215
216   /**
217    * Tests the setIfOwner() method when a key already exists but no object.
218    *
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.
221    */
222   public function testSetIfOwnerNoObject() {
223     $this->keyValue->expects($this->once())
224       ->method('setWithExpireIfNotExists')
225       ->will($this->returnValue(FALSE));
226
227     $this->keyValue->expects($this->once())
228       ->method('get')
229       ->with('test')
230       ->will($this->returnValue(FALSE));
231
232     $this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
233   }
234
235   /**
236    * Tests the setIfOwner() method with matching and non matching owners.
237    *
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.
240    */
241   public function testSetIfOwner() {
242     $this->lock->expects($this->once())
243       ->method('acquire')
244       ->with('test')
245       ->will($this->returnValue(TRUE));
246
247     $this->keyValue->expects($this->exactly(2))
248       ->method('setWithExpireIfNotExists')
249       ->will($this->returnValue(FALSE));
250
251     $this->keyValue->expects($this->exactly(2))
252       ->method('get')
253       ->with('test')
254       ->will($this->onConsecutiveCalls($this->ownObject, $this->otherObject));
255
256     $this->assertTrue($this->tempStore->setIfOwner('test', 'test_data'));
257     $this->assertFalse($this->tempStore->setIfOwner('test', 'test_data'));
258   }
259
260   /**
261    * Tests the getMetadata() method.
262    *
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.
265    */
266   public function testGetMetadata() {
267     $this->keyValue->expects($this->at(0))
268       ->method('get')
269       ->with('test')
270       ->will($this->returnValue($this->ownObject));
271
272     $this->keyValue->expects($this->at(1))
273       ->method('get')
274       ->with('test')
275       ->will($this->returnValue(FALSE));
276
277     $metadata = $this->tempStore->getMetadata('test');
278     $this->assertObjectHasAttribute('owner', $metadata);
279     // Data should get removed.
280     $this->assertObjectNotHasAttribute('data', $metadata);
281
282     $this->assertNull($this->tempStore->getMetadata('test'));
283   }
284
285   /**
286    * Tests the delete() method.
287    *
288    * @covers ::delete
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.
290    */
291   public function testDelete() {
292     $this->lock->expects($this->once())
293       ->method('acquire')
294       ->with('test')
295       ->will($this->returnValue(TRUE));
296     $this->lock->expects($this->never())
297       ->method('wait');
298     $this->lock->expects($this->once())
299       ->method('release')
300       ->with('test');
301
302     $this->keyValue->expects($this->once())
303       ->method('delete')
304       ->with('test');
305
306     $this->tempStore->delete('test');
307   }
308
309   /**
310    * Tests the delete() method with no lock available.
311    *
312    * @covers ::delete
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.
314    */
315   public function testDeleteWithNoLockAvailable() {
316     $this->lock->expects($this->at(0))
317       ->method('acquire')
318       ->with('test')
319       ->will($this->returnValue(FALSE));
320     $this->lock->expects($this->at(1))
321       ->method('wait')
322       ->with('test');
323     $this->lock->expects($this->at(2))
324       ->method('acquire')
325       ->with('test')
326       ->will($this->returnValue(FALSE));
327
328     $this->keyValue->expects($this->once())
329       ->method('getCollectionName');
330
331     $this->setExpectedException(TempStoreException::class);
332     $this->tempStore->delete('test');
333   }
334
335   /**
336    * Tests the deleteIfOwner() method.
337    *
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.
340    */
341   public function testDeleteIfOwner() {
342     $this->lock->expects($this->once())
343       ->method('acquire')
344       ->with('test_2')
345       ->will($this->returnValue(TRUE));
346
347     $this->keyValue->expects($this->at(0))
348       ->method('get')
349       ->with('test_1')
350       ->will($this->returnValue(FALSE));
351     $this->keyValue->expects($this->at(1))
352       ->method('get')
353       ->with('test_2')
354       ->will($this->returnValue($this->ownObject));
355     $this->keyValue->expects($this->at(2))
356       ->method('delete')
357       ->with('test_2');
358     $this->keyValue->expects($this->at(3))
359       ->method('get')
360       ->with('test_3')
361       ->will($this->returnValue($this->otherObject));
362
363     $this->assertTrue($this->tempStore->deleteIfOwner('test_1'));
364     $this->assertTrue($this->tempStore->deleteIfOwner('test_2'));
365     $this->assertFalse($this->tempStore->deleteIfOwner('test_3'));
366   }
367
368 }