You are here

DraftyTestCase.test in Drafty 7

File

tests/DraftyTestCase.test
View source
<?php

/**
 * Test drafty revision support with a single entity.
 */
class DraftyTestCase extends DraftyWebTestCase {

  /**
   * Define this test's meta data.
   */
  public static function getInfo() {
    return array(
      'name' => 'Drafty',
      'description' => 'Test revision manipulation.',
      'group' => 'Drafty',
    );
  }

  /**
   * Create a node and check creation of draft and published revisions.
   */
  function testDraftRevisions() {
    $node = new stdClass();
    $node->title = 'Title A';
    $node->type = 'article';
    $node->status = 1;
    $this
      ->setRevision($node);
    node_save($node);

    // Save the vid for later comparison.
    $published_vid = $node->vid;

    // Save a new draft.
    $node = node_load($node->nid);
    $this
      ->assertEqual($node->title, 'Title A');
    $node->title = 'Title B';
    $this
      ->setRevision($node);
    $node->is_draft_revision = TRUE;
    node_save($node);
    $draft_vid = $node->vid;

    // Confirm that the published and draft version IDs differ.
    $this
      ->assertNotEqual($published_vid, $draft_vid);

    // Confirm that loading the node gets the published revision.
    $node = node_load($node->nid);
    $this
      ->assertEqual($node->title, 'Title A');
    drafty()
      ->publishRevision('node', $node->nid, $draft_vid);
    $node = node_load($node->nid);
    $this
      ->assertEqual($node->title, 'Title B');
    $this
      ->assertNotEqual($node->vid, $draft_vid);
  }

  /**
   * Create a published node. Then create a draft and check if the previous
   * published revision was cleaned by cron.
   */
  function testDraftyRevisionCleanup() {
    $node = new stdClass();
    $node->title = 'Title A';
    $node->type = 'article';
    $node->status = 1;
    $this
      ->setRevision($node);
    node_save($node);

    // Save the vid for later comparison.
    $first_published_vid = $node->vid;

    // Save a new draft.
    $node = node_load($node->nid);
    $node->title = 'Title B';
    $this
      ->setRevision($node);
    $node->is_draft_revision = TRUE;
    node_save($node);
    $node = node_load($node->nid);
    $second_published_vid = $node->vid;

    // Run cron
    $this
      ->cronRun();

    // Check that the $first_published_vid is still present
    $nodes = node_load_multiple(array(
      $node->nid,
    ), array(
      'vid' => $first_published_vid,
    ));
    $this
      ->assertTrue(!empty($nodes), 'Old published revision is still present.');

    // Configure drafty to clean old published revisions
    variable_set('drafty_delete_old_revisions', TRUE);

    // Create new draft revision
    $node->title = 'Title C';
    $this
      ->setRevision($node);
    $node->is_draft_revision = TRUE;
    node_save($node);

    // Run cron
    $this
      ->cronRun();

    // Confirm the the second published revision has been deleted.
    $nodes = node_load_multiple(array(
      $node->nid,
    ), array(
      'vid' => $second_published_vid,
    ));
    $this
      ->assertTrue(empty($nodes), 'Old published revision has been deleted.');
  }

}

Classes

Namesort descending Description
DraftyTestCase Test drafty revision support with a single entity.