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

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() : void {
    $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([
      '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();
    $twig->method('load')
      ->willReturn(new FakeTemplateWrapper(self::PLUGIN_INFORMATION['body']));
    return $twig;
  }

}

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