View source
<?php
namespace Drupal\KernelTests\Core\Messenger;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\KernelTests\KernelTestBase;
class MessengerTest extends KernelTestBase {
protected $messenger;
protected function setUp() {
parent::setUp();
$this->messenger = \Drupal::service('messenger');
}
public function testRemoveSingleMessage() {
$this->messenger
->addStatus('First message (removed).');
$this->messenger
->addStatus(t('Second message with <em>markup!</em> (not removed).'));
$messages = $this->messenger
->deleteByType(MessengerInterface::TYPE_STATUS);
unset($messages[0]);
foreach ($messages as $message) {
$this->messenger
->addStatus($message);
}
$this
->assertCount(1, $this->messenger
->messagesByType(MessengerInterface::TYPE_STATUS));
$this
->assertContains('Second message with <em>markup!</em> (not removed).', $this->messenger
->deleteByType(MessengerInterface::TYPE_STATUS));
}
public function testAddNoDuplicates() {
$this->messenger
->addStatus('Non Duplicated status message');
$this->messenger
->addStatus('Non Duplicated status message');
$this
->assertCount(1, $this->messenger
->messagesByType(MessengerInterface::TYPE_STATUS));
$this->messenger
->addWarning('Non Duplicated warning message');
$this->messenger
->addWarning('Non Duplicated warning message');
$this
->assertCount(1, $this->messenger
->messagesByType(MessengerInterface::TYPE_WARNING));
$this->messenger
->addError('Non Duplicated error message');
$this->messenger
->addError('Non Duplicated error message');
$messages = $this->messenger
->messagesByType(MessengerInterface::TYPE_ERROR);
$this
->assertCount(1, $messages);
$messages = $this->messenger
->all();
$this
->assertCount(3, $messages);
$this
->assertArrayHasKey(MessengerInterface::TYPE_STATUS, $messages);
$this
->assertArrayHasKey(MessengerInterface::TYPE_WARNING, $messages);
$this
->assertArrayHasKey(MessengerInterface::TYPE_ERROR, $messages);
$this->messenger
->deleteAll();
$this
->assertCount(0, $this->messenger
->messagesByType(MessengerInterface::TYPE_STATUS));
$this
->assertCount(0, $this->messenger
->messagesByType(MessengerInterface::TYPE_WARNING));
$this
->assertCount(0, $this->messenger
->messagesByType(MessengerInterface::TYPE_ERROR));
}
public function testAddWithDuplicates() {
$this->messenger
->addStatus('Duplicated status message', TRUE);
$this->messenger
->addStatus('Duplicated status message', TRUE);
$this
->assertCount(2, $this->messenger
->deleteByType(MessengerInterface::TYPE_STATUS));
$this->messenger
->addWarning('Duplicated warning message', TRUE);
$this->messenger
->addWarning('Duplicated warning message', TRUE);
$this
->assertCount(2, $this->messenger
->deleteByType(MessengerInterface::TYPE_WARNING));
$this->messenger
->addError('Duplicated error message', TRUE);
$this->messenger
->addError('Duplicated error message', TRUE);
$this
->assertCount(2, $this->messenger
->deleteByType(MessengerInterface::TYPE_ERROR));
}
public function testAddMarkup() {
$this->messenger
->addStatus(Markup::create('Markup with <em>markup!</em>'));
$this->messenger
->addStatus(Markup::create('Markup with <em>markup!</em>'));
$this
->assertCount(1, $this->messenger
->messagesByType(MessengerInterface::TYPE_STATUS));
$this->messenger
->addStatus(Markup::create('Markup2 with <em>markup!</em>'));
$this
->assertCount(2, $this->messenger
->deleteByType(MessengerInterface::TYPE_STATUS));
$this->messenger
->addStatus(Markup::create('Non duplicate Markup / string.'));
$this->messenger
->addStatus('Non duplicate Markup / string.');
$this->messenger
->addStatus(Markup::create('Duplicate Markup / string.'), TRUE);
$this->messenger
->addStatus('Duplicate Markup / string.', TRUE);
$this
->assertCount(3, $this->messenger
->deleteByType(MessengerInterface::TYPE_STATUS));
$this->messenger
->deleteAll();
$this->messenger
->addStatus(new TranslatableMarkup('Translatable message'));
$messages = $this->messenger
->deleteByType(MessengerInterface::TYPE_STATUS);
$this
->assertInstanceOf(Markup::class, $messages[0]);
}
}