Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Functional / CommentFieldsTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
4
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;
9
10 /**
11  * Tests fields on comments.
12  *
13  * @group comment
14  */
15 class CommentFieldsTest extends CommentTestBase {
16
17   /**
18    * Install the field UI.
19    *
20    * @var array
21    */
22   public static $modules = ['field_ui'];
23
24   /**
25    * Tests that the default 'comment_body' field is correctly added.
26    */
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');
32
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');
36
37     $field->delete();
38
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');
43
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);
48
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]));
55
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);
60   }
61
62   /**
63    * Tests that you can remove a comment field.
64    */
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');
72
73     // Create a sample node.
74     $node = $this->drupalCreateNode([
75       'title' => 'Baloney',
76       'type' => 'test_node_type',
77     ]);
78
79     $this->drupalLogin($this->webUser);
80
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.');
84
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.');
90   }
91
92   /**
93    * Tests link building with non-default comment field names.
94    */
95   public function testCommentFieldLinksNonDefaultName() {
96     $this->drupalCreateContentType(['type' => 'test_node_type']);
97     $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
98
99     $web_user2 = $this->drupalCreateUser([
100       'access comments',
101       'post comments',
102       'create article content',
103       'edit own comments',
104       'skip comment approval',
105       'access content',
106     ]);
107
108     // Create a sample node.
109     $node = $this->drupalCreateNode([
110       'title' => 'Baloney',
111       'type' => 'test_node_type',
112     ]);
113
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();
118
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();
124
125     $this->drupalLogin($web_user2);
126
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
132     // module installer.
133     \Drupal::service('router.builder')->rebuild();
134     $this->drupalGet('node');
135
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']));
139   }
140
141   /**
142    * Tests creating a comment field through the interface.
143    */
144   public function testCommentFieldCreate() {
145     // Create user who can administer user fields.
146     $user = $this->drupalCreateUser([
147       'administer user fields',
148     ]);
149     $this->drupalLogin($user);
150
151     // Create comment field in account settings.
152     $edit = [
153       'new_storage_type' => 'comment',
154       'label' => 'User comment',
155       'field_name' => 'user_comment',
156     ];
157     $this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue');
158
159     // Try to save the comment field without selecting a comment type.
160     $edit = [];
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.'));
164
165     // Create a comment type for users.
166     $bundle = CommentType::create([
167       'id' => 'user_comment_type',
168       'label' => 'user_comment_type',
169       'description' => '',
170       'target_entity_type_id' => 'user',
171     ]);
172     $bundle->save();
173
174     // Select a comment type and try to save again.
175     $edit = [
176       'settings[comment_type]' => 'user_comment_type',
177     ];
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.'));
181   }
182
183   /**
184    * Tests that comment module works when installed after a content module.
185    */
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);
190
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();
195     }
196
197     // Purge field data now to allow comment module to be uninstalled once the
198     // field has been deleted.
199     field_purge_batch(10);
200
201     // Uninstall the comment module.
202     $edit = [];
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.');
208
209     // Install core content type module (book).
210     $edit = [];
211     $edit['modules[book][enable]'] = 'book';
212     $this->drupalPostForm('admin/modules', $edit, t('Install'));
213
214     // Now install the comment module.
215     $edit = [];
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.');
220
221     // Create nodes of each type.
222     $this->addDefaultCommentField('node', 'book');
223     $book_node = $this->drupalCreateNode(['type' => 'book']);
224
225     $this->drupalLogout();
226
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());
233   }
234
235 }