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;
101 * Tests the get() method.
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.
106 public function testGet() {
107 $this->keyValue->expects($this->at(0))
110 ->will($this->returnValue(FALSE));
111 $this->keyValue->expects($this->at(1))
114 ->will($this->returnValue($this->ownObject));
115 $this->keyValue->expects($this->at(2))
118 ->will($this->returnValue($this->otherObject));
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'));
126 * Tests the set() method with no lock available.
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.
131 public function testSetWithNoLockAvailable() {
132 $this->lock->expects($this->at(0))
135 ->will($this->returnValue(FALSE));
136 $this->lock->expects($this->at(1))
139 $this->lock->expects($this->at(2))
142 ->will($this->returnValue(FALSE));
144 $this->keyValue->expects($this->once())
145 ->method('getCollectionName');
147 $this->setExpectedException(TempStoreException::class);
148 $this->tempStore->set('test', 'value');
152 * Tests a successful set() call.
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.
157 public function testSet() {
158 $this->lock->expects($this->once())
161 ->will($this->returnValue(TRUE));
162 $this->lock->expects($this->never())
164 $this->lock->expects($this->once())
168 $this->keyValue->expects($this->once())
169 ->method('setWithExpire')
170 ->with('1:test', $this->ownObject, 604800);
172 $this->tempStore->set('test', 'test_data');
176 * Tests the getMetadata() method.
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.
181 public function testGetMetadata() {
182 $this->keyValue->expects($this->at(0))
185 ->will($this->returnValue($this->ownObject));
187 $this->keyValue->expects($this->at(1))
190 ->will($this->returnValue(FALSE));
192 $metadata = $this->tempStore->getMetadata('test');
193 $this->assertObjectHasAttribute('owner', $metadata);
194 // Data should get removed.
195 $this->assertObjectNotHasAttribute('data', $metadata);
197 $this->assertNull($this->tempStore->getMetadata('test'));
201 * Tests the locking in the delete() method.
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.
206 public function testDeleteLocking() {
207 $this->keyValue->expects($this->once())
210 ->will($this->returnValue($this->ownObject));
211 $this->lock->expects($this->once())
214 ->will($this->returnValue(TRUE));
215 $this->lock->expects($this->never())
217 $this->lock->expects($this->once())
221 $this->keyValue->expects($this->once())
225 $this->assertTrue($this->tempStore->delete('test'));
229 * Tests the delete() method with no lock available.
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.
234 public function testDeleteWithNoLockAvailable() {
235 $this->keyValue->expects($this->once())
238 ->will($this->returnValue($this->ownObject));
239 $this->lock->expects($this->at(0))
242 ->will($this->returnValue(FALSE));
243 $this->lock->expects($this->at(1))
246 $this->lock->expects($this->at(2))
249 ->will($this->returnValue(FALSE));
251 $this->keyValue->expects($this->once())
252 ->method('getCollectionName');
254 $this->setExpectedException(TempStoreException::class);
255 $this->tempStore->delete('test');
259 * Tests the delete() method.
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.
264 public function testDelete() {
265 $this->lock->expects($this->once())
268 ->will($this->returnValue(TRUE));
270 $this->keyValue->expects($this->at(0))
273 ->will($this->returnValue(FALSE));
274 $this->keyValue->expects($this->at(1))
277 ->will($this->returnValue($this->ownObject));
278 $this->keyValue->expects($this->at(2))
281 $this->keyValue->expects($this->at(3))
284 ->will($this->returnValue($this->otherObject));
286 $this->assertTrue($this->tempStore->delete('test_1'));
287 $this->assertTrue($this->tempStore->delete('test_2'));
288 $this->assertFalse($this->tempStore->delete('test_3'));