Newer
Older
Angie Byron
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Form\FormCacheTest.
*/
namespace Drupal\Tests\Core\Form;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Form\FormCache;
use Drupal\Core\Form\FormState;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Form\FormCache
* @group Form
*/
class FormCacheTest extends UnitTestCase {
/**
* The form cache object under test.
*
* @var \Drupal\Core\Form\FormCache
*/
protected $formCache;
/**
* The expirable key value factory.
*
* @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $keyValueExpirableFactory;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account;
/**
* The CSRF token generator.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $csrfToken;
/**
* The mocked module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The expirable key value store used by form cache.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $formCacheStore;
/**
* The expirable key value store used by form state cache.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $formStateCacheStore;
/**
* The logger channel.
*
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $logger;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configFactory;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit_Framework_MockObject_MockObject
*/
protected $requestStack;
/**
* A policy rule determining the cacheability of a request.
*
* @var \Drupal\Core\PageCache\RequestPolicyInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $requestPolicy;
Angie Byron
committed
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
catch
committed
$this->resetSafeMarkup();
Angie Byron
committed
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->formCacheStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
$this->formStateCacheStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
$this->keyValueExpirableFactory = $this->getMock('Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface');
$this->keyValueExpirableFactory->expects($this->any())
->method('get')
->will($this->returnValueMap([
['form', $this->formCacheStore],
['form_state', $this->formStateCacheStore],
]));
$this->csrfToken = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator')
->disableOriginalConstructor()
->getMock();
$this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
$this->logger = $this->getMock('Psr\Log\LoggerInterface');
$this->configFactory = $this->getConfigFactoryStub(['system.performance' => ['cache.page.use_internal' => FALSE]]);
$this->requestStack = $this->getMock('\Symfony\Component\HttpFoundation\RequestStack');
$this->requestPolicy = $this->getMock('\Drupal\Core\PageCache\RequestPolicyInterface');
$this->formCache = new FormCache($this->keyValueExpirableFactory, $this->moduleHandler, $this->account, $this->csrfToken, $this->logger, $this->configFactory, $this->requestStack, $this->requestPolicy);
Angie Byron
committed
}
catch
committed
/**
* {@inheritdoc}
*/
protected function tearDown() {
parent::tearDown();
$this->resetSafeMarkup();
}
Angie Byron
committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @covers ::getCache
*/
public function testGetCacheValidToken() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cache_token = 'the_cache_token';
$cached_form = ['#cache_token' => $cache_token];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->csrfToken->expects($this->once())
->method('validate')
->with($cache_token)
->willReturn(TRUE);
$this->account->expects($this->never())
->method('isAnonymous');
$form = $this->formCache->getCache($form_build_id, $form_state);
$this->assertSame($cached_form, $form);
}
/**
* @covers ::getCache
*/
public function testGetCacheInvalidToken() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cache_token = 'the_cache_token';
$cached_form = ['#cache_token' => $cache_token];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->csrfToken->expects($this->once())
->method('validate')
->with($cache_token)
->willReturn(FALSE);
$this->account->expects($this->never())
->method('isAnonymous');
$form = $this->formCache->getCache($form_build_id, $form_state);
$this->assertNull($form);
}
/**
* @covers ::getCache
*/
public function testGetCacheAnonUser() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cached_form = ['#cache_token' => NULL];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->account->expects($this->once())
->method('isAnonymous')
->willReturn(TRUE);
$this->csrfToken->expects($this->never())
->method('validate');
$form = $this->formCache->getCache($form_build_id, $form_state);
$this->assertSame($cached_form, $form);
}
/**
* @covers ::getCache
*/
public function testGetCacheAuthUser() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cached_form = ['#cache_token' => NULL];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->account->expects($this->once())
->method('isAnonymous')
->willReturn(FALSE);
$form = $this->formCache->getCache($form_build_id, $form_state);
$this->assertNull($form);
}
/**
* @covers ::getCache
*/
public function testGetCacheNoForm() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cached_form = NULL;
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->account->expects($this->never())
->method('isAnonymous');
$form = $this->formCache->getCache($form_build_id, $form_state);
$this->assertNull($form);
}
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @covers ::getCache
*/
public function testGetCacheImmutableForm() {
$form_build_id = 'the_form_build_id';
$form_state = (new FormState())
->addBuildInfo('immutable', TRUE);
$cached_form = [
'#build_id' => 'the_old_build_form_id',
];
$this->account->expects($this->once())
->method('isAnonymous')
->willReturn(TRUE);
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$form = $this->formCache->getCache($form_build_id, $form_state);
$this->assertSame($cached_form['#build_id'], $form['#build_id_old']);
$this->assertNotSame($cached_form['#build_id'], $form['#build_id']);
$this->assertSame($form['#build_id'], $form['form_build_id']['#value']);
$this->assertSame($form['#build_id'], $form['form_build_id']['#id']);
}
Angie Byron
committed
/**
* @covers ::loadCachedFormState
*/
public function testLoadCachedFormState() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cached_form = ['#cache_token' => NULL];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->account->expects($this->once())
->method('isAnonymous')
->willReturn(TRUE);
$cached_form_state = ['storage' => ['foo' => 'bar']];
$this->formStateCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form_state);
$this->formCache->getCache($form_build_id, $form_state);
$this->assertSame($cached_form_state['storage'], $form_state->getStorage());
Angie Byron
committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
}
/**
* @covers ::loadCachedFormState
*/
public function testLoadCachedFormStateWithFiles() {
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cached_form = ['#cache_token' => NULL];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->account->expects($this->once())
->method('isAnonymous')
->willReturn(TRUE);
$cached_form_state = ['build_info' => ['files' => [
[
'module' => 'a_module',
'type' => 'the_type',
'name' => 'some_name',
],
[
'module' => 'another_module',
],
]]];
$this->moduleHandler->expects($this->at(0))
->method('loadInclude')
->with('a_module', 'the_type', 'some_name');
$this->moduleHandler->expects($this->at(1))
->method('loadInclude')
->with('another_module', 'inc', 'another_module');
$this->formStateCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form_state);
$this->formCache->getCache($form_build_id, $form_state);
}
/**
* @covers ::loadCachedFormState
*/
public function testLoadCachedFormStateWithSafeStrings() {
$this->assertEmpty(SafeMarkup::getAll());
$form_build_id = 'the_form_build_id';
$form_state = new FormState();
$cached_form = ['#cache_token' => NULL];
$this->formCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form);
$this->account->expects($this->once())
->method('isAnonymous')
->willReturn(TRUE);
$cached_form_state = ['build_info' => ['safe_strings' => [
'a_safe_string' => ['html' => TRUE],
]]];
$this->formStateCacheStore->expects($this->once())
->method('get')
->with($form_build_id)
->willReturn($cached_form_state);
$this->formCache->getCache($form_build_id, $form_state);
}
/**
* @covers ::setCache
*/
public function testSetCacheWithForm() {
$form_build_id = 'the_form_build_id';
$form = [
'#form_id' => 'the_form_id'
];
$form_state = new FormState();
$this->formCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form, $this->isType('int'));
$form_state_data = $form_state->getCacheableArray();
$form_state_data['build_info']['safe_strings'] = [];
$this->formStateCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form_state_data, $this->isType('int'));
$this->formCache->setCache($form_build_id, $form, $form_state);
}
/**
* @covers ::setCache
*/
public function testSetCacheWithoutForm() {
$form_build_id = 'the_form_build_id';
$form = NULL;
$form_state = new FormState();
$this->formCacheStore->expects($this->never())
->method('setWithExpire');
$form_state_data = $form_state->getCacheableArray();
$form_state_data['build_info']['safe_strings'] = [];
$this->formStateCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form_state_data, $this->isType('int'));
$this->formCache->setCache($form_build_id, $form, $form_state);
}
/**
* @covers ::setCache
*/
public function testSetCacheAuthUser() {
$form_build_id = 'the_form_build_id';
$form = [];
$form_state = new FormState();
$cache_token = 'the_cache_token';
$form_data = $form;
$form_data['#cache_token'] = $cache_token;
$this->formCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form_data, $this->isType('int'));
$form_state_data = $form_state->getCacheableArray();
$form_state_data['build_info']['safe_strings'] = [];
$this->formStateCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form_state_data, $this->isType('int'));
$this->csrfToken->expects($this->once())
->method('get')
->willReturn($cache_token);
$this->account->expects($this->once())
->method('isAuthenticated')
->willReturn(TRUE);
$this->formCache->setCache($form_build_id, $form, $form_state);
}
/**
* @covers ::setCache
*/
public function testSetCacheWithSafeStrings() {
SafeMarkup::set('a_safe_string');
$form_build_id = 'the_form_build_id';
$form = [
'#form_id' => 'the_form_id'
];
$form_state = new FormState();
$this->formCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form, $this->isType('int'));
$form_state_data = $form_state->getCacheableArray();
$form_state_data['build_info']['safe_strings'] = [
'a_safe_string' => ['html' => TRUE],
];
$this->formStateCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form_state_data, $this->isType('int'));
$this->formCache->setCache($form_build_id, $form, $form_state);
}
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/**
* @covers ::setCache
*/
public function testSetCacheBuildIdMismatch() {
$form_build_id = 'the_form_build_id';
$form = [
'#form_id' => 'the_form_id',
'#build_id' => 'stale_form_build_id',
];
$form_state = new FormState();
$this->formCacheStore->expects($this->never())
->method('setWithExpire');
$this->formStateCacheStore->expects($this->never())
->method('setWithExpire');
$this->logger->expects($this->once())
->method('error')
->with('Form build-id mismatch detected while attempting to store a form in the cache.');
$this->formCache->setCache($form_build_id, $form, $form_state);
}
/**
* @covers ::setCache
*/
public function testSetCacheImmutableForm() {
$form_build_id = 'the_form_build_id';
$form = [
'#form_id' => 'the_form_id',
];
$form_state = new FormState();
$this->formCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form, $this->isType('int'));
$form_state_data = $form_state->getCacheableArray();
$form_state_data['build_info']['safe_strings'] = [];
// Ensure that the form is marked immutable.
$form_state_data['build_info']['immutable'] = TRUE;
$this->formStateCacheStore->expects($this->once())
->method('setWithExpire')
->with($form_build_id, $form_state_data, $this->isType('int'));
// Rebuild the FormCache with a config factory that will return a config
// object with the internal page cache enabled.
$this->configFactory = $this->getConfigFactoryStub(['system.performance' => ['cache.page.use_internal' => TRUE]]);
$this->formCache = $this->getMockBuilder('Drupal\Core\Form\FormCache')
->setConstructorArgs([$this->keyValueExpirableFactory, $this->moduleHandler, $this->account, $this->csrfToken, $this->logger, $this->configFactory, $this->requestStack, $this->requestPolicy])
->setMethods(['isPageCacheable'])
->getMock();
$this->formCache->expects($this->once())
->method('isPageCacheable')
->willReturn(TRUE);
$this->formCache->setCache($form_build_id, $form, $form_state);
}
catch
committed
/**
* Ensures SafeMarkup does not bleed from one test to another.
*/
protected function resetSafeMarkup() {
$property = (new \ReflectionClass('Drupal\Component\Utility\SafeMarkup'))->getProperty('safeStrings');
$property->setAccessible(TRUE);
$property->setValue(array());
}