postComment example

/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests comment label in admin view. */
  public function testCommentEdit() {
    $this->drupalLogin($this->adminUser);
    // Post a comment to node.     $node_comment = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName(), TRUE);
    $this->drupalGet('admin/content/comment');
    $this->assertSession()->pageTextContains($this->adminUser->label());
    $this->drupalGet($node_comment->toUrl('edit-form'));
    $edit = [
      'comment_body[0][value]' => $this->randomMachineName(),
    ];
    $this->submitForm($edit, 'Save');
    $this->drupalGet('admin/content/comment');
    $this->assertSession()->pageTextContains($this->adminUser->label());
  }

}
// Test that the comments page loads correctly when there are no comments     $this->drupalGet('admin/content/comment');
    $this->assertSession()->pageTextContains('No comments available.');

    $this->drupalLogout();

    // Post anonymous comment without contact info.     $subject = $this->randomMachineName();
    $body = $this->randomMachineName();
    // Set $contact to true so that it won't check for id and message.     $this->postComment($this->node, $body$subject, TRUE);
    $this->assertSession()->pageTextContains('Your comment has been queued for review by site administrators and will be published after approval.');

    // Get unapproved comment id.     $this->drupalLogin($this->adminUser);
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
    $anonymous_comment4 = Comment::create([
      'cid' => $anonymous_comment4,
      'subject' => $subject,
      'comment_body' => $body,
      'entity_id' => $this->node->id(),
      'entity_type' => 'node',
      
'skip comment approval',
      'post comments',
      'access comments',
      'access content',
      'administer nodes',
      'administer comments',
      'bypass node access',
    ]);
    $this->drupalLogin($administratorUser);

    $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
    $this->comment = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName());
    $this->assertInstanceOf(Comment::class$this->comment);

    $this->node2 = $this->drupalCreateNode([
      'title' => $this->randomMachineName(),
      'type' => 'article',
    ]);
  }

  /** * Tests that comments are correctly saved as entity references. */
  
// Set comment variables.     $this->setCommentSubject(TRUE);

    // To test hostname token field should be populated.     \Drupal::configFactory()
      ->getEditable('comment.settings')
      ->set('log_ip_addresses', TRUE)
      ->save(TRUE);

    // Create a node and a comment.     $node = $this->drupalCreateNode(['type' => 'article', 'title' => '<script>alert("123")</script>']);
    $parent_comment = $this->postComment($node$this->randomMachineName()$this->randomMachineName(), TRUE);

    // Post a reply to the comment.     $this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
    $child_comment = $this->postComment(NULL, $this->randomMachineName()$this->randomMachineName());
    $comment = Comment::load($child_comment->id());
    $comment->setHomepage('http://example.org/');

    // Add HTML to ensure that sanitation of some fields tested directly.     $comment->setSubject('<blink>Blinking Comment</blink>');

    // Generate and test tokens.
$edit = [];
    $date = new DrupalDateTime('2008-03-02 17:23');
    $edit['subject[0][value]'] = $this->randomMachineName(8);
    $edit['comment_body[0][value]'] = $this->randomMachineName(16);
    $edit['uid'] = $web_user->getAccountName() . ' (' . $web_user->id() . ')';
    $edit['date[date]'] = $date->format('Y-m-d');
    $edit['date[time]'] = $date->format('H:i:s');
    $raw_date = $date->getTimestamp();
    $expected_text_date = $this->container->get('date.formatter')->formatInterval(\Drupal::time()->getRequestTime() - $raw_date);
    $expected_form_date = $date->format('Y-m-d');
    $expected_form_time = $date->format('H:i:s');
    $comment = $this->postComment($this->node, $edit['subject[0][value]']$edit['comment_body[0][value]'], TRUE);
    $this->drupalGet('comment/' . $comment->id() . '/edit');
    $this->submitForm($edit, 'Preview');

    // Check that the preview is displaying the subject, comment, author and date correctly.     $this->assertSession()->titleEquals('Preview comment | Drupal');
    $this->assertSession()->pageTextContains($edit['subject[0][value]']);
    $this->assertSession()->pageTextContains($edit['comment_body[0][value]']);
    $this->assertSession()->pageTextContains($web_user->getAccountName());
    $this->assertSession()->pageTextContains($expected_text_date);

    // Check that the subject, comment, author and date fields are displayed with the correct values.
