You are here

class ThemeHandlerTest in Zircon Profile 8

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

@coversDefaultClass \Drupal\Core\Extension\ThemeHandler @group Extension

Hierarchy

Expanded class hierarchy of ThemeHandlerTest

File

core/tests/Drupal/Tests/Core/Extension/ThemeHandlerTest.php, line 21
Contains \Drupal\Tests\Core\Extension\ThemeHandlerTest.

Namespace

Drupal\Tests\Core\Extension
View source
class ThemeHandlerTest extends UnitTestCase {

  /**
   * The mocked info parser.
   *
   * @var \Drupal\Core\Extension\InfoParserInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $infoParser;

  /**
   * The mocked state backend.
   *
   * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $state;

  /**
   * The mocked config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * The mocked module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The extension discovery.
   *
   * @var \Drupal\Core\Extension\ExtensionDiscovery|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $extensionDiscovery;

  /**
   * The tested theme handler.
   *
   * @var \Drupal\Core\Extension\ThemeHandler|\Drupal\Tests\Core\Extension\StubThemeHandler
   */
  protected $themeHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->configFactory = $this
      ->getConfigFactoryStub(array(
      'core.extension' => array(
        'module' => array(),
        'theme' => array(),
        'disabled' => array(
          'theme' => array(),
        ),
      ),
    ));
    $this->moduleHandler = $this
      ->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->state = new State(new KeyValueMemoryFactory());
    $this->infoParser = $this
      ->getMock('Drupal\\Core\\Extension\\InfoParserInterface');
    $this->extensionDiscovery = $this
      ->getMockBuilder('Drupal\\Core\\Extension\\ExtensionDiscovery')
      ->disableOriginalConstructor()
      ->getMock();
    $this->themeHandler = new StubThemeHandler($this->root, $this->configFactory, $this->moduleHandler, $this->state, $this->infoParser, $this->extensionDiscovery);
    $cache_tags_invalidator = $this
      ->getMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
    $this
      ->getContainerWithCacheTagsInvalidator($cache_tags_invalidator);
  }

  /**
   * Tests rebuilding the theme data.
   *
   * @see \Drupal\Core\Extension\ThemeHandler::rebuildThemeData()
   */
  public function testRebuildThemeData() {
    $this->extensionDiscovery
      ->expects($this
      ->at(0))
      ->method('scan')
      ->with('theme')
      ->will($this
      ->returnValue(array(
      'seven' => new Extension($this->root, 'theme', $this->root . '/core/themes/seven/seven.info.yml', 'seven.theme'),
    )));
    $this->extensionDiscovery
      ->expects($this
      ->at(1))
      ->method('scan')
      ->with('theme_engine')
      ->will($this
      ->returnValue(array(
      'twig' => new Extension($this->root, 'theme_engine', $this->root . '/core/themes/engines/twig/twig.info.yml', 'twig.engine'),
    )));
    $this->infoParser
      ->expects($this
      ->once())
      ->method('parse')
      ->with($this->root . '/core/themes/seven/seven.info.yml')
      ->will($this
      ->returnCallback(function ($file) {
      $info_parser = new InfoParser();
      return $info_parser
        ->parse($file);
    }));
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('buildModuleDependencies')
      ->will($this
      ->returnArgument(0));
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('alter');
    $theme_data = $this->themeHandler
      ->rebuildThemeData();
    $this
      ->assertCount(1, $theme_data);
    $info = $theme_data['seven'];

    // Ensure some basic properties.
    $this
      ->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info);
    $this
      ->assertEquals('seven', $info
      ->getName());
    $this
      ->assertEquals($this->root . '/core/themes/seven/seven.info.yml', $info
      ->getPathname());
    $this
      ->assertEquals($this->root . '/core/themes/seven/seven.theme', $info
      ->getExtensionPathname());
    $this
      ->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info->owner);
    $this
      ->assertEquals('twig', $info->prefix);
    $this
      ->assertEquals('twig', $info->info['engine']);
    $this
      ->assertEquals(array(
      'seven/global-styling',
    ), $info->info['libraries']);
  }

  /**
   * Tests rebuild the theme data with theme parents.
   */
  public function testRebuildThemeDataWithThemeParents() {
    $this->extensionDiscovery
      ->expects($this
      ->at(0))
      ->method('scan')
      ->with('theme')
      ->will($this
      ->returnValue(array(
      'test_subtheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml', 'test_subtheme.info.yml'),
      'test_basetheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml', 'test_basetheme.info.yml'),
    )));
    $this->extensionDiscovery
      ->expects($this
      ->at(1))
      ->method('scan')
      ->with('theme_engine')
      ->will($this
      ->returnValue(array(
      'twig' => new Extension($this->root, 'theme_engine', $this->root . '/core/themes/engines/twig/twig.info.yml', 'twig.engine'),
    )));
    $this->infoParser
      ->expects($this
      ->at(0))
      ->method('parse')
      ->with($this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml')
      ->will($this
      ->returnCallback(function ($file) {
      $info_parser = new InfoParser();
      return $info_parser
        ->parse($file);
    }));
    $this->infoParser
      ->expects($this
      ->at(1))
      ->method('parse')
      ->with($this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml')
      ->will($this
      ->returnCallback(function ($file) {
      $info_parser = new InfoParser();
      return $info_parser
        ->parse($file);
    }));
    $this->moduleHandler
      ->expects($this
      ->once())
      ->method('buildModuleDependencies')
      ->will($this
      ->returnArgument(0));
    $theme_data = $this->themeHandler
      ->rebuildThemeData();
    $this
      ->assertCount(2, $theme_data);
    $info_basetheme = $theme_data['test_basetheme'];
    $info_subtheme = $theme_data['test_subtheme'];

    // Ensure some basic properties.
    $this
      ->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_basetheme);
    $this
      ->assertEquals('test_basetheme', $info_basetheme
      ->getName());
    $this
      ->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_subtheme);
    $this
      ->assertEquals('test_subtheme', $info_subtheme
      ->getName());

    // Test the parent/child-theme properties.
    $info_subtheme->info['base theme'] = 'test_basetheme';
    $info_basetheme->sub_themes = array(
      'test_subtheme',
    );
    $this
      ->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_basetheme->owner);
    $this
      ->assertEquals('twig', $info_basetheme->prefix);
    $this
      ->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_subtheme->owner);
    $this
      ->assertEquals('twig', $info_subtheme->prefix);
  }

  /**
   * Tests getting the base themes for a set a defines themes.
   *
   * @param array $themes
   *   An array of available themes, keyed by the theme name.
   * @param string $theme
   *   The theme name to find all its base themes.
   * @param array $expected
   *   The expected base themes.
   *
   * @dataProvider providerTestGetBaseThemes
   */
  public function testGetBaseThemes(array $themes, $theme, array $expected) {
    $base_themes = $this->themeHandler
      ->getBaseThemes($themes, $theme);
    $this
      ->assertEquals($expected, $base_themes);
  }

  /**
   * Provides test data for testGetBaseThemes.
   *
   * @return array
   *   An array of theme test data.
   */
  public function providerTestGetBaseThemes() {
    $data = array();

    // Tests a theme without any base theme.
    $themes = array();
    $themes['test_1'] = (object) array(
      'name' => 'test_1',
      'info' => array(
        'name' => 'test_1',
      ),
    );
    $data[] = array(
      $themes,
      'test_1',
      array(),
    );

    // Tests a theme with a non existing base theme.
    $themes = array();
    $themes['test_1'] = (object) array(
      'name' => 'test_1',
      'info' => array(
        'name' => 'test_1',
        'base theme' => 'test_2',
      ),
    );
    $data[] = array(
      $themes,
      'test_1',
      array(
        'test_2' => NULL,
      ),
    );

    // Tests a theme with a single existing base theme.
    $themes = array();
    $themes['test_1'] = (object) array(
      'name' => 'test_1',
      'info' => array(
        'name' => 'test_1',
        'base theme' => 'test_2',
      ),
    );
    $themes['test_2'] = (object) array(
      'name' => 'test_2',
      'info' => array(
        'name' => 'test_2',
      ),
    );
    $data[] = array(
      $themes,
      'test_1',
      array(
        'test_2' => 'test_2',
      ),
    );

    // Tests a theme with multiple base themes.
    $themes = array();
    $themes['test_1'] = (object) array(
      'name' => 'test_1',
      'info' => array(
        'name' => 'test_1',
        'base theme' => 'test_2',
      ),
    );
    $themes['test_2'] = (object) array(
      'name' => 'test_2',
      'info' => array(
        'name' => 'test_2',
        'base theme' => 'test_3',
      ),
    );
    $themes['test_3'] = (object) array(
      'name' => 'test_3',
      'info' => array(
        'name' => 'test_3',
      ),
    );
    $data[] = array(
      $themes,
      'test_1',
      array(
        'test_2' => 'test_2',
        'test_3' => 'test_3',
      ),
    );
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThemeHandlerTest::$configFactory protected property The mocked config factory.
ThemeHandlerTest::$extensionDiscovery protected property The extension discovery.
ThemeHandlerTest::$infoParser protected property The mocked info parser.
ThemeHandlerTest::$moduleHandler protected property The mocked module handler.
ThemeHandlerTest::$state protected property The mocked state backend.
ThemeHandlerTest::$themeHandler protected property The tested theme handler.
ThemeHandlerTest::providerTestGetBaseThemes public function Provides test data for testGetBaseThemes.
ThemeHandlerTest::setUp protected function Overrides UnitTestCase::setUp
ThemeHandlerTest::testGetBaseThemes public function Tests getting the base themes for a set a defines themes.
ThemeHandlerTest::testRebuildThemeData public function Tests rebuilding the theme data.
ThemeHandlerTest::testRebuildThemeDataWithThemeParents public function Tests rebuild the theme data with theme parents.
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.