Pull merge.
[yaffs-website] / web / core / modules / user / tests / src / Unit / PrivateTempStoreTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\user\PrivateTempStore;
7 use Drupal\user\TempStoreException;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\RequestStack;
10
11 /**
12  * @coversDefaultClass \Drupal\user\PrivateTempStore
13  * @group user
14  * @group legacy
15  * @runTestsInSeparateProcesses
16  * @preserveGlobalState disabled
17  */
18 class PrivateTempStoreTest 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\PrivateTempStore
38    */
39   protected $tempStore;
40
41   /**
42    * The current user.
43    *
44    * @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject
45    */
46   protected $currentUser;
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->currentUser = $this->getMock('Drupal\Core\Session\AccountProxyInterface');
78     $this->currentUser->expects($this->any())
79       ->method('id')
80       ->willReturn(1);
81
82     $this->requestStack = new RequestStack();
83     $request = Request::createFromGlobals();
84     $this->requestStack->push($request);
85
86     $this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
87
88     $this->ownObject = (object) [
89       'data' => 'test_data',
90       'owner' => $this->currentUser->id(),
91       'updated' => (int) $request->server->get('REQUEST_TIME'),
92     ];
93
94     // Clone the object but change the owner.
95     $this->otherObject = clone $this->ownObject;
96     $this->otherObject->owner = 2;
97   }
98
99   /**
100    * Tests the get() method.
101    *
102    * @covers ::get
103    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
104    */
105   public function testGet() {
106     $this->keyValue->expects($this->at(0))
107       ->method('get')
108       ->with('1:test_2')
109       ->will($this->returnValue(FALSE));
110     $this->keyValue->expects($this->at(1))
111       ->method('get')
112       ->with('1:test')
113       ->will($this->returnValue($this->ownObject));
114     $this->keyValue->expects($this->at(2))
115       ->method('get')
116       ->with('1:test')
117       ->will($this->returnValue($this->otherObject));
118
119     $this->assertNull($this->tempStore->get('test_2'));
120     $this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
121     $this->assertNull($this->tempStore->get('test'));
122   }
123
124   /**
125    * Tests the set() method with no lock available.
126    *
127    * @covers ::set
128    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
129    */
130   public function testSetWithNoLockAvailable() {
131     $this->lock->expects($this->at(0))
132       ->method('acquire')
133       ->with('1:test')
134       ->will($this->returnValue(FALSE));
135     $this->lock->expects($this->at(1))
136       ->method('wait')
137       ->with('1:test');
138     $this->lock->expects($this->at(2))
139       ->method('acquire')
140       ->with('1:test')
141       ->will($this->returnValue(FALSE));
142
143     $this->keyValue->expects($this->once())
144       ->method('getCollectionName');
145
146     $this->setExpectedException(TempStoreException::class);
147     $this->tempStore->set('test', 'value');
148   }
149
150   /**
151    * Tests a successful set() call.
152    *
153    * @covers ::set
154    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
155    */
156   public function testSet() {
157     $this->lock->expects($this->once())
158       ->method('acquire')
159       ->with('1:test')
160       ->will($this->returnValue(TRUE));
161     $this->lock->expects($this->never())
162       ->method('wait');
163     $this->lock->expects($this->once())
164       ->method('release')
165       ->with('1:test');
166
167     $this->keyValue->expects($this->once())
168       ->method('setWithExpire')
169       ->with('1:test', $this->ownObject, 604800);
170
171     $this->tempStore->set('test', 'test_data');
172   }
173
174   /**
175    * Tests the getMetadata() method.
176    *
177    * @covers ::getMetadata
178    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
179    */
180   public function testGetMetadata() {
181     $this->keyValue->expects($this->at(0))
182       ->method('get')
183       ->with('1:test')
184       ->will($this->returnValue($this->ownObject));
185
186     $this->keyValue->expects($this->at(1))
187       ->method('get')
188       ->with('1:test')
189       ->will($this->returnValue(FALSE));
190
191     $metadata = $this->tempStore->getMetadata('test');
192     $this->assertObjectHasAttribute('owner', $metadata);
193     // Data should get removed.
194     $this->assertObjectNotHasAttribute('data', $metadata);
195
196     $this->assertNull($this->tempStore->getMetadata('test'));
197   }
198
199   /**
200    * Tests the locking in the delete() method.
201    *
202    * @covers ::delete
203    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
204    */
205   public function testDeleteLocking() {
206     $this->keyValue->expects($this->once())
207       ->method('get')
208       ->with('1:test')
209       ->will($this->returnValue($this->ownObject));
210     $this->lock->expects($this->once())
211       ->method('acquire')
212       ->with('1:test')
213       ->will($this->returnValue(TRUE));
214     $this->lock->expects($this->never())
215       ->method('wait');
216     $this->lock->expects($this->once())
217       ->method('release')
218       ->with('1:test');
219
220     $this->keyValue->expects($this->once())
221       ->method('delete')
222       ->with('1:test');
223
224     $this->assertTrue($this->tempStore->delete('test'));
225   }
226
227   /**
228    * Tests the delete() method with no lock available.
229    *
230    * @covers ::delete
231    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
232    */
233   public function testDeleteWithNoLockAvailable() {
234     $this->keyValue->expects($this->once())
235       ->method('get')
236       ->with('1:test')
237       ->will($this->returnValue($this->ownObject));
238     $this->lock->expects($this->at(0))
239       ->method('acquire')
240       ->with('1:test')
241       ->will($this->returnValue(FALSE));
242     $this->lock->expects($this->at(1))
243       ->method('wait')
244       ->with('1:test');
245     $this->lock->expects($this->at(2))
246       ->method('acquire')
247       ->with('1:test')
248       ->will($this->returnValue(FALSE));
249
250     $this->keyValue->expects($this->once())
251       ->method('getCollectionName');
252
253     $this->setExpectedException(TempStoreException::class);
254     $this->tempStore->delete('test');
255   }
256
257   /**
258    * Tests the delete() method.
259    *
260    * @covers ::delete
261    * @expectedDeprecation \Drupal\user\PrivateTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\PrivateTempStore instead. See https://www.drupal.org/node/2935639.
262    */
263   public function testDelete() {
264     $this->lock->expects($this->once())
265       ->method('acquire')
266       ->with('1:test_2')
267       ->will($this->returnValue(TRUE));
268
269     $this->keyValue->expects($this->at(0))
270       ->method('get')
271       ->with('1:test_1')
272       ->will($this->returnValue(FALSE));
273     $this->keyValue->expects($this->at(1))
274       ->method('get')
275       ->with('1:test_2')
276       ->will($this->returnValue($this->ownObject));
277     $this->keyValue->expects($this->at(2))
278       ->method('delete')
279       ->with('1:test_2');
280     $this->keyValue->expects($this->at(3))
281       ->method('get')
282       ->with('1:test_3')
283       ->will($this->returnValue($this->otherObject));
284
285     $this->assertTrue($this->tempStore->delete('test_1'));
286     $this->assertTrue($this->tempStore->delete('test_2'));
287     $this->assertFalse($this->tempStore->delete('test_3'));
288   }
289
290 }