commentExists example

$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->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page.');

    // Set comments to one per page so that we are able to test paging without     // needing to insert large numbers of comments.     $this->setCommentsPerPage(1);

    // Check the first page of the node, and confirm the correct comments are     // shown.     $this->drupalGet('node/' . $node->id());
    $this->assertSession()->pageTextContains('next');
    
$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',
      'field_name' => 'comment',
    ]);
    $this->drupalLogout();

    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');

    // Approve comment.     $this->drupalLogin($this->adminUser);
    $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
    $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);
    
$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.');

    // Allow contact info.


  /** * 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.     $this->drupalLogout();
    
$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',
      'field_name' => 'comment',
    ]);
    $this->drupalLogout();

    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');

    // Approve comment.     $this->drupalLogin($this->adminUser);
    $edit = [];
    $edit['action'] = 'comment_publish_action';
    $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->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();
    $reply = $this->postComment(NULL, $reply_text$reply_subject, TRUE);
    
// 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);

    // Reply to comment #1 creating comment #1_3.
$this->assertSession()->fieldNotExists('cardinality');

    $this->drupalLogin($this->adminUser);

    // Test breadcrumb on comment add page.     $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
    $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $this->entity->label());

    // Post a comment.     /** @var \Drupal\comment\CommentInterface $comment1 */
    $comment1 = $this->postComment($this->entity, $this->randomMachineName()$this->randomMachineName());
    $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');

    // Test breadcrumb on comment reply page.     $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
    $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());

    // Test breadcrumb on comment edit page.     $this->drupalGet('comment/' . $comment1->id() . '/edit');
    $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());

    // Test breadcrumb on comment delete page.     $this->drupalGet('comment/' . $comment1->id() . '/delete');
    
// Post comment #1 and verify that title is rendered in h3.     $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();
    $comment1 = $this->postComment($this->node, $comment_text$subject_text, TRUE);

    // The entity fields for name and mail have no meaning if the user is not     // Anonymous.     $this->assertNull($comment1->name->value);
    $this->assertNull($comment1->mail->value);

    // Confirm that the comment was created.     $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
    // Tests that markup is created for comment with heading.     $this->assertSession()->responseMatches('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|');
    // Tests that the comment's title link is the permalink of the comment.     $comment_permalink = $this->cssSelect('.permalink');
    $comment_permalink = $comment_permalink[0]->getAttribute('href');
    // Tests that the comment's title link contains the URL fragment.     $this->assertStringContainsString('#comment-' . $comment1->id()$comment_permalink, "The comment's title link contains the url fragment.");
    $this->assertEquals($comment1->permalink()->toString()$comment_permalink, "The comment's title has the correct link.");
  }

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