commit 681f39f4516bc9e3635ce47b38135a430f28cbd1 Author: Bart Feenstra Date: Sat Jun 7 16:37:13 2014 -0500 Comment #7 diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 805f7b5..3e539e1 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -1158,7 +1158,7 @@ function watchdog($type, $message, array $variables = array(), $severity = WATCH * * @param string $message * (optional) The translated message to be displayed to the user. For - * consistency with other messages, it should begin with a capital letter and + * consistency with other messages it should begin with a capital letter and * end with a period. * @param string $type * (optional) The message's type. Defaults to 'status'. These values are @@ -1179,13 +1179,13 @@ function watchdog($type, $message, array $variables = array(), $severity = WATCH * @see theme_status_messages() * * @deprecated Deprecated as of Drupal 8.0. - * Use \Drupal::service('messenger')->setMessage() instead. + * Use \Drupal::service('messenger')->addMessage() instead. */ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ $messenger = \Drupal::service('messenger'); - $messenger->setMessage($messenger, $type, $repeat); + $messenger->addMessage($message, $type, $repeat); return $messenger->getMessages(); } diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php index 436f0a9..073ebc4 100644 --- a/core/lib/Drupal/Core/Messenger/Messenger.php +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -7,18 +7,22 @@ namespace Drupal\Core\Messenger; +/** + * Provides a session-based messenger. + * + * @todo Use Symfony's session components once http://drupal.org/node/1858196 + * has been fixed. + */ class Messenger implements MessengerInterface { /** * {@inheritdoc} */ - public function setMessage($message, $type = self::STATUS, $repeat = FALSE) { + public function addMessage($message, $type = self::STATUS, $repeat = FALSE) { if ($repeat || !array_key_exists('messages', $_SESSION) || !array_key_exists($type, $_SESSION['messages']) || !in_array($message, $_SESSION['messages'][$type])) { $_SESSION['messages'][$type][] = $message; - drupal_page_is_cacheable(FALSE); } - return $this; } diff --git a/core/lib/Drupal/Core/Messenger/MessengerInterface.php b/core/lib/Drupal/Core/Messenger/MessengerInterface.php index 2c90c8d..4b0a794 100644 --- a/core/lib/Drupal/Core/Messenger/MessengerInterface.php +++ b/core/lib/Drupal/Core/Messenger/MessengerInterface.php @@ -25,7 +25,7 @@ const ERROR = 'error'; /** - * Sets a new message. + * Adds a new message to the queue. * * @param string $message * (optional) The translated message to be displayed to the user. For @@ -40,7 +40,7 @@ * * @return $this */ - public function setMessage($message, $type = self::STATUS, $repeat = FALSE); + public function addMessage($message, $type = self::STATUS, $repeat = FALSE); /** * Gets all messages. diff --git a/core/modules/system/src/Tests/MessengerTest.php b/core/modules/system/src/Tests/MessengerTest.php new file mode 100644 index 0000000..4d3c2a5 --- /dev/null +++ b/core/modules/system/src/Tests/MessengerTest.php @@ -0,0 +1,79 @@ + '', + 'name' => '\Drupal\Core\Messenger\Messenger unit test', + 'group' => 'Messenger', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->messenger = new Messenger(); + } + + /** + * Tests the messenger. + */ + public function testMessenger() { + $message_a = $this->randomName(); + $type_a = $this->randomName(); + $message_b = $this->randomName(); + $type_b = $this->randomName(); + + // Test that if there are no messages, the default is an empty array. + $this->assertEqual($this->messenger->getMessages(), array()); + + // Test that adding a message returns the messenger and that the message can + // be retrieved. + $this->assertEqual($this->messenger->addMessage($message_a, $type_a), $this->messenger); + $this->messenger->addMessage($message_a, $type_a); + $this->messenger->addMessage($message_a, $type_a, TRUE); + $this->messenger->addMessage($message_b, $type_b, TRUE); + $this->assertEqual($this->messenger->getMessages(), array( + $type_a => array($message_a, $message_a), + $type_b => array($message_b), + )); + + // Test deleting messages of a certain type. + $this->assertEqual($this->messenger->deleteMessagesByType($type_a), $this->messenger); + $this->assertEqual($this->messenger->getMessages(), array( + $type_b => array($message_b), + )); + + // Test deleting all messages. + $this->assertEqual($this->messenger->deleteMessages(), $this->messenger); + $this->assertEqual($this->messenger->getMessages(), array()); + + + } + +} diff --git a/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php b/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php deleted file mode 100644 index bc5d12c..0000000 --- a/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php +++ /dev/null @@ -1,58 +0,0 @@ - '', - 'name' => '\Drupal\Core\Messenger\Messenger unit test', - 'group' => 'Messenger', - ); - } - - /** - * {@inheritdoc} - */ - public function setUp() { - $this->messenger = new Messenger(); - } - - /** - * @covers ::setMessage - * @covers ::getMessages - */ - public function testSetMessage() { - session_start(); - - $message = $this->randomName(); - - $this->assertSame(array(), $this->messenger->getMessages()); - $this->assertSame($this->messenger, $this->messenger->setMessage($message)); - - session_destroy(); - } - -}