Newer
Older
Salvador Molina
committed
<?php
/**
* @file
catch
committed
* Contains \Drupal\comment\Tests\Views\DefaultViewRecentComments.
Salvador Molina
committed
*/
catch
committed
namespace Drupal\comment\Tests\Views;
Salvador Molina
committed
Angie Byron
committed
use Drupal\comment\CommentInterface;
catch
committed
use Drupal\views\Views;
Angie Byron
committed
use Drupal\views\Tests\ViewTestBase;
Salvador Molina
committed
class DefaultViewRecentComments extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
Alex Pott
committed
public static $modules = array('node', 'comment', 'block');
Salvador Molina
committed
/**
* Number of results for the Master display.
*
* @var int
*/
protected $masterDisplayResults = 5;
/**
* Number of results for the Block display.
*
* @var int
*/
protected $blockDisplayResults = 5;
/**
* Number of results for the Page display.
*
* @var int
*/
protected $pageDisplayResults = 5;
/**
* Will hold the comments created for testing.
*
* @var array
*/
protected $commentsCreated = array();
/**
* Contains the node object used for comments of this test.
*
Alex Pott
committed
* @var \Drupal\node\Node
Salvador Molina
committed
*/
public $node;
public static function getInfo() {
return array(
'name' => '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->container->get('comment.manager')->addDefaultField('node', $content_type->type);
Salvador Molina
committed
$this->node = $this->drupalCreateNode($node_data);
// Force a flush of the in-memory storage.
$this->container->get('views.views_data')->clear();
Salvador Molina
committed
// Create some comments and attach them to the created node.
for ($i = 0; $i < $this->masterDisplayResults; $i++) {
/** @var \Drupal\comment\CommentInterface $comment */
$comment = entity_create('comment', array(
Angie Byron
committed
'status' => CommentInterface::PUBLISHED,
'field_name' => 'comment',
'entity_type' => 'node',
'entity_id' => $this->node->id(),
));
$comment->setOwnerId(0);
$comment->setSubject('Test comment ' . $i);
$comment->comment_body->value = 'Test body ' . $i;
$comment->comment_body->format = 'full_html';
Salvador Molina
committed
// Ensure comments are sorted in ascending order.
$time = REQUEST_TIME + ($this->masterDisplayResults - $i);
$comment->setCreatedTime($time);
$comment->changed->value = $time;
$comment->save();
Salvador Molina
committed
}
// Store all the nodes just created to access their properties on the tests.
$this->commentsCreated = entity_load_multiple('comment');
Angie Byron
committed
// Sort created comments in descending order.
ksort($this->commentsCreated, SORT_NUMERIC);
Salvador Molina
committed
}
/**
* Tests the block defined by the comments_recent view.
*/
public function testBlockDisplay() {
catch
committed
$view = Views::getView('comments_recent');
Angie Byron
committed
$view->setDisplay('block_1');
Salvador Molina
committed
$this->executeView($view);
$map = array(
'comment_entity_id' => 'entity_id',
Salvador Molina
committed
'comment_subject' => 'subject',
'cid' => 'cid',
Angie Byron
committed
'comment_created' => 'created'
Salvador Molina
committed
);
$expected_result = array();
foreach (array_values($this->commentsCreated) as $key => $comment) {
$expected_result[$key]['entity_id'] = $comment->getCommentedEntityId();
$expected_result[$key]['subject'] = $comment->getSubject();
$expected_result[$key]['cid'] = $comment->id();
$expected_result[$key]['created'] = $comment->getCreatedTime();
Salvador Molina
committed
}
$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)
)
);
}
}