class AjaxBasePageNegotiatorTest in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest
- 9 core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest
@coversDefaultClass \Drupal\Core\Theme\AjaxBasePageNegotiator @group Theme
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest
Expanded class hierarchy of AjaxBasePageNegotiatorTest
File
- core/
tests/ Drupal/ Tests/ Core/ Theme/ AjaxBasePageNegotiatorTest.php, line 18
Namespace
Drupal\Tests\Core\ThemeView source
class AjaxBasePageNegotiatorTest extends UnitTestCase {
/**
* @var \Drupal\Core\Theme\AjaxBasePageNegotiator
*
* The AJAX base page negotiator.
*/
protected $negotiator;
/**
* @var \Drupal\Core\Access\CsrfTokenGenerator|\Prophecy\Prophecy\ProphecyInterface
*
* The CSRF token generator.
*/
protected $tokenGenerator;
/**
* @var \Symfony\Component\HttpFoundation\RequestStack
*
* The request stack.
*/
protected $requestStack;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->tokenGenerator = $this
->prophesize(CsrfTokenGenerator::class);
$config_factory = $this
->getConfigFactoryStub([
'system.theme' => [
'default' => 'stark',
],
]);
$this->requestStack = new RequestStack();
$this->negotiator = new AjaxBasePageNegotiator($this->tokenGenerator
->reveal(), $config_factory, $this->requestStack);
}
/**
* @covers ::applies
* @dataProvider providerTestApplies
*/
public function testApplies($request_data, $expected) {
$request = new Request([], $request_data);
$request->request = new InputBag($request->request
->all());
$route_match = RouteMatch::createFromRequest($request);
$this->requestStack
->push($request);
$result = $this->negotiator
->applies($route_match);
$this
->assertSame($expected, $result);
}
public function providerTestApplies() {
$data = [];
$data['empty'] = [
[],
FALSE,
];
$data['no_theme'] = [
[
'ajax_page_state' => [
'theme' => '',
'theme_token' => '',
],
],
FALSE,
];
$data['valid_theme_empty_theme_token'] = [
[
'ajax_page_state' => [
'theme' => 'claro',
'theme_token' => '',
],
],
TRUE,
];
$data['valid_theme_valid_theme_token'] = [
[
'ajax_page_state' => [
'theme' => 'claro',
'theme_token' => 'valid_theme_token',
],
],
TRUE,
];
return $data;
}
/**
* @covers ::determineActiveTheme
*/
public function testDetermineActiveThemeValidToken() {
$theme = 'claro';
$theme_token = 'valid_theme_token';
$request = new Request([], [
'ajax_page_state' => [
'theme' => $theme,
'theme_token' => $theme_token,
],
]);
$request->request = new InputBag($request->request
->all());
$this->requestStack
->push($request);
$route_match = RouteMatch::createFromRequest($request);
$this->tokenGenerator
->validate($theme_token, $theme)
->willReturn(TRUE);
$result = $this->negotiator
->determineActiveTheme($route_match);
$this
->assertSame($theme, $result);
}
/**
* @covers ::determineActiveTheme
*/
public function testDetermineActiveThemeInvalidToken() {
$theme = 'claro';
$theme_token = 'invalid_theme_token';
$request = new Request([], [
'ajax_page_state' => [
'theme' => $theme,
'theme_token' => $theme_token,
],
]);
$request->request = new InputBag($request->request
->all());
$this->requestStack
->push($request);
$route_match = RouteMatch::createFromRequest($request);
$this->tokenGenerator
->validate($theme_token, $theme)
->willReturn(FALSE);
$result = $this->negotiator
->determineActiveTheme($route_match);
$this
->assertSame(NULL, $result);
}
/**
* @covers ::determineActiveTheme
*/
public function testDetermineActiveThemeDefaultTheme() {
$theme = 'stark';
// When the theme is the system default, an empty string is provided as the
// theme token. See system_js_settings_alter().
$theme_token = '';
$request = new Request([], [
'ajax_page_state' => [
'theme' => $theme,
'theme_token' => $theme_token,
],
]);
$request->request = new InputBag($request->request
->all());
$this->requestStack
->push($request);
$route_match = RouteMatch::createFromRequest($request);
$this->tokenGenerator
->validate(Argument::cetera())
->shouldNotBeCalled();
$result = $this->negotiator
->determineActiveTheme($route_match);
$this
->assertSame($theme, $result);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxBasePageNegotiatorTest:: |
protected | property | The AJAX base page negotiator. | |
AjaxBasePageNegotiatorTest:: |
protected | property | The request stack. | |
AjaxBasePageNegotiatorTest:: |
protected | property | The CSRF token generator. | |
AjaxBasePageNegotiatorTest:: |
public | function | ||
AjaxBasePageNegotiatorTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AjaxBasePageNegotiatorTest:: |
public | function | @covers ::applies @dataProvider providerTestApplies | |
AjaxBasePageNegotiatorTest:: |
public | function | @covers ::determineActiveTheme | |
AjaxBasePageNegotiatorTest:: |
public | function | @covers ::determineActiveTheme | |
AjaxBasePageNegotiatorTest:: |
public | function | @covers ::determineActiveTheme | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |