Version 1
[yaffs-website] / web / core / modules / forum / tests / src / Functional / ForumBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Functional;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests the forum blocks.
11  *
12  * @group forum
13  */
14 class ForumBlockTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['forum', 'block'];
22
23   /**
24    * A user with various administrative privileges.
25    */
26   protected $adminUser;
27
28   protected function setUp() {
29     parent::setUp();
30
31     // Create users.
32     $this->adminUser = $this->drupalCreateUser([
33       'access administration pages',
34       'administer blocks',
35       'administer nodes',
36       'create forum content',
37       'post comments',
38       'skip comment approval',
39     ]);
40   }
41
42   /**
43    * Tests the "New forum topics" block.
44    */
45   public function testNewForumTopicsBlock() {
46     $this->drupalLogin($this->adminUser);
47
48     // Enable the new forum topics block.
49     $block = $this->drupalPlaceBlock('forum_new_block');
50     $this->drupalGet('');
51
52     // Create 5 forum topics.
53     $topics = $this->createForumTopics();
54
55
56     $this->assertLink(t('More'), 0, 'New forum topics block has a "more"-link.');
57     $this->assertLinkByHref('forum', 0, 'New forum topics block has a "more"-link.');
58
59     // We expect all 5 forum topics to appear in the "New forum topics" block.
60     foreach ($topics as $topic) {
61       $this->assertLink($topic, 0, format_string('Forum topic @topic found in the "New forum topics" block.', ['@topic' => $topic]));
62     }
63
64     // Configure the new forum topics block to only show 2 topics.
65     $block->getPlugin()->setConfigurationValue('block_count', 2);
66     $block->save();
67
68     $this->drupalGet('');
69     // We expect only the 2 most recent forum topics to appear in the "New forum
70     // topics" block.
71     for ($index = 0; $index < 5; $index++) {
72       if (in_array($index, [3, 4])) {
73         $this->assertLink($topics[$index], 0, format_string('Forum topic @topic found in the "New forum topics" block.', ['@topic' => $topics[$index]]));
74       }
75       else {
76         $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "New forum topics" block.', ['@topic' => $topics[$index]]));
77       }
78     }
79   }
80
81   /**
82    * Tests the "Active forum topics" block.
83    */
84   public function testActiveForumTopicsBlock() {
85     $this->drupalLogin($this->adminUser);
86
87     // Create 10 forum topics.
88     $topics = $this->createForumTopics(10);
89
90     // Comment on the first 5 topics.
91     $date = new DrupalDateTime();
92     for ($index = 0; $index < 5; $index++) {
93       // Get the node from the topic title.
94       $node = $this->drupalGetNodeByTitle($topics[$index]);
95       $date->modify('+1 minute');
96       $comment = Comment::create([
97         'entity_id' => $node->id(),
98         'field_name' => 'comment_forum',
99         'entity_type' => 'node',
100         'node_type' => 'node_type_' . $node->bundle(),
101         'subject' => $this->randomString(20),
102         'comment_body' => $this->randomString(256),
103         'created' => $date->getTimestamp(),
104       ]);
105       $comment->save();
106     }
107
108     // Enable the block.
109     $block = $this->drupalPlaceBlock('forum_active_block');
110     $this->drupalGet('');
111     $this->assertLink(t('More'), 0, 'Active forum topics block has a "more"-link.');
112     $this->assertLinkByHref('forum', 0, 'Active forum topics block has a "more"-link.');
113
114     // We expect the first 5 forum topics to appear in the "Active forum topics"
115     // block.
116     $this->drupalGet('<front>');
117     for ($index = 0; $index < 10; $index++) {
118       if ($index < 5) {
119         $this->assertLink($topics[$index], 0, format_string('Forum topic @topic found in the "Active forum topics" block.', ['@topic' => $topics[$index]]));
120       }
121       else {
122         $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "Active forum topics" block.', ['@topic' => $topics[$index]]));
123       }
124     }
125
126     // Configure the active forum block to only show 2 topics.
127     $block->getPlugin()->setConfigurationValue('block_count', 2);
128     $block->save();
129
130     $this->drupalGet('');
131
132     // We expect only the 2 forum topics with most recent comments to appear in
133     // the "Active forum topics" block.
134     for ($index = 0; $index < 10; $index++) {
135       if (in_array($index, [3, 4])) {
136         $this->assertLink($topics[$index], 0, 'Forum topic found in the "Active forum topics" block.');
137       }
138       else {
139         $this->assertNoText($topics[$index], 'Forum topic not found in the "Active forum topics" block.');
140       }
141     }
142   }
143
144   /**
145    * Creates a forum topic.
146    *
147    * @return string
148    *   The title of the newly generated topic.
149    */
150   protected function createForumTopics($count = 5) {
151     $topics = [];
152     $date = new DrupalDateTime();
153     $date->modify('-24 hours');
154
155     for ($index = 0; $index < $count; $index++) {
156       // Generate a random subject/body.
157       $title = $this->randomMachineName(20);
158       $body = $this->randomMachineName(200);
159       // Forum posts are ordered by timestamp, so force a unique timestamp by
160       // changing the date.
161       $date->modify('+1 minute');
162
163       $edit = [
164         'title[0][value]' => $title,
165         'body[0][value]' => $body,
166         // Forum posts are ordered by timestamp, so force a unique timestamp by
167         // adding the index.
168         'created[0][value][date]' => $date->format('Y-m-d'),
169         'created[0][value][time]' => $date->format('H:i:s'),
170       ];
171
172       // Create the forum topic, preselecting the forum ID via a URL parameter.
173       $this->drupalPostForm('node/add/forum', $edit, t('Save and publish'), ['query' => ['forum_id' => 1]]);
174       $topics[] = $title;
175     }
176
177     return $topics;
178   }
179
180 }