You are here

function BotchaBaseWebTestCase::getForm in BOTCHA Spam Prevention 7.3

Same name and namespace in other branches
  1. 6.2 botcha.test \BotchaBaseWebTestCase::getForm()
  2. 6.3 tests/botcha.simpletest.test \BotchaBaseWebTestCase::getForm()
  3. 7.2 botcha.test \BotchaBaseWebTestCase::getForm()

Get one of predefined forms. Used to unify the process of testing.

4 calls to BotchaBaseWebTestCase::getForm()
BotchaNoResubmitTestCase::setFormValues in tests/botcha.simpletest.test
Helper function to generate a default form values array for any form.
BotchaTestCase::testFormSubmission in tests/botcha.simpletest.test
BotchaTestFormUI::testFormUI in tests/botcha.simpletest.test
Tests for creating, modifying and deleting botcha forms.
BotchaTestRecipebookUI::testRecipebookUI in tests/botcha.simpletest.test
Tests for creating, modifying and deleting recipe books.

File

tests/botcha.simpletest.test, line 346
Simpletest-tests for BOTCHA module.

Class

BotchaBaseWebTestCase
Base class for BOTCHA tests.

Code

function getForm($form, &$parameters = array()) {
  $form_controller = $this->application
    ->getController(Botcha::CONTROLLER_TYPE_FORM);
  $recipe_controller = $this->application
    ->getController(Botcha::CONTROLLER_TYPE_RECIPE);
  $recipebook_controller = $this->application
    ->getController(Botcha::CONTROLLER_TYPE_RECIPEBOOK);

  // @todo Refactor all these switches with classes.
  switch ($form) {

    // These ones for testing FormUI.
    case 'addForm':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $this
        ->drupalGet(Botcha::ADMIN_PATH . '/form/add');
      foreach (array(
        'botcha_form_id',
        'botcha_enabled',
        'botcha_form_recipebook',
      ) as $field) {
        $this
          ->assertField($field, "There should be a {$field} field on the form", 'BOTCHA');
      }
      break;
    case 'editForm':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $form_id = drupal_strtolower($this
        ->randomName(12));

      // Pass this newly created form to other methods.
      $parameters['botcha_form_id'] = $form_id;

      // Random enabled state generation.
      // Converting to boolean is a workaround for drupalPost which interprets 0 as TRUE else.
      $enabled = (bool) rand(0, 1);
      $parameters['botcha_enabled'] = $enabled;

      // The form should be already binded to some (let's say default) recipe book.
      $rbid = 'default';
      $botcha_form = $form_controller
        ->getForm($form_id, TRUE)
        ->setEnabled($enabled)
        ->setRecipebook($rbid);
      $form_controller
        ->save($botcha_form);

      // Assert form existence.
      $this
        ->assertTrue(!$form_controller
        ->getForm($parameters['botcha_form_id'], FALSE) instanceof BotchaFormNone, "Form {$parameters['botcha_form_id']} exists", 'BOTCHA');

      // Assert recipe book of the form.
      $recipebook_id = $form_controller
        ->getForm($parameters['botcha_form_id'], FALSE)
        ->getRecipebook();
      $this
        ->assertEqual($rbid, $recipebook_id, "BOTCHA form has recipe book {$recipebook_id} (should have {$rbid})", 'BOTCHA');

      // Assert enabled or not.
      $this
        ->assertEqual($enabled, $botcha_form
        ->isEnabled(), 'BOTCHA form has correct state', 'BOTCHA');
      $this
        ->drupalGet(Botcha::ADMIN_PATH . "/form/{$form_id}");
      foreach (array(
        'botcha_form_id',
        'botcha_enabled',
        'botcha_form_recipebook',
      ) as $field) {
        $this
          ->assertField($field, "There should be a {$field} field on the form", 'BOTCHA');
      }

      // @todo Check that id field is disabled.
      break;
    case 'deleteForm':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $form_id = drupal_strtolower($this
        ->randomName(12));

      // Pass this newly created form to other methods.
      $parameters['botcha_form_id'] = $form_id;

      // The form should be already binded to some (let's say default) recipe book.
      $botcha_form = $form_controller
        ->getForm($form_id, TRUE)
        ->setRecipebook('default');
      $form_controller
        ->save($botcha_form);
      $this
        ->drupalGet(Botcha::ADMIN_PATH . "/form/{$form_id}/delete");
      break;

    // These ones for testing RecipebookUI.
    case 'addRecipebook':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $this
        ->drupalGet(Botcha::ADMIN_PATH . '/recipebook/add');

      // @todo Implement recipes checking.
      foreach (array(
        'id',
        'title',
        'description',
      ) as $field) {
        $this
          ->assertField($field, "There should be a {$field} field on the form", 'BOTCHA');
      }
      break;
    case 'editRecipebook':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $id = drupal_strtolower($this
        ->randomName(12));

      // Save this id to the parameters.
      $parameters['id'] = $id;
      $title = $this
        ->randomName(12);
      $description = $this
        ->randomString(255);

      // We need some recipes already set to test unsetting.
      $recipe_id = 'honeypot';
      $recipebook = $recipebook_controller
        ->getRecipebook($id, TRUE)
        ->setTitle($title)
        ->setDescription($description)
        ->setRecipe($recipe_id);
      $recipebook_controller
        ->save($recipebook);
      $this
        ->drupalGet(Botcha::ADMIN_PATH . "/recipebook/{$id}");

      // @todo Implement recipes appearance checking.
      foreach (array(
        'id',
        'title',
        'description',
      ) as $field) {
        $this
          ->assertField($field, "There should be a {$field} field on the form", 'BOTCHA');
      }
      break;
    case 'deleteRecipebook':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));

      // @todo Case deleteRecipebook real logic.
      break;

    // And these ones are for testing form submissions.
    case 'node':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $this
        ->drupalGet('node/add/page');
      $this
        ->assertBotchaPresence(TRUE);
      break;
    case 'user_login':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $this
        ->drupalGet('user');
      $this
        ->assertBotchaPresence(TRUE);
      break;
    case 'comment':
    default:
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));

      // Create node to post comment to.
      $node = $this
        ->createNodeWithCommentsEnabled();
      $this
        ->drupalGet("comment/reply/{$node->nid}");
      $this
        ->assertBotchaPresence(TRUE);

      // Make sure comments on pages can be saved directly without preview.
      // @todo Abstract it.

      //variable_set('comment_preview_page', 0);
      variable_set('comment_preview_page', DRUPAL_OPTIONAL);
      break;
  }
}