HelpTopicTwigLoaderTest.php in Drupal 10
File
core/modules/help_topics/tests/src/Unit/HelpTopicTwigLoaderTest.php
View source
<?php
namespace Drupal\Tests\help_topics\Unit;
use Drupal\help_topics\HelpTopicTwigLoader;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Twig\Error\LoaderError;
class HelpTopicTwigLoaderTest extends UnitTestCase {
protected $helpLoader;
protected $directories;
protected function setUp() : void {
$this
->setUpVfs();
$this->helpLoader = new HelpTopicTwigLoader('\\fake\\root\\path', $this
->getHandlerMock('module'), $this
->getHandlerMock('theme'));
}
public function testConstructor() {
$paths = $this->helpLoader
->getPaths(HelpTopicTwigLoader::MAIN_NAMESPACE);
$this
->assertCount(2, $paths);
$this
->assertContains($this->directories['module']['test'] . '/help_topics', $paths);
$this
->assertContains($this->directories['theme']['test'] . '/help_topics', $paths);
}
public function testGetSourceContext() {
$source = $this->helpLoader
->getSourceContext('@' . HelpTopicTwigLoader::MAIN_NAMESPACE . '/test.topic.html.twig');
$this
->assertEquals('{% line 4 %}<h2>Test</h2>', $source
->getCode());
}
public function testGetSourceContextException() {
$this
->expectException(LoaderError::class);
$this
->expectExceptionMessage("Malformed YAML in help topic \"vfs://root/modules/test/help_topics/test.invalid_yaml.html.twig\":");
$this->helpLoader
->getSourceContext('@' . HelpTopicTwigLoader::MAIN_NAMESPACE . '/test.invalid_yaml.html.twig');
}
protected function getHandlerMock($type) {
if ($type == 'module') {
$class = 'Drupal\\Core\\Extension\\ModuleHandlerInterface';
$method = 'getModuleDirectories';
}
else {
$class = 'Drupal\\Core\\Extension\\ThemeHandlerInterface';
$method = 'getThemeDirectories';
}
$handler = $this
->getMockBuilder($class)
->disableOriginalConstructor()
->getMock();
$handler
->method($method)
->willReturn($this->directories[$type]);
return $handler;
}
protected function setUpVfs() {
$content = <<<EOF
---
label: Test
---
<h2>Test</h2>
EOF;
$invalid_content = <<<EOF
---
foo : [bar}
---
<h2>Test</h2>
EOF;
$help_topics_dir = [
'help_topics' => [
'test.topic.html.twig' => $content,
'test.invalid_yaml.html.twig' => $invalid_content,
],
];
vfsStream::setup('root');
vfsStream::create([
'modules' => [
'test' => $help_topics_dir,
],
'themes' => [
'test' => $help_topics_dir,
],
]);
$this->directories = [
'root' => vfsStream::url('root'),
'module' => [
'test' => vfsStream::url('root/modules/test'),
'not_a_dir' => vfsStream::url('root/modules/not_a_dir'),
],
'theme' => [
'test' => vfsStream::url('root/themes/test'),
'not_a_dir' => vfsStream::url('root/themes/not_a_dir'),
],
];
}
}