Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
Chris@0
|
6 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
8 use Drupal\comment\Entity\CommentType;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Tests fields on comments.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group comment
|
Chris@0
|
14 */
|
Chris@0
|
15 class CommentFieldsTest extends CommentTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Install the field UI.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['field_ui'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Tests that the default 'comment_body' field is correctly added.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function testCommentDefaultFields() {
|
Chris@0
|
28 // Do not make assumptions on default node types created by the test
|
Chris@0
|
29 // installation profile, and create our own.
|
Chris@0
|
30 $this->drupalCreateContentType(['type' => 'test_node_type']);
|
Chris@0
|
31 $this->addDefaultCommentField('node', 'test_node_type');
|
Chris@0
|
32
|
Chris@0
|
33 // Check that the 'comment_body' field is present on the comment bundle.
|
Chris@0
|
34 $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
|
Chris@0
|
35 $this->assertTrue(!empty($field), 'The comment_body field is added when a comment bundle is created');
|
Chris@0
|
36
|
Chris@0
|
37 $field->delete();
|
Chris@0
|
38
|
Chris@0
|
39 // Check that the 'comment_body' field is not deleted since it is persisted
|
Chris@0
|
40 // even if it has no fields.
|
Chris@0
|
41 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
Chris@0
|
42 $this->assertTrue($field_storage, 'The comment_body field storage was not deleted');
|
Chris@0
|
43
|
Chris@0
|
44 // Create a new content type.
|
Chris@0
|
45 $type_name = 'test_node_type_2';
|
Chris@0
|
46 $this->drupalCreateContentType(['type' => $type_name]);
|
Chris@0
|
47 $this->addDefaultCommentField('node', $type_name);
|
Chris@0
|
48
|
Chris@0
|
49 // Check that the 'comment_body' field exists and has an instance on the
|
Chris@0
|
50 // new comment bundle.
|
Chris@0
|
51 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
Chris@0
|
52 $this->assertTrue($field_storage, 'The comment_body field exists');
|
Chris@0
|
53 $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
|
Chris@0
|
54 $this->assertTrue(isset($field), format_string('The comment_body field is present for comments on type @type', ['@type' => $type_name]));
|
Chris@0
|
55
|
Chris@0
|
56 // Test adding a field that defaults to CommentItemInterface::CLOSED.
|
Chris@0
|
57 $this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
|
Chris@18
|
58 $field = FieldConfig::load('node.test_node_type.who_likes_ponies');
|
Chris@0
|
59 $this->assertEqual($field->getDefaultValueLiteral()[0]['status'], CommentItemInterface::CLOSED);
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Tests that you can remove a comment field.
|
Chris@0
|
64 */
|
Chris@0
|
65 public function testCommentFieldDelete() {
|
Chris@0
|
66 $this->drupalCreateContentType(['type' => 'test_node_type']);
|
Chris@0
|
67 $this->addDefaultCommentField('node', 'test_node_type');
|
Chris@0
|
68 // We want to test the handling of removing the primary comment field, so we
|
Chris@0
|
69 // ensure there is at least one other comment field attached to a node type
|
Chris@0
|
70 // so that comment_entity_load() runs for nodes.
|
Chris@0
|
71 $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
|
Chris@0
|
72
|
Chris@0
|
73 // Create a sample node.
|
Chris@0
|
74 $node = $this->drupalCreateNode([
|
Chris@0
|
75 'title' => 'Baloney',
|
Chris@0
|
76 'type' => 'test_node_type',
|
Chris@0
|
77 ]);
|
Chris@0
|
78
|
Chris@0
|
79 $this->drupalLogin($this->webUser);
|
Chris@0
|
80
|
Chris@0
|
81 $this->drupalGet('node/' . $node->nid->value);
|
Chris@0
|
82 $elements = $this->cssSelect('.field--type-comment');
|
Chris@0
|
83 $this->assertEqual(2, count($elements), 'There are two comment fields on the node.');
|
Chris@0
|
84
|
Chris@0
|
85 // Delete the first comment field.
|
Chris@0
|
86 FieldStorageConfig::loadByName('node', 'comment')->delete();
|
Chris@0
|
87 $this->drupalGet('node/' . $node->nid->value);
|
Chris@0
|
88 $elements = $this->cssSelect('.field--type-comment');
|
Chris@0
|
89 $this->assertEqual(1, count($elements), 'There is one comment field on the node.');
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Tests link building with non-default comment field names.
|
Chris@0
|
94 */
|
Chris@0
|
95 public function testCommentFieldLinksNonDefaultName() {
|
Chris@0
|
96 $this->drupalCreateContentType(['type' => 'test_node_type']);
|
Chris@0
|
97 $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
|
Chris@0
|
98
|
Chris@0
|
99 $web_user2 = $this->drupalCreateUser([
|
Chris@0
|
100 'access comments',
|
Chris@0
|
101 'post comments',
|
Chris@0
|
102 'create article content',
|
Chris@0
|
103 'edit own comments',
|
Chris@0
|
104 'skip comment approval',
|
Chris@0
|
105 'access content',
|
Chris@0
|
106 ]);
|
Chris@0
|
107
|
Chris@0
|
108 // Create a sample node.
|
Chris@0
|
109 $node = $this->drupalCreateNode([
|
Chris@0
|
110 'title' => 'Baloney',
|
Chris@0
|
111 'type' => 'test_node_type',
|
Chris@0
|
112 ]);
|
Chris@0
|
113
|
Chris@0
|
114 // Go to the node first so that webuser2 see new comments.
|
Chris@0
|
115 $this->drupalLogin($web_user2);
|
Chris@18
|
116 $this->drupalGet($node->toUrl());
|
Chris@0
|
117 $this->drupalLogout();
|
Chris@0
|
118
|
Chris@0
|
119 // Test that buildCommentedEntityLinks() does not break when the 'comment'
|
Chris@0
|
120 // field does not exist. Requires at least one comment.
|
Chris@0
|
121 $this->drupalLogin($this->webUser);
|
Chris@0
|
122 $this->postComment($node, 'Here is a comment', '', NULL, 'comment2');
|
Chris@0
|
123 $this->drupalLogout();
|
Chris@0
|
124
|
Chris@0
|
125 $this->drupalLogin($web_user2);
|
Chris@0
|
126
|
Chris@0
|
127 // We want to check the attached drupalSettings of
|
Chris@0
|
128 // \Drupal\comment\CommentLinkBuilder::buildCommentedEntityLinks. Therefore
|
Chris@0
|
129 // we need a node listing, let's use views for that.
|
Chris@0
|
130 $this->container->get('module_installer')->install(['views'], TRUE);
|
Chris@0
|
131 // We also need a router rebuild, as the router is lazily rebuild in the
|
Chris@0
|
132 // module installer.
|
Chris@0
|
133 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
134 $this->drupalGet('node');
|
Chris@0
|
135
|
Chris@0
|
136 $link_info = $this->getDrupalSettings()['comment']['newCommentsLinks']['node']['comment2']['2'];
|
Chris@0
|
137 $this->assertIdentical($link_info['new_comment_count'], 1);
|
Chris@18
|
138 $this->assertIdentical($link_info['first_new_comment_link'], $node->toUrl('canonical', ['fragment' => 'new'])->toString());
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Tests creating a comment field through the interface.
|
Chris@0
|
143 */
|
Chris@0
|
144 public function testCommentFieldCreate() {
|
Chris@0
|
145 // Create user who can administer user fields.
|
Chris@0
|
146 $user = $this->drupalCreateUser([
|
Chris@0
|
147 'administer user fields',
|
Chris@0
|
148 ]);
|
Chris@0
|
149 $this->drupalLogin($user);
|
Chris@0
|
150
|
Chris@0
|
151 // Create comment field in account settings.
|
Chris@0
|
152 $edit = [
|
Chris@0
|
153 'new_storage_type' => 'comment',
|
Chris@0
|
154 'label' => 'User comment',
|
Chris@0
|
155 'field_name' => 'user_comment',
|
Chris@0
|
156 ];
|
Chris@0
|
157 $this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue');
|
Chris@0
|
158
|
Chris@0
|
159 // Try to save the comment field without selecting a comment type.
|
Chris@0
|
160 $edit = [];
|
Chris@0
|
161 $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
|
Chris@0
|
162 // We should get an error message.
|
Chris@0
|
163 $this->assertText(t('An illegal choice has been detected. Please contact the site administrator.'));
|
Chris@0
|
164
|
Chris@0
|
165 // Create a comment type for users.
|
Chris@0
|
166 $bundle = CommentType::create([
|
Chris@0
|
167 'id' => 'user_comment_type',
|
Chris@0
|
168 'label' => 'user_comment_type',
|
Chris@0
|
169 'description' => '',
|
Chris@0
|
170 'target_entity_type_id' => 'user',
|
Chris@0
|
171 ]);
|
Chris@0
|
172 $bundle->save();
|
Chris@0
|
173
|
Chris@0
|
174 // Select a comment type and try to save again.
|
Chris@0
|
175 $edit = [
|
Chris@0
|
176 'settings[comment_type]' => 'user_comment_type',
|
Chris@0
|
177 ];
|
Chris@0
|
178 $this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
|
Chris@0
|
179 // We shouldn't get an error message.
|
Chris@0
|
180 $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.'));
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Tests that comment module works when installed after a content module.
|
Chris@0
|
185 */
|
Chris@0
|
186 public function testCommentInstallAfterContentModule() {
|
Chris@0
|
187 // Create a user to do module administration.
|
Chris@0
|
188 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer modules']);
|
Chris@0
|
189 $this->drupalLogin($this->adminUser);
|
Chris@0
|
190
|
Chris@0
|
191 // Drop default comment field added in CommentTestBase::setup().
|
Chris@0
|
192 FieldStorageConfig::loadByName('node', 'comment')->delete();
|
Chris@0
|
193 if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
|
Chris@0
|
194 $field_storage->delete();
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 // Purge field data now to allow comment module to be uninstalled once the
|
Chris@0
|
198 // field has been deleted.
|
Chris@0
|
199 field_purge_batch(10);
|
Chris@0
|
200
|
Chris@0
|
201 // Uninstall the comment module.
|
Chris@0
|
202 $edit = [];
|
Chris@0
|
203 $edit['uninstall[comment]'] = TRUE;
|
Chris@0
|
204 $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
|
Chris@0
|
205 $this->drupalPostForm(NULL, [], t('Uninstall'));
|
Chris@0
|
206 $this->rebuildContainer();
|
Chris@0
|
207 $this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.');
|
Chris@0
|
208
|
Chris@0
|
209 // Install core content type module (book).
|
Chris@0
|
210 $edit = [];
|
Chris@0
|
211 $edit['modules[book][enable]'] = 'book';
|
Chris@0
|
212 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
213
|
Chris@0
|
214 // Now install the comment module.
|
Chris@0
|
215 $edit = [];
|
Chris@0
|
216 $edit['modules[comment][enable]'] = 'comment';
|
Chris@0
|
217 $this->drupalPostForm('admin/modules', $edit, t('Install'));
|
Chris@0
|
218 $this->rebuildContainer();
|
Chris@0
|
219 $this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.');
|
Chris@0
|
220
|
Chris@0
|
221 // Create nodes of each type.
|
Chris@0
|
222 $this->addDefaultCommentField('node', 'book');
|
Chris@0
|
223 $book_node = $this->drupalCreateNode(['type' => 'book']);
|
Chris@0
|
224
|
Chris@0
|
225 $this->drupalLogout();
|
Chris@0
|
226
|
Chris@0
|
227 // Try to post a comment on each node. A failure will be triggered if the
|
Chris@0
|
228 // comment body is missing on one of these forms, due to postComment()
|
Chris@0
|
229 // asserting that the body is actually posted correctly.
|
Chris@0
|
230 $this->webUser = $this->drupalCreateUser(['access content', 'access comments', 'post comments', 'skip comment approval']);
|
Chris@0
|
231 $this->drupalLogin($this->webUser);
|
Chris@0
|
232 $this->postComment($book_node, $this->randomMachineName(), $this->randomMachineName());
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 }
|