You are here

class AjaxBasePageNegotiatorTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Theme/AjaxBasePageNegotiatorTest.php \Drupal\Tests\Core\Theme\AjaxBasePageNegotiatorTest
  2. 10 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 17

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}
   */
  public function setUp() {
    parent::setUp();
    $this->tokenGenerator = $this
      ->prophesize(CsrfTokenGenerator::class);
    $config_factory = $this
      ->getConfigFactoryStub([
      'system.theme' => [
        'default' => 'bartik',
      ],
    ]);
    $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);
    $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' => 'seven',
          'theme_token' => '',
        ],
      ],
      TRUE,
    ];
    $data['valid_theme_valid_theme_token'] = [
      [
        'ajax_page_state' => [
          'theme' => 'seven',
          'theme_token' => 'valid_theme_token',
        ],
      ],
      TRUE,
    ];
    return $data;
  }

  /**
   * @covers ::determineActiveTheme
   */
  public function testDetermineActiveThemeValidToken() {
    $theme = 'seven';
    $theme_token = 'valid_theme_token';
    $request = new Request([], [
      '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() {
    $theme = 'seven';
    $theme_token = 'invalid_theme_token';
    $request = new Request([], [
      '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(FALSE);
    $result = $this->negotiator
      ->determineActiveTheme($route_match);
    $this
      ->assertSame(NULL, $result);
  }

  /**
   * @covers ::determineActiveTheme
   */
  public function testDetermineActiveThemeDefaultTheme() {
    $theme = 'bartik';

    // 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,
      ],
    ]);
    $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

Namesort descending Modifiers Type Description 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 function
AjaxBasePageNegotiatorTest::setUp public 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
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.