View source
<?php
namespace Drupal\Tests\honeypot\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\contact\Entity\ContactForm;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
class HoneypotFormCacheTest extends BrowserTestBase {
use CommentTestTrait;
protected $defaultTheme = 'stark';
protected $node;
public static $modules = [
'honeypot',
'node',
'comment',
'contact',
];
protected function setUp() {
parent::setUp();
$honeypot_config = \Drupal::configFactory()
->getEditable('honeypot.settings');
$honeypot_config
->set('element_name', 'url');
$honeypot_config
->set('time_limit', 5);
$honeypot_config
->set('protect_all_forms', TRUE);
$honeypot_config
->set('log', FALSE);
$honeypot_config
->save();
$user_config = \Drupal::configFactory()
->getEditable('user.settings');
$user_config
->set('verify_mail', TRUE);
$user_config
->set('register', UserInterface::REGISTER_VISITORS);
$user_config
->save();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->addDefaultCommentField('node', 'article');
}
}
public function testCacheContactForm() {
$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();
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('access site-wide contact form')
->save();
$this
->drupalGet('contact/feedback');
$this
->drupalGet('contact/feedback');
$this
->assertEquals('', $this
->drupalGetHeader('X-Drupal-Cache'), 'Page was not cached.');
\Drupal::configFactory()
->getEditable('honeypot.settings')
->set('time_limit', 0)
->save();
$this
->drupalGet('contact/feedback');
$this
->drupalGet('contact/feedback');
$this
->assertEquals('HIT', $this
->drupalGetHeader('X-Drupal-Cache'), 'Page was cached.');
\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.');
}
public function testCacheCommentForm() {
$this->node = $this
->drupalCreateNode([
'type' => 'article',
'comment' => CommentItemInterface::OPEN,
]);
Role::load(RoleInterface::ANONYMOUS_ID)
->grantPermission('post comments')
->grantPermission('access comments')
->save();
$this
->drupalGet('node/' . $this->node
->id());
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertEquals('', $this
->drupalGetHeader('X-Drupal-Cache'), 'Page was not cached.');
\Drupal::configFactory()
->getEditable('honeypot.settings')
->set('time_limit', 0)
->save();
$this
->drupalGet('node/' . $this->node
->id());
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertEquals('HIT', $this
->drupalGetHeader('X-Drupal-Cache'), 'Page was cached.');
}
}