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