You are here

class ComponentsLoaderTest in Components! 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Unit/ComponentsLoaderTest.php \Drupal\Tests\components\Unit\ComponentsLoaderTest

@coversDefaultClass \Drupal\components\Template\Loader\ComponentsLoader @group components

Hierarchy

Expanded class hierarchy of ComponentsLoaderTest

File

tests/src/Unit/ComponentsLoaderTest.php, line 12

Namespace

Drupal\Tests\components\Unit
View source
class ComponentsLoaderTest extends UnitTestCase {

  /**
   * The components info service.
   *
   * @var \Drupal\components\Template\ComponentsInfo|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $componentsInfo;

  /**
   * The theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $themeManager;

  /**
   * The system under test.
   *
   * @var \Drupal\components\Template\Loader\ComponentsLoader
   */
  protected $systemUnderTest;

  /**
   * {@inheritdoc}
   *
   * @throws \Twig\Error\LoaderError
   */
  public function setUp() {
    parent::setUp();

    // Set up components info service.
    $this->componentsInfo = $this
      ->createMock('\\Drupal\\components\\Template\\ComponentsInfo');
    $this->componentsInfo
      ->expects($this
      ->atLeastOnce())
      ->method('getAllThemeInfo')
      ->willReturn([
      'earth' => [
        'namespaces' => [
          'earth' => [
            '/earth/templates',
          ],
        ],
      ],
      'jupiter' => [
        'namespaces' => [
          'jupiter' => [
            '/jupiter/templates',
          ],
          'components' => [
            '/jupiter/components',
          ],
          'jupiter_extras' => [
            '/jupiter/extras',
          ],
        ],
      ],
      'luna' => [
        'namespaces' => [
          'luna' => [
            '/luna/templates',
          ],
          'components' => [
            '/luna/components',
          ],
          'luna_extras' => [
            '/luna/extras',
          ],
          'system' => [
            '/luna/system',
          ],
        ],
      ],
      'sol' => [
        'namespaces' => [
          'sol' => [
            '/sol/templates',
          ],
          'components' => [
            '/sol/components',
          ],
          'sol_extras' => [
            '/sol/extras',
          ],
        ],
      ],
    ]);
    $this->componentsInfo
      ->expects($this
      ->atLeastOnce())
      ->method('getAllModuleInfo')
      ->willReturn([
      'components' => [
        'namespaces' => [
          'components' => [
            '/components/components',
          ],
          'components_extras' => [
            '/components/extras',
          ],
          'system' => [
            '/components/system',
          ],
        ],
      ],
    ]);
    $this->componentsInfo
      ->expects($this
      ->atLeastOnce())
      ->method('isProtectedNamespace')
      ->will($this
      ->returnValueMap([
      [
        'system',
        TRUE,
      ],
      [
        'components',
        FALSE,
      ],
      [
        'components_extras',
        FALSE,
      ],
      [
        'earth',
        FALSE,
      ],
      [
        'jupiter',
        FALSE,
      ],
      [
        'jupiter_extras',
        FALSE,
      ],
      [
        'luna',
        FALSE,
      ],
      [
        'luna_extras',
        FALSE,
      ],
      [
        'sol',
        FALSE,
      ],
      [
        'sol_extras',
        FALSE,
      ],
    ]));
    $this->componentsInfo
      ->expects($this
      ->atLeastOnce())
      ->method('getProtectedNamespaceExtensionInfo')
      ->will($this
      ->returnValueMap([
      [
        'system',
        [
          'name' => 'System',
          'type' => 'module',
          'package' => 'Core',
        ],
      ],
    ]));

    // Set up theme manager data.
    $themes = [
      'sol' => $this
        ->createMock('\\Drupal\\Core\\Extension\\Extension'),
      'earth' => $this
        ->createMock('\\Drupal\\Core\\Extension\\Extension'),
      'luna' => $this
        ->createMock('\\Drupal\\Core\\Extension\\Extension'),
      'jupiter' => $this
        ->createMock('\\Drupal\\Core\\Extension\\Extension'),
    ];
    foreach (array_keys($themes) as $key) {
      $themes[$key]
        ->method('getName')
        ->willReturn($key);
    }
    $active_themes = [
      'luna' => $this
        ->createMock('\\Drupal\\Core\\Theme\\ActiveTheme'),
      'jupiter' => $this
        ->createMock('\\Drupal\\Core\\Theme\\ActiveTheme'),
    ];
    foreach (array_keys($active_themes) as $key) {
      $active_themes[$key]
        ->method('getName')
        ->willReturn($key);
    }
    $active_themes['luna']
      ->expects($this
      ->atLeastOnce())
      ->method('getBaseThemeExtensions')
      ->willReturn([
      $themes['earth'],
      $themes['sol'],
    ]);
    $active_themes['jupiter']
      ->method('getBaseThemeExtensions')
      ->willReturn([
      $themes['sol'],
    ]);
    $this->themeManager = $this
      ->createMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface');
    $this->themeManager
      ->expects($this
      ->atLeastOnce())
      ->method('getActiveTheme')
      ->willReturn($active_themes['luna'], $active_themes['jupiter'], $active_themes['luna']);
    $this->systemUnderTest = new ComponentsLoader($this->componentsInfo, $this->themeManager);
    $this->systemUnderTest
      ->checkActiveTheme();
  }

