diff --git a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php index 8556561..bf62c68 100644 --- a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php @@ -8,6 +8,7 @@ namespace Drupal\Core\EventSubscriber; use Drupal\Component\Utility\String; +use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\Routing\RouteBuildEvent; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Cmf\Component\Routing\RouteObjectInterface; @@ -17,6 +18,7 @@ * Provides a route subscriber which checks for invalid pattern variables. */ class SpecialAttributesRouteSubscriber extends RouteSubscriberBase { + use MessengerTrait; /** * {@inheritdoc} @@ -37,7 +39,7 @@ protected function alterRoutes(RouteCollection $collection) { foreach ($collection->all() as $route) { if ($not_allowed_variables = array_intersect($route->compile()->getVariables(), $special_variables)) { $placeholders = array('@variables' => implode(', ', $not_allowed_variables)); - drupal_set_message(String::format('The following variables are reserved names by drupal: @variables', $placeholders)); + $this->addMessage(String::format('The following variables are reserved names by drupal: @variables', $placeholders)); watchdog('error', 'The following variables are reserved names by drupal: @variables', $placeholders); return FALSE; } diff --git a/core/lib/Drupal/Core/Messenger/MessengerTrait.php b/core/lib/Drupal/Core/Messenger/MessengerTrait.php index e69de29..af65590 100644 --- a/core/lib/Drupal/Core/Messenger/MessengerTrait.php +++ b/core/lib/Drupal/Core/Messenger/MessengerTrait.php @@ -0,0 +1,63 @@ +getMessenger()->addMessage($message, $type, $repeat); + return $this; + } + + /** + * Gets the messenger service. + * + * @return \Drupal\Core\Messenger\MessengerInterface + * The messenger service. + */ + protected function getMessenger() { + if (!$this->messenger) { + $this->messenger = \Drupal::service('messenger'); + } + + return $this->messenger; + } + + /** + * Sets the messenger service to use. + * + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. + * + * @return $this + */ + public function setMessenger(MessengerInterface $messenger) { + $this->messenger = $messenger; + + return $this; + } + + +} diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php index 65ba07d..934f96b 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php @@ -43,6 +43,8 @@ protected function setUp() { parent::setUp(); $this->specialAttributesRouteSubscriber = new SpecialAttributesRouteSubscriber(); + $messenger = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->specialAttributesRouteSubscriber->setMessenger($messenger); } /** @@ -121,8 +123,4 @@ public function testOnRouteBuildingInvalidVariables(Route $route) { function watchdog($type, $message, array $args = array()) { } } - if (!function_exists('drupal_set_message')) { - function drupal_set_message($type = NULL, $message = '') { - } - } } diff --git a/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php b/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php index e69de29..8a7fa9c 100644 --- a/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php +++ b/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php @@ -0,0 +1,85 @@ + 'Messenger trait', + 'description' => 'Tests the messenger trait.', + 'group' => 'Messenger', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->messengerTrait = $this->getObjectForTrait('\Drupal\Core\Messenger\MessengerTrait'); + + $this->reflection = new \ReflectionClass(get_class($this->messengerTrait)); + } + + /** + * @covers ::addMessage + */ + public function testAddMessage() { + $stub = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->messengerTrait->setMessenger($stub); + + $message_string = 'Beep boop'; + + $stub->expects($this->once()) + ->method('addMessage') + ->with($this->equalTo($message_string)); + + $method = $this->reflection->getMethod('addMessage'); + $method->setAccessible(TRUE); + + $this->assertEquals($this->messengerTrait, $method->invoke($this->messengerTrait, $message_string)); + } + + /** + * @covers ::getMessenger + */ + public function testGetMessenger() { + $stub = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->messengerTrait->setMessenger($stub); + + $method = $this->reflection->getMethod('getMessenger'); + $method->setAccessible(TRUE); + + $this->assertSame($stub, $method->invoke($this->messengerTrait)); + } + +}