You are here

public function WebformCacheTest::testCache in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Functional/Cache/WebformCacheTest.php \Drupal\Tests\webform\Functional\Cache\WebformCacheTest::testCache()

Test cache.

File

tests/src/Functional/Cache/WebformCacheTest.php, line 19

Class

WebformCacheTest
Tests for #cache properties.

Namespace

Drupal\Tests\webform\Functional\Cache

Code

public function testCache() {

  /** @var \Drupal\Core\Entity\EntityFormBuilder $entity_form_builder */
  $entity_form_builder = \Drupal::service('entity.form_builder');
  $account = $this
    ->createUser();

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::load('contact');

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = WebformSubmission::create([
    'webform_id' => 'contact',
  ]);

  /**************************************************************************/
  $form = $entity_form_builder
    ->getForm($webform_submission, 'add');

  // Check that the form includes 'user.roles:authenticated' because the
  // '[current-user:mail]' token.
  $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,
  ]);

  // Check that the name element does not have #cache because the
  // '[current-user:mail]' is set via
  // \Drupal\webform\WebformSubmissionForm::setEntity
  $this
    ->assertFalse(isset($form['elements']['email']['#cache']));
  $this
    ->assertEqual($form['elements']['email']['#default_value'], '');

  // Login and check the #cache property.
  $this
    ->drupalLogin($account);
  $webform_submission
    ->setOwnerId($account);
  \Drupal::currentUser()
    ->setAccount($account);

  // Must create a new submission with new data which is set via
  // WebformSubmissionForm::setEntity.
  // @see \Drupal\webform\WebformSubmissionForm::setEntity
  $webform_submission = WebformSubmission::create([
    'webform_id' => 'contact',
  ]);
  $form = $entity_form_builder
    ->getForm($webform_submission, 'add');

  // Check that the form includes 'user.roles:authenticated' because the
  // '[current-user:mail]' token.
  $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());

  // Add the '[current-user:mail]' to the name elements' description.
  $element = $webform
    ->getElementDecoded('email') + [
    '#description' => '[current-user:mail]',
  ];
  $webform
    ->setElementProperties('email', $element)
    ->save();
  $form = $entity_form_builder
    ->getForm($webform_submission, 'add');

  // Check that the 'email' element does have '#cache' property because the
  // '#description' is using the '[current-user:mail]' token.
  $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());
}