diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 18635a6..ecc150a 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -553,9 +553,6 @@ function watchdog_exception($type, Exception $exception, $message = NULL, $varia * @endcode * If there are no messages set, the function returns NULL. * - * @throws \UnexpectedValueException - * When passed a render array with #attached. - * * @see drupal_get_messages() * @see status-messages.html.twig */ @@ -577,10 +574,8 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) if (is_array($message)) { $rendered_message = \Drupal::service('renderer')->renderPlain($message); // The rendering will have bubbled any #attached assets to the top level - // of $message. Throw an exception if there were any. - if (!empty($message['#attached'])) { - throw new \UnexpectedValueException('Render arrays passed to drupal_set_message() cannot have attached assets.'); - } + // of $message. Assert that there are none. + assert('empty($message[\'#attached\']);', 'Render arrays passed to drupal_set_message() cannot have attached assets.'); $message = $rendered_message; } diff --git a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php index 85a66e0..e345f37 100644 --- a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php +++ b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php @@ -59,25 +59,20 @@ function testDrupalSetMessage() { // Ensure that strings that are not marked as safe are escaped. $this->assertEscaped('Thismarkup will be escaped.'); - // Test render arrays that incorrectly add #attached. - try { - drupal_set_message(['#attached' => ['drupalSettings' => ['pecary' => TRUE]]]); - $this->fail('Render arrays passed to drupal_set_message() cannot have attached assets.'); - } - catch (\UnexpectedValueException $e) { - $this->pass('Render arrays passed to drupal_set_message() cannot have attached assets.'); - } + // Ensure render arrays fail with #attached when assertions are enabled. + $this->drupalGet('system-test/drupal-set-message-fail'); + $this->assertText('Render arrays passed to drupal_set_message() cannot have attached assets.'); + } - // Test render arrays that incorrectly add #attached in a child element. - try { - $array_with_attachments['foo']['bar']['#markup'] = t('Render array with assets'); - $array_with_attachments['foo']['bar']['#attached']['drupalSettings'] = ['pecary' => TRUE]; - drupal_set_message($array_with_attachments); - $this->fail('Render arrays passed to drupal_set_message() cannot have attached assets in child elements.'); - } - catch (\UnexpectedValueException $e) { - $this->pass('Render arrays passed to drupal_set_message() cannot have attached assets in child elements.'); + /** + * {@inheritdoc} + */ + protected function error($message = '', $group = 'Other', array $caller = NULL) { + // @todo Clean this up in https://www.drupal.org/node/2536560. + if (strpos($message, 'Render arrays passed to drupal_set_message() cannot have attached assets.') !== FALSE) { + // We're expecting this error. + return FALSE; } + parent::error($message, $group, $caller); } - } diff --git a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php index 63cd091..ddb1a83 100644 --- a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php +++ b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php @@ -148,6 +148,26 @@ public function drupalSetMessageTest() { } /** + * Tests setting messages with a render array containing #attached. + * + * @return array + * Empty array, we just test the setting of messages. + */ + public function drupalSetMessageFailTest() { + // Enable assert testing. + // @todo Clean this up in https://www.drupal.org/node/2536560. + $old_assert_active = assert_options(ASSERT_ACTIVE, 1); + + $array_with_attachments['foo']['bar']['#markup'] = t('Render array with assets'); + $array_with_attachments['foo']['bar']['#attached']['drupalSettings'] = ['pecary' => TRUE]; + drupal_set_message($array_with_attachments); + + // Reset assert settings. + assert_options(ASSERT_ACTIVE, $old_assert_active); + return []; + } + + /** * Controller to return $_GET['destination'] for testing. * * @param \Symfony\Component\HttpFoundation\Request $request diff --git a/core/modules/system/tests/modules/system_test/system_test.routing.yml b/core/modules/system/tests/modules/system_test/system_test.routing.yml index ecb9921..8062111 100644 --- a/core/modules/system/tests/modules/system_test/system_test.routing.yml +++ b/core/modules/system/tests/modules/system_test/system_test.routing.yml @@ -21,6 +21,14 @@ system_test.drupal_set_message: requirements: _access: 'TRUE' +system_test.drupal_set_message.fail: + path: '/system-test/drupal-set-message-fail' + defaults: + _title: 'Uses #attached in render array with drupal_set_message()' + _controller: '\Drupal\system_test\Controller\SystemTestController::drupalSetMessageFailTest' + requirements: + _access: 'TRUE' + system_test.main_content_fallback: path: '/system-test/main-content-fallback' defaults: