function IconPackManagerTest::testIconPackLibraryName

Test the library names.

Attributes

#[DataProvider('providerIconPackLibraryName')]

Parameters

string $name: The icon data.

bool $is_valid: The result expected is valid or not.

File

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

Class

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

Namespace

Drupal\Tests\Core\Theme\Icon

Code

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);
    }
  }
}

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