You are here

save_draft.test in Save Draft 7

Same filename and directory in other branches
  1. 6.2 save_draft.test

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

File

save_draft.test
View source
<?php

/**
 * @file
 * Link base test file - contains common functions for testing links.
 */
class SaveDraftTestCase extends DrupalWebTestCase {
  protected $admin_user;
  protected $save_draft_user;
  protected $title_key;
  protected $body_key;

  /**
   * Button text variables.
   */
  private $button_unpublish;
  private $button_publish;
  private $button_save_draft;
  private $button_save;
  private $button_preview;
  private $button_delete;

  /**
   * Return a basic info array .
   */
  public static function getInfo() {
    return array(
      'name' => 'Save draft',
      'description' => 'Make sure the node form still works with Save Draft enabled.',
      'group' => 'Save draft',
    );
  }

  /**
   * Setup title, body and user.
   */
  public function setUp() {
    parent::setUp(array(
      'save_draft',
    ));
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer nodes',
      'bypass node access',
    ));
    $this->save_draft_user = $this
      ->drupalCreateUser(array(
      'create article content',
      'edit any article content',
      'view own unpublished content',
      'save draft',
    ));
    $langcode = LANGUAGE_NONE;
    $this->title_key = "title";
    $this->body_key = "body[{$langcode}][0][value]";
    $this->button_unpublish = t('Unpublish');
    $this->button_publish = t('Publish');
    $this->button_save_draft = t('Save as draft');
    $this->button_save = t('Save');
    $this->button_preview = t('Preview');
    $this->button_delete = t('Delete');
  }

  /**
   * Return a basic $edit array that can be used to save a node.
   */
  public function getNodeArray() {
    $edit = array();
    $edit[$this->title_key] = $this
      ->randomName(8);
    $edit[$this->body_key] = $this
      ->randomName(8);
    return $edit;
  }

  /**
   * Make sure nodes save with the right publication status.
   */
  public function testNodeSave() {

    // Log in as a user who should see the save draft button.
    $this
      ->drupalLogin($this->save_draft_user);

    // Testing with drafts enabled.
    variable_set('save_draft_enabled_article', SAVE_DRAFT_ENABLED);

    // Make sure the publish and save as draft buttons are present when adding a
    // node.
    $this
      ->drupalGet('node/add/article');
    $this
      ->assertRaw('<input type="submit" id="edit-submit" name="op" value="' . $this->button_publish . '" class="form-submit" />', t('Publish button visible on node create.'));
    $this
      ->assertRaw('<input type="submit" id="edit-draft" name="op" value="' . $this->button_save_draft . '" class="form-submit" />', t('Save as draft button visible on node create.'));

    // Publish a node, and make sure it's published.
    $edit = $this
      ->getNodeArray();
    $this
      ->drupalPost('node/add/article', $edit, $this->button_publish);
    $node = $this
      ->drupalGetNodeByTitle($edit[$this->title_key]);
    $this
      ->assertEqual($node->status, NODE_PUBLISHED, t('Node saved correctly.'));

    // Make sure the save and unpublish buttons are present when on a published
    // node.
    $this
      ->drupalGet('node/' . $node->nid . '/edit');
    $this
      ->assertRaw('<input type="submit" id="edit-submit" name="op" value="' . $this->button_save . '" class="form-submit" />', t('Save button visible on published node edit.'));
    $this
      ->assertRaw('<input type="submit" id="edit-draft" name="op" value="' . $this->button_unpublish . '" class="form-submit" />', t('Unpublish button visible on published node edit.'));

    // Unpublish it, and make sure it's unpublished.
    $this
      ->drupalPost('node/' . $node->nid . '/edit', array(), $this->button_unpublish);
    $node = node_load($node->nid, NULL, TRUE);
    $this
      ->assertEqual($node->status, NODE_NOT_PUBLISHED, t('Node unpublished correctly.'));

    // Save a new node as a draft, and make sure it's unpublished.
    $edit = $this
      ->getNodeArray();
    $this
      ->drupalPost('node/add/article', $edit, $this->button_save_draft);
    $node = $this
      ->drupalGetNodeByTitle($edit[$this->title_key]);
    $this
      ->assertEqual($node->status, NODE_NOT_PUBLISHED, t('Node saved correctly as draft.'));

    // Make sure the publish and save draft buttons are present when on an
    // unpublished draft.
    $this
      ->drupalGet('node/' . $node->nid . '/edit');
    $this
      ->assertRaw('<input type="submit" id="edit-submit" name="op" value="' . $this->button_publish . '" class="form-submit" />', t('Publish button visible on draft node edit.'));
    $this
      ->assertRaw('<input type="submit" id="edit-draft" name="op" value="' . $this->button_save_draft . '" class="form-submit" />', t('Save draft button visible on draft node edit.'));

    // Publish the node, and make sure it's published.
    $this
      ->drupalPost('node/' . $node->nid . '/edit', array(), $this->button_publish);
    $node = node_load($node->nid, NULL, TRUE);
    $this
      ->assertEqual($node->status, NODE_PUBLISHED, t('Node published correctly.'));

    // Testing with drafts disabled.
    variable_set('save_draft_enabled_article', SAVE_DRAFT_DISABLED);

    // Make sure the save draft button is not present when adding a node and
    // drafts are disabled.
    $this
      ->drupalGet('node/add/article');
    $this
      ->assertRaw('<input type="submit" id="edit-submit" name="op" value="' . $this->button_save . '" class="form-submit" />', t('Save button visible on node create.'));
    $this
      ->assertNoRaw('<input type="submit" id="edit-draft" name="op" value="' . $this->button_save_draft . '" class="form-submit" />', t('Save draft button not visible on node create.'));

    // Publish a node and edit it again.
    $edit = $this
      ->getNodeArray();
    $this
      ->drupalPost('node/add/article', $edit, $this->button_save);
    $node = $this
      ->drupalGetNodeByTitle($edit[$this->title_key]);

    // Make sure the unpublish button is present when on a published node and
    // drafts are disabled.
    $this
      ->drupalGet('node/' . $node->nid . '/edit');
    $this
      ->assertRaw('<input type="submit" id="edit-submit" name="op" value="' . $this->button_save . '" class="form-submit" />', t('Save button visible on published node edit.'));
    $this
      ->assertNoRaw('<input type="submit" id="edit-draft" name="op" value="' . $this->button_unpublish . '" class="form-submit" />', t('Save draft disabled successfully on published node edit.'));
  }

  /**
   * Make sure node validation still runs even after we've altered the form.
   */
  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'),
          )));
        }
      }
    }
  }

}

Classes

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