3 namespace Drupal\Tests\node\Functional;
5 use Drupal\node\Entity\Node;
8 * Tests $node->save() for saving content.
12 class NodeSaveTest extends NodeTestBase {
15 * A normal logged in user.
17 * @var \Drupal\user\UserInterface
26 public static $modules = ['node_test'];
28 protected function setUp() {
31 // Create a user that is allowed to post; we'll use this to test the submission.
32 $web_user = $this->drupalCreateUser(['create article content']);
33 $this->drupalLogin($web_user);
34 $this->webUser = $web_user;
38 * Checks whether custom node IDs are saved properly during an import operation.
41 * - first create a piece of content
43 * - check if node exists
45 public function testImport() {
46 // Node ID must be a number that is not in the database.
47 $nids = \Drupal::entityManager()->getStorage('node')->getQuery()
51 $max_nid = reset($nids);
52 $test_nid = $max_nid + mt_rand(1000, 1000000);
53 $title = $this->randomMachineName(8);
56 'body' => [['value' => $this->randomMachineName(32)]],
57 'uid' => $this->webUser->id(),
61 /** @var \Drupal\node\NodeInterface $node */
62 $node = Node::create($node);
63 $node->enforceIsNew();
65 $this->assertEqual($node->getOwnerId(), $this->webUser->id());
69 $node_by_nid = Node::load($test_nid);
70 $this->assertTrue($node_by_nid, 'Node load by node ID.');
72 $node_by_title = $this->drupalGetNodeByTitle($title);
73 $this->assertTrue($node_by_title, 'Node load by node title.');
77 * Verifies accuracy of the "created" and "changed" timestamp functionality.
79 public function testTimestamps() {
80 // Use the default timestamps.
82 'uid' => $this->webUser->id(),
84 'title' => $this->randomMachineName(8),
87 Node::create($edit)->save();
88 $node = $this->drupalGetNodeByTitle($edit['title']);
89 $this->assertEqual($node->getCreatedTime(), REQUEST_TIME, 'Creating a node sets default "created" timestamp.');
90 $this->assertEqual($node->getChangedTime(), REQUEST_TIME, 'Creating a node sets default "changed" timestamp.');
92 // Store the timestamps.
93 $created = $node->getCreatedTime();
96 $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
97 $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');
99 // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
100 $node->title = 'testing_node_presave';
103 $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE);
104 $this->assertEqual($node->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.');
105 $this->assertEqual($node->getChangedTime(), 979534800, 'Saving a node uses "changed" timestamp set in presave hook.');
107 // Programmatically set the timestamps on the node.
109 'uid' => $this->webUser->id(),
111 'title' => $this->randomMachineName(8),
112 'created' => 280299600, // Sun, 19 Nov 1978 05:00:00 GMT
113 'changed' => 979534800, // Drupal 1.0 release.
116 Node::create($edit)->save();
117 $node = $this->drupalGetNodeByTitle($edit['title']);
118 $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.');
119 $this->assertEqual($node->getChangedTime(), 979534800, 'Creating a node programmatically uses programmatically set "changed" timestamp.');
121 // Update the timestamps.
122 $node->setCreatedTime(979534800);
123 $node->changed = 280299600;
126 $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
127 $this->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.');
128 // Allowing setting changed timestamps is required, see
129 // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
131 $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.');
135 * Tests node presave and static node load cache.
137 * This test determines changes in hook_ENTITY_TYPE_presave() and verifies
138 * that the static node load cache is cleared upon save.
140 public function testDeterminingChanges() {
142 $node = Node::create([
143 'uid' => $this->webUser->id(),
145 'title' => 'test_changes',
149 // Update the node without applying changes.
151 $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
154 $node->title = 'updated';
157 // The hook implementations node_test_node_presave() and
158 // node_test_node_update() determine changes and change the title.
159 $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.');
161 // Test the static node load cache to be cleared.
162 $node = Node::load($node->id());
163 $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
167 * Tests saving a node on node insert.
169 * This test ensures that a node has been fully saved when
170 * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again
171 * in a hook implementation without errors.
173 * @see node_test_node_insert()
175 public function testNodeSaveOnInsert() {
176 // node_test_node_insert() triggers a save on insert if the title equals
178 $node = $this->drupalCreateNode(['title' => 'new']);
179 $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');