function DefaultViewRecentCommentsTest::setUp

Same name and namespace in other branches
  1. 8.9.x core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest::setUp()
  2. 10 core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest::setUp()
  3. 11.x core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest::setUp()

Sets up the test.

Parameters

bool $import_test_views: Should the views specified on the test class be imported. If you need to setup some additional stuff, like fields, you need to call false and then call createTestViews for your own.

array $modules: The module directories to look in for test views.

Overrides ViewTestBase::setUp

File

core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php, line 71

Class

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

Namespace

Drupal\Tests\comment\Functional\Views

Code

protected function setUp($import_test_views = TRUE, $modules = []) : void {
    parent::setUp($import_test_views, $modules);
    // Create a new content type
    $content_type = $this->drupalCreateContentType();
    // Add a node of the new content type.
    $node_data = [
        '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->defaultDisplayResults; $i++) {
        
        /** @var \Drupal\comment\CommentInterface $comment */
        $comment = Comment::create([
            '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->defaultDisplayResults - $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.