You are here

public function HoneypotFormCacheTest::testCacheContactForm in Honeypot 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/HoneypotFormCacheTest.php \Drupal\Tests\honeypot\Functional\HoneypotFormCacheTest::testCacheContactForm()

Test enabling and disabling of page cache based on time limit settings.

File

tests/src/Functional/HoneypotFormCacheTest.php, line 76

Class

HoneypotFormCacheTest
Tests page caching on Honeypot protected forms.

Namespace

Drupal\Tests\honeypot\Functional

Code

public function testCacheContactForm() {

  // Create a Website feedback contact form.
  $feedback_form = ContactForm::create([
    'id' => 'feedback',
    'label' => 'Website feedback',
    'recipients' => [],
    'reply' => '',
    'weight' => 0,
  ]);
  $feedback_form
    ->save();
  $contact_settings = \Drupal::configFactory()
    ->getEditable('contact.settings');
  $contact_settings
    ->set('default_form', 'feedback')
    ->save();

  // Give anonymous users permission to view contact form.
  Role::load(RoleInterface::ANONYMOUS_ID)
    ->grantPermission('access site-wide contact form')
    ->save();

  // Prime the cache.
  $this
    ->drupalGet('contact/feedback');

  // Test on cache header with time limit enabled, cache should miss.
  $this
    ->drupalGet('contact/feedback');
  $this
    ->assertEquals('', $this
    ->drupalGetHeader('X-Drupal-Cache'), 'Page was not cached.');

  // Disable time limit.
  \Drupal::configFactory()
    ->getEditable('honeypot.settings')
    ->set('time_limit', 0)
    ->save();

  // Prime the cache.
  $this
    ->drupalGet('contact/feedback');

  // Test on cache header with time limit disabled, cache should hit.
  $this
    ->drupalGet('contact/feedback');
  $this
    ->assertEquals('HIT', $this
    ->drupalGetHeader('X-Drupal-Cache'), 'Page was cached.');

  // Re-enable the time limit, we should not be seeing the cached version.
  \Drupal::configFactory()
    ->getEditable('honeypot.settings')
    ->set('time_limit', 5)
    ->save();
  $this
    ->drupalGet('contact/feedback');
  $this
    ->assertEquals('', $this
    ->drupalGetHeader('X-Drupal-Cache'), 'Page was not cached.');
}