WebformMessageManagerTest.php in Webform 8.5
File
tests/src/Unit/WebformMessageManagerTest.php
View source
<?php
namespace Drupal\Tests\webform\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformMessageManager;
use Drupal\webform\WebformMessageManagerInterface;
use Drupal\webform\WebformRequestInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Psr\Log\LoggerInterface;
class WebformMessageManagerTest extends UnitTestCase {
public function testMessageManager() {
$webform = $this
->getMockBuilder(WebformInterface::class)
->disableOriginalConstructor()
->getMock();
$webform
->method('getSettings')
->will($this
->returnCallback(function () {
return [
WebformMessageManagerInterface::DRAFT_PENDING_SINGLE => '{single}',
WebformMessageManagerInterface::DRAFT_PENDING_MULTIPLE => '[none]',
];
}));
$url = $this
->getMockBuilder('\\Drupal\\Core\\Url')
->disableOriginalConstructor()
->getMock();
$url
->method('toString')
->willReturn('http://example.com/');
$current_user = $this
->getMockBuilder(AccountInterface::class)
->disableOriginalConstructor()
->getMock();
$config_factory = $this
->getConfigFactoryStub([
'webform.settings' => [
'html_editor.tidy' => TRUE,
'html_editor.element_format' => '',
'element.allowed_tags' => 'p',
],
]);
$webform_submission_storage = $this
->getMockBuilder(WebformSubmissionStorageInterface::class)
->getMock();
$entity_type_manager = $this
->getMockBuilder(EntityTypeManagerInterface::class)
->getMock();
$entity_type_manager
->method('getStorage')
->willReturnMap([
[
'webform_submission',
$webform_submission_storage,
],
]);
$logger = $this
->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$renderer = $this
->getMockBuilder(RendererInterface::class)
->disableOriginalConstructor()
->getMock();
$messenger = $this
->getMockBuilder(MessengerInterface::class)
->disableOriginalConstructor()
->getMock();
$request_handler = $this
->getMockBuilder(WebformRequestInterface::class)
->disableOriginalConstructor()
->getMock();
$request_handler
->method('getUrl')
->willReturn($url);
$token_manager = $this
->getMockBuilder(WebformTokenManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$token_manager
->method('replace')
->will($this
->returnCallback(function ($text) {
return $text;
}));
$container = new ContainerBuilder();
$container
->set('string_translation', $this
->getStringTranslationStub());
$container
->set('config.factory', $config_factory);
\Drupal::setContainer($container);
$message_manager = new WebformMessageManager($current_user, $config_factory, $entity_type_manager, $logger, $renderer, $messenger, $request_handler, $token_manager);
$message_manager
->setWebform($webform);
$expected = [
'#theme' => 'webform_html_editor_markup',
'#markup' => '{single}',
'#allowed_tags' => [
0 => 'p',
],
];
$result = $message_manager
->get(WebformMessageManagerInterface::DRAFT_PENDING_SINGLE);
$this
->assertEquals($expected, $result);
$result = $message_manager
->get(WebformMessageManagerInterface::DRAFT_PENDING_MULTIPLE);
$this
->assertFalse($result);
}
}