3 namespace Drupal\Tests\user\Unit;
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;
12 * @coversDefaultClass \Drupal\user\PrivateTempStore
15 * @runTestsInSeparateProcesses
16 * @preserveGlobalState disabled
18 class PrivateTempStoreTest extends UnitTestCase {
21 * The mock key value expirable backend.
23 * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
28 * The mock lock backend.
30 * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
35 * The user temp store.
37 * @var \Drupal\user\PrivateTempStore
44 * @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject
46 protected $currentUser;
51 * @var \Symfony\Component\HttpFoundation\RequestStack
53 protected $requestStack;
56 * A tempstore object belonging to the owner.
63 * A tempstore object not belonging to the owner.
67 protected $otherObject;
72 protected function setUp() {
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())
82 $this->requestStack = new RequestStack();
83 $request = Request::createFromGlobals();
84 $this->requestStack->push($request);
86 $this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
88 $this->ownObject = (object) [
89 'data' => 'test_data',
90 'owner' => $this->currentUser->id(),
91 'updated' => (int) $request->server->get('REQUEST_TIME'),
94 // Clone the object but change the owner.
95 $this->otherObject = clone $this->ownObject;
96 $this->otherObject->owner = 2;
100 * Tests the get() method.
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.
105 public function testGet() {
106 $this->keyValue->expects($this->at(0))
109 ->will($this->returnValue(FALSE));
110 $this->keyValue->expects($this->at(1))
113 ->will($this->returnValue($this->ownObject));
114 $this->keyValue->expects($this->at(2))
117 ->will($this->returnValue($this->otherObject));
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'));
125 * Tests the set() method with no lock available.
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.
130 public function testSetWithNoLockAvailable() {
131 $this->lock->expects($this->at(0))
134 ->will($this->returnValue(FALSE));
135 $this->lock->expects($this->at(1))
138 $this->lock->expects($this->at(2))
141 ->will($this->returnValue(FALSE));
143 $this->keyValue->expects($this->once())
144 ->method('getCollectionName');
146 $this->setExpectedException(TempStoreException::class);
147 $this->tempStore->set('test', 'value');
151 * Tests a successful set() call.
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.
156 public function testSet() {
157 $this->lock->expects($this->once())
160 ->will($this->returnValue(TRUE));
161 $this->lock->expects($this->never())
163 $this->lock->expects($this->once())
167 $this->keyValue->expects($this->once())
168 ->method('setWithExpire')
169 ->with('1:test', $this->ownObject, 604800);
171 $this->tempStore->set('test', 'test_data');
175 * Tests the getMetadata() method.
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.
180 public function testGetMetadata() {
181 $this->keyValue->expects($this->at(0))
184 ->will($this->returnValue($this->ownObject));
186 $this->keyValue->expects($this->at(1))
189 ->will($this->returnValue(FALSE));
191 $metadata = $this->tempStore->getMetadata('test');
192 $this->assertObjectHasAttribute('owner', $metadata);
193 // Data should get removed.
194 $this->assertObjectNotHasAttribute('data', $metadata);
196 $this->assertNull($this->tempStore->getMetadata('test'));
200 * Tests the locking in the delete() method.
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.
205 public function testDeleteLocking() {
206 $this->keyValue->expects($this->once())
209 ->will($this->returnValue($this->ownObject));
210 $this->lock->expects($this->once())
213 ->will($this->returnValue(TRUE));
214 $this->lock->expects($this->never())
216 $this->lock->expects($this->once())
220 $this->keyValue->expects($this->once())
224 $this->assertTrue($this->tempStore->delete('test'));
228 * Tests the delete() method with no lock available.
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.
233 public function testDeleteWithNoLockAvailable() {
234 $this->keyValue->expects($this->once())
237 ->will($this->returnValue($this->ownObject));
238 $this->lock->expects($this->at(0))
241 ->will($this->returnValue(FALSE));
242 $this->lock->expects($this->at(1))
245 $this->lock->expects($this->at(2))
248 ->will($this->returnValue(FALSE));
250 $this->keyValue->expects($this->once())
251 ->method('getCollectionName');
253 $this->setExpectedException(TempStoreException::class);
254 $this->tempStore->delete('test');
258 * Tests the delete() method.
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.
263 public function testDelete() {
264 $this->lock->expects($this->once())
267 ->will($this->returnValue(TRUE));
269 $this->keyValue->expects($this->at(0))
272 ->will($this->returnValue(FALSE));
273 $this->keyValue->expects($this->at(1))
276 ->will($this->returnValue($this->ownObject));
277 $this->keyValue->expects($this->at(2))
280 $this->keyValue->expects($this->at(3))
283 ->will($this->returnValue($this->otherObject));
285 $this->assertTrue($this->tempStore->delete('test_1'));
286 $this->assertTrue($this->tempStore->delete('test_2'));
287 $this->assertFalse($this->tempStore->delete('test_3'));