annotate core/modules/comment/src/Tests/CommentTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\comment\Tests;
Chris@0 4
Chris@0 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);
Chris@0 6
Chris@0 7 use Drupal\comment\Entity\CommentType;
Chris@0 8 use Drupal\comment\Entity\Comment;
Chris@0 9 use Drupal\comment\CommentInterface;
Chris@0 10 use Drupal\field\Entity\FieldConfig;
Chris@0 11 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
Chris@0 12 use Drupal\node\Entity\NodeType;
Chris@0 13 use Drupal\simpletest\WebTestBase;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Provides setup and helper methods for comment tests.
Chris@0 17 *
Chris@0 18 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
Chris@0 19 * Use \Drupal\Tests\comment\Functional\CommentTestBase instead.
Chris@0 20 *
Chris@0 21 * @see https://www.drupal.org/node/2908490
Chris@0 22 */
Chris@0 23 abstract class CommentTestBase extends WebTestBase {
Chris@0 24
Chris@0 25 use CommentTestTrait;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Modules to install.
Chris@0 29 *
Chris@0 30 * @var array
Chris@0 31 */
Chris@0 32 public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime'];
Chris@0 33
Chris@0 34 /**
Chris@0 35 * An administrative user with permission to configure comment settings.
Chris@0 36 *
Chris@0 37 * @var \Drupal\user\UserInterface
Chris@0 38 */
Chris@0 39 protected $adminUser;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * A normal user with permission to post comments.
Chris@0 43 *
Chris@0 44 * @var \Drupal\user\UserInterface
Chris@0 45 */
Chris@0 46 protected $webUser;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * A test node to which comments will be posted.
Chris@0 50 *
Chris@0 51 * @var \Drupal\node\NodeInterface
Chris@0 52 */
Chris@0 53 protected $node;
Chris@0 54
Chris@0 55 protected function setUp() {
Chris@0 56 parent::setUp();
Chris@0 57
Chris@0 58 // Create an article content type only if it does not yet exist, so that
Chris@0 59 // child classes may specify the standard profile.
Chris@0 60 $types = NodeType::loadMultiple();
Chris@0 61 if (empty($types['article'])) {
Chris@0 62 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
Chris@0 63 }
Chris@0 64
Chris@0 65 // Create two test users.
Chris@0 66 $this->adminUser = $this->drupalCreateUser([
Chris@0 67 'administer content types',
Chris@0 68 'administer comments',
Chris@0 69 'administer comment types',
Chris@0 70 'administer comment fields',
Chris@0 71 'administer comment display',
Chris@0 72 'skip comment approval',
Chris@0 73 'post comments',
Chris@0 74 'access comments',
Chris@0 75 // Usernames aren't shown in comment edit form autocomplete unless this
Chris@0 76 // permission is granted.
Chris@0 77 'access user profiles',
Chris@0 78 'access content',
Chris@0 79 ]);
Chris@0 80 $this->webUser = $this->drupalCreateUser([
Chris@0 81 'access comments',
Chris@0 82 'post comments',
Chris@0 83 'create article content',
Chris@0 84 'edit own comments',
Chris@0 85 'skip comment approval',
Chris@0 86 'access content',
Chris@0 87 ]);
Chris@0 88
Chris@0 89 // Create comment field on article.
Chris@0 90 $this->addDefaultCommentField('node', 'article');
Chris@0 91
Chris@0 92 // Create a test node authored by the web user.
Chris@0 93 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
Chris@0 94 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Posts a comment.
Chris@0 99 *
Chris@0 100 * @param \Drupal\Core\Entity\EntityInterface|null $entity
Chris@0 101 * Node to post comment on or NULL to post to the previously loaded page.
Chris@0 102 * @param string $comment
Chris@0 103 * Comment body.
Chris@0 104 * @param string $subject
Chris@0 105 * Comment subject.
Chris@0 106 * @param string $contact
Chris@0 107 * Set to NULL for no contact info, TRUE to ignore success checking, and
Chris@0 108 * array of values to set contact info.
Chris@0 109 * @param string $field_name
Chris@0 110 * (optional) Field name through which the comment should be posted.
Chris@0 111 * Defaults to 'comment'.
Chris@0 112 *
Chris@0 113 * @return \Drupal\comment\CommentInterface|null
Chris@0 114 * The posted comment or NULL when posted comment was not found.
Chris@0 115 */
Chris@0 116 public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
Chris@0 117 $edit = [];
Chris@0 118 $edit['comment_body[0][value]'] = $comment;
Chris@0 119
Chris@0 120 if ($entity !== NULL) {
Chris@0 121 $field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
Chris@0 122 }
Chris@0 123 else {
Chris@0 124 $field = FieldConfig::loadByName('node', 'article', $field_name);
Chris@0 125 }
Chris@0 126 $preview_mode = $field->getSetting('preview');
Chris@0 127
Chris@0 128 // Must get the page before we test for fields.
Chris@0 129 if ($entity !== NULL) {
Chris@0 130 $this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
Chris@0 131 }
Chris@0 132
Chris@0 133 // Determine the visibility of subject form field.
Chris@0 134 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
Chris@0 135 // Subject input allowed.
Chris@0 136 $edit['subject[0][value]'] = $subject;
Chris@0 137 }
Chris@0 138 else {
Chris@0 139 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
Chris@0 140 }
Chris@0 141
Chris@0 142 if ($contact !== NULL && is_array($contact)) {
Chris@0 143 $edit += $contact;
Chris@0 144 }
Chris@0 145 switch ($preview_mode) {
Chris@0 146 case DRUPAL_REQUIRED:
Chris@0 147 // Preview required so no save button should be found.
Chris@0 148 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
Chris@0 149 $this->drupalPostForm(NULL, $edit, t('Preview'));
Chris@0 150 // Don't break here so that we can test post-preview field presence and
Chris@0 151 // function below.
Chris@0 152 case DRUPAL_OPTIONAL:
Chris@0 153 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
Chris@0 154 $this->assertFieldByName('op', t('Save'), 'Save button found.');
Chris@0 155 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 156 break;
Chris@0 157
Chris@0 158 case DRUPAL_DISABLED:
Chris@0 159 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
Chris@0 160 $this->assertFieldByName('op', t('Save'), 'Save button found.');
Chris@0 161 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 162 break;
Chris@0 163 }
Chris@0 164 $match = [];
Chris@0 165 // Get comment ID
Chris@0 166 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
Chris@0 167
Chris@0 168 // Get comment.
Chris@0 169 if ($contact !== TRUE) {
Chris@0 170 // If true then attempting to find error message.
Chris@0 171 if ($subject) {
Chris@0 172 $this->assertText($subject, 'Comment subject posted.');
Chris@0 173 }
Chris@0 174 $this->assertText($comment, 'Comment body posted.');
Chris@0 175 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
Chris@0 176 }
Chris@0 177
Chris@0 178 if (isset($match[1])) {
Chris@0 179 \Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]);
Chris@0 180 return Comment::load($match[1]);
Chris@0 181 }
Chris@0 182 }
Chris@0 183
Chris@0 184 /**
Chris@0 185 * Checks current page for specified comment.
Chris@0 186 *
Chris@0 187 * @param \Drupal\comment\CommentInterface $comment
Chris@0 188 * The comment object.
Chris@0 189 * @param bool $reply
Chris@0 190 * Boolean indicating whether the comment is a reply to another comment.
Chris@0 191 *
Chris@0 192 * @return bool
Chris@0 193 * Boolean indicating whether the comment was found.
Chris@0 194 */
Chris@0 195 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
Chris@0 196 if ($comment) {
Chris@18 197 $comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . 'article#comment-' . $comment->id());
Chris@0 198 if (empty($comment_element)) {
Chris@0 199 return FALSE;
Chris@0 200 }
Chris@0 201
Chris@0 202 $comment_title = $comment_element[0]->xpath('div/h3/a');
Chris@0 203 if (empty($comment_title) || ((string) $comment_title[0]) !== $comment->getSubject()) {
Chris@0 204 return FALSE;
Chris@0 205 }
Chris@0 206
Chris@0 207 $comment_body = $comment_element[0]->xpath('div/div/p');
Chris@0 208 if (empty($comment_body) || ((string) $comment_body[0]) !== $comment->comment_body->value) {
Chris@0 209 return FALSE;
Chris@0 210 }
Chris@0 211
Chris@0 212 return TRUE;
Chris@0 213 }
Chris@0 214 else {
Chris@0 215 return FALSE;
Chris@0 216 }
Chris@0 217 }
Chris@0 218
Chris@0 219 /**
Chris@0 220 * Deletes a comment.
Chris@0 221 *
Chris@0 222 * @param \Drupal\comment\CommentInterface $comment
Chris@0 223 * Comment to delete.
Chris@0 224 */
Chris@0 225 public function deleteComment(CommentInterface $comment) {
Chris@0 226 $this->drupalPostForm('comment/' . $comment->id() . '/delete', [], t('Delete'));
Chris@0 227 $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
Chris@0 228 }
Chris@0 229
Chris@0 230 /**
Chris@0 231 * Sets the value governing whether the subject field should be enabled.
Chris@0 232 *
Chris@0 233 * @param bool $enabled
Chris@0 234 * Boolean specifying whether the subject field should be enabled.
Chris@0 235 */
Chris@0 236 public function setCommentSubject($enabled) {
Chris@0 237 $form_display = entity_get_form_display('comment', 'comment', 'default');
Chris@0 238 if ($enabled) {
Chris@0 239 $form_display->setComponent('subject', [
Chris@0 240 'type' => 'string_textfield',
Chris@0 241 ]);
Chris@0 242 }
Chris@0 243 else {
Chris@0 244 $form_display->removeComponent('subject');
Chris@0 245 }
Chris@0 246 $form_display->save();
Chris@0 247 // Display status message.
Chris@0 248 $this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
Chris@0 249 }
Chris@0 250
Chris@0 251 /**
Chris@0 252 * Sets the value governing the previewing mode for the comment form.
Chris@0 253 *
Chris@0 254 * @param int $mode
Chris@0 255 * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED.
Chris@0 256 * @param string $field_name
Chris@0 257 * (optional) Field name through which the comment should be posted.
Chris@0 258 * Defaults to 'comment'.
Chris@0 259 */
Chris@0 260 public function setCommentPreview($mode, $field_name = 'comment') {
Chris@0 261 switch ($mode) {
Chris@0 262 case DRUPAL_DISABLED:
Chris@0 263 $mode_text = 'disabled';
Chris@0 264 break;
Chris@0 265
Chris@0 266 case DRUPAL_OPTIONAL:
Chris@0 267 $mode_text = 'optional';
Chris@0 268 break;
Chris@0 269
Chris@0 270 case DRUPAL_REQUIRED:
Chris@0 271 $mode_text = 'required';
Chris@0 272 break;
Chris@0 273 }
Chris@0 274 $this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name);
Chris@0 275 }
Chris@0 276
Chris@0 277 /**
Chris@0 278 * Sets the value governing whether the comment form is on its own page.
Chris@0 279 *
Chris@0 280 * @param bool $enabled
Chris@0 281 * TRUE if the comment form should be displayed on the same page as the
Chris@0 282 * comments; FALSE if it should be displayed on its own page.
Chris@0 283 * @param string $field_name
Chris@0 284 * (optional) Field name through which the comment should be posted.
Chris@0 285 * Defaults to 'comment'.
Chris@0 286 */
Chris@0 287 public function setCommentForm($enabled, $field_name = 'comment') {
Chris@0 288 $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
Chris@0 289 }
Chris@0 290
Chris@0 291 /**
Chris@0 292 * Sets the value governing restrictions on anonymous comments.
Chris@0 293 *
Chris@0 294 * @param int $level
Chris@0 295 * The level of the contact information allowed for anonymous comments:
Chris@0 296 * - 0: No contact information allowed.
Chris@0 297 * - 1: Contact information allowed but not required.
Chris@0 298 * - 2: Contact information required.
Chris@0 299 */
Chris@0 300 public function setCommentAnonymous($level) {
Chris@0 301 $this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', ['@level' => $level]));
Chris@0 302 }
Chris@0 303
Chris@0 304 /**
Chris@0 305 * Sets the value specifying the default number of comments per page.
Chris@0 306 *
Chris@0 307 * @param int $number
Chris@0 308 * Comments per page value.
Chris@0 309 * @param string $field_name
Chris@0 310 * (optional) Field name through which the comment should be posted.
Chris@0 311 * Defaults to 'comment'.
Chris@0 312 */
Chris@0 313 public function setCommentsPerPage($number, $field_name = 'comment') {
Chris@0 314 $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', ['@number' => $number]), $field_name);
Chris@0 315 }
Chris@0 316
Chris@0 317 /**
Chris@0 318 * Sets a comment settings variable for the article content type.
Chris@0 319 *
Chris@0 320 * @param string $name
Chris@0 321 * Name of variable.
Chris@0 322 * @param string $value
Chris@0 323 * Value of variable.
Chris@0 324 * @param string $message
Chris@0 325 * Status message to display.
Chris@0 326 * @param string $field_name
Chris@0 327 * (optional) Field name through which the comment should be posted.
Chris@0 328 * Defaults to 'comment'.
Chris@0 329 */
Chris@0 330 public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
Chris@0 331 $field = FieldConfig::loadByName('node', 'article', $field_name);
Chris@0 332 $field->setSetting($name, $value);
Chris@0 333 $field->save();
Chris@0 334 // Display status message.
Chris@0 335 $this->pass($message);
Chris@0 336 }
Chris@0 337
Chris@0 338 /**
Chris@0 339 * Checks whether the commenter's contact information is displayed.
Chris@0 340 *
Chris@0 341 * @return bool
Chris@0 342 * Contact info is available.
Chris@0 343 */
Chris@0 344 public function commentContactInfoAvailable() {
Chris@0 345 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
Chris@0 346 }
Chris@0 347
Chris@0 348 /**
Chris@0 349 * Performs the specified operation on the specified comment.
Chris@0 350 *
Chris@0 351 * @param \Drupal\comment\CommentInterface $comment
Chris@0 352 * Comment to perform operation on.
Chris@0 353 * @param string $operation
Chris@0 354 * Operation to perform.
Chris@0 355 * @param bool $approval
Chris@0 356 * Operation is found on approval page.
Chris@0 357 */
Chris@0 358 public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
Chris@0 359 $edit = [];
Chris@0 360 $edit['operation'] = $operation;
Chris@0 361 $edit['comments[' . $comment->id() . ']'] = TRUE;
Chris@0 362 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
Chris@0 363
Chris@0 364 if ($operation == 'delete') {
Chris@0 365 $this->drupalPostForm(NULL, [], t('Delete'));
Chris@0 366 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
Chris@0 367 }
Chris@0 368 else {
Chris@0 369 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
Chris@0 370 }
Chris@0 371 }
Chris@0 372
Chris@0 373 /**
Chris@0 374 * Gets the comment ID for an unapproved comment.
Chris@0 375 *
Chris@0 376 * @param string $subject
Chris@0 377 * Comment subject to find.
Chris@0 378 *
Chris@0 379 * @return int
Chris@0 380 * Comment id.
Chris@0 381 */
Chris@0 382 public function getUnapprovedComment($subject) {
Chris@0 383 $this->drupalGet('admin/content/comment/approval');
Chris@0 384 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
Chris@0 385
Chris@0 386 return $match[2];
Chris@0 387 }
Chris@0 388
Chris@0 389 /**
Chris@0 390 * Creates a comment comment type (bundle).
Chris@0 391 *
Chris@0 392 * @param string $label
Chris@0 393 * The comment type label.
Chris@0 394 *
Chris@0 395 * @return \Drupal\comment\Entity\CommentType
Chris@0 396 * Created comment type.
Chris@0 397 */
Chris@0 398 protected function createCommentType($label) {
Chris@0 399 $bundle = CommentType::create([
Chris@0 400 'id' => $label,
Chris@0 401 'label' => $label,
Chris@0 402 'description' => '',
Chris@0 403 'target_entity_type_id' => 'node',
Chris@0 404 ]);
Chris@0 405 $bundle->save();
Chris@0 406 return $bundle;
Chris@0 407 }
Chris@0 408
Chris@0 409 }