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. 10 core/modules/help/tests/src/Unit/HelpTopicTwigTest.php \Drupal\Tests\help\Unit\HelpTopicTwigTest
  3. 11.x 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_topics\HelpTopicTwig @group help_topics

Hierarchy

Expanded class hierarchy of HelpTopicTwigTest

File

core/modules/help_topics/tests/src/Unit/HelpTopicTwigTest.php, line 19

Namespace

Drupal\Tests\help_topics\Unit
View source
class HelpTopicTwigTest extends UnitTestCase {
    
    /**
     * The help topic instance to test.
     *
     * @var \Drupal\help_topics\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() {
        $this->helpTopic = new HelpTopicTwig([], self::PLUGIN_INFORMATION['id'], self::PLUGIN_INFORMATION, $this->getTwigMock());
    }
    
    /**
     * @covers ::getBody
     * @covers ::getLabel
     */
    public function testText() {
        $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() {
        $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() {
        $this->assertEquals($this->helpTopic
            ->getCacheContexts(), []);
        $this->assertEquals($this->helpTopic
            ->getCacheTags(), [
            'core.extension',
        ]);
        $this->assertEquals($this->helpTopic
            ->getCacheMaxAge(), Cache::PERMANENT);
    }
    
    /**
     * Creates a mock Twig loader class for the test.
     */
    protected function getTwigMock() {
        $twig = $this->getMockBuilder('Drupal\\Core\\Template\\TwigEnvironment')
            ->disableOriginalConstructor()
            ->getMock();
        $twig->method('load')
            ->willReturn(new FakeTemplateWrapper(self::PLUGIN_INFORMATION['body']));
        return $twig;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
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
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::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.

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