You are here

answers.test in Answers 7.4

Same filename and directory in other branches
  1. 7.3 answers.test

Tests for answers.module.

File

answers.test
View source
<?php

/**
 * @file
 * Tests for answers.module.
 */

/**
 * Tests the functionality of the answers module.
 */
class AnswersTestCase extends DrupalWebTestCase {

  /**
   * Default users for running tests.
   *
   * @var User
   */
  protected $answersUser;

  /**
   * Test getInfo.
   */
  public static function getInfo() {
    return array(
      'name' => 'Answers',
      'description' => 'Answers Tests',
      'group' => 'Answers',
    );
  }

  /**
   * Enable modules and create users with specific permissions.
   */
  public function setUp() {
    parent::setUp('answers');
    $this->answersUser = $this
      ->drupalCreateUser(array(
      'administer content types',
      'create answers_question content',
    ));
  }

  /**
   * Check node labels.
   */
  public function testLabels() {
    $langcode = LANGUAGE_NONE;
    $this
      ->drupalLogin($this->answersUser);
    $this
      ->drupalGet('node/add/answers-question');
    $this
      ->assertResponse(200);
    $this
      ->assertTitle(t('Create Question | Drupal'), 'The title on the create answer_question page is "Create Question | Drupal".');
    $this
      ->assertRaw('<label for="edit-title">Title <span class="form-required" title="This field is required.">*</span></label>', 'The question title is Title');
    $this
      ->assertRaw('<label for="edit-body-und-0-value">Body </label>', 'The question body is Body');
    $this
      ->assertFieldByName('title', '', 'The title field exists');
    $this
      ->assertFieldByName("body[{$langcode}][0][value]", '', 'The body field exists');
    $question = array();
    $question['title'] = 'Woodchucks';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->assertRaw('<h2 class="new-answer-form-title">Your Answer</h2>', 'Answers Form exists');
    $this
      ->assertRaw('<div class="view-header">
      No Answers yet.    </div>', 'No Answers yet');
    $this
      ->assertNoRaw('<label for="edit-body-und-0-value">Body </label>', 'No answer body label');
    $this
      ->assertNoFieldByName('title', '', 'No title field exists');
    $this
      ->assertFieldByName("body[{$langcode}][0][value]", '', 'The body field exists');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "42 cords of wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->assertRaw('<h2 class="new-answer-form-title">Your Answer</h2>', 'Answers Form exists');
    $this
      ->assertRaw('<div class="view-header">
      1 Answer    </div>', '1 Answer');
    $answer["body[{$langcode}][0][value]"] = "Woodchucks don't chuck wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->assertRaw('<h2 class="new-answer-form-title">Your Answer</h2>', 'Answers Form exists');
    $this
      ->assertRaw('<div class="view-header">
      2 Answers    </div>', '2 Answers');
  }

  /**
   * Tests permissions for default roles.
   */
  public function testDefaultPermissions() {
    $admin = $this
      ->drupalCreateUser(array(
      'administer permissions',
    ));
    $this
      ->drupalLogin($admin);
    $this
      ->drupalGet('admin/people/permissions');

    // Authenticated Users.
    $this
      ->assertFieldChecked('edit-2-create-answers-question-content', "Authenticated role has create question.");
    $this
      ->assertFieldChecked('edit-2-edit-own-answers-question-content', "Authenticated role has edit own question.");
    $this
      ->assertFieldChecked('edit-2-create-answers-answer-content', "Authenticated role has create answer`.");
    $this
      ->assertFieldChecked('edit-2-edit-own-answers-answer-content', "Authenticated role has edit own answer`.");

    // Administrators.
    $this
      ->assertFieldChecked('edit-3-create-answers-question-content', "Administrators role have create question.");
    $this
      ->assertFieldChecked('edit-3-edit-own-answers-question-content', "Administrators role has edit own question.");
    $this
      ->assertFieldChecked('edit-3-edit-any-answers-question-content', "Administrators role has edit any question.");
    $this
      ->assertFieldChecked('edit-3-delete-own-answers-question-content', "Administrators role has delete own question.");
    $this
      ->assertFieldChecked('edit-3-delete-any-answers-question-content', "Administrators role has delete any question.");
    $this
      ->assertFieldChecked('edit-3-create-answers-answer-content', "Administrators role has create answer.");
    $this
      ->assertFieldChecked('edit-3-edit-own-answers-answer-content', "Administrators role has edit own answer.");
    $this
      ->assertFieldChecked('edit-3-edit-any-answers-question-content', "Administrators role has edit any answer.");
    $this
      ->assertFieldChecked('edit-3-delete-own-answers-answer-content', "Administrators role has delete own answer.");
    $this
      ->assertFieldChecked('edit-3-delete-any-answers-answer-content', "Administrators role has delete any answer.");
  }

  /**
   * Tests authenticated users permissions.
   */
  public function testDefaultAccess() {
    $langcode = LANGUAGE_NONE;
    $this
      ->drupalLogin($this->answersUser);
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->clickLink(t('Edit'));
    $question = array();
    $question['title'] = 'How much wood?';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck, if a woodchuck could chuck wood?";
    $this
      ->drupalPost('node/1/edit', $question, 'Save');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "Exactly 42 cords of wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->clickLink(t('Edit'), 1);
    $answer["body[{$langcode}][0][value]"] = "As much wood as a woodchuck could chuck, If a woodchuck could chuck wood.";
    $this
      ->drupalPost('node/2/edit', $answer, 'Save');
  }

  /**
   * Test add content overview with other types.
   */
  public function testAddContentOverview() {
    $settings = array(
      'type' => 'my_special_node_type',
    );
    $content_type = $this
      ->drupalCreateContentType($settings);
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer content types',
      'create my_special_node_type content',
    ));
    $this
      ->drupalLogin($admin_user);
    $this
      ->drupalGet('node/add');
    $this
      ->assertLink($content_type->name);
    $this
      ->assertLink('Question');
    $this
      ->assertNoLink('Answer');
  }

  /**
   * Tests body is required on answers_answer.
   */
  public function testAnswerWithNoBody() {
    $langcode = LANGUAGE_NONE;
    $user2 = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($user2);
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->assertRaw('<textarea class="text-full form-textarea required error" id="edit-body-und-0-value" name="body[und][0][value]" cols="60" rows="15"></textarea>', 'Body field is required');
  }

  /**
   * Tests the Orphaned answers report.
   */
  public function testAnswersOrphanReport() {
    $user = $this
      ->drupalCreateUser(array(
      'access site reports',
      'administer site configuration',
      'administer content types',
    ));
    $this
      ->drupalLogin($user);
    $settings = array(
      'type' => 'answers_question',
      'nid' => NULL,
    );
    $question = $this
      ->drupalCreateNode($settings);
    $settings['type'] = 'answers_answer';
    $settings['answers_related_question'][LANGUAGE_NONE][0]['target_id'] = $question->nid;
    $answer_1 = $this
      ->drupalCreateNode($settings);
    $answer_2 = $this
      ->drupalCreateNode($settings);
    db_delete('node')
      ->condition('nid', $question->nid)
      ->execute();
    db_delete('field_data_answers_related_question')
      ->condition('entity_id', $answer_1->nid)
      ->condition('bundle', 'answers_answer')
      ->condition('answers_related_question_target_id', $question->nid)
      ->execute();
    $this
      ->drupalGet('admin/reports/status');
    $this
      ->assertLink('Orphaned', 0, 'Orphaned answers report link is present.');
    $this
      ->clickLink('Orphaned');
    $this
      ->assertLinkByHref('node/2/edit', 0, 'A link to orphan 1 appears on the page');
    $this
      ->assertLinkByHref('node/3/edit', 0, 'A link to orphan 2 appears on the page');
  }

}

/**
 * Tests the functionality of the answers module views.
 */
class AnswersViewTestCase extends DrupalWebTestCase {

