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