View source
<?php
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use Drupal\Core\Site\Settings;
use Drupal\DrupalExtension\Context\RawDrupalContext;
class VarnishPurgeFeatureContext extends RawDrupalContext implements SnippetAcceptingContext {
public static function prepare(BeforeSuiteScope $scope) {
$data = Settings::getAll();
$data['extension_discovery_scan_tests'] = TRUE;
$data['reverse_proxy_addresses'] = [
'127.0.0.1',
];
$data['varnish_purge_zeroconfig_port'] = 8080;
new Settings($data);
$moduleHandler = \Drupal::service('module_handler');
if (!$moduleHandler
->moduleExists('varnish_purger')) {
\Drupal::service('module_installer')
->install([
'varnish_purger',
'varnish_purge_tags',
'purge_processor_test',
]);
}
if ($moduleHandler
->moduleExists('inline_form_errors')) {
\Drupal::service('module_installer')
->uninstall([
'inline_form_errors',
]);
}
\Drupal::service('config.factory')
->getEditable('system.performance')
->set('cache', [
'page' => [
'max_age' => 365 * 24 * 60 * 60,
],
])
->save();
\Drupal::service('config.factory')
->getEditable('purge.plugins')
->set('purgers', [
[
'order_index' => 1,
'instance_id' => '340fedee82',
'plugin_id' => 'varnish_zeroconfig_purger',
],
])
->save();
}
public static function tearDown(\Behat\Testwork\Hook\Scope\AfterTestScope $scope) {
$nodes = node_load_multiple();
foreach ($nodes as $node) {
$node
->delete();
}
self::purgeEverything();
}
private static function purgeEverything() {
$p = \Drupal::service('purge.purgers');
$a = \Drupal::service('purge.processors')
->get('a');
$invalidations = [
\Drupal::service('purge.invalidation.factory')
->get('everything', NULL),
];
$p
->invalidate($a, $invalidations);
}
public function iPurgeNodes() {
$this
->purgeTags([
'node_list',
]);
}
public function iPurgeNodesAndMedia() {
$this
->purgeTags([
'user_list',
'media_list',
'node_list',
]);
}
public function iPurgeEverything() {
self::purgeEverything();
}
public function iPurgeTheHomepage() {
$url = \Drupal::request()
->getSchemeAndHttpHost() . '/';
$this
->purgeUrl($url);
}
public function iPurgeTheNodeList() {
$url = \Drupal::request()
->getSchemeAndHttpHost() . '/';
static::purgeWildcardUrl($url . 'no.*');
}
private static function purgeTags(array $tags) {
$p = \Drupal::service('purge.purgers');
$a = \Drupal::service('purge.processors')
->get('a');
$invalidations = [];
foreach ($tags as $tag) {
$invalidations[] = \Drupal::service('purge.invalidation.factory')
->get('tag', $tag);
}
$p
->invalidate($a, $invalidations);
}
private static function purgeWildcardUrl(string $url) {
$p = \Drupal::service('purge.purgers');
$a = \Drupal::service('purge.processors')
->get('a');
$invalidations = [
\Drupal::service('purge.invalidation.factory')
->get('wildcardurl', $url),
];
$p
->invalidate($a, $invalidations);
}
private static function purgeUrl(string $url) {
$p = \Drupal::service('purge.purgers');
$a = \Drupal::service('purge.processors')
->get('a');
$invalidations = [
\Drupal::service('purge.invalidation.factory')
->get('url', $url),
];
$p
->invalidate($a, $invalidations);
}
}