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