View source
<?php
namespace Drupal\Tests\help_topics\Unit;
use Drupal\Core\Cache\Cache;
use Drupal\help_topics\HelpTopicTwig;
use Drupal\Tests\UnitTestCase;
use Twig\Template;
use Twig\TemplateWrapper;
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() : void {
$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([
'core.extension',
], $this->helpTopic
->getCacheTags());
$this
->assertEquals(Cache::PERMANENT, $this->helpTopic
->getCacheMaxAge());
}
protected function getTwigMock() {
$twig = $this
->getMockBuilder('Drupal\\Core\\Template\\TwigEnvironment')
->disableOriginalConstructor()
->getMock();
$template = $this
->getMockForAbstractClass(Template::class, [
$twig,
], '', TRUE, TRUE, TRUE, [
'render',
]);
$template
->method('render')
->willReturn(self::PLUGIN_INFORMATION['body']);
$twig
->method('load')
->willReturn(new TemplateWrapper($twig, $template));
return $twig;
}
}