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 // Sun, 19 Nov 1978 05:00:00 GMT.
113 'created' => 280299600,
114 // Drupal 1.0 release.
115 'changed' => 979534800,
118 Node::create($edit)->save();
119 $node = $this->drupalGetNodeByTitle($edit['title']);
120 $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.');
121 $this->assertEqual($node->getChangedTime(), 979534800, 'Creating a node programmatically uses programmatically set "changed" timestamp.');
123 // Update the timestamps.
124 $node->setCreatedTime(979534800);
125 $node->changed = 280299600;
128 $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
129 $this->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.');
130 // Allowing setting changed timestamps is required, see
131 // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
133 $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.');
137 * Tests node presave and static node load cache.
139 * This test determines changes in hook_ENTITY_TYPE_presave() and verifies
140 * that the static node load cache is cleared upon save.
142 public function testDeterminingChanges() {
144 $node = Node::create([
145 'uid' => $this->webUser->id(),
147 'title' => 'test_changes',
151 // Update the node without applying changes.
153 $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
156 $node->title = 'updated';
159 // The hook implementations node_test_node_presave() and
160 // node_test_node_update() determine changes and change the title.
161 $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.');
163 // Test the static node load cache to be cleared.
164 $node = Node::load($node->id());
165 $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
169 * Tests saving a node on node insert.
171 * This test ensures that a node has been fully saved when
172 * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again
173 * in a hook implementation without errors.
175 * @see node_test_node_insert()
177 public function testNodeSaveOnInsert() {
178 // node_test_node_insert() triggers a save on insert if the title equals
180 $node = $this->drupalCreateNode(['title' => 'new']);
181 $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');