  /**
   * Test getInfo.
   */
  public static function getInfo() {
    return array(
      'name' => 'Answers Views',
      'description' => 'Answers View Tests',
      'group' => 'Answers',
    );
  }

  /**
   * Enable modules and create users with specific permissions.
   */
  public function setUp() {
    parent::setUp('answers');
  }

  /**
   * Test Qustions view page.
   */
  public function testQuestionsView() {
    $langcode = LANGUAGE_NONE;
    $answers_user = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($answers_user);
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "Exactly 42 cords of wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Is it true?';
    $question["body[{$langcode}][0][value]"] = "Is what they say about Woodchuck true?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->drupalGet('questions');
    $this
      ->assertLink('Is it true?');
    $this
      ->assertLink('Woodchucks');
  }

  /**
   * Test Own Questions view page.
   */
  public function testOwnQuestionsView() {
    $langcode = LANGUAGE_NONE;
    $user2 = $this
      ->drupalCreateUser();
    $user3 = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($user2);
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks are young werewolves?';
    $question["body[{$langcode}][0][value]"] = "Are woodchuck really just werewolves in disguise?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($user3);
    $this
      ->drupalGet('node/1');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "Exactly 42 cords of wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Is it true?';
    $question["body[{$langcode}][0][value]"] = "Is what they say about Woodchuck true?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->drupalGet('user/3/questions');
    $this
      ->assertNoLink('Woodchucks');
    $this
      ->assertNoLink('Woodchucks are young werewolves?');
    $this
      ->assertLink('Is it true?');
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($user2);
    $this
      ->drupalGet('user/2/questions');
    $this
      ->assertLink('Woodchucks');
    $this
      ->assertLink('Woodchucks are young werewolves?');
  }

  /**
   * Test Own Answers view page.
   */
  public function testOwnAnswersView() {
    $langcode = LANGUAGE_NONE;
    $user2 = $this
      ->drupalCreateUser();
    $user3 = $this
      ->drupalCreateUser();
    $this
      ->drupalLogin($user2);
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks';
    $question["body[{$langcode}][0][value]"] = "How much wood could a woodchuck chuck?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "Woodchucks do not chuck wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Woodchucks are young werewolves?';
    $question["body[{$langcode}][0][value]"] = "Are woodchuck really just werewolves in disguise?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($user3);
    $this
      ->drupalGet('node/1');
    $answer["body[{$langcode}][0][value]"] = "Exactly 42 cords of wood.";
    $this
      ->drupalPost('node/1', $answer, 'Save');
    $this
      ->drupalGet('node/add/answers-question');
    $question = array();
    $question['title'] = 'Is it true?';
    $question["body[{$langcode}][0][value]"] = "Is what they say about Woodchuck true?";
    $this
      ->drupalPost('node/add/answers-question', $question, 'Save');
    $this
      ->drupalGet('user/3/answers');
    $this
      ->assertLink('Woodchucks');
    $this
      ->assertNoLink('Is it true?');
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($user2);
    $this
      ->drupalGet('node/5');
    $answer = array();
    $answer["body[{$langcode}][0][value]"] = "Woodchucks are clearly smaller werewolves. They were sent to infiltrate and spy on our society until the invasion.";
    $this
      ->drupalPost('node/5', $answer, 'Save');
    $this
      ->drupalGet('user/2/answers');
    $this
      ->assertLink('Woodchucks');
    $this
      ->assertNoLink('Woodchucks are young werewolves?');
    $this
      ->assertLink('Is it true?');
  }

}

Classes

Namesort descending Description
AnswersTestCase Tests the functionality of the answers module.
AnswersViewTestCase Tests the functionality of the answers module views.