View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\node\Entity\Node;
class NodeSaveTest extends NodeTestBase {
protected $webUser;
protected static $modules = [
'node_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'create article content',
]);
$this
->drupalLogin($web_user);
$this->webUser = $web_user;
}
public function testImport() {
$nids = \Drupal::entityTypeManager()
->getStorage('node')
->getQuery()
->accessCheck(FALSE)
->sort('nid', 'DESC')
->range(0, 1)
->execute();
$max_nid = reset($nids);
$test_nid = $max_nid + mt_rand(1000, 1000000);
$title = $this
->randomMachineName(8);
$node = [
'title' => $title,
'body' => [
[
'value' => $this
->randomMachineName(32),
],
],
'uid' => $this->webUser
->id(),
'type' => 'article',
'nid' => $test_nid,
];
$node = Node::create($node);
$node
->enforceIsNew();
$this
->assertEquals($this->webUser
->id(), $node
->getOwnerId());
$node
->save();
$node_by_nid = Node::load($test_nid);
$this
->assertNotEmpty($node_by_nid, 'Node load by node ID.');
$node_by_title = $this
->drupalGetNodeByTitle($title);
$this
->assertNotEmpty($node_by_title, 'Node load by node title.');
}
public function testTimestamps() {
$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.');
$created = $node
->getCreatedTime();
$node
->save();
$node = $this
->drupalGetNodeByTitle($edit['title'], TRUE);
$this
->assertEquals($created, $node
->getCreatedTime(), 'Updating a node preserves "created" timestamp.');
$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.');
$edit = [
'uid' => $this->webUser
->id(),
'type' => 'article',
'title' => $this
->randomMachineName(8),
'created' => 280299600,
'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.');
$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.');
$this
->assertEquals(280299600, $node
->getChangedTime(), 'Updating a node uses user-set "changed" timestamp.');
}
public function testDeterminingChanges() {
$node = Node::create([
'uid' => $this->webUser
->id(),
'type' => 'article',
'title' => 'test_changes',
]);
$node
->save();
$node
->save();
$this
->assertEquals('test_changes', $node
->label(), 'No changes have been determined.');
$node->title = 'updated';
$node
->save();
$this
->assertEquals('updated_presave_update', $node
->label(), 'Changes have been determined.');
$node = Node::load($node
->id());
$this
->assertEquals('updated_presave', $node
->label(), 'Static cache has been cleared.');
}
public function testNodeSaveOnInsert() {
$node = $this
->drupalCreateNode([
'title' => 'new',
]);
$this
->assertEquals('Node ' . $node
->id(), $node
->getTitle(), 'Node saved on node insert.');
}
}