View source
<?php
namespace Drupal\Tests\webform\Functional\Cache;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformCacheTest extends WebformBrowserTestBase {
public function testCache() {
$entity_form_builder = \Drupal::service('entity.form_builder');
$account = $this
->createUser();
$webform = Webform::load('contact');
$webform_submission = WebformSubmission::create([
'webform_id' => 'contact',
]);
$form = $entity_form_builder
->getForm($webform_submission, 'add');
$this
->assertEqual($form['#cache'], [
'contexts' => [
'session',
'user.roles:authenticated',
],
'tags' => [
'config:core.entity_form_display.webform_submission.contact.add',
'config:webform.settings',
'config:webform.webform.contact',
'webform:contact',
],
'max-age' => -1,
]);
$this
->assertFalse(isset($form['elements']['email']['#cache']));
$this
->assertEqual($form['elements']['email']['#default_value'], '');
$this
->drupalLogin($account);
$webform_submission
->setOwnerId($account);
\Drupal::currentUser()
->setAccount($account);
$webform_submission = WebformSubmission::create([
'webform_id' => 'contact',
]);
$form = $entity_form_builder
->getForm($webform_submission, 'add');
$this
->assertEqual($form['#cache'], [
'contexts' => [
'user',
'user.roles:authenticated',
],
'tags' => [
'config:core.entity_form_display.webform_submission.contact.add',
'config:webform.settings',
'config:webform.webform.contact',
'user:2',
'webform:contact',
],
'max-age' => -1,
]);
$this
->assertFalse(isset($form['elements']['email']['#cache']));
$this
->assertEqual($form['elements']['email']['#default_value'], $account
->getEmail());
$element = $webform
->getElementDecoded('email') + [
'#description' => '[current-user:mail]',
];
$webform
->setElementProperties('email', $element)
->save();
$form = $entity_form_builder
->getForm($webform_submission, 'add');
$this
->assertEqual($form['elements']['email']['#cache'], [
'contexts' => [
'user',
],
'tags' => [
'config:webform.settings',
'config:webform.webform.contact',
'user:2',
'webform:contact',
],
'max-age' => -1,
]);
$this
->assertEqual($form['elements']['email']['#default_value'], $account
->getEmail());
$this
->assertEqual($form['elements']['email']['#description']['#markup'], $account
->getEmail());
}
}