class SvgSpriteExtractorTest
@coversDefaultClass \Drupal\Core\Theme\Plugin\IconExtractor\SvgSpriteExtractor
@group icon
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Theme\Icon\Plugin\SvgSpriteExtractorTest extends \Drupal\Tests\UnitTestCase uses \Drupal\Tests\Core\Theme\Icon\IconTestTrait
Expanded class hierarchy of SvgSpriteExtractorTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Theme/ Icon/ Plugin/ SvgSpriteExtractorTest.php, line 19
Namespace
Drupal\Tests\Core\Theme\Icon\PluginView source
class SvgSpriteExtractorTest extends UnitTestCase {
use IconTestTrait;
/**
* This test plugin id (icon pack id).
*/
private string $pluginId = 'test_svg_sprite';
/**
* The SvgSpriteExtractor instance.
*
* @var \Drupal\Core\Theme\Plugin\IconExtractor\SvgSpriteExtractor
*/
private SvgSpriteExtractor $svgSpriteExtractorPlugin;
/**
* The IconFinder instance.
*
* @var \Drupal\Core\Theme\Icon\IconFinder|\PHPUnit\Framework\MockObject\MockObject
*/
private IconFinder $iconFinder;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->iconFinder = $this->createMock(IconFinder::class);
$this->svgSpriteExtractorPlugin = new SvgSpriteExtractor([
'id' => $this->pluginId,
'config' => [
'sources' => [
'foo/bar/{icon_id}.svg',
],
],
'template' => '_foo_',
'relative_path' => 'modules/my_module',
], $this->pluginId, [], $this->iconFinder);
}
/**
* Data provider for ::testDiscoverIconsSvgSprite().
*
* @return \Generator
* The test cases.
*/
public static function providerDiscoverIconsSvgSprite() : iterable {
(yield 'empty' => []);
(yield 'svg not sprite is ignored' => [
[
'foo' => [
'icon_id' => 'foo',
'source' => 'source/foo.svg',
'absolute_path' => '/path/source/foo.svg',
],
],
[
[
'/path/source/foo.svg',
'<svg xmlns="https://www.w3.org/2000/svg"><path d="M8 15a.5.5 0 0 0"/></svg>',
],
],
[],
]);
(yield 'svg sprite with one symbol' => [
[
'foo' => [
'icon_id' => 'foo',
'source' => 'source/foo.svg',
'absolute_path' => '/path/source/foo.svg',
'group' => NULL,
],
],
[
[
'/path/source/foo.svg',
'<svg><symbol id="bar"></symbol></svg>',
],
],
[
'bar',
],
]);
(yield 'single file with multiple symbol' => [
[
'foo' => [
'icon_id' => 'foo',
'source' => 'source/foo.svg',
'absolute_path' => '/path/source/foo.svg',
],
],
[
[
'/path/source/foo.svg',
'<svg><symbol id="foo"></symbol><symbol id="bar"></symbol></svg>',
],
],
[
'foo',
'bar',
],
]);
(yield 'single file with multiple symbol in defs' => [
[
'foo' => [
'icon_id' => 'foo',
'source' => 'source/foo.svg',
'absolute_path' => '/path/source/foo.svg',
],
],
[
[
'/path/source/foo.svg',
'<svg><defs><symbol id="foo"></symbol><symbol id="bar"></symbol></defs></svg>',
],
],
[
'foo',
'bar',
],
]);
(yield 'suspicious symbol id ignored' => [
[
'foo' => [
'icon_id' => 'foo',
'source' => 'source/foo.svg',
'absolute_path' => '/path/source/foo.svg',
],
],
[
[
'/path/source/foo.svg',
'<svg><symbol id="!script"></symbol><symbol id="not valid"></symbol><symbol id="_foo-bar_"></symbol></svg>',
],
],
[
'_foo-bar_',
],
]);
}
/**
* Test the SvgSpriteExtractor::discoverIcons() method.
*
* @param array<array<string, string>> $files
* The files to test from IconFinder::getFilesFromSources.
* @param array<int, array<int, mixed>> $contents_map
* The content returned by fileGetContents() based on absolute_path.
* @param array<string> $expected
* The icon ids expected.
*
* @dataProvider providerDiscoverIconsSvgSprite
*/
public function testDiscoverIconsSvgSprite(array $files = [], array $contents_map = [], array $expected = []) : void {
$this->iconFinder
->method('getFilesFromSources')
->willReturn($files);
$this->iconFinder
->method('getFileContents')
->willReturnMap($contents_map);
$result = $this->svgSpriteExtractorPlugin
->discoverIcons();
if (empty($expected)) {
$this->assertEmpty($result);
return;
}
// Basic data are not altered and can be compared directly.
$index = 0;
foreach ($result as $icon_id => $icon_data) {
$expected_id = $this->pluginId . IconDefinition::ICON_SEPARATOR . $expected[$index];
$this->assertSame($expected_id, $icon_id);
$index++;
}
}
/**
* Test the SvgSpriteExtractor::discoverIcons() method with invalid svg.
*/
public function testDiscoverIconsSvgSpriteInvalid() : void {
$icon = [
'icon_id' => 'foo',
'source' => '/path/source/foo.svg',
];
$this->iconFinder
->method('getFilesFromSources')
->willReturn($icon);
$this->iconFinder
->method('getFileContents')
->willReturn('Not valid svg');
$icons = $this->svgSpriteExtractorPlugin
->discoverIcons();
$this->assertEmpty($icons);
foreach (libxml_get_errors() as $error) {
$this->assertSame("Start tag expected, '<' not found", trim($error->message));
}
}
/**
* Test the SvgSpriteExtractor::discoverIcons() method with invalid content.
*/
public function testDiscoverIconsSvgSpriteInvalidContent() : void {
$icon = [
'icon_id' => 'foo',
'source' => '/path/source/foo.svg',
];
$this->iconFinder
->method('getFilesFromSources')
->willReturn($icon);
$this->iconFinder
->method('getFileContents')
->willReturn(FALSE);
$icons = $this->svgSpriteExtractorPlugin
->discoverIcons();
$this->assertEmpty($icons);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
IconTestTrait::createTestIcon | protected | function | Create an icon. | |
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. | |
SvgSpriteExtractorTest::$iconFinder | private | property | The IconFinder instance. | |
SvgSpriteExtractorTest::$pluginId | private | property | This test plugin id (icon pack id). | |
SvgSpriteExtractorTest::$svgSpriteExtractorPlugin | private | property | The SvgSpriteExtractor instance. | |
SvgSpriteExtractorTest::providerDiscoverIconsSvgSprite | public static | function | Data provider for ::testDiscoverIconsSvgSprite(). | |
SvgSpriteExtractorTest::setUp | public | function | Overrides UnitTestCase::setUp | |
SvgSpriteExtractorTest::testDiscoverIconsSvgSprite | public | function | Test the SvgSpriteExtractor::discoverIcons() method. | |
SvgSpriteExtractorTest::testDiscoverIconsSvgSpriteInvalid | public | function | Test the SvgSpriteExtractor::discoverIcons() method with invalid svg. | |
SvgSpriteExtractorTest::testDiscoverIconsSvgSpriteInvalidContent | public | function | Test the SvgSpriteExtractor::discoverIcons() method with invalid content. | |
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::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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.