function NodeSaveTest::testTimestamps

Same name in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeSaveTest.php \Drupal\Tests\node\Functional\NodeSaveTest::testTimestamps()
  2. 10 core/modules/node/tests/src/Functional/NodeSaveTest.php \Drupal\Tests\node\Functional\NodeSaveTest::testTimestamps()
  3. 11.x core/modules/node/tests/src/Functional/NodeSaveTest.php \Drupal\Tests\node\Functional\NodeSaveTest::testTimestamps()

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

File

core/modules/node/tests/src/Functional/NodeSaveTest.php, line 88

Class

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

Namespace

Drupal\Tests\node\Functional

Code

public function testTimestamps() {
    // Use the default timestamps.
    $edit = [
        'uid' => $this->webUser
            ->id(),
        'type' => 'article',
        'title' => $this->randomMachineName(8),
    ];
    Node::create($edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertEquals(REQUEST_TIME, $node->getCreatedTime(), 'Creating a node sets default "created" timestamp.');
    $this->assertEquals(REQUEST_TIME, $node->getChangedTime(), 'Creating a node sets default "changed" timestamp.');
    // Store the timestamps.
    $created = $node->getCreatedTime();
    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
    $this->assertEquals($created, $node->getCreatedTime(), '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->assertEquals(280299600, $node->getCreatedTime(), 'Saving a node uses "created" timestamp set in presave hook.');
    $this->assertEquals(979534800, $node->getChangedTime(), 'Saving a node uses "changed" timestamp set in presave hook.');
    // Programmatically set the timestamps on the node.
    $edit = [
        'uid' => $this->webUser
            ->id(),
        'type' => 'article',
        'title' => $this->randomMachineName(8),
        // Sun, 19 Nov 1978 05:00:00 GMT.
'created' => 280299600,
        // Drupal 1.0 release.
'changed' => 979534800,
    ];
    Node::create($edit)->save();
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->assertEquals(280299600, $node->getCreatedTime(), 'Creating a node programmatically uses programmatically set "created" timestamp.');
    $this->assertEquals(979534800, $node->getChangedTime(), '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->assertEquals(979534800, $node->getCreatedTime(), 'Updating a node uses user-set "created" timestamp.');
    // Allowing setting changed timestamps is required, see
    // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
    // for example.
    $this->assertEquals(280299600, $node->getChangedTime(), 'Updating a node uses user-set "changed" timestamp.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.