You are here

public function HoneypotFormCacheTest::testCacheCommentForm in Honeypot 2.0.x

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

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

File

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

Class

HoneypotFormCacheTest
Tests page caching on Honeypot protected forms.

Namespace

Drupal\Tests\honeypot\Functional

Code

public function testCacheCommentForm() {

  // Set up example node.
  $this->node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'comment' => CommentItemInterface::OPEN,
  ]);

  // Give anonymous users permission to post comments.
  Role::load(RoleInterface::ANONYMOUS_ID)
    ->grantPermission('post comments')
    ->grantPermission('access comments')
    ->save();

  // Prime the cache.
  $this
    ->drupalGet('node/' . $this->node
    ->id());

  // Test on cache header with time limit enabled, cache should miss.
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $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('node/' . $this->node
    ->id());

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