3 namespace Drupal\comment\Tests;
5 @trigger_error(__NAMESPACE__ . '\CommentTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\comment\Functional\CommentTestBase instead. See http://www.drupal.org/node/2908490', E_USER_DEPRECATED);
7 use Drupal\comment\Entity\CommentType;
8 use Drupal\comment\Entity\Comment;
9 use Drupal\comment\CommentInterface;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
12 use Drupal\node\Entity\NodeType;
13 use Drupal\simpletest\WebTestBase;
16 * Provides setup and helper methods for comment tests.
18 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
19 * Use \Drupal\Tests\comment\Functional\CommentTestBase instead.
21 * @see https://www.drupal.org/node/2908490
23 abstract class CommentTestBase extends WebTestBase {
32 public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime'];
35 * An administrative user with permission to configure comment settings.
37 * @var \Drupal\user\UserInterface
42 * A normal user with permission to post comments.
44 * @var \Drupal\user\UserInterface
49 * A test node to which comments will be posted.
51 * @var \Drupal\node\NodeInterface
55 protected function setUp() {
58 // Create an article content type only if it does not yet exist, so that
59 // child classes may specify the standard profile.
60 $types = NodeType::loadMultiple();
61 if (empty($types['article'])) {
62 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
65 // Create two test users.
66 $this->adminUser = $this->drupalCreateUser([
67 'administer content types',
68 'administer comments',
69 'administer comment types',
70 'administer comment fields',
71 'administer comment display',
72 'skip comment approval',
75 // Usernames aren't shown in comment edit form autocomplete unless this
76 // permission is granted.
77 'access user profiles',
80 $this->webUser = $this->drupalCreateUser([
83 'create article content',
85 'skip comment approval',
89 // Create comment field on article.
90 $this->addDefaultCommentField('node', 'article');
92 // Create a test node authored by the web user.
93 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
94 $this->drupalPlaceBlock('local_tasks_block');
100 * @param \Drupal\Core\Entity\EntityInterface|null $entity
101 * Node to post comment on or NULL to post to the previously loaded page.
102 * @param string $comment
104 * @param string $subject
106 * @param string $contact
107 * Set to NULL for no contact info, TRUE to ignore success checking, and
108 * array of values to set contact info.
109 * @param string $field_name
110 * (optional) Field name through which the comment should be posted.
111 * Defaults to 'comment'.
113 * @return \Drupal\comment\CommentInterface|null
114 * The posted comment or NULL when posted comment was not found.
116 public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
118 $edit['comment_body[0][value]'] = $comment;
120 if ($entity !== NULL) {
121 $field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
124 $field = FieldConfig::loadByName('node', 'article', $field_name);
126 $preview_mode = $field->getSetting('preview');
128 // Must get the page before we test for fields.
129 if ($entity !== NULL) {
130 $this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
133 // Determine the visibility of subject form field.
134 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
135 // Subject input allowed.
136 $edit['subject[0][value]'] = $subject;
139 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
142 if ($contact !== NULL && is_array($contact)) {
145 switch ($preview_mode) {
146 case DRUPAL_REQUIRED:
147 // Preview required so no save button should be found.
148 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
149 $this->drupalPostForm(NULL, $edit, t('Preview'));
150 // Don't break here so that we can test post-preview field presence and
152 case DRUPAL_OPTIONAL:
153 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
154 $this->assertFieldByName('op', t('Save'), 'Save button found.');
155 $this->drupalPostForm(NULL, $edit, t('Save'));
158 case DRUPAL_DISABLED:
159 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
160 $this->assertFieldByName('op', t('Save'), 'Save button found.');
161 $this->drupalPostForm(NULL, $edit, t('Save'));
166 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
169 if ($contact !== TRUE) {
170 // If true then attempting to find error message.
172 $this->assertText($subject, 'Comment subject posted.');
174 $this->assertText($comment, 'Comment body posted.');
175 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
178 if (isset($match[1])) {
179 \Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]);
180 return Comment::load($match[1]);
185 * Checks current page for specified comment.
187 * @param \Drupal\comment\CommentInterface $comment
188 * The comment object.
190 * Boolean indicating whether the comment is a reply to another comment.
193 * Boolean indicating whether the comment was found.
195 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
197 $comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . '#comment-' . $comment->id() . ' ~ article');
198 if (empty($comment_element)) {
202 $comment_title = $comment_element[0]->xpath('div/h3/a');
203 if (empty($comment_title) || ((string) $comment_title[0]) !== $comment->getSubject()) {
207 $comment_body = $comment_element[0]->xpath('div/div/p');
208 if (empty($comment_body) || ((string) $comment_body[0]) !== $comment->comment_body->value) {
222 * @param \Drupal\comment\CommentInterface $comment
225 public function deleteComment(CommentInterface $comment) {
226 $this->drupalPostForm('comment/' . $comment->id() . '/delete', [], t('Delete'));
227 $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
231 * Sets the value governing whether the subject field should be enabled.
233 * @param bool $enabled
234 * Boolean specifying whether the subject field should be enabled.
236 public function setCommentSubject($enabled) {
237 $form_display = entity_get_form_display('comment', 'comment', 'default');
239 $form_display->setComponent('subject', [
240 'type' => 'string_textfield',
244 $form_display->removeComponent('subject');
246 $form_display->save();
247 // Display status message.
248 $this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
252 * Sets the value governing the previewing mode for the comment form.
255 * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED.
256 * @param string $field_name
257 * (optional) Field name through which the comment should be posted.
258 * Defaults to 'comment'.
260 public function setCommentPreview($mode, $field_name = 'comment') {
262 case DRUPAL_DISABLED:
263 $mode_text = 'disabled';
266 case DRUPAL_OPTIONAL:
267 $mode_text = 'optional';
270 case DRUPAL_REQUIRED:
271 $mode_text = 'required';
274 $this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name);
278 * Sets the value governing whether the comment form is on its own page.
280 * @param bool $enabled
281 * TRUE if the comment form should be displayed on the same page as the
282 * comments; FALSE if it should be displayed on its own page.
283 * @param string $field_name
284 * (optional) Field name through which the comment should be posted.
285 * Defaults to 'comment'.
287 public function setCommentForm($enabled, $field_name = 'comment') {
288 $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
292 * Sets the value governing restrictions on anonymous comments.
295 * The level of the contact information allowed for anonymous comments:
296 * - 0: No contact information allowed.
297 * - 1: Contact information allowed but not required.
298 * - 2: Contact information required.
300 public function setCommentAnonymous($level) {
301 $this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', ['@level' => $level]));
305 * Sets the value specifying the default number of comments per page.
308 * Comments per page value.
309 * @param string $field_name
310 * (optional) Field name through which the comment should be posted.
311 * Defaults to 'comment'.
313 public function setCommentsPerPage($number, $field_name = 'comment') {
314 $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', ['@number' => $number]), $field_name);
318 * Sets a comment settings variable for the article content type.
320 * @param string $name
322 * @param string $value
324 * @param string $message
325 * Status message to display.
326 * @param string $field_name
327 * (optional) Field name through which the comment should be posted.
328 * Defaults to 'comment'.
330 public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
331 $field = FieldConfig::loadByName('node', 'article', $field_name);
332 $field->setSetting($name, $value);
334 // Display status message.
335 $this->pass($message);
339 * Checks whether the commenter's contact information is displayed.
342 * Contact info is available.
344 public function commentContactInfoAvailable() {
345 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
349 * Performs the specified operation on the specified comment.
351 * @param \Drupal\comment\CommentInterface $comment
352 * Comment to perform operation on.
353 * @param string $operation
354 * Operation to perform.
355 * @param bool $approval
356 * Operation is found on approval page.
358 public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
360 $edit['operation'] = $operation;
361 $edit['comments[' . $comment->id() . ']'] = TRUE;
362 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
364 if ($operation == 'delete') {
365 $this->drupalPostForm(NULL, [], t('Delete'));
366 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
369 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
374 * Gets the comment ID for an unapproved comment.
376 * @param string $subject
377 * Comment subject to find.
382 public function getUnapprovedComment($subject) {
383 $this->drupalGet('admin/content/comment/approval');
384 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
390 * Creates a comment comment type (bundle).
392 * @param string $label
393 * The comment type label.
395 * @return \Drupal\comment\Entity\CommentType
396 * Created comment type.
398 protected function createCommentType($label) {
399 $bundle = CommentType::create([
403 'target_entity_type_id' => 'node',