Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / forum / tests / src / Functional / Views / ForumIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Functional\Views;
4
5 use Drupal\node\NodeInterface;
6 use Drupal\views\Views;
7 use Drupal\Tests\views\Functional\ViewTestBase;
8 use Drupal\views\Tests\ViewTestData;
9
10 /**
11  * Tests the forum integration into views.
12  *
13  * @group forum
14  */
15 class ForumIntegrationTest extends ViewTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['forum_test_views'];
23
24   /**
25    * Views used by this test.
26    *
27    * @var array
28    */
29   public static $testViews = ['test_forum_index'];
30
31   protected function setUp($import_test_views = TRUE) {
32     parent::setUp($import_test_views);
33
34     ViewTestData::createTestViews(get_class($this), ['forum_test_views']);
35   }
36
37
38   /**
39    * Tests the integration.
40    */
41   public function testForumIntegration() {
42     // Create a forum.
43     $entity_manager = $this->container->get('entity.manager');
44     $term = $entity_manager->getStorage('taxonomy_term')->create(['vid' => 'forums', 'name' => $this->randomMachineName()]);
45     $term->save();
46
47     $comment_storage = $entity_manager->getStorage('comment');
48
49     // Create some nodes which are part of this forum with some comments.
50     $nodes = [];
51     for ($i = 0; $i < 3; $i++) {
52       $node = $this->drupalCreateNode(['type' => 'forum', 'taxonomy_forums' => [$term->id()], 'sticky' => $i == 0 ? NodeInterface::STICKY : NodeInterface::NOT_STICKY]);
53       $nodes[] = $node;
54     }
55
56     $account = $this->drupalCreateUser(['skip comment approval']);
57     $this->drupalLogin($account);
58
59     $comments = [];
60     foreach ($nodes as $index => $node) {
61       for ($i = 0; $i <= $index; $i++) {
62         $comment = $comment_storage->create(['entity_type' => 'node', 'entity_id' => $node->id(), 'field_name' => 'comment_forum']);
63         $comment->save();
64         $comments[$comment->get('entity_id')->target_id][$comment->id()] = $comment;
65       }
66     }
67
68     $view = Views::getView('test_forum_index');
69     $this->executeView($view);
70
71     $expected_result = [];
72     $expected_result[] = [
73       'nid' => $nodes[0]->id(),
74       'sticky' => NodeInterface::STICKY,
75       'comment_count' => 1.
76     ];
77     $expected_result[] = [
78       'nid' => $nodes[1]->id(),
79       'sticky' => NodeInterface::NOT_STICKY,
80       'comment_count' => 2.
81     ];
82     $expected_result[] = [
83       'nid' => $nodes[2]->id(),
84       'sticky' => NodeInterface::NOT_STICKY,
85       'comment_count' => 3.
86     ];
87     $column_map = [
88       'nid' => 'nid',
89       'forum_index_sticky' => 'sticky',
90       'forum_index_comment_count' => 'comment_count',
91     ];
92     $this->assertIdenticalResultset($view, $expected_result, $column_map);
93   }
94
95 }