class AjaxBasePageNegotiatorTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest
  3. 11.x core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest

@coversDefaultClass \Drupal\Core\Theme\AjaxBasePageNegotiator @group Theme

Hierarchy

Expanded class hierarchy of AjaxBasePageNegotiatorTest

File

core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php, line 20

Namespace

Drupal\Tests\Core\Theme
View 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) : void {
        $request = new Request();
        foreach ($request_data as $key => $data) {
            $request->query
                ->set($key, $data);
        }
        $route_match = RouteMatch::createFromRequest($request);
        $this->requestStack
            ->push($request);
        $result = $this->negotiator
            ->applies($route_match);
        $this->assertSame($expected, $result);
    }
    public static 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() : void {
        $theme = 'claro';
        $theme_token = 'valid_theme_token';
        $request = new Request();
        $request->query
            ->set('ajax_page_state', [
            'theme' => $theme,
            'theme_token' => $theme_token,
        ]);
        $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() : void {
        $theme = 'claro';
        $theme_token = 'invalid_theme_token';
        $request = new Request();
        $request->query
            ->set('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->assertNull($result);
    }
    
    /**
     * @covers ::determineActiveTheme
     */
    public function testDetermineActiveThemeDefaultTheme() : void {
        $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([]);
        $request->query
            ->set('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

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
AjaxBasePageNegotiatorTest::$negotiator protected property The AJAX base page negotiator.
AjaxBasePageNegotiatorTest::$requestStack protected property The request stack.
AjaxBasePageNegotiatorTest::$tokenGenerator protected property The CSRF token generator.
AjaxBasePageNegotiatorTest::providerTestApplies public static function
AjaxBasePageNegotiatorTest::setUp protected function Overrides UnitTestCase::setUp
AjaxBasePageNegotiatorTest::testApplies public function @covers ::applies
@dataProvider providerTestApplies
AjaxBasePageNegotiatorTest::testDetermineActiveThemeDefaultTheme public function @covers ::determineActiveTheme
AjaxBasePageNegotiatorTest::testDetermineActiveThemeInvalidToken public function @covers ::determineActiveTheme
AjaxBasePageNegotiatorTest::testDetermineActiveThemeValidToken public function @covers ::determineActiveTheme
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
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::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.