Skip to content

Commit

Permalink
Merge pull request #71 from City-of-Helsinki/UHF-8570
Browse files Browse the repository at this point in the history
UHF-8570: Improve test coverage
  • Loading branch information
tuutti authored Mar 26, 2024
2 parents 20dda60 + 846b4b6 commit edc8440
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Cache/Context/SitePrefixCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ public static function getLabel() : string {
/**
* {@inheritdoc}
*/
public function getContext($prefix = NULL) : string {
public function getContext($parameter = NULL) : string {
$prefixes = $this->sitePrefix->getPrefixes();

if ($prefix === NULL) {
if ($parameter === NULL) {
return implode(',', $prefixes);
}

return isset($prefixes[$prefix]) ? '1' : '0';
return isset($prefixes[$parameter]) ? '1' : '0';
}

/**
* {@inheritdoc}
*/
public function getCacheableMetadata($prefix = NULL) : CacheableMetadata {
return (new CacheableMetadata())->setCacheTags(['site_prefix:' . $prefix]);
public function getCacheableMetadata($parameter = NULL) : CacheableMetadata {
return (new CacheableMetadata())->setCacheTags(['site_prefix:' . $parameter]);
}

}
61 changes: 61 additions & 0 deletions tests/src/Kernel/Controller/FrontControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_proxy\Kernel\Controller;

use Drupal\helfi_proxy\Controller\FrontController;
use Drupal\helfi_proxy\ProxyManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\helfi_api_base\Traits\ApiTestTrait;

/**
* Tests Front controller.
*
* @group helfi_proxy
*/
class FrontControllerTest extends KernelTestBase {

use ApiTestTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'path_alias',
'helfi_proxy',
];

/**
* Tests access.
*/
public function testControllerAccess() : void {
$request = $this->getMockedRequest('/front');
$response = $this->processRequest($request);
$this->assertEquals(200, $response->getStatusCode());
}

/**
* Tests build cache tags.
*/
public function testBuildCacheTags() : void {
$sut = FrontController::create($this->container);
$build = $sut->index();
$this->assertArrayHasKey('content', $build);
$this->assertEquals(['config:helfi_proxy.settings'], $build['content']['#cache']['tags']);
}

/**
* Tests page title.
*/
public function testTitle() : void {
$sut = FrontController::create($this->container);
$this->assertEquals('Front', $sut->title());

$this->config('helfi_proxy.settings')
->set(ProxyManagerInterface::FRONT_PAGE_TITLE, 'Title test')
->save();
$this->assertEquals('Title test', $sut->title());
}

}
44 changes: 44 additions & 0 deletions tests/src/Kernel/Plugin/DebugDataItem/RobotsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_proxy\Kernel\Plugin\DebugDataItem;

use Drupal\helfi_proxy\Plugin\DebugDataItem\Robots;
use Drupal\KernelTests\KernelTestBase;

/**
* Tests Robots debug data.
*
* @group helfi_proxy
*/
class RobotsTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'path_alias',
'helfi_proxy',
];

/**
* Tests plugin when robot header setting is not enabled.
*/
public function testDisabledHeader() : void {
$sut = Robots::create($this->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());
}

}
57 changes: 57 additions & 0 deletions tests/src/Kernel/SitePrefixCacheContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_proxy\Kernel;

use Drupal\helfi_proxy\Cache\Context\SitePrefixCacheContext;
use Drupal\KernelTests\KernelTestBase;

/**
* Tests Site prefix cache context.
*
* @group helfi_proxy
*/
class SitePrefixCacheContextTest extends KernelTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'path_alias',
'language',
'helfi_proxy',
];

/**
* Tests without configured proxy prefixes.
*/
public function testEmptyPrefix() : void {
$sut = new SitePrefixCacheContext($this->container->get('helfi_proxy.active_prefix'));
$this->assertEquals('', $sut->getContext(NULL));
$this->assertEquals('0', $sut->getContext('en'));
$this->assertEquals(['site_prefix:'], $sut->getCacheableMetadata(NULL)->getCacheTags());
$this->assertEquals(['site_prefix:en'], $sut->getCacheableMetadata('en')->getCacheTags());
}

/**
* Tests with configured proxy prefixes.
*/
public function testWithActivePrefix() : void {
$this->config('helfi_proxy.settings')
->set('prefixes', [
'sv' => 'sv',
'en' => 'en',
'fi' => 'fi',
])
->save();
$this->container->get('kernel')->rebuildContainer();

$sut = new SitePrefixCacheContext($this->container->get('helfi_proxy.active_prefix'));
$this->assertEquals('sv,en,fi', $sut->getContext(NULL));
$this->assertEquals('1', $sut->getContext('en'));
$this->assertEquals(['site_prefix:'], $sut->getCacheableMetadata(NULL)->getCacheTags());
$this->assertEquals(['site_prefix:en'], $sut->getCacheableMetadata('en')->getCacheTags());
}

}

0 comments on commit edc8440

Please sign in to comment.