class FinishResponseSubscriberTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\FinishResponseSubscriberTest
- 10 core/tests/Drupal/Tests/Core/EventSubscriber/FinishResponseSubscriberTest.php \Drupal\Tests\Core\EventSubscriber\FinishResponseSubscriberTest
Tests Drupal\Core\EventSubscriber\FinishResponseSubscriber.
Attributes
#[CoversClass(FinishResponseSubscriber::class)]
#[Group('EventSubscriber')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\EventSubscriber\FinishResponseSubscriberTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of FinishResponseSubscriberTest
File
-
core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ FinishResponseSubscriberTest.php, line 26
Namespace
Drupal\Tests\Core\EventSubscriberView source
class FinishResponseSubscriberTest extends UnitTestCase {
/**
* The mock Kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $kernel;
/**
* The mock language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $languageManager;
/**
* The mock request policy.
*
* @var \Drupal\Core\PageCache\RequestPolicyInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $requestPolicy;
/**
* The mock response policy.
*
* @var \Drupal\Core\PageCache\ResponsePolicyInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $responsePolicy;
/**
* The mock cache contexts manager.
*
* @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit\Framework\MockObject\MockObject
*/
protected $cacheContextsManager;
/**
* The mock time service.
*
* @var \Drupal\Component\Datetime\TimeInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $time;
protected function setUp() : void {
parent::setUp();
$this->kernel = $this->createMock(HttpKernelInterface::class);
$this->languageManager = $this->createMock(LanguageManagerInterface::class);
$this->requestPolicy = $this->createMock(RequestPolicyInterface::class);
$this->responsePolicy = $this->createMock(ResponsePolicyInterface::class);
$this->cacheContextsManager = $this->createMock(CacheContextsManager::class);
$this->time = $this->createMock(TimeInterface::class);
}
/**
* Finish subscriber should set some default header values.
*
* @legacy-covers ::onRespond
*/
public function testDefaultHeaders() : void {
$finishSubscriber = new FinishResponseSubscriber($this->languageManager, $this->getConfigFactoryStub(), $this->requestPolicy, $this->responsePolicy, $this->cacheContextsManager, $this->time, FALSE);
$this->languageManager
->method('getCurrentLanguage')
->willReturn(new Language([
'id' => 'en',
]));
$request = $this->createMock(Request::class);
$response = $this->createMock(Response::class);
$response->headers = new ResponseHeaderBag();
$event = new ResponseEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
$finishSubscriber->onRespond($event);
$this->assertEquals([
'en',
], $response->headers
->all('Content-language'));
$this->assertEquals([
'nosniff',
], $response->headers
->all('X-Content-Type-Options'));
$this->assertEquals([
'SAMEORIGIN',
], $response->headers
->all('X-Frame-Options'));
}
/**
* Finish subscriber should not overwrite existing header values.
*
* @legacy-covers ::onRespond
*/
public function testExistingHeaders() : void {
$finishSubscriber = new FinishResponseSubscriber($this->languageManager, $this->getConfigFactoryStub(), $this->requestPolicy, $this->responsePolicy, $this->cacheContextsManager, $this->time, FALSE);
$this->languageManager
->method('getCurrentLanguage')
->willReturn(new Language([
'id' => 'en',
]));
$request = $this->createMock(Request::class);
$response = $this->createMock(Response::class);
$response->headers = new ResponseHeaderBag();
$event = new ResponseEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
$response->headers
->set('X-Content-Type-Options', 'foo');
$response->headers
->set('X-Frame-Options', 'DENY');
$finishSubscriber->onRespond($event);
$this->assertEquals([
'en',
], $response->headers
->all('Content-language'));
// 'X-Content-Type-Options' will be unconditionally set by core.
$this->assertEquals([
'nosniff',
], $response->headers
->all('X-Content-Type-Options'));
$this->assertEquals([
'DENY',
], $response->headers
->all('X-Frame-Options'));
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | |||
| FinishResponseSubscriberTest::$cacheContextsManager | protected | property | The mock cache contexts manager. | ||
| FinishResponseSubscriberTest::$kernel | protected | property | The mock Kernel. | ||
| FinishResponseSubscriberTest::$languageManager | protected | property | The mock language manager. | ||
| FinishResponseSubscriberTest::$requestPolicy | protected | property | The mock request policy. | ||
| FinishResponseSubscriberTest::$responsePolicy | protected | property | The mock response policy. | ||
| FinishResponseSubscriberTest::$time | protected | property | The mock time service. | ||
| FinishResponseSubscriberTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
| FinishResponseSubscriberTest::testDefaultHeaders | public | function | Finish subscriber should set some default header values. | ||
| FinishResponseSubscriberTest::testExistingHeaders | public | function | Finish subscriber should not overwrite existing header values. | ||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | ||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | ||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | ||
| UnitTestCase::$root | protected | property | The app root. | ||
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.