You are here

class ThemeNegotiatorTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php \Drupal\Tests\Core\Theme\ThemeNegotiatorTest

@coversDefaultClass \Drupal\Core\Theme\ThemeNegotiator @group Theme

Hierarchy

Expanded class hierarchy of ThemeNegotiatorTest

File

core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php, line 19
Contains \Drupal\Tests\Core\Theme\ThemeNegotiatorTest.

Namespace

Drupal\Tests\Core\Theme
View source
class ThemeNegotiatorTest extends UnitTestCase {

  /**
   * The mocked theme access checker.
   *
   * @var \Drupal\Core\Theme\ThemeAccessCheck|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $themeAccessCheck;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The actual tested theme negotiator.
   *
   * @var \Drupal\Core\Theme\ThemeNegotiator
   */
  protected $themeNegotiator;
  protected function setUp() {
    $this->themeAccessCheck = $this
      ->getMockBuilder('\\Drupal\\Core\\Theme\\ThemeAccessCheck')
      ->disableOriginalConstructor()
      ->getMock();
    $this->themeNegotiator = new ThemeNegotiator($this->themeAccessCheck);
  }

  /**
   * Tests determining the theme.
   *
   * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
   */
  public function testDetermineActiveTheme() {
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->once())
      ->method('determineActiveTheme')
      ->will($this
      ->returnValue('example_test'));
    $negotiator
      ->expects($this
      ->once())
      ->method('applies')
      ->will($this
      ->returnValue(TRUE));
    $this->themeNegotiator
      ->addNegotiator($negotiator, 0);
    $this->themeAccessCheck
      ->expects($this
      ->any())
      ->method('checkAccess')
      ->will($this
      ->returnValue(TRUE));
    $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array());
    $theme = $this->themeNegotiator
      ->determineActiveTheme($route_match);
    $this
      ->assertEquals('example_test', $theme);
  }

  /**
   * Tests determining with two negotiators checking the priority.
   *
   * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
   */
  public function testDetermineActiveThemeWithPriority() {
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->once())
      ->method('determineActiveTheme')
      ->will($this
      ->returnValue('example_test'));
    $negotiator
      ->expects($this
      ->once())
      ->method('applies')
      ->will($this
      ->returnValue(TRUE));
    $this->themeNegotiator
      ->addNegotiator($negotiator, 10);
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->never())
      ->method('determineActiveTheme');
    $negotiator
      ->expects($this
      ->never())
      ->method('applies');
    $this->themeNegotiator
      ->addNegotiator($negotiator, 0);
    $this->themeAccessCheck
      ->expects($this
      ->any())
      ->method('checkAccess')
      ->will($this
      ->returnValue(TRUE));
    $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array());
    $theme = $this->themeNegotiator
      ->determineActiveTheme($route_match);
    $this
      ->assertEquals('example_test', $theme);
  }

  /**
   * Tests determining with two negotiators of which just one returns access.
   *
   * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
   */
  public function testDetermineActiveThemeWithAccessCheck() {
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->once())
      ->method('determineActiveTheme')
      ->will($this
      ->returnValue('example_test'));
    $negotiator
      ->expects($this
      ->once())
      ->method('applies')
      ->will($this
      ->returnValue(TRUE));
    $this->themeNegotiator
      ->addNegotiator($negotiator, 10);
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->once())
      ->method('determineActiveTheme')
      ->will($this
      ->returnValue('example_test2'));
    $negotiator
      ->expects($this
      ->once())
      ->method('applies')
      ->will($this
      ->returnValue(TRUE));
    $this->themeNegotiator
      ->addNegotiator($negotiator, 0);
    $this->themeAccessCheck
      ->expects($this
      ->at(0))
      ->method('checkAccess')
      ->with('example_test')
      ->will($this
      ->returnValue(FALSE));
    $this->themeAccessCheck
      ->expects($this
      ->at(1))
      ->method('checkAccess')
      ->with('example_test2')
      ->will($this
      ->returnValue(TRUE));
    $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array());
    $theme = $this->themeNegotiator
      ->determineActiveTheme($route_match);
    $this
      ->assertEquals('example_test2', $theme);
  }

  /**
   * Tests determining with two negotiators of which one does not apply.
   *
   * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
   */
  public function testDetermineActiveThemeWithNotApplyingNegotiator() {
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->never())
      ->method('determineActiveTheme');
    $negotiator
      ->expects($this
      ->once())
      ->method('applies')
      ->will($this
      ->returnValue(FALSE));
    $this->themeNegotiator
      ->addNegotiator($negotiator, 10);
    $negotiator = $this
      ->getMock('Drupal\\Core\\Theme\\ThemeNegotiatorInterface');
    $negotiator
      ->expects($this
      ->once())
      ->method('determineActiveTheme')
      ->will($this
      ->returnValue('example_test2'));
    $negotiator
      ->expects($this
      ->once())
      ->method('applies')
      ->will($this
      ->returnValue(TRUE));
    $this->themeNegotiator
      ->addNegotiator($negotiator, 0);
    $this->themeAccessCheck
      ->expects($this
      ->any())
      ->method('checkAccess')
      ->will($this
      ->returnValue(TRUE));
    $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array());
    $theme = $this->themeNegotiator
      ->determineActiveTheme($route_match);
    $this
      ->assertEquals('example_test2', $theme);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThemeNegotiatorTest::$requestStack protected property The request stack.
ThemeNegotiatorTest::$themeAccessCheck protected property The mocked theme access checker.
ThemeNegotiatorTest::$themeNegotiator protected property The actual tested theme negotiator.
ThemeNegotiatorTest::setUp protected function Overrides UnitTestCase::setUp
ThemeNegotiatorTest::testDetermineActiveTheme public function Tests determining the theme.
ThemeNegotiatorTest::testDetermineActiveThemeWithAccessCheck public function Tests determining with two negotiators of which just one returns access.
ThemeNegotiatorTest::testDetermineActiveThemeWithNotApplyingNegotiator public function Tests determining with two negotiators of which one does not apply.
ThemeNegotiatorTest::testDetermineActiveThemeWithPriority public function Tests determining with two negotiators checking the priority.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.