You are here

function NodeSaveTest::testTimestamps in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Tests/NodeSaveTest.php \Drupal\node\Tests\NodeSaveTest::testTimestamps()

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

File

core/modules/node/src/Tests/NodeSaveTest.php, line 84
Contains \Drupal\node\Tests\NodeSaveTest.

Class

NodeSaveTest
Tests $node->save() for saving content.

Namespace

Drupal\node\Tests

Code

function testTimestamps() {

  // Use the default timestamps.
  $edit = array(
    'uid' => $this->webUser
      ->id(),
    'type' => 'article',
    'title' => $this
      ->randomMachineName(8),
  );
  entity_create('node', $edit)
    ->save();
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->assertEqual($node
    ->getCreatedTime(), REQUEST_TIME, 'Creating a node sets default "created" timestamp.');
  $this
    ->assertEqual($node
    ->getChangedTime(), REQUEST_TIME, 'Creating a node sets default "changed" timestamp.');

  // Store the timestamps.
  $created = $node
    ->getCreatedTime();
  $node
    ->save();
  $node = $this
    ->drupalGetNodeByTitle($edit['title'], TRUE);
  $this
    ->assertEqual($node
    ->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');

  // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
  $node->title = 'testing_node_presave';
  $node
    ->save();
  $node = $this
    ->drupalGetNodeByTitle('testing_node_presave', TRUE);
  $this
    ->assertEqual($node
    ->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.');
  $this
    ->assertEqual($node
    ->getChangedTime(), 979534800, 'Saving a node uses "changed" timestamp set in presave hook.');

  // Programmatically set the timestamps on the node.
  $edit = array(
    'uid' => $this->webUser
      ->id(),
    'type' => 'article',
    'title' => $this
      ->randomMachineName(8),
    'created' => 280299600,
    // Sun, 19 Nov 1978 05:00:00 GMT
    'changed' => 979534800,
  );
  entity_create('node', $edit)
    ->save();
  $node = $this
    ->drupalGetNodeByTitle($edit['title']);
  $this
    ->assertEqual($node
    ->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.');
  $this
    ->assertEqual($node
    ->getChangedTime(), 979534800, 'Creating a node programmatically uses programmatically set "changed" timestamp.');

  // Update the timestamps.
  $node
    ->setCreatedTime(979534800);
  $node->changed = 280299600;
  $node
    ->save();
  $node = $this
    ->drupalGetNodeByTitle($edit['title'], TRUE);
  $this
    ->assertEqual($node
    ->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.');

  // Allowing setting changed timestamps is required, see
  // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
  // for example.
  $this
    ->assertEqual($node
    ->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.');
}