You are here

class CommentModuleTestCase in SimpleTest 6

Hierarchy

Expanded class hierarchy of CommentModuleTestCase

File

tests/comment_module.test, line 4

View source
class CommentModuleTestCase extends DrupalTestCase {
  var $admin_user;
  var $web_user;
  var $node;
  function get_info() {
    return array(
      'name' => t('Comment functionality'),
      'desc' => t('Thoroughly test comment administration and user interfaces.'),
      'group' => t('Comment Tests'),
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('comment');

    // Create users.
    $this->admin_user = $this
      ->drupalCreateUserRolePerm(array(
      'administer content types',
      'administer comments',
      'administer permissions',
    ));
    $this->web_user = $this
      ->drupalCreateUserRolePerm(array(
      'access comments',
      'post comments',
      'create story content',
    ));
    $this
      ->drupalLoginUser($this->web_user);
    $this->node = $this
      ->drupalCreateNode(array(
      'type' => 'story',
    ));
    $this
      ->assertTrue($this->node, 'Story node created.');
    $this
      ->drupalGet('logout');
  }

  /**
   * Test comment interface.
   */

  //  function testCommentInterface() {
  //    // Set comments to not have subject.
  //    $this->drupalLoginUser($this->admin_user);
  //    $this->set_comment_preview(TRUE);
  //    $this->set_comment_subject(FALSE);
  //    $this->drupalGet('logout');
  //
  //    // Post comment without subject
  //    $this->drupalLoginUser($this->web_user);
  //    $this->drupalGet('comment/reply/'. $this->node->nid);
  //    $this->assertNoPattern('/(input)(.*?)(name="subject")/', 'Subject field not found.');
  //
  //    // Set comments to have subject and preview to required.
  //    $this->drupalGet('logout');
  //    $this->drupalLoginUser($this->admin_user);
  //    $this->set_comment_subject(true);
  //    $this->set_comment_preview(true);
  //    $this->drupalGet('logout');
  //
  //    // Create comment that requires preview.
  //    $this->drupalLoginUser($this->web_user);
  //    $comment = $this->post_comment($this->node, $this->randomName(), $this->randomName());
  //    $this->assertTrue($this->comment_exists($comment), 'Comment found.');
  //
  //    // Reply to comment.
  //    $this->drupalGet('comment/reply/'. $this->node->nid .'/'. $comment->id);
  //    $reply = $this->post_comment(NULL, $this->randomName(), $this->randomName());
  //    $this->assertTrue($this->comment_exists($reply, TRUE), 'Reply found.');
  //
  //    // Edit reply.
  //    $this->drupalGet('comment/edit/'. $reply->id);
  //    $reply = $this->post_comment(NULL, $this->randomName(), $this->randomName());
  //    $this->assertTrue($this->comment_exists($reply, TRUE), 'Modified reply found.');
  //
  //    // Delete comment and make sure that reply is also removed.
  //    $this->drupalGet('logout');
  //    $this->drupalLoginUser($this->admin_user);
  //    $this->delete_comment($comment);
  //
  //    $this->drupalGet('node/'. $this->node->nid);
  //    $this->assertFalse($this->comment_exists($comment), 'Comment not found.');
  //    $this->assertFalse($this->comment_exists($reply, TRUE), 'Reply not found.');
  //  }

  /**
   * Test comment form on node page.
   */
  function testFormOnPage() {

    // Enabled comment form on node page.
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->set_comment_form(TRUE);
    $this
      ->drupalGet('logout');

    // Submit comment through node form.
    $this
      ->drupalLoginUser($this->web_user);
    $this
      ->drupalGet('node/' . $this->node->nid);
    $form_comment = $this
      ->post_comment(NULL, $this
      ->randomName(), $this
      ->randomName());
    $this
      ->assertTrue($this
      ->comment_exists($form_comment), 'Form comment found.');

    // Disable comment form on node page.
    $this
      ->drupalGet('logout');
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->set_comment_form(FALSE);
  }

  /**
   * Test anonymous comment functionality.
   */
  function testAnonymous() {
    $this
      ->drupalLoginUser($this->admin_user);

    // Enabled anonymous user comments.
    $this
      ->set_anonymous_user_comment(TRUE, TRUE);
    $this
      ->set_comment_anonymous('0');

    // Ensure that doesn't require contact info.
    $this
      ->drupalGet('logout');

    // Post anonymous comment without contact info.
    $anonymous_comment1 = $this
      ->post_comment($this->node, $this
      ->randomName(), $this
      ->randomName());
    $this
      ->assertTrue($this
      ->comment_exists($anonymous_comment1), 'Anonymous comment without contact info found.');

    // Allow contact info.
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->set_comment_anonymous('1');
    $this
      ->drupalGet('logout');

    // Post anonymous comment with contact info (optional).
    $this
      ->drupalGet('comment/reply/' . $this->node->nid);
    $this
      ->assertTrue($this
      ->comment_contact_info_available(), 'Contact information available.');
    $anonymous_comment2 = $this
      ->post_comment($this->node, $this
      ->randomName(), $this
      ->randomName());
    $this
      ->assertTrue($this
      ->comment_exists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');

    // Require contact info.
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->set_comment_anonymous('2');
    $this
      ->drupalGet('logout');

    // Try to post comment with contact info (required).
    $this
      ->drupalGet('comment/reply/' . $this->node->nid);
    $this
      ->assertTrue($this
      ->comment_contact_info_available(), 'Contact information available.');
    $anonymous_comment3 = $this
      ->post_comment($this->node, $this
      ->randomName(), $this
      ->randomName(), TRUE, TRUE);
    $this
      ->assertText(t('E-mail field is required.'), 'E-mail required.');

    // Name should have 'Anonymous' for value by default.
    $this
      ->assertFalse($this
      ->comment_exists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');

    // Post comment with contact info (required).
    $anonymous_comment3 = $this
      ->post_comment($this->node, $this
      ->randomName(), $this
      ->randomName(), TRUE, array(
      'mail' => 'tester@simpletest.org',
    ));
    $this
      ->assertTrue($this
      ->comment_exists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');

    // Unpublish comment.
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->perform_comment_operation($anonymous_comment3, 'unpublish');
    $this
      ->drupalGet('admin/content/comment/approval');
    $this
      ->assertWantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was unpublished.');

    // Publish comment.
    $this
      ->perform_comment_operation($anonymous_comment3, 'publish', TRUE);
    $this
      ->drupalGet('admin/content/comment');
    $this
      ->assertWantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was published.');

    // Delete comment.
    $this
      ->perform_comment_operation($anonymous_comment3, 'delete');
    $this
      ->drupalGet('admin/content/comment');
    $this
      ->assertNoUnwantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was deleted.');

    // Set anonymouse comments to require approval.
    $this
      ->set_anonymous_user_comment(TRUE, FALSE);
    $this
      ->set_comment_anonymous('0');

    // Ensure that doesn't require contact info.
    $this
      ->drupalGet('logout');

    // Post anonymous comment without contact info.
    $subject = $this
      ->randomName();
    $body = $this
      ->randomName();
    $this
      ->post_comment($this->node, $subject, $body, TRUE, TRUE);

    // Set $contact to true so that it won't check for id and message.
    $this
      ->assertText(t('Your comment has been queued for moderation by site administrators and will be published after approval.'), 'Comment requires approval.');

    // Get unaproved comment id.
    $this
      ->drupalLoginUser($this->admin_user);
    $anonymous_comment4 = $this
      ->get_unaproved_comment($subject);
    $anonymous_comment4 = (object) array(
      'id' => $anonymous_comment4,
      'subject' => $subject,
      'comment' => $body,
    );
    $this
      ->drupalGet('logout');
    $this
      ->assertFalse($this
      ->comment_exists($anonymous_comment4), 'Anonymous comment was not published.');

    // Approve comment.
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->perform_comment_operation($anonymous_comment4, 'publish', TRUE);
    $this
      ->drupalGet('logout');
    $this
      ->drupalGet('node/' . $this->node->nid);
    $this
      ->assertTrue($this
      ->comment_exists($anonymous_comment4), 'Anonymous comment visible.');

    // Reset.
    $this
      ->drupalLoginUser($this->admin_user);
    $this
      ->set_anonymous_user_comment(FALSE, FALSE);
  }

  /**
   * Post comment.
   *
   * @param object $node Node to post comment on.
   * @param string $subject Comment subject.
   * @param string $comment Comment body.
   * @param boolean $preview Should preview be required.
   * @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
   */
  function post_comment($node, $subject, $comment, $preview = TRUE, $contact = NULL) {
    $edit = array();
    $edit['subject'] = $subject;
    $edit['comment'] = $comment;
    if ($contact !== NULL && is_array($contact)) {
      $edit += $contact;
    }
    if ($node !== NULL) {
      $this
        ->drupalGet('comment/reply/' . $node->nid);
    }
    if ($preview) {
      $this
        ->assertFieldById('edit-comment');
      $this
        ->assertNoPattern('/(input)(.*?)(value="Save")/', 'Save button not found.');

      // Preview required so no save button should be found.
      $this
        ->drupalPost(NULL, $edit, 'Preview');
    }
    $this
      ->drupalPost(NULL, array(), 'Save');
    $match = array();

    // Get comment ID
    preg_match('/#comment-([^"]+)/', $this->_browser
      ->getURL(), $match);

    // get comment
    if ($contact !== TRUE) {

      // If true then attempting to find error message.
      $this
        ->assertText($subject, 'Comment posted.');
      $this
        ->assertTrue(!empty($match) && !empty($match[1]), 'Comment id found.');
    }
    if (isset($match[1])) {
      return (object) array(
        'id' => $match[1],
        'subject' => $subject,
        'comment' => $comment,
      );
    }
    else {
      return NULL;
    }
  }

  /**
   * Checks current pag for specified comment.
   *
   * @param object $comment Comment object.
   * @param boolean $reply The comment is a reply to another comment.
   * @return boolean Comment found.
   */
  function comment_exists($comment, $reply = FALSE) {
    if ($comment && is_object($comment)) {
      $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
      $regex .= '<a id="comment-' . $comment->id . '"(.*?)';

      // Comment anchor.
      $regex .= '<div(.*?)';

      // Begin in comment div.
      $regex .= $comment->subject . '(.*?)';

      // Match subject.
      $regex .= $comment->comment . '(.*?)';

      // Match comment.
      $regex .= '<\\/div>/s';

      // Dot matches newlines and ensure that match doesn't bleed outside comment div.
      return preg_match($regex, $this
        ->drupalGetContent());
    }
    else {
      return FALSE;
    }
  }

  /**
   * Delete comment.
   *
   * @param object $comment Comment to delete.
   */
  function delete_comment($comment) {
    $this
      ->drupalPost('comment/delete/' . $comment->id, array(), 'Delete');
    $this
      ->assertWantedText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
  }

  /**
   * Set comment subject setting.
   *
   * @param boolean $enabled Subject value.
   */
  function set_comment_subject($enabled) {
    $this
      ->set_comment_settings('comment_subject_field', $enabled ? '1' : '0', 'Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
  }

  /**
   * Set comment preview setting.
   *
   * @param boolean $required Preview value.
   */
  function set_comment_preview($required) {
    $this
      ->set_comment_settings('comment_preview', $required ? '1' : '0', 'Comment preview ' . ($required ? 'required' : 'optional') . '.');
  }

  /**
   * Set comment form setting.
   *
   * @param boolean $enabled Form value.
   */
  function set_comment_form($enabled) {
    $this
      ->set_comment_settings('comment_form_location', $enabled ? '1' : '3', 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.');
  }

  /**
   * Set comment anonymous level setting.
   *
   * @param integer $level Anonymous level.
   */
  function set_comment_anonymous($level) {
    $this
      ->set_comment_settings('comment_anonymous', $level, 'Anonymous commenting set to level ' . $level . '.');
  }

  /**
   * Set comment setting for story content type.
   *
   * @param string $name Name of variable.
   * @param string $vale Value of variable.
   * @param string $message Status message to display.
   */
  function set_comment_settings($name, $value, $message) {
    $this
      ->drupalVariableSet($name . '_story', $value);
    $this
      ->assertTrue(TRUE, $message);

    // Display status message.
  }

  /**
   * Set anonymous comment setting.
   *
   * @param boolean $enabled Allow anonymous commenting.
   * @param boolean $without_approval Allow anonymous commenting without approval.
   */
  function set_anonymous_user_comment($enabled, $without_approval) {
    $edit = array();
    $edit['1-access-comments'] = $enabled;
    $edit['1-post-comments'] = $enabled;
    $edit['1-post-comments-without-approval'] = $without_approval;
    $this
      ->drupalPost('admin/user/permissions', $edit, 'Save permissions');
    $this
      ->assertText(t('The changes have been saved.'), 'Anonymous user comments ' . ($enabled ? 'enabled' : 'disabled') . '.');
  }

  /**
   * Check for contact info.
   *
   * @return boolean Contact info is avialable.
   */
  function comment_contact_info_available() {
    return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this
      ->drupalGetContent());
  }

  /**
   * Perform the specified operation on the specified comment.
   *
   * @param object $comment Comment to perform operation on.
   * @param string $operation Operation to perform.
   * @param boolean $aproval Operation is found on approval page.
   */
  function perform_comment_operation($comment, $operation, $approval = FALSE) {
    $edit = array();
    $edit['operation'] = $operation;
    $edit['comments[' . $comment->id . ']'] = TRUE;
    $this
      ->drupalPost('admin/content/comment' . ($approval ? '/approval' : ''), $edit, 'Update');
    if ($operation == 'delete') {
      $this
        ->drupalPost(NULL, array(), 'Delete comments');
      $this
        ->assertText(t('The comments have been deleted.'), 'Operation "' . $operation . '" was performed on comment.');
    }
    else {
      $this
        ->assertText(t('The update has been performed.'), 'Operation "' . $operation . '" was performed on comment.');
    }
  }

  /**
   * Get the comment id for an unaproved comment.
   *
   * @param string $subject Comment subject to find.
   * @return integer Comment id.
   */
  function get_unaproved_comment($subject) {
    $this
      ->drupalGet('admin/content/comment/approval');
    preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this
      ->drupalGetContent(), $match);
    return $match[2];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentModuleTestCase::$admin_user property
CommentModuleTestCase::$node property
CommentModuleTestCase::$web_user property
CommentModuleTestCase::comment_contact_info_available function Check for contact info.
CommentModuleTestCase::comment_exists function Checks current pag for specified comment.
CommentModuleTestCase::delete_comment function Delete comment.
CommentModuleTestCase::get_info function
CommentModuleTestCase::get_unaproved_comment function Get the comment id for an unaproved comment.
CommentModuleTestCase::perform_comment_operation function Perform the specified operation on the specified comment.
CommentModuleTestCase::post_comment function Post comment.
CommentModuleTestCase::setUp function
CommentModuleTestCase::set_anonymous_user_comment function Set anonymous comment setting.
CommentModuleTestCase::set_comment_anonymous function Set comment anonymous level setting.
CommentModuleTestCase::set_comment_form function Set comment form setting.
CommentModuleTestCase::set_comment_preview function Set comment preview setting.
CommentModuleTestCase::set_comment_settings function Set comment setting for story content type.
CommentModuleTestCase::set_comment_subject function Set comment subject setting.
CommentModuleTestCase::testAnonymous function Test anonymous comment functionality.
CommentModuleTestCase::testFormOnPage function Test comment form on node page.
DrupalTestCase::$_cleanupContentTypes property
DrupalTestCase::$_cleanupNodes property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::$_modules property
DrupalTestCase::$_originalModules property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::checkOriginalModules function Retrieves and saves current modules list into $_originalModules and $_modules.
DrupalTestCase::clickLink function Follows a link by name.
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateContentType function Creates a custom content type based on default settings.
DrupalTestCase::drupalCreateNode function Creates a node based on default settings.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Broker for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPost function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser By $reporting you specify if this request does assertions or not Warning: empty ("") returns will cause fails with $reporting
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a drupal variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
DrupalTestCase::tearDown function tearDown implementation, setting back switched modules etc 8