You are here

public function SaveDraftTestCase::testNodeValidation in Save Draft 7

Make sure node validation still runs even after we've altered the form.

File

./save_draft.test, line 144
Link base test file - contains common functions for testing links.

Class

SaveDraftTestCase
@file Link base test file - contains common functions for testing links.

Code

public function testNodeValidation() {

  // Log in as an administrator, who should be able to see the save draft
  // button and also edit the node's author.
  $this
    ->drupalLogin($this->admin_user);

  // Enable save draft functionality.
  variable_set('save_draft_enabled_article', SAVE_DRAFT_ENABLED);

  // Test with & without required validation.
  foreach (array(
    TRUE,
    FALSE,
  ) as $skip_required_validation) {
    debug('Skip required validation: ' . ($skip_required_validation ? 'true' : 'false'));
    variable_set('save_draft_skip_required_article', $skip_required_validation);

    // Test clicking all the different buttons on the node add page.
    foreach (array(
      $this->button_publish,
      $this->button_save_draft,
      $this->button_preview,
    ) as $button_value) {
      debug('Node add. Button value: ' . $button_value);

      // Try to create a node with a nonexistent author.
      $edit = $this
        ->getNodeArray();

      // Remove the title, which is a required field.
      unset($edit[$this->title_key]);

      // This username does not exist.
      $edit['name'] = $this
        ->randomName(8);
      $this
        ->drupalPost('node/add/article', $edit, $button_value);

      // Username validation should always fail.
      $this
        ->assertRaw(t('The username %name does not exist.', array(
        '%name' => $edit['name'],
      )));

      // Required validation for the title should have passed, unless we are
      // clicking the Publish button, or skip_required_validation is FALSE, in
      // which case title should be required.
      // t() functions are like this to replicate how the string would
      // normally be created.
      if (!$skip_required_validation || $button_value == $this->button_publish) {
        $this
          ->assertRaw(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
      else {
        $this
          ->assertNoRaw(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
    }

    // Test clicking all the different buttons on the node edit page of a
    // published node.
    foreach (array(
      $this->button_save,
      $this->button_unpublish,
      $this->button_preview,
      $this->button_delete,
    ) as $button_value) {
      debug('Published node edit. Button value: ' . $button_value);
      $edit = $this
        ->getNodeArray();
      $this
        ->drupalPost('node/add/article', $edit, $this->button_publish);
      $node = $this
        ->drupalGetNodeByTitle($edit[$this->title_key]);

      // Remove the title, which is a required field.
      $edit[$this->title_key] = '';

      // This username does not exist.
      $edit['name'] = $this
        ->randomName(8);
      $this
        ->drupalPost('node/' . $node->nid . '/edit', $edit, $button_value);

      // Username validation should always fail.
      $this
        ->assertRaw(t('The username %name does not exist.', array(
        '%name' => $edit['name'],
      )));

      // Required validation for the title should have passed, unless we are
      // clicking the Save button, or skip_required_validation is FALSE, in
      // which case title should be required.
      // t() functions are like this to replicate how the string would
      // normally be created.
      if (!$skip_required_validation || $button_value == $this->button_save) {
        $this
          ->assertRaw(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
      else {
        $this
          ->assertNoRaw(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
    }

    // Test clicking all the different buttons on the node edit page of a
    // draft node.
    foreach (array(
      $this->button_save_draft,
      $this->button_publish,
      $this->button_preview,
      $this->button_delete,
    ) as $button_value) {
      debug('Draft node edit. Button value: ' . $button_value);
      $edit = $this
        ->getNodeArray();
      $this
        ->drupalPost('node/add/article', $edit, $this->button_save_draft);
      $node = $this
        ->drupalGetNodeByTitle($edit[$this->title_key]);

      // Remove the title, which is a required field.
      $edit[$this->title_key] = '';

      // This username does not exist.
      $edit['name'] = $this
        ->randomName(8);
      $this
        ->drupalPost('node/' . $node->nid . '/edit', $edit, $button_value);

      // Username validation should always fail.
      $this
        ->assertRaw(t('The username %name does not exist.', array(
        '%name' => $edit['name'],
      )));

      // Required validation for the title should have passed, unless we are
      // clicking the Publish button, or skip_required_validation is FALSE, in
      // which case title should be required.
      // t() functions are like this to replicate how the string would
      // normally be created.
      if (!$skip_required_validation || $button_value == $this->button_publish) {
        $this
          ->assertRaw(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
      else {
        $this
          ->assertNoRaw(t('!name field is required.', array(
          '!name' => t('Title'),
        )));
      }
    }
  }
}