core/modules/comment/comment.install | 1 - core/modules/comment/comment.module | 53 ----- .../comment/config/views.view.comments_recent.yml | 217 +++++++++++++++++++++ .../Plugin/block/block/RecentCommentsBlock.php | 71 ------- .../lib/Drupal/comment/Tests/CommentBlockTest.php | 68 +++---- .../Tests/Views/DefaultViewRecentComments.php | 157 --------------- .../views/config/views.view.comments_recent.yml | 135 ------------- 7 files changed, 246 insertions(+), 456 deletions(-) diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index 48bb5a4..029925f 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -13,7 +13,6 @@ function comment_uninstall() { field_delete_field('comment_body'); // Remove variables. - variable_del('comment_block_count'); $node_types = array_keys(node_type_get_types()); foreach ($node_types as $node_type) { field_attach_delete_bundle('comment', 'comment_node_' . $node_type); diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index dd87b29..103a260 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -196,9 +196,6 @@ function comment_field_extra_fields() { */ function comment_theme() { return array( - 'comment_block' => array( - 'variables' => array('number' => NULL), - ), 'comment_preview' => array( 'variables' => array('comment' => NULL), ), @@ -454,36 +451,6 @@ function comment_permalink($cid) { } /** - * Finds the most recent comments that are available to the current user. - * - * @param integer $number - * (optional) The maximum number of comments to find. Defaults to 10. - * - * @return - * An array of comment objects or an empty array if there are no recent - * comments visible to the current user. - */ -function comment_get_recent($number = 10) { - $query = db_select('comment', 'c'); - $query->innerJoin('node', 'n', 'n.nid = c.nid'); - $query->addTag('node_access'); - $query->addMetaData('base_table', 'comment'); - $comments = $query - ->fields('c') - ->condition('c.status', COMMENT_PUBLISHED) - ->condition('n.status', NODE_PUBLISHED) - ->orderBy('c.created', 'DESC') - // Additionally order by cid to ensure that comments with the same timestamp - // are returned in the exact order posted. - ->orderBy('c.cid', 'DESC') - ->range(0, $number) - ->execute() - ->fetchAll(); - - return $comments ? $comments : array(); -} - -/** * Calculates the page number for the first new comment. * * @param $num_comments @@ -552,26 +519,6 @@ function comment_new_page_count($num_comments, $new_replies, EntityInterface $no } /** - * Returns HTML for a list of recent comments. - * - * @ingroup themeable - */ -function theme_comment_block($variables) { - $items = array(); - $number = $variables['number']; - foreach (comment_get_recent($number) as $comment) { - $items[] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)) . ' ' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . ''; - } - - if ($items) { - return theme('item_list', array('items' => $items)); - } - else { - return t('No comments available.'); - } -} - -/** * Implements hook_node_view(). */ function comment_node_view(EntityInterface $node, EntityDisplay $display, $view_mode) { diff --git a/core/modules/comment/config/views.view.comments_recent.yml b/core/modules/comment/config/views.view.comments_recent.yml new file mode 100755 index 0000000..c1ae9d1 --- /dev/null +++ b/core/modules/comment/config/views.view.comments_recent.yml @@ -0,0 +1,217 @@ +base_field: cid +base_table: comment +core: 8.x +description: '' +status: '1' +display: + block_1: + display_plugin: block + id: block_1 + display_title: Block + position: '' + display_options: + block_description: 'Recent comments' + default: + display_plugin: default + id: default + display_title: Master + position: '' + display_options: + access: + type: perm + options: + perm: 'access comments' + cache: + type: none + query: + type: views_query + exposed_form: + type: basic + pager: + type: some + options: + items_per_page: '10' + offset: '0' + style: + type: html_list + options: + grouping: { } + row_class: '' + default_row_class: '1' + row_class_special: '1' + type: ul + wrapper_class: item-list + class: '' + row: + type: fields + options: + default_field_elements: '1' + inline: + subject: subject + changed: changed + separator: ' ' + hide_empty: '0' + relationships: + nid: + id: nid + table: comment + field: nid + required: '1' + fields: + subject: + id: subject + table: comment + field: subject + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: '0' + alter: + alter_text: '0' + text: '' + make_link: '0' + path: '' + absolute: '0' + external: '0' + replace_spaces: '0' + path_case: none + trim_whitespace: '0' + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: '0' + max_length: '' + word_boundary: '0' + ellipsis: '0' + more_link: '0' + more_link_text: '' + more_link_path: '' + strip_tags: '0' + trim: '0' + preserve_tags: '' + html: '0' + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: '0' + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: '1' + empty: '' + hide_empty: '0' + empty_zero: '0' + hide_alter_empty: '1' + link_to_comment: '1' + link_to_node: '0' + changed: + id: changed + table: comment + field: changed + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: '0' + alter: + alter_text: '0' + text: '' + make_link: '0' + path: '' + absolute: '0' + external: '0' + replace_spaces: '0' + path_case: none + trim_whitespace: '0' + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: '0' + max_length: '' + word_boundary: '1' + ellipsis: '1' + more_link: '0' + more_link_text: '' + more_link_path: '' + strip_tags: '0' + trim: '0' + preserve_tags: '' + html: '0' + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: '0' + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: '1' + empty: '' + hide_empty: '0' + empty_zero: '0' + hide_alter_empty: '1' + date_format: 'time ago' + custom_date_format: '' + timezone: '' + plugin_id: date + filters: + status: + value: '1' + table: comment + field: status + id: status + expose: + operator: '0' + group: '1' + status_node: + value: '1' + table: node + field: status + relationship: nid + id: status_node + expose: + operator: '0' + group: '1' + sorts: + created: + id: created + table: comment + field: created + order: DESC + cid: + id: cid + table: comment + field: cid + relationship: none + group_type: group + admin_label: '' + order: DESC + exposed: '0' + expose: + label: '' + plugin_id: standard + title: 'Recent comments' + empty: + area_text_custom: + id: area_text_custom + table: views + field: area_text_custom + relationship: none + group_type: group + admin_label: '' + label: '' + empty: '1' + content: 'No comments available.' + tokenize: '0' + plugin_id: text_custom +human_name: 'Recent comments' +module: views +id: comments_recent +tag: '' +langcode: en diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/block/block/RecentCommentsBlock.php b/core/modules/comment/lib/Drupal/comment/Plugin/block/block/RecentCommentsBlock.php deleted file mode 100644 index bd7b49b..0000000 --- a/core/modules/comment/lib/Drupal/comment/Plugin/block/block/RecentCommentsBlock.php +++ /dev/null @@ -1,71 +0,0 @@ - 10, - ); - } - - /** - * Overrides \Drupal\block\BlockBase::access(). - */ - public function blockAccess() { - return user_access('access comments'); - } - - /** - * Overrides \Drupal\block\BlockBase::blockForm(). - */ - public function blockForm($form, &$form_state) { - $form['block_count'] = array( - '#type' => 'select', - '#title' => t('Number of recent comments'), - '#default_value' => $this->configuration['block_count'], - '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)), - ); - return $form; - } - - /** - * Overrides \Drupal\block\BlockBase::blockSubmit(). - */ - public function blockSubmit($form, &$form_state) { - $this->configuration['block_count'] = $form_state['values']['block_count']; - } - - /** - * Implements \Drupal\block\BlockBase::build(). - */ - public function build() { - return array( - '#theme' => 'comment_block', - '#number' => $this->configuration['block_count'], - ); - } - -} diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentBlockTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentBlockTest.php index 901d325..c60e153 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentBlockTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentBlockTest.php @@ -17,7 +17,7 @@ class CommentBlockTest extends CommentTestBase { * * @var array */ - public static $modules = array('block'); + public static $modules = array('block', 'views'); function setUp() { parent::setUp(); @@ -46,12 +46,15 @@ public static function getInfo() { */ function testRecentCommentBlock() { $this->drupalLogin($this->admin_user); - $block = $this->drupalPlaceBlock('recent_comments', array(), array('block_count' => 2)); + $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1'); - // Add some test comments, one without a subject. - $comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName()); - $comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName()); - $comment3 = $this->postComment($this->node, $this->randomName()); + // Add some test comments, with and without subjects. Because the 10 newest + // comments should be shown by the block, we create 11 to test that behavior + // below. + for ($i = 0; $i < 11; ++$i) { + $subject = ($i % 2) ? $this->randomName() : ''; + $comments[$i] = $this->postComment($this->node, $this->randomName(), $subject); + } // Test that a user without the 'access comments' permission cannot see the // block. @@ -61,48 +64,35 @@ function testRecentCommentBlock() { // posting a node from a node form. cache_invalidate_tags(array('content' => TRUE)); $this->drupalGet(''); - $label = $block->label(); - $this->assertNoText($label, 'Block was not found.'); + $this->assertNoText(t('Recent comments')); user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access comments')); // Test that a user with the 'access comments' permission can see the // block. $this->drupalLogin($this->web_user); $this->drupalGet(''); - $this->assertText($label, 'Block was found.'); - - // Test the only the 2 latest comments are shown and in the proper order. - $this->assertNoText($comment1->subject->value, 'Comment not found in block.'); - $this->assertText($comment2->subject->value, 'Comment found in block.'); - $this->assertText($comment3->comment_body->value, 'Comment found in block.'); - $this->assertTrue(strpos($this->drupalGetContent(), $comment3->comment_body->value) < strpos($this->drupalGetContent(), $comment2->subject->value), 'Comments were ordered correctly in block.'); - - // Set the number of recent comments to show to 10. - $block->set('settings', array('block_count' => 10)); - $block->save(); - - // Post an additional comment. - $comment4 = $this->postComment($this->node, $this->randomName(), $this->randomName()); - - // Test that all four comments are shown. - $this->assertText($comment1->subject->value, 'Comment found in block.'); - $this->assertText($comment2->subject->value, 'Comment found in block.'); - $this->assertText($comment3->comment_body->value, 'Comment found in block.'); - $this->assertText($comment4->subject->value, 'Comment found in block.'); + $this->assertText(t('Recent comments')); + + // Test the only the 10 latest comments are shown and in the proper order. + $this->assertNoText($comments[0]->subject->value, 'Comment 11 not found in block.'); + for ($i = 1; $i < 11; $i++) { + $this->assertText($comments[$i]->subject->value, format_string('Comment @number found in block.', array('@number' => 11 - $i))); + if ($i > 1) { + $previous_position = $position; + $position = strpos($this->drupalGetContent(), $comments[$i]->subject->value); + $this->assertTrue($position < $previous_position, format_string('Comment @a appears before comment @b', array('@a' => 11 - $i, '@b' => 12 - $i))); + } + $position = strpos($this->drupalGetContent(), $comments[$i]->subject->value); + } // Test that links to comments work when comments are across pages. $this->setCommentsPerPage(1); - $this->drupalGet(''); - $this->clickLink($comment1->subject->value); - $this->assertText($comment1->subject->value, 'Comment link goes to correct page.'); - $this->drupalGet(''); - $this->clickLink($comment2->subject->value); - $this->assertText($comment2->subject->value, 'Comment link goes to correct page.'); - $this->clickLink($comment4->subject->value); - $this->assertText($comment4->subject->value, 'Comment link goes to correct page.'); - // Check that when viewing a comment page from a link to the comment, that - // rel="canonical" is added to the head of the document. - $this->assertRaw('clickLink($comments[$i]->subject->value); + $this->assertText($comments[$i]->subject->value, 'Comment link goes to correct page.'); + $this->assertRaw(' 'Default View - Recent Comments', - 'description' => 'Test results for the Recent Comments view shipped with the module', - 'group' => 'Views Config', - ); - } - - public function setUp() { - parent::setUp(); - - // Create a new content type - $content_type = $this->drupalCreateContentType(); - - // Add a node of the new content type. - $node_data = array( - 'type' => $content_type->type, - ); - - $this->node = $this->drupalCreateNode($node_data); - - // Create some comments and attach them to the created node. - for ($i = 0; $i < $this->masterDisplayResults; $i++) { - $comment = entity_create('comment', array('node_type' => 'comment_node_' . $this->node->type)); - $comment->uid->target_id = 0; - $comment->nid->target_id = $this->node->nid; - $comment->subject->value = 'Test comment ' . $i; - $comment->comment_body->value = 'Test body ' . $i; - $comment->comment_body->format = 'full_html'; - - comment_save($comment); - } - - // Store all the nodes just created to access their properties on the tests. - $this->commentsCreated = entity_load_multiple('comment'); - } - - /** - * Tests the block defined by the comments_recent view. - */ - public function testBlockDisplay() { - $view = views_get_view('comments_recent'); - $view->setDisplay('block'); - $this->executeView($view); - - $map = array( - 'comment_nid' => 'nid', - 'comment_subject' => 'subject', - 'cid' => 'cid', - 'comment_changed' => 'changed' - ); - $expected_result = array(); - foreach (array_values($this->commentsCreated) as $key => $comment) { - $expected_result[$key]['nid'] = $comment->nid->target_id; - $expected_result[$key]['subject'] = $comment->subject->value; - $expected_result[$key]['cid'] = $comment->id(); - $expected_result[$key]['changed'] = $comment->changed->value; - } - $this->assertIdenticalResultset($view, $expected_result, $map); - - // Check the number of results given by the display is the expected. - $this->assertEqual(sizeof($view->result), $this->blockDisplayResults, - format_string('There are exactly @results comments. Expected @expected', - array('@results' => count($view->result), '@expected' => $this->blockDisplayResults) - ) - ); - } - - /** - * Tests the page defined by the comments_recent view. - */ - public function testPageDisplay() { - $view = views_get_view('comments_recent'); - $view->setDisplay('page'); - $this->executeView($view); - - $map = array( - 'comment_nid' => 'nid', - 'comment_subject' => 'subject', - 'comment_changed' => 'changed', - 'comment_changed' => 'created', - 'cid' => 'cid' - ); - $expected_result = array(); - foreach (array_values($this->commentsCreated) as $key => $comment) { - $expected_result[$key]['nid'] = $comment->nid->target_id; - $expected_result[$key]['subject'] = $comment->subject->value; - $expected_result[$key]['changed'] = $comment->changed->value; - $expected_result[$key]['created'] = $comment->created->value; - $expected_result[$key]['cid'] = $comment->id(); - } - $this->assertIdenticalResultset($view, $expected_result, $map); - - // Check the number of results given by the display is the expected. - $this->assertEqual(count($view->result), $this->pageDisplayResults, - format_string('There are exactly @results comments. Expected @expected', - array('@results' => count($view->result), '@expected' => $this->pageDisplayResults) - ) - ); - } -} diff --git a/core/modules/views/config/views.view.comments_recent.yml b/core/modules/views/config/views.view.comments_recent.yml deleted file mode 100644 index f71d671..0000000 --- a/core/modules/views/config/views.view.comments_recent.yml +++ /dev/null @@ -1,135 +0,0 @@ -langcode: en -status: '0' -module: comment -id: comments_recent -description: 'A block and a page with recent comments.' -tag: default -base_table: comment -human_name: 'Recent comments' -core: '8' -display: - default: - id: default - display_title: Master - display_plugin: default - position: '1' - display_options: - query: - type: views_query - options: - query_comment: false - title: 'Recent comments' - use_more: true - access: - type: none - cache: - type: none - exposed_form: - type: basic - pager: - type: some - options: - items_per_page: 5 - relationships: - nid: - id: nid - table: comment - field: nid - plugin_id: standard - fields: - subject: - id: subject - table: comment - field: subject - label: '' - link_to_comment: 1 - plugin_id: comment - timestamp: - id: timestamp - table: comment - field: changed - label: '' - date_format: 'time ago' - plugin_id: date - sorts: - timestamp: - id: timestamp - table: comment - field: changed - order: DESC - plugin_id: date - filters: - status_extra: - id: status_extra - table: node - field: status_extra - relationship: nid - group: 0 - plugin_id: node_status - style: - type: html_list - row: - type: fields - page_1: - id: page_1 - display_title: Page - display_plugin: page - position: '2' - display_options: - query: - type: views_query - options: { } - defaults: - style_plugin: false - style_options: false - row_plugin: false - row_options: false - fields: false - fields: - title: - id: title - table: node - field: title - relationship: nid - label: 'Reply to' - link_to_node: 1 - plugin_id: node - timestamp: - id: timestamp - table: comment - field: changed - label: '' - date_format: 'time ago' - plugin_id: date - subject: - id: subject - table: comment - field: subject - label: '' - link_to_comment: 1 - plugin_id: comment - comment: - id: comment - table: field_data_comment_body - field: comment_body - label: '' - plugin_id: field - path: comments/recent - style: - type: html_list - row: - type: fields - options: - inline: - title: title - timestamp: timestamp - separator: ' ' - block_1: - id: block_1 - display_title: Block - display_plugin: block - position: '3' - display_options: - query: - type: views_query - options: { }