class HelpTopicTwigLoaderTest

Same name in other branches
  1. 9 core/modules/help_topics/tests/src/Unit/HelpTopicTwigLoaderTest.php \Drupal\Tests\help_topics\Unit\HelpTopicTwigLoaderTest
  2. 8.9.x core/modules/help_topics/tests/src/Unit/HelpTopicTwigLoaderTest.php \Drupal\Tests\help_topics\Unit\HelpTopicTwigLoaderTest
  3. 11.x core/modules/help/tests/src/Unit/HelpTopicTwigLoaderTest.php \Drupal\Tests\help\Unit\HelpTopicTwigLoaderTest

Unit test for the HelpTopicTwigLoader class.

@coversDefaultClass \Drupal\help\HelpTopicTwigLoader @group help

Hierarchy

Expanded class hierarchy of HelpTopicTwigLoaderTest

File

core/modules/help/tests/src/Unit/HelpTopicTwigLoaderTest.php, line 20

Namespace

Drupal\Tests\help\Unit
View source
class HelpTopicTwigLoaderTest extends UnitTestCase {
    
    /**
     * The help topic loader instance to test.
     *
     * @var \Drupal\help\HelpTopicTwigLoader
     */
    protected $helpLoader;
    
    /**
     * The virtual directories to use in testing.
     *
     * @var array
     */
    protected $directories;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->setUpVfs();
        $module_handler = $this->createMock(ModuleHandlerInterface::class);
        $module_handler->method('getModuleDirectories')
            ->willReturn($this->directories['module']);
        
        /** @var \Drupal\Core\Extension\ThemeHandlerInterface|\Prophecy\Prophecy\ObjectProphecy $module_handler */
        $theme_handler = $this->createMock(ThemeHandlerInterface::class);
        $theme_handler->method('getThemeDirectories')
            ->willReturn($this->directories['theme']);
        $this->helpLoader = new HelpTopicTwigLoader('\\fake\\root\\path', $module_handler, $theme_handler);
    }
    
    /**
     * @covers ::__construct
     */
    public function testConstructor() : void {
        // Verify that the module/theme directories were added in the constructor,
        // and non-existent directories were omitted.
        $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);
    }
    
    /**
     * @covers ::getSourceContext
     */
    public function testGetSourceContext() : void {
        $source = $this->helpLoader
            ->getSourceContext('@' . HelpTopicTwigLoader::MAIN_NAMESPACE . '/test.topic.html.twig');
        $this->assertEquals('{% line 4 %}<h2>Test</h2>', $source->getCode());
    }
    
    /**
     * @covers ::getSourceContext
     */
    public function testGetSourceContextException() : void {
        $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');
    }
    
    /**
     * Sets up the virtual file system.
     */
    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'),
            ],
        ];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
HelpTopicTwigLoaderTest::$directories protected property The virtual directories to use in testing.
HelpTopicTwigLoaderTest::$helpLoader protected property The help topic loader instance to test.
HelpTopicTwigLoaderTest::setUp protected function Overrides UnitTestCase::setUp
HelpTopicTwigLoaderTest::setUpVfs protected function Sets up the virtual file system.
HelpTopicTwigLoaderTest::testConstructor public function @covers ::__construct
HelpTopicTwigLoaderTest::testGetSourceContext public function @covers ::getSourceContext
HelpTopicTwigLoaderTest::testGetSourceContextException public function @covers ::getSourceContext
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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