You are here

private function ForumAccessTestCase::testForumAccessCreateComment in Forum Access 6

This function test if the user can create a comment Three steps: is there a create link, does the create comment page opens and can the new comment be saved

1 call to ForumAccessTestCase::testForumAccessCreateComment()
ForumAccessTestCase::testForumAccessRun in ./forum_access.test
The main function which is used to start testing a specific forum configuration.

File

./forum_access.test, line 831
Test file for forum_access.module.

Class

ForumAccessTestCase
This is the base class for forum access testing.

Code

private function testForumAccessCreateComment($topic_id = 0) {
  if (intval($topic_id) > 0) {
    $allowed = $this
      ->testForumAccessAllowed('comment_create');

    // Check to see if the reply link is on the view page
    $this
      ->drupalGet('node/' . $topic_id);
    if ($allowed) {
      if (!$this
        ->assertPattern("/comment\\/reply\\/{$topic_id}/", t('For @user there should be a reply link on this page for a new comment: @page', array(
        '@user' => $this->loggedInUser ? t('this user') : t('anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
      }
    }
    else {
      if (!$this
        ->assertNoPattern("/comment\\/reply\\/{$topic_id}/", t('For @user there should NOT be any reply link on this page for a new comment: @page', array(
        '@user' => $this->loggedInUser ? t('this user') : t('anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
      }
    }

    // Check to see if the user/anonymous is allowed to open the reply page
    $page_opened = FALSE;
    $this
      ->drupalGet('comment/reply/' . $topic_id);
    if ($allowed) {
      if (!$this
        ->assertNoText("not authorized", t('@user should be allowed to open the reply page for this topic: @page', array(
        '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
      }
      else {
        $page_opened = TRUE;
      }
    }
    else {
      if (!$this
        ->assertText("not authorized", t('@user should NOT be allowed to open the reply page for this topic: @page', array(
        '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
        $page_opened = TRUE;
      }
    }
    if ($page_opened) {

      // Check to see if the user/anonymous is allowed to create a new comment
      $comment_options = array(
        'subject' => 'Comment subject: ' . $this
          ->randomName(32),
        'comment' => 'Comment comment: ' . $this
          ->randomName(64),
      );
      $this
        ->drupalPost('comment/reply/' . $topic_id, $comment_options, t('Preview'));
      $html = $this
        ->drupalPost(NULL, $comment_options, t('Save'));
      if ($allowed) {
        if ($this
          ->assertNoText("not authorized", t('@user should be allowed to reply on this topic: @page', array(
          '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
          '@page' => $this
            ->getUrl(),
        )))) {
          preg_match('/node\\/(?<topic_id>\\d+)#comment-(?<comment_id>\\d+)/', $this->url, $matches);
          return array_key_exists('comment_id', $matches) ? intval($matches['comment_id']) : 0;
        }
        else {
          $this
            ->testForumAccessDumpDebugInfo('topic', $topic_id);
        }
      }
      else {
        if (!$this
          ->assertText("not authorized", t('@user should NOT be allowed to reply on this topic: @page', array(
          '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
          '@page' => $this
            ->getUrl(),
        )))) {
          $this
            ->testForumAccessDumpDebugInfo('topic', $topic_id);
          preg_match('/node\\/(?<topic_id>\\d+)#comment-(?<comment_id>\\d+)/', $this->url, $matches);
          return array_key_exists('comment_id', $matches) ? intval($matches['comment_id']) : 0;
        }
      }
    }
  }
  return 0;
}