diff --git a/config/schema/helfi_proxy.schema.yml b/config/schema/helfi_proxy.schema.yml index 76bde22..6ccf3f0 100644 --- a/config/schema/helfi_proxy.schema.yml +++ b/config/schema/helfi_proxy.schema.yml @@ -13,8 +13,6 @@ helfi_proxy.settings: type: string session_suffix: type: string - robots_header_enabled: - type: boolean prefixes: type: sequence sequence: diff --git a/helfi_proxy.install b/helfi_proxy.install index ce8bdc3..e4951fa 100644 --- a/helfi_proxy.install +++ b/helfi_proxy.install @@ -63,3 +63,12 @@ function helfi_proxy_update_9002() : void { $loggers->set('channels', $channels) ->save(); } + +/** + * Remove unused 'robots_header_enabled' config. + */ +function helfi_proxy_update_9003() : void { + \Drupal::configFactory()->getEditable('helfi_proxy.settings') + ->clear('robots_header_enabled') + ->save(); +} diff --git a/src/EventSubscriber/RobotsResponseSubscriber.php b/src/EventSubscriber/RobotsResponseSubscriber.php index 7a1c85b..95123f9 100644 --- a/src/EventSubscriber/RobotsResponseSubscriber.php +++ b/src/EventSubscriber/RobotsResponseSubscriber.php @@ -6,13 +6,11 @@ use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Path\PathMatcherInterface; use Drupal\helfi_proxy\ProxyManagerInterface; use Drupal\path_alias\AliasManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -21,15 +19,6 @@ */ final class RobotsResponseSubscriber implements EventSubscriberInterface { - public const X_ROBOTS_TAG_HEADER_NAME = 'DRUPAL_X_ROBOTS_TAG_HEADER'; - - /** - * The config. - * - * @var \Drupal\Core\Config\ImmutableConfig - */ - private ImmutableConfig $config; - /** * Constructs a new instance. * @@ -43,38 +32,11 @@ final class RobotsResponseSubscriber implements EventSubscriberInterface { * The path matcher. */ public function __construct( - ConfigFactoryInterface $configFactory, + private ConfigFactoryInterface $configFactory, private CurrentPathStack $pathStack, private AliasManagerInterface $aliasManager, private PathMatcherInterface $pathMatcher ) { - $this->config = $configFactory->get('helfi_proxy.settings'); - } - - /** - * Adds the robots response header. - * - * @param \Symfony\Component\HttpFoundation\Response $response - * The response. - */ - private function addRobotHeader(Response $response) : void { - $response->headers->add(['X-Robots-Tag' => 'noindex, nofollow']); - } - - /** - * Checks whether robots header should be present. - * - * @return bool - * TRUE if robots header is enabled for all pages. - */ - private function robotsHeaderEnabled() : bool { - $value = $this->config->get(ProxyManagerInterface::ROBOTS_HEADER_ENABLED); - - if ($value !== NULL) { - return (bool) $value; - } - // @todo Remove this in 3.0.0 release. - return (bool) getenv(self::X_ROBOTS_TAG_HEADER_NAME); } /** @@ -85,26 +47,20 @@ private function robotsHeaderEnabled() : bool { */ public function onResponse(ResponseEvent $event) : void { $response = $event->getResponse(); + $config = $this->configFactory->get('helfi_proxy.settings'); - if ($response instanceof CacheableResponseInterface) { - $response->addCacheableDependency($this->config); - } - - if ($this->robotsHeaderEnabled()) { - $this->addRobotHeader($response); - // No need to check individual paths if robots header should be - // added for every page. - return; - } - - if (!$paths = implode("\n", $this->config->get(ProxyManagerInterface::ROBOTS_PATHS) ?? [])) { + if (!$paths = implode("\n", $config->get(ProxyManagerInterface::ROBOTS_PATHS) ?? [])) { return; } $alias = $this->aliasManager ->getAliasByPath($this->pathStack->getPath($event->getRequest())); if ($this->pathMatcher->matchPath($alias, $paths)) { - $this->addRobotHeader($response); + $response->headers->add(['X-Robots-Tag' => 'noindex, nofollow']); + + if ($response instanceof CacheableResponseInterface) { + $response->addCacheableDependency($config); + } } } diff --git a/src/Plugin/DebugDataItem/Robots.php b/src/Plugin/DebugDataItem/Robots.php deleted file mode 100644 index 4203b90..0000000 --- a/src/Plugin/DebugDataItem/Robots.php +++ /dev/null @@ -1,50 +0,0 @@ -robotsHeader = (bool) $container->get('config.factory') - ->get('helfi_proxy.settings') - ->get('robots_header_enabled'); - - return $instance; - } - - /** - * {@inheritdoc} - */ - public function collect(): array { - $data['DRUPAL_X_ROBOTS_TAG_HEADER'] = $this->robotsHeader; - return $data; - } - -} diff --git a/src/ProxyManagerInterface.php b/src/ProxyManagerInterface.php index 76865b0..a1e5f3c 100644 --- a/src/ProxyManagerInterface.php +++ b/src/ProxyManagerInterface.php @@ -14,7 +14,6 @@ interface ProxyManagerInterface { public const ROBOTS_PATHS = 'robots_paths'; public const FRONT_PAGE_TITLE = 'front_page_title'; public const SESSION_SUFFIX = 'session_suffix'; - public const ROBOTS_HEADER_ENABLED = 'robots_header_enabled'; public const TUNNISTAMO_RETURN_URL = 'tunnistamo_return_url'; /** diff --git a/tests/src/Kernel/Plugin/DebugDataItem/RobotsTest.php b/tests/src/Kernel/Plugin/DebugDataItem/RobotsTest.php deleted file mode 100644 index e305cec..0000000 --- a/tests/src/Kernel/Plugin/DebugDataItem/RobotsTest.php +++ /dev/null @@ -1,44 +0,0 @@ -container, [], 'robots', []); - $this->assertEquals(['DRUPAL_X_ROBOTS_TAG_HEADER' => FALSE], $sut->collect()); - } - - /** - * Tests plugin when robot header setting is enabled. - */ - public function testHeader() : void { - $this->config('helfi_proxy.settings') - ->set('robots_header_enabled', TRUE) - ->save(); - $sut = Robots::create($this->container, [], 'robots', []); - $this->assertEquals(['DRUPAL_X_ROBOTS_TAG_HEADER' => TRUE], $sut->collect()); - } - -} diff --git a/tests/src/Kernel/RobotsResponseSubscriberTest.php b/tests/src/Kernel/RobotsResponseSubscriberTest.php index 734a52c..dcc5ddd 100644 --- a/tests/src/Kernel/RobotsResponseSubscriberTest.php +++ b/tests/src/Kernel/RobotsResponseSubscriberTest.php @@ -84,54 +84,6 @@ private function assertResponseEventNoHeader(ResponseEvent $event) : void { $this->assertNotContains('X-Robots-Tag', $event->getResponse()->headers); } - /** - * Tests that robots header is present when configuration is set. - */ - public function testConfig() : void { - $this->config('helfi_proxy.settings') - ->set(ProxyManagerInterface::ROBOTS_HEADER_ENABLED, FALSE) - ->save(); - // Make sure setting config to false does not enable the feature. - $event = $this->getResponseEvent(); - $this->getSut()->onResponse($event); - $this->assertResponseEventNoHeader($event); - - $this->config('helfi_proxy.settings') - ->set(ProxyManagerInterface::ROBOTS_HEADER_ENABLED, TRUE) - ->save(); - $event = $this->getResponseEvent(); - $this->getSut()->onResponse($event); - $this->assertResponseEventHasHeader($event); - } - - /** - * Tests that robots header is not present when no env variable is set. - */ - public function testNoEnvVariable() : void { - $event = $this->getResponseEvent(); - $this->getSut()->onResponse($event); - $this->assertResponseEventNoHeader($event); - - // Make sure setting null values to robots env variable does not - // enable the feature. - foreach ([0, NULL, ''] as $value) { - putenv(RobotsResponseSubscriber::X_ROBOTS_TAG_HEADER_NAME . '=' . $value); - $event = $this->getResponseEvent(); - $this->getSut()->onResponse($event); - $this->assertResponseEventNoHeader($event); - } - } - - /** - * Tests that robots header is added when env variable is present. - */ - public function testEnvVariable() : void { - putenv(RobotsResponseSubscriber::X_ROBOTS_TAG_HEADER_NAME . '=1'); - $event = $this->getResponseEvent(); - $this->getSut()->onResponse($event); - $this->assertResponseEventHasHeader($event); - } - /** * Tests robots path handler. */