Commit 4462e204 authored by webchick's avatar webchick

Issue #1938062 by dawehner, tstoeckler, slashrsm, tim.plunkett, olli, pcambra,...

Issue #1938062 by dawehner, tstoeckler, slashrsm, tim.plunkett, olli, pcambra, YesCT, damiankloip: Convert the recent_comments block to a view.
parent be70033e
......@@ -701,6 +701,13 @@ function comment_update_8008(&$sandbox) {
db_drop_field('node_revision', 'comment');
}
/**
* Remove the comment_block_count variable.
*/
function comment_update_8009() {
variable_del('comment_block_count');
}
/**
* @} End of "addtogroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.
......
......@@ -163,9 +163,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),
),
......@@ -419,30 +416,6 @@ function comment_new_page_count($num_comments, $new_replies, EntityInterface $en
return $pagenum;
}
/**
* 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)) . '&nbsp;<span>' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . '</span>';
}
if ($items) {
$item_list = array(
'#theme' => 'item_list',
'#items' => $items,
);
return drupal_render($item_list);
}
else {
return t('No comments available.');
}
}
/**
* Implements hook_entity_view().
*/
......
<?php
/**
* @file
* Contains \Drupal\comment\Plugin\Block\RecentCommentsBlock.
*/
namespace Drupal\comment\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'Recent comments' block.
*
* @Block(
* id = "recent_comments",
* admin_label = @Translation("Recent comments"),
* category = @Translation("Lists (Views)")
* )
*/
class RecentCommentsBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'block_count' => 10,
);
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account) {
return $account->hasPermission('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'];
}
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#theme' => 'comment_block',
'#number' => $this->configuration['block_count'],
);
}
}
......@@ -6,6 +6,7 @@
*/
namespace Drupal\comment\Tests;
use Drupal\Component\Utility\String;
/**
* Tests the Comment module blocks.
......@@ -17,7 +18,7 @@ class CommentBlockTest extends CommentTestBase {
*
* @var array
*/
public static $modules = array('block');
public static $modules = array('block', 'views');
function setUp() {
parent::setUp();
......@@ -46,12 +47,18 @@ public static function getInfo() {
*/
function testRecentCommentBlock() {
$this->drupalLogin($this->admin_user);
$block = $this->drupalPlaceBlock('recent_comments', array('block_count' => 2));
// 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());
$block = $this->drupalPlaceBlock('views_block:comments_recent-block_1');
// 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.
$timestamp = REQUEST_TIME;
for ($i = 0; $i < 11; ++$i) {
$subject = ($i % 2) ? $this->randomName() : '';
$comments[$i] = $this->postComment($this->node, $this->randomName(), $subject);
$comments[$i]->created->value = $timestamp--;
$comments[$i]->save();
}
// Test that a user without the 'access comments' permission cannot see the
// block.
......@@ -61,48 +68,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->getPlugin()->setConfigurationValue('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[10]->subject->value, 'Comment 11 not found in block.');
for ($i = 0; $i < 10; $i++) {
$this->assertText($comments[$i]->subject->value, String::format('Comment @number found in block.', array('@number' => 10 - $i)));
if ($i > 1) {
$previous_position = $position;
$position = strpos($this->drupalGetContent(), $comments[$i]->subject->value);
$this->assertTrue($position > $previous_position, String::format('Comment @a appears after comment @b', array('@a' => 10 - $i, '@b' => 11 - $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('<link rel="canonical"', 'Canonical URL was found in the HTML head');
for ($i = 0; $i < 10; $i++) {
$this->clickLink($comments[$i]->subject->value);
$this->assertText($comments[$i]->subject->value, 'Comment link goes to correct page.');
$this->assertRaw('<link rel="canonical"', 'Canonical URL was found in the HTML head');
}
}
}
......@@ -7,8 +7,9 @@
namespace Drupal\comment\Tests\Views;
use Drupal\comment\CommentInterface;
use Drupal\entity\DatabaseStorageController;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestBase;
class DefaultViewRecentComments extends ViewTestBase {
......@@ -82,13 +83,12 @@ public function setUp() {
// Create some comments and attach them to the created node.
for ($i = 0; $i < $this->masterDisplayResults; $i++) {
$comment = entity_create('comment', array(
'status' => CommentInterface::PUBLISHED,
'field_name' => 'comment',
'entity_type' => 'node',
'entity_id' => $this->node->id(),
));
$comment->uid->target_id = 0;
// Stagger the comments so the timestamp sorting works.
$comment->created->value = REQUEST_TIME - $i;
$comment->subject->value = 'Test comment ' . $i;
$comment->comment_body->value = 'Test body ' . $i;
$comment->comment_body->format = 'full_html';
......@@ -104,7 +104,7 @@ public function setUp() {
// Store all the nodes just created to access their properties on the tests.
$this->commentsCreated = entity_load_multiple('comment');
// Sort created comments in ascending order.
// Sort created comments in descending order.
ksort($this->commentsCreated, SORT_NUMERIC);
}
......@@ -120,14 +120,14 @@ public function testBlockDisplay() {
'comment_entity_id' => 'entity_id',
'comment_subject' => 'subject',
'cid' => 'cid',
'comment_changed' => 'changed'
'comment_created' => 'created'
);
$expected_result = array();
foreach (array_values($this->commentsCreated) as $key => $comment) {
$expected_result[$key]['entity_id'] = $comment->entity_id->value;
$expected_result[$key]['subject'] = $comment->subject->value;
$expected_result[$key]['cid'] = $comment->id();
$expected_result[$key]['changed'] = $comment->changed->value;
$expected_result[$key]['created'] = $comment->created->value;
}
$this->assertIdenticalResultset($view, $expected_result, $map);
......@@ -139,34 +139,4 @@ public function testBlockDisplay() {
);
}
/**
* Tests the page defined by the comments_recent view.
*/
public function testPageDisplay() {
$view = views_get_view('comments_recent');
$view->setDisplay('page_1');
$this->executeView($view);
$map = array(
'comment_entity_id' => 'entity_id',
'comment_subject' => 'subject',
'comment_changed' => 'changed',
'cid' => 'cid'
);
$expected_result = array();
foreach (array_values($this->commentsCreated) as $key => $comment) {
$expected_result[$key]['entity_id'] = $comment->entity_id->value;
$expected_result[$key]['subject'] = $comment->subject->value;
$expected_result[$key]['changed'] = $comment->changed->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)
)
);
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\views\Tests;
use Drupal\comment\CommentInterface;
use Drupal\Core\Language\Language;
use Drupal\simpletest\WebTestBase;
use Drupal\views\ViewExecutable;
......@@ -106,6 +107,7 @@ protected function setUp() {
$comment = array(
'uid' => $user->id(),
'status' => CommentInterface::PUBLISHED,
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment