Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Drupal 8.8 + add action debug chacheability headers #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Here is the list of the current available actions and how they help you:
| Display Dump Location | Display location when you use the `dump()` function of the Symfony VarDumper component |
| Display Pretty Exceptions | Display a better looking exception page and log exceptions (active when the `Request` is handled by the Kernel and if the exceptions are caught) |
| Display Pretty Exceptions ASAP | Display a better looking exception page (active as soon as the Kernel is instantiated) |
| Enable Cacheability header | Enable the [debug_cacheability_header](https://www.drupal.org/docs/8/api/responses/cacheableresponseinterface#debugging) |
| Enable Debug Class Loader | Enable the `DebugClassLoader` of the Symfony Debug component |
| Enable Twig Debug | Enable Twig Debug mode |
| ~~Enable Twig Strict Variables~~ | ~~Enable Twig `strict_variables` option~~ (disabled at the moment because the [Drupal core is not ready](https://www.drupal.org/project/drupal/issues/2445705) at all) |
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"require": {
"php": ">=7.1",
"composer-plugin-api": "^1.1",
"drupal/core": "~8.7.0",
"drupal/core": "~8.7 || ~8.8",
"monolog/monolog": "^1.3",
"nesbot/carbon": "^1.24",
"symfony/config": "^3.4 || >=4.0 <4.3",
Expand Down
42 changes: 42 additions & 0 deletions src/Action/AbstractOverrideBoolParameterAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ekino Drupal Debug project.
*
* (c) ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ekino\Drupal\Debug\Action;

use Ekino\Drupal\Debug\Exception\NotSupportedException;
use Symfony\Component\DependencyInjection\ContainerBuilder;

abstract class AbstractOverrideBoolParameterAction implements CompilerPassActionInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter(static::getParameterId())) {
throw new NotSupportedException(\sprintf('The "%s" parameter should already be set in the container builder.', static::getParameterId()));
}

$container->setParameter(static::getParameterId(), $this->getOverride());
}

/**
* @return string
*/
abstract protected static function getParameterId(): string;

/**
* @return bool
*/
abstract protected function getOverride(): bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ekino Drupal Debug project.
*
* (c) ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ekino\Drupal\Debug\Action\EnableDebugCacheabilityHeader;

use Ekino\Drupal\Debug\Action\AbstractOverrideBoolParameterAction;

class EnableDebugCacheabilityHeaderAction extends AbstractOverrideBoolParameterAction
{
/**
* {@inheritdoc}
*/
protected static function getParameterId(): string
{
return 'http.response.debug_cacheability_headers';
}

/**
* {@inheritdoc}
*/
protected function getOverride(): bool
{
return true;
}
}
6 changes: 6 additions & 0 deletions src/ActionMetadata/ActionMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptions\DisplayPrettyExceptionsOptions;
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptionsASAP\DisplayPrettyExceptionsASAPAction;
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptionsASAP\DisplayPrettyExceptionsASAPOptions;
use Ekino\Drupal\Debug\Action\EnableDebugCacheabilityHeader\EnableDebugCacheabilityHeaderAction;
use Ekino\Drupal\Debug\Action\EnableDebugClassLoader\EnableDebugClassLoaderAction;
use Ekino\Drupal\Debug\Action\EnableTwigDebug\EnableTwigDebugAction;
use Ekino\Drupal\Debug\Action\EnableTwigStrictVariables\EnableTwigStrictVariablesAction;
Expand Down Expand Up @@ -90,6 +91,11 @@ class ActionMetadataManager
DisplayPrettyExceptionsASAPOptions::class,
),
),
'enable_debug_cacheability_header' => array(
ActionMetadata::class,
EnableDebugCacheabilityHeaderAction::class,
array(),
),
'enable_debug_class_loader' => array(
ActionMetadata::class,
EnableDebugClassLoaderAction::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ekino Drupal Debug project.
*
* (c) ekino
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Ekino\Drupal\Debug\Tests\Integration\Action\EnableDebugCacheabilityHeader;

use Ekino\Drupal\Debug\Tests\Integration\Action\AbstractActionTestCase;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\BrowserKit\Response;

class EnableDebugCacheabilityHeaderActionTest extends AbstractActionTestCase
{
/**
* {@inheritdoc}
*/
protected function doTestInitialBehaviorWithDrupalKernel(Client $client): void
{
$headers = \array_keys($this->executeScenario($client));
$this->assertNotContains(array('X-Drupal-Cache-Tags', 'X-Drupal-Cache-Contexts'), $headers);
}

/**
* {@inheritdoc}
*/
protected function doTestTargetedBehaviorWithDebugKernel(Client $client): void
{
$headers = $this->executeScenario($client);
$this->assertArrayHasKey('X-Drupal-Cache-Tags', $headers);
$this->assertArrayHasKey('X-Drupal-Cache-Contexts', $headers);
}

private function executeScenario(Client $client): array
{
$client->request('GET', '/');

/** @var Response $response */
$response = $client->getResponse();

return $response->getHeaders();
}
}
4 changes: 3 additions & 1 deletion tests/Unit/src/Action/ActionRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Ekino\Drupal\Debug\Action\DisplayDumpLocation\DisplayDumpLocationAction;
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptions\DisplayPrettyExceptionsAction;
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptionsASAP\DisplayPrettyExceptionsASAPAction;
use Ekino\Drupal\Debug\Action\EnableDebugCacheabilityHeader\EnableDebugCacheabilityHeaderAction;
use Ekino\Drupal\Debug\Action\EnableDebugClassLoader\EnableDebugClassLoaderAction;
use Ekino\Drupal\Debug\Action\EnableTwigDebug\EnableTwigDebugAction;
use Ekino\Drupal\Debug\Action\EnableTwigStrictVariables\EnableTwigStrictVariablesAction;
Expand Down Expand Up @@ -122,11 +123,12 @@ public function testAddCompilerPassActionsToContainerBuilder(): void
{
$containerBuilder = $this->createMock(ContainerBuilder::class);
$containerBuilder
->expects($this->exactly(5))
->expects($this->exactly(6))
->method('addCompilerPass')
->withConsecutive(
array($this->isInstanceOf(DisableTwigCacheAction::class)),
array($this->isInstanceOf(DisplayPrettyExceptionsAction::class)),
array($this->isInstanceOf(EnableDebugCacheabilityHeaderAction::class)),
array($this->isInstanceOf(EnableTwigDebugAction::class)),
array($this->isInstanceOf(EnableTwigStrictVariablesAction::class)),
array($this->isInstanceOf(WatchModulesHooksImplementationsAction::class))
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/src/Composer/Helper/fixtures/drupal-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ drupal-debug:
enabled: true
charset: null
file_link_format: null
enable_debug_cacheability_header:
enabled: true
enable_debug_class_loader:
enabled: true
enable_twig_debug:
Expand Down