HelpTopicTwigTest.php in Drupal 8
File
core/modules/help_topics/tests/src/Unit/HelpTopicTwigTest.php
View source
<?php
namespace Drupal\Tests\help_topics\Unit;
use Drupal\Core\Cache\Cache;
use Drupal\help_topics\HelpTopicTwig;
use Drupal\Tests\UnitTestCase;
class HelpTopicTwigTest extends UnitTestCase {
protected $helpTopic;
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>',
];
protected function setUp() {
$this->helpTopic = new HelpTopicTwig([], self::PLUGIN_INFORMATION['id'], self::PLUGIN_INFORMATION, $this
->getTwigMock());
}
public function testText() {
$this
->assertEquals($this->helpTopic
->getBody(), [
'#markup' => self::PLUGIN_INFORMATION['body'],
]);
$this
->assertEquals($this->helpTopic
->getLabel(), self::PLUGIN_INFORMATION['label']);
}
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']);
}
public function testCacheInfo() {
$this
->assertEquals($this->helpTopic
->getCacheContexts(), []);
$this
->assertEquals($this->helpTopic
->getCacheTags(), [
'core.extension',
]);
$this
->assertEquals($this->helpTopic
->getCacheMaxAge(), Cache::PERMANENT);
}
protected function getTwigMock() {
$twig = $this
->getMockBuilder('Drupal\\Core\\Template\\TwigEnvironment')
->disableOriginalConstructor()
->getMock();
$twig
->method('load')
->willReturn(new FakeTemplateWrapper(self::PLUGIN_INFORMATION['body']));
return $twig;
}
}
class FakeTemplateWrapper {
protected $body;
public function __construct($body) {
$this->body = $body;
}
public function render(array $context = []) {
return $this->body;
}
}