Updated to Drupal 8.5. Core Media not yet in use.
[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   /**
101    * Tests the get() method.
102    *
103    * @covers ::get
104    * @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.
105    */
106   public function testGet() {
107     $this->keyValue->expects($this->at(0))
108       ->method('get')
109       ->with('1:test_2')
110       ->will($this->returnValue(FALSE));
111     $this->keyValue->expects($this->at(1))
112       ->method('get')
113       ->with('1:test')
114       ->will($this->returnValue($this->ownObject));
115     $this->keyValue->expects($this->at(2))
116       ->method('get')
117       ->with('1:test')
118       ->will($this->returnValue($this->otherObject));
119
120     $this->assertNull($this->tempStore->get('test_2'));
121     $this->assertSame($this->ownObject->data, $this->tempStore->get('test'));
122     $this->assertNull($this->tempStore->get('test'));
123   }
124
125   /**
126    * Tests the set() method with no lock available.
127    *
128    * @covers ::set
129    * @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.
130    */
131   public function testSetWithNoLockAvailable() {
132     $this->lock->expects($this->at(0))
133       ->method('acquire')
134       ->with('1:test')
135       ->will($this->returnValue(FALSE));
136     $this->lock->expects($this->at(1))
137       ->method('wait')
138       ->with('1:test');
139     $this->lock->expects($this->at(2))
140       ->method('acquire')
141       ->with('1:test')
142       ->will($this->returnValue(FALSE));
143
144     $this->keyValue->expects($this->once())
145       ->method('getCollectionName');
146
147     $this->setExpectedException(TempStoreException::class);
148     $this->tempStore->set('test', 'value');
149   }
150
151   /**
152    * Tests a successful set() call.
153    *
154    * @covers ::set
155    * @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.
156    */
157   public function testSet() {
158     $this->lock->expects($this->once())
159       ->method('acquire')
160       ->with('1:test')
161       ->will($this->returnValue(TRUE));
162     $this->lock->expects($this->never())
163       ->method('wait');
164     $this->lock->expects($this->once())
165       ->method('release')
166       ->with('1:test');
167
168     $this->keyValue->expects($this->once())
169       ->method('setWithExpire')
170       ->with('1:test', $this->ownObject, 604800);
171
172     $this->tempStore->set('test', 'test_data');
173   }
174
175   /**
176    * Tests the getMetadata() method.
177    *
178    * @covers ::getMetadata
179    * @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.
180    */
181   public function testGetMetadata() {
182     $this->keyValue->expects($this->at(0))
183       ->method('get')
184       ->with('1:test')
185       ->will($this->returnValue($this->ownObject));
186
187     $this->keyValue->expects($this->at(1))
188       ->method('get')
189       ->with('1:test')
190       ->will($this->returnValue(FALSE));
191
192     $metadata = $this->tempStore->getMetadata('test');
193     $this->assertObjectHasAttribute('owner', $metadata);
194     // Data should get removed.
195     $this->assertObjectNotHasAttribute('data', $metadata);
196
197     $this->assertNull($this->tempStore->getMetadata('test'));
198   }
199
200   /**
201    * Tests the locking in the delete() method.
202    *
203    * @covers ::delete
204    * @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.
205    */
206   public function testDeleteLocking() {
207     $this->keyValue->expects($this->once())
208       ->method('get')
209       ->with('1:test')
210       ->will($this->returnValue($this->ownObject));
211     $this->lock->expects($this->once())
212       ->method('acquire')
213       ->with('1:test')
214       ->will($this->returnValue(TRUE));
215     $this->lock->expects($this->never())
216       ->method('wait');
217     $this->lock->expects($this->once())
218       ->method('release')
219       ->with('1:test');
220
221     $this->keyValue->expects($this->once())
222       ->method('delete')
223       ->with('1:test');
224
225     $this->assertTrue($this->tempStore->delete('test'));
226   }
227
228   /**
229    * Tests the delete() method with no lock available.
230    *
231    * @covers ::delete
232    * @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.
233    */
234   public function testDeleteWithNoLockAvailable() {
235     $this->keyValue->expects($this->once())
236       ->method('get')
237       ->with('1:test')
238       ->will($this->returnValue($this->ownObject));
239     $this->lock->expects($this->at(0))
240       ->method('acquire')
241       ->with('1:test')
242       ->will($this->returnValue(FALSE));
243     $this->lock->expects($this->at(1))
244       ->method('wait')
245       ->with('1:test');
246     $this->lock->expects($this->at(2))
247       ->method('acquire')
248       ->with('1:test')
249       ->will($this->returnValue(FALSE));
250
251     $this->keyValue->expects($this->once())
252       ->method('getCollectionName');
253
254     $this->setExpectedException(TempStoreException::class);
255     $this->tempStore->delete('test');
256   }
257
258   /**
259    * Tests the delete() method.
260    *
261    * @covers ::delete
262    * @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.
263    */
264   public function testDelete() {
265     $this->lock->expects($this->once())
266       ->method('acquire')
267       ->with('1:test_2')
268       ->will($this->returnValue(TRUE));
269
270     $this->keyValue->expects($this->at(0))
271       ->method('get')
272       ->with('1:test_1')
273       ->will($this->returnValue(FALSE));
274     $this->keyValue->expects($this->at(1))
275       ->method('get')
276       ->with('1:test_2')
277       ->will($this->returnValue($this->ownObject));
278     $this->keyValue->expects($this->at(2))
279       ->method('delete')
280       ->with('1:test_2');
281     $this->keyValue->expects($this->at(3))
282       ->method('get')
283       ->with('1:test_3')
284       ->will($this->returnValue($this->otherObject));
285
286     $this->assertTrue($this->tempStore->delete('test_1'));
287     $this->assertTrue($this->tempStore->delete('test_2'));
288     $this->assertFalse($this->tempStore->delete('test_3'));
289   }
290
291 }