class HelpTopicTwigTest

Same name and namespace in other branches
  1. 11.x core/modules/help/tests/src/Unit/HelpTopicTwigTest.php \Drupal\Tests\help\Unit\HelpTopicTwigTest
  2. 10 core/modules/help/tests/src/Unit/HelpTopicTwigTest.php \Drupal\Tests\help\Unit\HelpTopicTwigTest
  3. 9 core/modules/help_topics/tests/src/Unit/HelpTopicTwigTest.php \Drupal\Tests\help_topics\Unit\HelpTopicTwigTest
  4. 8.9.x core/modules/help_topics/tests/src/Unit/HelpTopicTwigTest.php \Drupal\Tests\help_topics\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.

Attributes

#[CoversClass(HelpTopicTwig::class)] #[Group('help')]

Hierarchy

Expanded class hierarchy of HelpTopicTwigTest

File

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

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());
  }
  
  /**
   * Tests text.
   *
   * @legacy-covers ::getBody
   * @legacy-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']);
  }
  
  /**
   * Tests definition.
   *
   * @legacy-covers ::getProvider
   * @legacy-covers ::isTopLevel
   * @legacy-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']);
  }
  
  /**
   * Tests cache info.
   *
   * @legacy-covers ::getCacheContexts
   * @legacy-covers ::getCacheTags
   * @legacy-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(Template::class)
      ->onlyMethods([
      'render',
      'getTemplateName',
      'getDebugInfo',
      'getSourceContext',
      'doDisplay',
    ])
      ->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 Deprecated Modifiers Object type Summary Overriden Title
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
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 Tests cache info.
HelpTopicTwigTest::testDefinition public function Tests definition.
HelpTopicTwigTest::testText public function Tests text.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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