// Checks the initial values of node comment statistics with no comment.     $node = $node_storage->load($this->node->id());
    $this->assertEquals($this->node->getCreatedTime()$node->get('comment')->last_comment_timestamp, 'The initial value of node last_comment_timestamp is the node created date.');
    $this->assertNull($node->get('comment')->last_comment_name, 'The initial value of node last_comment_name is NULL.');
    $this->assertEquals($this->webUser->id()$node->get('comment')->last_comment_uid, 'The initial value of node last_comment_uid is the node uid.');
    $this->assertEquals(0, $node->get('comment')->comment_count, 'The initial value of node comment_count is zero.');

    // Post comment #1 as web_user2.     $this->drupalLogin($this->webUser2);
    $comment_text = $this->randomMachineName();
    $this->postComment($this->node, $comment_text);

    // Checks the new values of node comment statistics with comment #1.     // The node cache needs to be reset before reload.     $node_storage->resetCache([$this->node->id()]);
    $node = $node_storage->load($this->node->id());
    $this->assertSame('', $node->get('comment')->last_comment_name, 'The value of node last_comment_name should be an empty string.');
    $this->assertEquals($this->webUser2->id()$node->get('comment')->last_comment_uid, 'The value of node last_comment_uid is the comment #1 uid.');
    $this->assertEquals(1, $node->get('comment')->comment_count, 'The value of node comment_count is 1.');

    // Prepare for anonymous comment submission (comment approval enabled).     // Note we don't use user_role_change_permissions(), because that caused
$edit['comment_bulk_form[0]'] = $anonymous_comment4->id();
    $this->drupalGet('admin/content/comment/approval');
    $this->submitForm($edit, 'Apply to selected items');

    $this->assertSession()->pageTextContains('Publish comment was applied to 1 item.');
    $this->drupalLogout();

    $this->drupalGet('node/' . $this->node->id());
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');

    // Post 2 anonymous comments without contact info.     $comments[] = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName(), TRUE);

    // Publish multiple comments in one operation.     $this->drupalLogin($this->adminUser);
    $this->drupalGet('admin/content/comment/approval');
    $this->assertSession()->pageTextContains('Unapproved comments (2)');

    // Assert the expose filters on the admin page.     $this->assertSession()->fieldExists('subject');
    $this->assertSession()->fieldExists('author_name');
    $this->assertSession()->fieldExists('langcode');

    
$this->drupalLogout();

    // Create a node.     $this->drupalLogin($this->webUser);
    $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);

    // Post comment #1.     $this->drupalLogin($this->webUser);
    $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();

    $comment1 = $this->postComment($this->node, $comment_text$subject_text, TRUE);
    // Confirm that the comment was created and has the correct threading.     $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
    $this->assertEquals('01/', $comment1->getThread());
    // Confirm that there is no reference to a parent comment.     $this->assertNoParentLink($comment1->id());

    // Post comment #2 following the comment #1 to test if it correctly jumps     // out the indentation in case there is a thread above.     $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();
    $this->postComment($this->node, $comment_text$subject_text, TRUE);

    
/** * {@inheritdoc} */
  protected $defaultTheme = 'stark';

  /** * Tests that comments are deleted with the node. */
  public function testNodeDeletion() {
    $this->drupalLogin($this->webUser);
    $comment = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName());
    $this->assertInstanceOf(Comment::class$comment);
    $this->node->delete();
    $this->assertNull(Comment::load($comment->id()), 'The comment could not be loaded after the node was deleted.');
    // Make sure the comment field storage and all its fields are deleted when     // the node type is deleted.     $this->assertNotNull(FieldStorageConfig::load('node.comment'), 'Comment field storage exists');
    $this->assertNotNull(FieldConfig::load('node.article.comment'), 'Comment field exists');
    // Delete the node type.     $this->node->get('type')->entity->delete();
    $this->assertNull(FieldStorageConfig::load('node.comment'), 'Comment field storage deleted');
    $this->assertNull(FieldConfig::load('node.article.comment'), 'Comment field deleted');
  }
