You are here

function BotchaBaseWebTestCase::getForm in BOTCHA Spam Prevention 6.2

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

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

2 calls to BotchaBaseWebTestCase::getForm()
BotchaNoResubmitTestCase::setFormValues in ./botcha.test
Helper function to generate a default form values array for any form.
BotchaTestCase::testFormSubmission in ./botcha.test

File

./botcha.test, line 348
Tests for BOTCHA module.

Class

BotchaBaseWebTestCase
Base class for BOTCHA tests.

Code

function getForm($form, &$parameters = array()) {
  switch ($form) {

    // These ones for testing FormUI.
    case 'addForm':
      $this
        ->debug("Entered %method %case", array(
        '%method' => __METHOD__,
        '%case' => $form,
      ));
      $this
        ->drupalGet(Botcha::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;

      // 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 = Botcha::getForm($form_id, TRUE)
        ->setEnabled($enabled)
        ->setRecipebook($rbid)
        ->save();

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

      // Assert recipe book of the form.
      $recipebook = Botcha::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::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 getForm 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::getForm($form_id, TRUE)
        ->setRecipebook('default')
        ->save();
      $this
        ->drupalGet(Botcha::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::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';
      Botcha::getRecipebook($id, TRUE)
        ->setTitle($title)
        ->setDescription($description)
        ->setRecipe($recipe_id)
        ->save();
      $this
        ->drupalGet(Botcha::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', DRUPAL_OPTIONAL);
      variable_set('comment_preview_page', 0);
      break;
  }
}