  /**
   * Tests checking the active theme.
   *
   * @covers ::checkActiveTheme
   *
   * @throws \Twig\Error\LoaderError
   */
  public function testCheckActiveTheme() {
    $result = $this->systemUnderTest
      ->checkActiveTheme();
    $this
      ->assertEquals('jupiter', $result);
    $result = $this->systemUnderTest
      ->checkActiveTheme();
    $this
      ->assertEquals('luna', $result);
  }

  /**
   * Tests prepending paths to a namespace.
   *
   * @covers ::setActiveTheme
   *
   * @throws \Twig\Error\LoaderError
   */
  public function testSetActiveTheme() {
    $namespaces = [
      'components' => [
        '/luna/components',
        '/sol/components',
        '/components/components',
      ],
      'components_extras' => [
        '/components/extras',
      ],
      'sol' => [
        '/sol/templates',
      ],
      'sol_extras' => [
        '/sol/extras',
      ],
      'earth' => [
        '/earth/templates',
      ],
      'luna' => [
        '/luna/templates',
      ],
      'luna_extras' => [
        '/luna/extras',
      ],
    ];
    $expected = array_keys($namespaces);
    $result = $this->systemUnderTest
      ->getNamespaces();
    $this
      ->assertEquals($expected, $result);
    foreach ($expected as $namespace) {
      $expected = $namespaces[$namespace];
      $result = $this->systemUnderTest
        ->getPaths($namespace);
      $this
        ->assertEquals($expected, $result);
    }

    // The mocked theme manager will swap the active theme.
    $this->systemUnderTest
      ->checkActiveTheme();
    $namespaces = [
      'components' => [
        '/jupiter/components',
        '/sol/components',
        '/components/components',
      ],
      'components_extras' => [
        '/components/extras',
      ],
      'sol' => [
        '/sol/templates',
      ],
      'sol_extras' => [
        '/sol/extras',
      ],
      'jupiter' => [
        '/jupiter/templates',
      ],
      'jupiter_extras' => [
        '/jupiter/extras',
      ],
    ];
    $expected = array_keys($namespaces);
    $result = $this->systemUnderTest
      ->getNamespaces();
    $this
      ->assertEquals($expected, $result);
    foreach ($expected as $namespace) {
      $expected = $namespaces[$namespace];
      $result = $this->systemUnderTest
        ->getPaths($namespace);
      $this
        ->assertEquals($expected, $result);
    }
  }