public function testCommentPaging() {
    $this->drupalLogin($this->adminUser);

    // Set comment variables.     $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
    $this->setCommentPreview(DRUPAL_DISABLED);

    // Create a node and three comments.     $node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1]);
    $comments = [];
    $comments[] = $this->postComment($node$this->randomMachineName()$this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($node$this->randomMachineName()$this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($node$this->randomMachineName()$this->randomMachineName(), TRUE);

    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');

    // Set "Comments per page" as zero and verify that all comments are appearing     // on the page.     $this->setCommentsPerPage(0);
    $this->drupalGet('node/' . $node->id());
    $this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page.');
    $this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page.');
    
    $this->drupalLogin($this->adminUser);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
    $this->drupalLogout();

    // Post comment.     $this->drupalLogin($this->webUser);
    $comment_text = $this->randomMachineName();
    $comment_subject = $this->randomMachineName();
    $comment = $this->postComment($this->node, $comment_text$comment_subject);
    $this->assertTrue($this->commentExists($comment), 'Comment found.');

    // Check comment display.     $this->drupalGet('node/' . $this->node->id());
    $this->assertSession()->pageTextContains($comment_subject);
    $this->assertSession()->pageTextContains($comment_text);

    // Reply to comment, creating second comment.     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
    $reply_text = $this->randomMachineName();
    $reply_subject = $this->randomMachineName();
    
$this->drupalLogout();
  }

  /** * Tests the comment interface. */
  public function testCommentInterface() {

    // Post comment #1 without subject or preview.     $this->drupalLogin($this->webUser);
    $comment_text = $this->randomMachineName();
    $comment = $this->postComment($this->node, $comment_text);
    $this->assertTrue($this->commentExists($comment), 'Comment found.');

    // Test that using an invalid entity-type does not raise an error.     $this->drupalGet('comment/reply/yeah-this-is-not-an-entity-type/' . $this->node->id() . '/comment/' . $comment->id());
    $this->assertSession()->statusCodeEquals(404);

    // Test the comment field title is displayed when there's comments.     $this->drupalGet($this->node->toUrl());
    $this->assertSession()->responseMatches('@<h2[^>]*>Comments</h2>@');

    // Set comments to have subject and preview to required.

  public function testRecentCommentBlock() {
    $this->drupalLogin($this->adminUser);
    $this->drupalPlaceBlock('views_block:comments_recent-block_1');

    // Add some test comments, with and without subjects. Because the 10 newest     // comments should be shown by the block, we create 11 to test that behavior     // below.     $timestamp = REQUEST_TIME;
    for ($i = 0; $i < 11; ++$i) {
      $subject = ($i % 2) ? $this->randomMachineName() : '';
      $comments[$i] = $this->postComment($this->node, $this->randomMachineName()$subject);
      $comments[$i]->created->value = $timestamp--;
      $comments[$i]->save();
    }

    // Test that a user without the 'access comments' permission cannot see the     // block.     $this->drupalLogout();
    user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
    $this->drupalGet('');
    $this->assertSession()->pageTextNotContains('Recent comments');
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);

    
/** * Tests markup for comments with empty titles. */
  public function testCommentEmptyTitles() {
    // Create a node.     $this->drupalLogin($this->webUser);
    $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);

    // Post comment #1 and verify that h3 is rendered.     $subject_text = "Test subject";
    $comment_text = "Test comment";
    $this->postComment($this->node, $comment_text$subject_text, TRUE);
    // Tests that markup is generated for the comment title.     $regex_h3 = '|<h3[^>]*>.*?</h3>|';
    $this->assertSession()->responseMatches($regex_h3);

    // Installs module that sets comment title to an empty string.     \Drupal::service('module_installer')->install(['comment_empty_title_test']);

    // Set comments to have a subject with preview disabled.     $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);

    
$edit['subject[0][value]'] = $title;
    $edit['comment_body[0][value]'] = $body;
    $this->drupalGet($this->node->toUrl());
    $this->submitForm($edit, 'Preview');
    // Cannot use assertRaw here since both title and body are in the form.     $preview = (string) $this->cssSelect('[data-drupal-selector="edit-comment-preview"]')[0]->getHtml();
    $this->assertStringContainsString($title$preview, 'Anonymous user can preview comment title.');
    $this->assertStringContainsString($body$preview, 'Anonymous user can preview comment body.');
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);

    // Post anonymous comment without contact info.     $anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName()$this->randomMachineName());
    $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');

    // Ensure anonymous users cannot post in the name of registered users.     $edit = [
      'name' => $this->adminUser->getAccountName(),
      'comment_body[0][value]' => $this->randomMachineName(),
    ];
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
    $this->submitForm($edit, 'Save');
    $this->assertSession()->pageTextContains('The name you used (' . $this->adminUser->getAccountName() . ') belongs to a registered user.');

    
Home | Imprint | This part of the site doesn't use cookies.