You are here

public function StateFlowWebTestCase::testStateFlowStateMachine in State Machine 7.3

Test basic state changes and forward revisions.

@todo, can this test be broken up to test forward revisions separate from the basic state changes.

File

modules/state_flow/tests/state_flow.test, line 51
state_flow.test

Class

StateFlowWebTestCase
Unit tests for the StateFlow revision state machine.

Code

public function testStateFlowStateMachine() {
  $node = $this
    ->drupalCreateNode(array(
    'status' => 0,
  ));
  $states = db_select('state_flow_states')
    ->fields('state_flow_states', array())
    ->execute()
    ->fetchAll();
  $history = db_select('state_flow_history')
    ->fields('state_flow_history', array())
    ->execute()
    ->fetchAll();

  // Make sure that a record actually saved to the db.
  $this
    ->assert(!empty($history), t('There is a record in state_flow_history after a programmatic save'));
  $this
    ->assert(!empty($states), t('There is a record in state_flow_states after a programmatic save'));

  // Make sure first (default) state is draft.
  $machine = state_flow_entity_load_state_machine($node, 'node');
  $this
    ->assertEqual($machine
    ->get_current_state(), 'draft', t('New nodes should default to the "draft" state.'));

  // Keep in draft should be the default event when editing a draft.
  $this
    ->drupalGet("node/{$node->nid}/edit");
  $this
    ->assertRaw('<option value="keep in draft" selected="selected">', t('Keep in Draft is the default when editing a draft.'));

  // Publish node by firing event.
  $machine
    ->fire_event('publish');
  $this
    ->assertEqual($machine
    ->get_current_state(), 'published', t('State should be "published" after firing the "publish" event.'));

  // Check for Duplicate State records.
  $this
    ->_testOfDuplicateStates();

  // Choose edit link
  // Make sure revision checkbox is checked.
  $this
    ->drupalGet("node/{$node->nid}/edit");
  $this
    ->assertFieldChecked('edit-revision', t('Revision checkbox should be checked after editing a "published" node.'));

  // Edit node (new title)
  // Submit node, confirm creation
  // Ensure new title is updated, but not currently visible.
  $old_title = $node->title;
  $edit = array();
  $new_title = $this
    ->randomName(8);
  $edit['title'] = $new_title;
  $edit['event'] = 'to draft';
  $edit['event_comment'] = $this
    ->randomName(8);
  $this
    ->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
  $this
    ->drupalGet("node/{$node->nid}");
  $this
    ->assertText($old_title, t('Node title should stay the original despite the update.'));

  // Check for Duplicate State records.
  $this
    ->_testOfDuplicateStates();

  // Get the second-highest vid for this node. We need the second highest
  // because drafty creates a forward revision for the published revision.
  $versions = db_select('node_revision', 'nr')
    ->fields('nr', array(
    'vid',
  ))
    ->condition('nid', $node->nid)
    ->orderBy('nr.vid', 'DESC')
    ->range(1, 1)
    ->execute()
    ->fetchCol();
  $forward_revision_vid = $versions[0];

  // Verify that the new body text is present on the forward revision.
  $this
    ->drupalGet("node/{$node->nid}/revisions/" . $forward_revision_vid . "/view");
  $this
    ->assertText($new_title, t('New title text is present on forward version of node.'));

  // Load the forward node.
  $forward_revision_node = node_load($node->nid, $forward_revision_vid, TRUE);
  $forward_machine = state_flow_entity_load_state_machine($forward_revision_node, 'node');

  // Check state of second revision.
  // Should be unpublished.
  $this
    ->assertEqual($forward_machine
    ->get_current_state(), 'draft', t('Updated published nodes should default to the "draft" state for their latest revision.'));

  // @TODO This test has to be removed since drafty handles this differenty
  // Verify that the forward revision vid is greater than the live revision
  // vid.
  //    $live_revision = db_select('node', 'n')
  //      ->fields('n', array('vid'))
  //      ->condition('nid', $node->nid)
  //      ->execute()
  //      ->fetchCol();
  //    $live_revision_vid = $live_revision[0];
  //    $this->assertTrue($forward_revision_vid > $live_revision_vid, 'Forward revision vid is greater than live revision vid.');
  // Publish second revision.
  $forward_machine
    ->fire_event('publish');

  // This would also check that $machine / $forward_machine are in sync.
  // @TODO Figure out if this is necessary / desirable.
  // $this->assertEqual($machine->get_current_state(), 'published', t('State should be "published" after firing the "publish" event.'));
  $this
    ->assertEqual($forward_machine
    ->get_current_state(), 'published', t('State should be "published" after firing the "publish" event.'));

  // Check the live page.
  $this
    ->drupalGet("node/{$node->nid}");

  // Check for the new title.
  $this
    ->assertText($new_title, t('Node title should be updated to second revision, now published.'));

  // Check for Duplicate State records.
  $this
    ->_testOfDuplicateStates();
}