You are here

protected function DefaultViewRecentCommentsTest::setUp in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/comment/src/Tests/Views/DefaultViewRecentCommentsTest.php \Drupal\comment\Tests\Views\DefaultViewRecentCommentsTest::setUp()

Sets up a Drupal site for running functional and integration tests.

Installs Drupal with the installation profile specified in \Drupal\simpletest\WebTestBase::$profile into the prefixed database.

Afterwards, installs any additional modules specified in the static \Drupal\simpletest\WebTestBase::$modules property of each class in the class hierarchy.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Overrides ViewTestBase::setUp

File

core/modules/comment/src/Tests/Views/DefaultViewRecentCommentsTest.php, line 67
Contains \Drupal\comment\Tests\Views\DefaultViewRecentCommentsTest.

Class

DefaultViewRecentCommentsTest
Tests results for the Recent Comments view shipped with the module.

Namespace

Drupal\comment\Tests\Views

Code

protected 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
      ->id(),
  );
  $this
    ->addDefaultCommentField('node', $content_type
    ->id());
  $this->node = $this
    ->drupalCreateNode($node_data);

  // Force a flush of the in-memory storage.
  $this->container
    ->get('views.views_data')
    ->clear();

  // 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(
      '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';

    // Ensure comments are sorted in ascending order.
    $time = REQUEST_TIME + ($this->masterDisplayResults - $i);
    $comment
      ->setCreatedTime($time);
    $comment->changed->value = $time;
    $comment
      ->save();
  }

  // Store all the nodes just created to access their properties on the tests.
  $this->commentsCreated = Comment::loadMultiple();

  // Sort created comments in descending order.
  ksort($this->commentsCreated, SORT_NUMERIC);
}