  /**
   * Tests the use of the active theme cache.
   *
   * @covers ::setActiveTheme
   *
   * @throws \Twig\Error\LoaderError
   */
  public function testSetActiveThemeCache() {

    // Add a path to the sol namespace.
    $expected = [
      '/sol/templates',
      '/test/templates',
    ];
    $this->systemUnderTest
      ->addPath('/test/templates', 'sol');
    $result = $this->systemUnderTest
      ->getPaths('sol');
    $this
      ->assertEquals($expected, $result);

    // The mocked theme manager will swap the active theme twice.
    $this->systemUnderTest
      ->checkActiveTheme();
    $this->systemUnderTest
      ->checkActiveTheme();

    // The cache doesn't have the path added earlier.
    $expected = [
      '/sol/templates',
    ];
    $result = $this->systemUnderTest
      ->getPaths('sol');
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Tests adding paths to a namespace.
   *
   * @param string $path
   *   Path to prepend.
   * @param string $namespace
   *   Namespace to alter.
   * @param array $expected
   *   Expected namespace paths.
   *
   * @covers ::addPath
   *
   * @throws \Twig\Error\LoaderError
   *
   * @dataProvider providerTestAddPath
   */
  public function testAddPath(string $path, string $namespace, array $expected) {
    $this->systemUnderTest
      ->addPath($path, $namespace);
    $result = $this->systemUnderTest
      ->getPaths($namespace);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testAddPath().
   *
   * @see testAddPath()
   */
  public function providerTestAddPath() : array {
    return [
      'basic add' => [
        'path' => '/test/templates',
        'namespace' => 'sol',
        'expected' => [
          '/sol/templates',
          '/test/templates',
        ],
      ],
      'Test appending on a namespace that does not exist' => [
        'path' => '/pluto/templates',
        'namespace' => 'pluto',
        'expected' => [
          '/pluto/templates',
        ],
      ],
      'Test trimming the trailing slash off of the path' => [
        'path' => '/test/trim/',
        'namespace' => 'sol',
        'expected' => [
          '/sol/templates',
          '/test/trim',
        ],
      ],
    ];
  }

  /**
   * Tests prepending paths to a namespace.
   *
   * @param string $path
   *   Path to prepend.
   * @param string $namespace
   *   Namespace to alter.
   * @param array $expected
   *   Expected namespace paths.
   *
   * @covers ::prependPath
   *
   * @throws \Twig\Error\LoaderError
   *
   * @dataProvider providerTestPrependPath
   */
  public function testPrependPath(string $path, string $namespace, array $expected) {
    $this->systemUnderTest
      ->prependPath($path, $namespace);
    $result = $this->systemUnderTest
      ->getPaths($namespace);
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Data provider for testPrependPath().
   *
   * @see testPrependPath()
   */
  public function providerTestPrependPath() : array {
    return [
      'basic prepend' => [
        'path' => '/test/templates',
        'namespace' => 'sol',
        'expected' => [
          '/test/templates',
          '/sol/templates',
        ],
      ],
      'Test prepending on a namespace that does not exist' => [
        'path' => '/pluto/templates',
        'namespace' => 'pluto',
        'expected' => [
          '/pluto/templates',
        ],
      ],
      'Test trimming the trailing slash off of the path' => [
        'path' => '/test/trim/',
        'namespace' => 'sol',
        'expected' => [
          '/test/trim',
          '/sol/templates',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ComponentsLoaderTest::$componentsInfo protected property The components info service.
ComponentsLoaderTest::$systemUnderTest protected property The system under test.
ComponentsLoaderTest::$themeManager protected property The theme manager.
ComponentsLoaderTest::providerTestAddPath public function Data provider for testAddPath().
ComponentsLoaderTest::providerTestPrependPath public function Data provider for testPrependPath().
ComponentsLoaderTest::setUp public function Overrides UnitTestCase::setUp
ComponentsLoaderTest::testAddPath public function Tests adding paths to a namespace.
ComponentsLoaderTest::testCheckActiveTheme public function Tests checking the active theme.
ComponentsLoaderTest::testPrependPath public function Tests prepending paths to a namespace.
ComponentsLoaderTest::testSetActiveTheme public function Tests prepending paths to a namespace.
ComponentsLoaderTest::testSetActiveThemeCache public function Tests the use of the active theme cache.
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.