class HelpTopicDiscoveryTest

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

@coversDefaultClass \Drupal\help_topics\HelpTopicDiscovery @group help_topics

Hierarchy

Expanded class hierarchy of HelpTopicDiscoveryTest

File

core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php, line 17

Namespace

Drupal\Tests\help_topics\Unit
View source
class HelpTopicDiscoveryTest extends UnitTestCase {
    
    /**
     * @covers ::findAll
     */
    public function testDiscoveryExceptionProviderMismatch() {
        vfsStream::setup('root');
        vfsStream::create([
            'modules' => [
                'foo' => [
                    'help_topics' => [
                        // The content of the help topic does not matter.
'test.topic.html.twig' => '',
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'foo' => vfsStream::url('root/modules/foo/help_topics'),
        ]);
        $this->expectException(DiscoveryException::class);
        $this->expectExceptionMessage("vfs://root/modules/foo/help_topics/test.topic.html.twig file name should begin with 'foo'");
        $discovery->getDefinitions();
    }
    
    /**
     * @covers ::findAll
     */
    public function testDiscoveryExceptionMissingLabel() {
        vfsStream::setup('root');
        vfsStream::create([
            'modules' => [
                'test' => [
                    'help_topics' => [
                        // The content of the help topic does not matter.
'test.topic.html.twig' => '',
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'test' => vfsStream::url('root/modules/test/help_topics'),
        ]);
        $this->expectException(DiscoveryException::class);
        $this->expectExceptionMessage("vfs://root/modules/test/help_topics/test.topic.html.twig does not contain the required key with name='label'");
        $discovery->getDefinitions();
    }
    
    /**
     * @covers ::findAll
     */
    public function testDiscoveryExceptionInvalidYamlKey() {
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
label: 'A label'
foo: bar
---
EOF;
        vfsStream::create([
            'modules' => [
                'test' => [
                    'help_topics' => [
                        'test.topic.html.twig' => $topic_content,
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'test' => vfsStream::url('root/modules/test/help_topics'),
        ]);
        $this->expectException(DiscoveryException::class);
        $this->expectExceptionMessage("vfs://root/modules/test/help_topics/test.topic.html.twig contains invalid key='foo'");
        $discovery->getDefinitions();
    }
    
    /**
     * @covers ::findAll
     */
    public function testDiscoveryExceptionInvalidTopLevel() {
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
label: 'A label'
top_level: bar
---
EOF;
        vfsStream::create([
            'modules' => [
                'test' => [
                    'help_topics' => [
                        'test.topic.html.twig' => $topic_content,
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'test' => vfsStream::url('root/modules/test/help_topics'),
        ]);
        $this->expectException(DiscoveryException::class);
        $this->expectExceptionMessage("vfs://root/modules/test/help_topics/test.topic.html.twig contains invalid value for 'top_level' key, the value must be a Boolean");
        $discovery->getDefinitions();
    }
    
    /**
     * @covers ::findAll
     */
    public function testDiscoveryExceptionInvalidRelated() {
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
label: 'A label'
related: "one, two"
---
EOF;
        vfsStream::create([
            'modules' => [
                'test' => [
                    'help_topics' => [
                        'test.topic.html.twig' => $topic_content,
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'test' => vfsStream::url('root/modules/test/help_topics'),
        ]);
        $this->expectException(DiscoveryException::class);
        $this->expectExceptionMessage("vfs://root/modules/test/help_topics/test.topic.html.twig contains invalid value for 'related' key, the value must be an array of strings");
        $discovery->getDefinitions();
    }
    
    /**
     * @covers ::findAll
     */
    public function testHelpTopicsExtensionProviderSpecialCase() {
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
label: Test
---
<h2>Test</h2>
EOF;
        vfsStream::create([
            'modules' => [
                'help_topics' => [
                    'help_topics' => [
                        'core.topic.html.twig' => $topic_content,
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'help_topics' => vfsStream::url('root/modules/help_topics/help_topics'),
        ]);
        $this->assertArrayHasKey('core.topic', $discovery->getDefinitions());
    }
    
    /**
     * @covers ::findAll
     */
    public function testHelpTopicsInCore() {
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
label: Test
---
<h2>Test</h2>
EOF;
        vfsStream::create([
            'core' => [
                'help_topics' => [
                    'core.topic.html.twig' => $topic_content,
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'core' => vfsStream::url('root/core/help_topics'),
        ]);
        $this->assertArrayHasKey('core.topic', $discovery->getDefinitions());
    }
    
    /**
     * @covers ::findAll
     */
    public function testHelpTopicsBrokenYaml() {
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
foo : [bar}
---
<h2>Test</h2>
EOF;
        vfsStream::create([
            'modules' => [
                'help_topics' => [
                    'help_topics' => [
                        'core.topic.html.twig' => $topic_content,
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'help_topics' => vfsStream::url('root/modules/help_topics/help_topics'),
        ]);
        $this->expectException(DiscoveryException::class);
        $this->expectExceptionMessage("Malformed YAML in help topic \"vfs://root/modules/help_topics/help_topics/core.topic.html.twig\":");
        $discovery->getDefinitions();
    }
    
    /**
     * @covers ::findAll
     */
    public function testHelpTopicsDefinition() {
        $container = new ContainerBuilder();
        $container->set('string_translation', $this->getStringTranslationStub());
        \Drupal::setContainer($container);
        vfsStream::setup('root');
        $topic_content = <<<EOF
---
label: 'Test'
top_level: true
related:
  - one
  - two
  - three
---
<h2>Test</h2>
EOF;
        vfsStream::create([
            'modules' => [
                'foo' => [
                    'help_topics' => [
                        'foo.topic.html.twig' => $topic_content,
                    ],
                ],
            ],
        ]);
        $discovery = new HelpTopicDiscovery([
            'foo' => vfsStream::url('root/modules/foo/help_topics'),
        ]);
        $definition = $discovery->getDefinitions()['foo.topic'];
        $this->assertEquals('Test', $definition['label']);
        $this->assertInstanceOf(TranslatableMarkup::class, $definition['label']);
        $this->assertTrue($definition['top_level']);
        // Each related plugin ID should be trimmed.
        $this->assertSame([
            'one',
            'two',
            'three',
        ], $definition['related']);
        $this->assertSame('foo', $definition['provider']);
        $this->assertSame(HelpTopicTwig::class, $definition['class']);
        $this->assertSame(vfsStream::url('root/modules/foo/help_topics/foo.topic.html.twig'), $definition['_discovered_file_path']);
        $this->assertSame('foo.topic', $definition['id']);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
HelpTopicDiscoveryTest::testDiscoveryExceptionInvalidRelated public function @covers ::findAll
HelpTopicDiscoveryTest::testDiscoveryExceptionInvalidTopLevel public function @covers ::findAll
HelpTopicDiscoveryTest::testDiscoveryExceptionInvalidYamlKey public function @covers ::findAll
HelpTopicDiscoveryTest::testDiscoveryExceptionMissingLabel public function @covers ::findAll
HelpTopicDiscoveryTest::testDiscoveryExceptionProviderMismatch public function @covers ::findAll
HelpTopicDiscoveryTest::testHelpTopicsBrokenYaml public function @covers ::findAll
HelpTopicDiscoveryTest::testHelpTopicsDefinition public function @covers ::findAll
HelpTopicDiscoveryTest::testHelpTopicsExtensionProviderSpecialCase public function @covers ::findAll
HelpTopicDiscoveryTest::testHelpTopicsInCore public function @covers ::findAll
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.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 338
UnitTestCase::setUpBeforeClass public static function

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