From 1d707401bf4163565de0e71e136fa60abcde7469 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 8 Oct 2014 10:47:08 +0100
Subject: [PATCH] Issue #1946240 by eiriksm, David_Rothstein, Yaron Tal,
 madhusudanmca, rszrama: Remove the hardcoded 0 index in
 status-messages.html.twig.

---
 .../Tests/Bootstrap/DrupalSetMessageTest.php  | 37 +++++++++++++++++++
 .../templates/status-messages.html.twig       |  2 +-
 .../src/Controller/SystemTestController.php   | 16 ++++++++
 .../system_test/system_test.routing.yml       |  8 ++++
 4 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php

diff --git a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php
new file mode 100644
index 000000000000..c34f8dd728ce
--- /dev/null
+++ b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\system\Tests\Bootstrap\DrupalSetMessageTest.
+ */
+
+namespace Drupal\system\Tests\Bootstrap;
+
+use Drupal\simpletest\WebTestBase;
+
+/**
+ * Tests drupal_set_message() and related functions.
+ *
+ * @group Bootstrap
+ */
+class DrupalSetMessageTest extends WebTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('system_test');
+
+  /**
+   * Tests setting messages and removing one before it is displayed.
+   */
+  function testSetRemoveMessages() {
+    // The page at system-test/drupal-set-message sets two messages and then
+    // removes the first before it is displayed.
+    $this->drupalGet('system-test/drupal-set-message');
+    $this->assertNoText('First message (removed).');
+    $this->assertText('Second message (not removed).');
+  }
+
+}
diff --git a/core/modules/system/templates/status-messages.html.twig b/core/modules/system/templates/status-messages.html.twig
index f3fa61bd63f2..e1fd08fbe1d0 100644
--- a/core/modules/system/templates/status-messages.html.twig
+++ b/core/modules/system/templates/status-messages.html.twig
@@ -44,7 +44,7 @@
           {% endfor %}
         </ul>
       {% else %}
-        {{ messages.0 }}
+        {{ messages|first }}
       {% endif %}
     {% if type == 'error' %}
       </div>
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 c5a10b8179d2..195bafd66d84 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
@@ -24,6 +24,22 @@ public function mainContentFallback() {
     return $this->t('Content to test main content fallback');
   }
 
+  /**
+   * Tests setting messages and removing one before it is displayed.
+   *
+   * @return string
+   *   Empty string, we just test the setting of messages.
+   */
+  public function drupalSetMessageTest() {
+    // Set two messages.
+    drupal_set_message('First message (removed).');
+    drupal_set_message('Second message (not removed).');
+
+    // Remove the first.
+    unset($_SESSION['messages']['status'][0]);
+    return '';
+  }
+
   /**
    * @todo Remove system_test_lock_acquire().
    */
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 520ba3737fbc..32d235bab9fa 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
@@ -13,6 +13,14 @@ system_test.main_content_handling:
   requirements:
     _access: 'TRUE'
 
+system_test.drupal_set_message:
+  path: '/system-test/drupal-set-message'
+  defaults:
+    _title: 'Set messages with drupal_set_message()'
+    _content: '\Drupal\system_test\Controller\SystemTestController::drupalSetMessageTest'
+  requirements:
+    _access: 'TRUE'
+
 system_test.main_content_fallback:
   path: '/system-test/main-content-fallback'
   defaults:
-- 
GitLab