1 node.test NodeSaveTestCase::testTimestamps()

Verifies accuracy of the "created" and "changed" timestamp functionality.

File

core/modules/node/tests/node.test, line 1883
Tests for node.module.

Class

NodeSaveTestCase
Tests node save related functionality, including import-save.

Code

function testTimestamps() {
  // Use the default timestamps.
  $edit = array(
    'uid' => $this->web_user->uid,
    'type' => 'post',
    'title' => $this->randomName(8),
  );

  entity_create('node', $edit)->save();
  $node = $this->backdropGetNodeByTitle($edit['title']);
  $this->assertEqual($node->created, REQUEST_TIME, 'Creating a node sets default "created" timestamp.');
  $this->assertEqual($node->changed, REQUEST_TIME, 'Creating a node sets default "changed" timestamp.');

  // Store the timestamps.
  $created = $node->created;
  $changed = $node->changed;

  $node->save();
  $node = $this->backdropGetNodeByTitle($edit['title'], TRUE);
  $this->assertEqual($node->created, $created, 'Updating a node preserves "created" timestamp.');

  // Programmatically set the timestamps using hook_node_presave.
  $node->title = 'testing_node_presave';

  $node->save();
  $node = $this->backdropGetNodeByTitle('testing_node_presave', TRUE);
  $this->assertEqual($node->created, 1421394600, 'Saving a node uses "created" timestamp set in presave hook.');
  $this->assertEqual($node->changed, 1474011660, 'Saving a node uses "changed" timestamp set in presave hook.');

  // Programmatically set the timestamps on the node.
  $edit = array(
    'uid' => $this->web_user->uid,
    'type' => 'post',
    'title' => $this->randomName(8),
    'created' => 1421394600, // Backdrop v1.0.0 release.
    'changed' => 1474011660, // Backdrop v1.5.0 release.
  );

  entity_create('node', $edit)->save();
  $node = $this->backdropGetNodeByTitle($edit['title']);
  $this->assertEqual($node->created, 1421394600, 'Creating a node uses user-set "created" timestamp.');
  $this->assertNotEqual($node->changed, 1474011660, 'Creating a node doesn\'t use user-set "changed" timestamp.');

  // Update the timestamps.
  $node->created = 1474011660;
  $node->changed = 1421394600;

  $node->save();
  $node = $this->backdropGetNodeByTitle($edit['title'], TRUE);
  $this->assertEqual($node->created, 1474011660, 'Updating a node uses user-set "created" timestamp.');
  $this->assertNotEqual($node->changed, 1421394600, 'Updating a node doesn\'t use user-set "changed" timestamp.');
}