summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-07-06 16:46:56 (GMT)
committerAlex Pott2014-07-06 16:46:56 (GMT)
commit70016d80e8b1f9630e4bda2f6aee83ce5b7e8671 (patch)
tree5a0a2a56c0a3845889cf30efe7fb56532b866b83
parent310065c31ce7aaa8dd4cde49980feda1f4834491 (diff)
Issue #2170251 by lauriii, mgifford, joshi.rohit100, lokapujya, mparker17, Mirabuck, Jalandhar: Fixed Comment headings can spit out empty heading tags.
-rw-r--r--core/modules/comment/src/Tests/CommentTitleTest.php73
-rw-r--r--core/modules/comment/templates/comment.html.twig10
-rw-r--r--core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.info.yml8
-rw-r--r--core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.module16
-rw-r--r--core/themes/bartik/templates/comment.html.twig8
5 files changed, 109 insertions, 6 deletions
diff --git a/core/modules/comment/src/Tests/CommentTitleTest.php b/core/modules/comment/src/Tests/CommentTitleTest.php
new file mode 100644
index 0000000..d0b6b40
--- /dev/null
+++ b/core/modules/comment/src/Tests/CommentTitleTest.php
@@ -0,0 +1,73 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\comment\Tests\CommentTitleTest.
+ */
+
+namespace Drupal\comment\Tests;
+
+/**
+ * Tests comment titles.
+ */
+class CommentTitleTest extends CommentTestBase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Comment titles',
+ 'description' => 'Test to ensure that appropriate and accessible markup is created for comment titles.',
+ 'group' => 'Comment',
+ );
+ }
+
+ /**
+ * Tests markup for comments with empty titles.
+ */
+ public function testCommentEmptyTitles() {
+ // Enables module that sets comments to an empty string.
+ \Drupal::moduleHandler()->install(array('comment_empty_title_test'));
+
+ // Set comments to have a subject with preview disabled.
+ $this->setCommentPreview(DRUPAL_DISABLED);
+ $this->setCommentForm(TRUE);
+ $this->setCommentSubject(TRUE);
+
+ // Create a node.
+ $this->drupalLogin($this->web_user);
+ $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->id()));
+
+ // Post comment #1 and verify that h3's are not rendered.
+ $subject_text = $this->randomName();
+ $comment_text = $this->randomName();
+ $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
+ // Confirm that the comment was created.
+ $regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
+ $regex .= $comment->comment_body->value . '(.*?)';
+ $regex .= '/s';
+ $this->assertPattern($regex, 'Comment is created succesfully');
+ // Tests that markup is not generated for the comment without header.
+ $this->assertNoPattern('|<h3[^>]*></h3>|', 'Comment title H3 element not found when title is an empty string.');
+ }
+
+ /**
+ * Tests markup for comments with populated titles.
+ */
+ public function testCommentPopulatedTitles() {
+ // Set comments to have a subject with preview disabled.
+ $this->setCommentPreview(DRUPAL_DISABLED);
+ $this->setCommentForm(TRUE);
+ $this->setCommentSubject(TRUE);
+
+ // Create a node.
+ $this->drupalLogin($this->web_user);
+ $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->web_user->id()));
+
+ // Post comment #1 and verify that title is rendered in h3.
+ $subject_text = $this->randomName();
+ $comment_text = $this->randomName();
+ $comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
+ // Confirm that the comment was created.
+ $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
+ // Tests that markup is created for comment with heading.
+ $this->assertPattern('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|', 'Comment title is rendered in h3 when title populated.');
+ }
+}
diff --git a/core/modules/comment/templates/comment.html.twig b/core/modules/comment/templates/comment.html.twig
index 5fca73b..7ffb7c0 100644
--- a/core/modules/comment/templates/comment.html.twig
+++ b/core/modules/comment/templates/comment.html.twig
@@ -65,7 +65,9 @@
*/
#}
<article{{ attributes }}>
- {{ title_prefix }}
+ {% if title %}
+ {{ title_prefix }}
+ {% endif %}
{#
Hide the "new" indicator by default, let a piece of JavaScript ask
@@ -74,9 +76,11 @@
#}
<mark class="hidden new" data-comment-timestamp="{{ new_indicator_timestamp }}"></mark>
- <h3{{ title_attributes }}>{{ title }}</h3>
+ {% if title %}
+ <h3{{ title_attributes }}>{{ title }}</h3>
+ {{ title_suffix }}
+ {% endif %}
- {{ title_suffix }}
<footer>
{{ user_picture }}
diff --git a/core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.info.yml b/core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.info.yml
new file mode 100644
index 0000000..94e6af2
--- /dev/null
+++ b/core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.info.yml
@@ -0,0 +1,8 @@
+name: 'Comment empty titles test'
+type: module
+description: 'Support module for testing empty title accessibility with Comment module.'
+package: Testing
+version: VERSION
+core: 8.x
+dependencies:
+ - comment
diff --git a/core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.module b/core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.module
new file mode 100644
index 0000000..015fe05
--- /dev/null
+++ b/core/modules/comment/tests/modules/comment_empty_title_test/comment_empty_title_test.module
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * @file
+ * Dummy module emptying comment titles to test for approriate and accessible
+ * markup in edge case scenarios where comments have empty titles.
+ */
+
+use Drupal\comment\CommentInterface;
+
+/**
+ * Implements hook_preprocess_comment().
+ */
+function comment_empty_titles_test_preprocess_comment(&$variables) {
+ $variables['title'] = '';
+}
diff --git a/core/themes/bartik/templates/comment.html.twig b/core/themes/bartik/templates/comment.html.twig
index d2c4584..c00a9c5 100644
--- a/core/themes/bartik/templates/comment.html.twig
+++ b/core/themes/bartik/templates/comment.html.twig
@@ -102,9 +102,11 @@
#}
<span class="hidden new" data-comment-timestamp="{{ new_indicator_timestamp }}"></span>
- {{ title_prefix }}
- <h3{{ title_attributes }}>{{ title }}</h3>
- {{ title_suffix }}
+ {% if title %}
+ {{ title_prefix }}
+ <h3{{ title_attributes }}>{{ title }}</h3>
+ {{ title_suffix }}
+ {% endif %}
<div{{ content_attributes }}>
{{ content|without('links') }}