class IconPackManagerTest

Tests Drupal\Core\Theme\Icon\Plugin\IconPackManager.

@group icon

Attributes

#[CoversClass(IconPackManager::class)] #[Group('icon')]

Hierarchy

Expanded class hierarchy of IconPackManagerTest

File

core/tests/Drupal/Tests/Core/Theme/Icon/IconPackManagerTest.php, line 24

Namespace

Drupal\Tests\Core\Theme\Icon
View source
class IconPackManagerTest extends UnitTestCase {
  
  /**
   * Data provider for ::testLibraryName().
   *
   * @return array
   *   Provide test data as:
   *   - (string) Library name to test
   *   - (bool) Flag for validity
   */
  public static function providerIconPackLibraryName() : array {
    return [
      'valid name with underscore' => [
        'my_theme/my_library',
        TRUE,
      ],
      'valid name with hyphen' => [
        'my_theme/my-library',
        TRUE,
      ],
      'valid name with hyphen and numbers' => [
        'my_theme/my-long-library_with_mixed-hyphens123',
        TRUE,
      ],
      'invalid theme name case 1' => [
        'my-theme/my-library',
        FALSE,
      ],
      'invalid theme name case 2' => [
        'my-theme/my_library',
        FALSE,
      ],
      'Invalid library name case 1' => [
        'my_theme/my library',
        FALSE,
      ],
      'Invalid library name special characters' => [
        'my-theme/$#random!',
        FALSE,
      ],
    ];
  }
  
  /**
   * Test the library names.
   *
   * @param string $name
   *   The icon data.
   * @param bool $is_valid
   *   The result expected is valid or not.
   */
  public function testIconPackLibraryName(string $name, bool $is_valid) : void {
    // Minimal required values.
    $definition = [
      'id' => 'foo',
      'provider' => 'icon_test',
      'extractor' => 'bar',
      'template' => '',
      'library' => $name,
    ];
    // Mock all dependencies for IconPackManager.
    $module_handler = $this->createMock(ModuleHandlerInterface::class);
    $theme_handler = $this->createMock(ThemeHandlerInterface::class);
    $cache_backend = $this->createMock(CacheBackendInterface::class);
    // Add the IconExtractorPluginManager mock here.
    $icon_extractor_manager = $this->createMock(IconExtractorPluginManager::class);
    $icon_collector = $this->createMock(IconCollector::class);
    // Create the IconPackManager instance and pass the manager with definition.
    $iconPackManager = new IconPackManager($module_handler, $theme_handler, $cache_backend, $icon_extractor_manager, $icon_collector, $this->root);
    $iconPackManager->setValidator();
    $reflection = new \ReflectionClass($iconPackManager);
    $method = $reflection->getMethod('validateDefinition');
    $method->setAccessible(TRUE);
    try {
      $result = $method->invoke($iconPackManager, $definition);
      if ($is_valid) {
        $this->assertTrue($result);
      }
    } catch (\Exception $e) {
      if (!$is_valid) {
        $this->assertSame('icon_test:foo Error in definition `foo`:[library] Does not match the regex pattern ^\\w+/[A-Za-z]+[\\w-]*$', $e->getMessage());
        $this->assertInstanceOf(IconPackConfigErrorException::class, $e);
      }
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
IconPackManagerTest::providerIconPackLibraryName public static function Data provider for ::testLibraryName().
IconPackManagerTest::testIconPackLibraryName public function Test the library names.
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.
UnitTestCase::$root protected property The app root.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 364
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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