class HelpTopicTwigTest

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

Unit test for the HelpTopicTwig class.

Note that the toUrl() and toLink() methods are not covered, because they have calls to new Url() and new Link() in them, so they cannot be unit tested.

@coversDefaultClass \Drupal\help\HelpTopicTwig @group help

Hierarchy

Expanded class hierarchy of HelpTopicTwigTest

File

core/modules/help/tests/src/Unit/HelpTopicTwigTest.php, line 23

Namespace

Drupal\Tests\help\Unit
View source
class HelpTopicTwigTest extends UnitTestCase {
    
    /**
     * The help topic instance to test.
     *
     * @var \Drupal\help\HelpTopicTwig
     */
    protected $helpTopic;
    
    /**
     * The plugin information to use for setting up a test topic.
     *
     * @var array
     */
    const PLUGIN_INFORMATION = [
        'id' => 'test.topic',
        'provider' => 'test',
        'label' => 'This is the topic label',
        'top_level' => TRUE,
        'related' => [
            'something',
        ],
        'body' => '<p>This is the topic body</p>',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->helpTopic = new HelpTopicTwig([], self::PLUGIN_INFORMATION['id'], self::PLUGIN_INFORMATION, $this->getTwigMock());
    }
    
    /**
     * @covers ::getBody
     * @covers ::getLabel
     */
    public function testText() : void {
        $this->assertEquals($this->helpTopic
            ->getBody(), [
            '#markup' => self::PLUGIN_INFORMATION['body'],
        ]);
        $this->assertEquals($this->helpTopic
            ->getLabel(), self::PLUGIN_INFORMATION['label']);
    }
    
    /**
     * @covers ::getProvider
     * @covers ::isTopLevel
     * @covers ::getRelated
     */
    public function testDefinition() : void {
        $this->assertEquals($this->helpTopic
            ->getProvider(), self::PLUGIN_INFORMATION['provider']);
        $this->assertEquals($this->helpTopic
            ->isTopLevel(), self::PLUGIN_INFORMATION['top_level']);
        $this->assertEquals($this->helpTopic
            ->getRelated(), self::PLUGIN_INFORMATION['related']);
    }
    
    /**
     * @covers ::getCacheContexts
     * @covers ::getCacheTags
     * @covers ::getCacheMaxAge
     */
    public function testCacheInfo() : void {
        $this->assertEquals([], $this->helpTopic
            ->getCacheContexts());
        $this->assertEquals([
            'core.extension',
        ], $this->helpTopic
            ->getCacheTags());
        $this->assertEquals(Cache::PERMANENT, $this->helpTopic
            ->getCacheMaxAge());
    }
    
    /**
     * Creates a mock Twig loader class for the test.
     */
    protected function getTwigMock() {
        $twig = $this->getMockBuilder('Drupal\\Core\\Template\\TwigEnvironment')
            ->disableOriginalConstructor()
            ->getMock();
        $template = $this->getMockBuilder(StubTwigTemplate::class)
            ->onlyMethods([
            'render',
        ])
            ->setConstructorArgs([
            $twig,
        ])
            ->getMock();
        $template->method('render')
            ->willReturn(self::PLUGIN_INFORMATION['body']);
        $twig->method('load')
            ->willReturn(new TemplateWrapper($twig, $template));
        return $twig;
    }

}

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.
HelpTopicTwigTest::$helpTopic protected property The help topic instance to test.
HelpTopicTwigTest::getTwigMock protected function Creates a mock Twig loader class for the test.
HelpTopicTwigTest::PLUGIN_INFORMATION constant The plugin information to use for setting up a test topic.
HelpTopicTwigTest::setUp protected function Overrides UnitTestCase::setUp
HelpTopicTwigTest::testCacheInfo public function @covers ::getCacheContexts
@covers ::getCacheTags
@covers ::getCacheMaxAge
HelpTopicTwigTest::testDefinition public function @covers ::getProvider
@covers ::isTopLevel
@covers ::getRelated
HelpTopicTwigTest::testText public function @covers ::getBody
@covers ::getLabel
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.
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.