function MessengerTest::testRemoveSingleMessage

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php \Drupal\KernelTests\Core\Messenger\MessengerTest::testRemoveSingleMessage()
  2. 10 core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php \Drupal\KernelTests\Core\Messenger\MessengerTest::testRemoveSingleMessage()
  3. 11.x core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php \Drupal\KernelTests\Core\Messenger\MessengerTest::testRemoveSingleMessage()

@covers ::addStatus @covers ::deleteByType @covers ::messagesByType

File

core/tests/Drupal/KernelTests/Core/Messenger/MessengerTest.php, line 36

Class

MessengerTest
@group Messenger @coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Messenger%21Messenger.php/class/Messenger/8.9.x" title="The messenger service." class="local">\Drupal\Core\Messenger\Messenger</a>

Namespace

Drupal\KernelTests\Core\Messenger

Code

public function testRemoveSingleMessage() {
    // Set two messages.
    $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);
    // Remove the first.
    unset($messages[0]);
    // Re-add the second.
    foreach ($messages as $message) {
        $this->messenger
            ->addStatus($message);
    }
    // Check we only have the second one.
    $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));
}

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