FeatureContext.php in Pantheon Advanced Page Cache 8
File
tests/behat/helper_classes/Contexts/FeatureContext.php
View source
<?php
namespace PantheonSystems\CDNBehatHelpers\Contexts;
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use PantheonSystems\CDNBehatHelpers\AgeTracker;
use Drupal\DrupalExtension\Context\RawDrupalContext;
final class FeatureContext extends RawDrupalContext implements Context {
public function __construct(array $parameters = []) {
}
private $minkContext;
private $ageTracker;
private $DrupalContext;
public function gatherContexts(BeforeScenarioScope $scope) {
$environment = $scope
->getEnvironment();
$this->minkContext = $environment
->getContext('Drupal\\DrupalExtension\\Context\\MinkContext');
$this->DrupalContext = $environment
->getContext('Drupal\\DrupalExtension\\Context\\DrupalContext');
}
public function whenIGenerateSomeNodes($type, $number_of_nodes = 2) {
$i = 0;
while ($i < $number_of_nodes) {
$this
->whenIGenerateANode($type);
$i++;
}
}
public function whenIGenerateANode($type) {
$random_node_title = "Random Node Title: " . rand();
$this->minkContext
->visit('node/add/' . $type);
$this->minkContext
->fillField('Title', $random_node_title);
$this->minkContext
->pressButton('Save');
$this->minkContext
->assertTextVisible($random_node_title);
sleep(2);
$this->minkContext
->visit('/');
}
public function pageIsCaching($page) {
$age = $this
->getAge($page);
if (!empty($age)) {
return true;
}
else {
sleep(2);
$age = $this
->getAge($page);
if (empty($age)) {
throw new \Exception('not cached');
}
else {
return true;
}
}
}
public function assertPathAgeIncreased($path) {
$age = $this
->getAge($path);
$ageTracker = $this
->getAgeTracker();
if (!$ageTracker
->ageIncreasedBetweenLastTwoRequests($path)) {
throw new \Exception('Cache age did not increase');
}
}
public function assertPathHasBeenPurged($path) {
$age = $this
->getAge($path);
$ageTracker = $this
->getAgeTracker();
if (!$ageTracker
->wasCacheClearedBetweenLastTwoRequests($path)) {
throw new \Exception('Cache was not cleared between requests');
}
}
protected function getAge($page) {
$this->minkContext
->visit($page);
$this
->getAgeTracker()
->trackSessionHeaders($page, $this->minkContext
->getSession());
$age = $this->minkContext
->getSession()
->getResponseHeader('Age');
return $age;
}
protected function getAgeTracker() {
if (empty($this->ageTracker)) {
$this->ageTracker = new AgeTracker();
}
return $this->ageTracker;
}
public function thereAreArticleNodesWithAHugeNumberOfTaxonomyTermsEach($number_of_nodes) {
$i = 0;
while ($i < $number_of_nodes) {
$this
->whenIGenerateAnArticleWithLotsOfTerms();
$i++;
}
}
public function whenIGenerateAnArticleWithLotsOfTerms() {
$random_node_title = "Random Node Title: " . rand();
$this->minkContext
->visit('node/add/article');
$this->minkContext
->fillField('Title', $random_node_title);
$this->minkContext
->fillField('Tags', $this
->generateRandomTaxonomyString());
$this->minkContext
->pressButton('Save');
$this->minkContext
->assertTextVisible($random_node_title);
}
private function generateRandomTaxonomyString() {
$letters = explode(' ', 'a b c d e f g h i j k l m n o p q r s t u v w x y z');
$i = 0;
$random_three_letter_combos = array();
while ($i < 250) {
$random_three_letter_combos[] = $letters[rand(0, 25)] . $letters[rand(0, 25)] . $letters[rand(0, 25)];
$i++;
}
return implode(",", $random_three_letter_combos);
}
}