Chris@0: save() for saving content. Chris@0: * Chris@0: * @group node Chris@0: */ Chris@0: class NodeSaveTest extends NodeTestBase { Chris@0: Chris@0: /** Chris@0: * A normal logged in user. Chris@0: * Chris@0: * @var \Drupal\user\UserInterface Chris@0: */ Chris@0: protected $webUser; Chris@0: Chris@0: /** Chris@0: * Modules to enable. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public static $modules = ['node_test']; Chris@0: Chris@0: protected function setUp() { Chris@0: parent::setUp(); Chris@0: Chris@0: // Create a user that is allowed to post; we'll use this to test the submission. Chris@0: $web_user = $this->drupalCreateUser(['create article content']); Chris@0: $this->drupalLogin($web_user); Chris@0: $this->webUser = $web_user; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether custom node IDs are saved properly during an import operation. Chris@0: * Chris@0: * Workflow: Chris@0: * - first create a piece of content Chris@0: * - save the content Chris@0: * - check if node exists Chris@0: */ Chris@0: public function testImport() { Chris@0: // Node ID must be a number that is not in the database. Chris@0: $nids = \Drupal::entityManager()->getStorage('node')->getQuery() Chris@0: ->sort('nid', 'DESC') Chris@0: ->range(0, 1) Chris@0: ->execute(); Chris@0: $max_nid = reset($nids); Chris@0: $test_nid = $max_nid + mt_rand(1000, 1000000); Chris@0: $title = $this->randomMachineName(8); Chris@0: $node = [ Chris@0: 'title' => $title, Chris@0: 'body' => [['value' => $this->randomMachineName(32)]], Chris@0: 'uid' => $this->webUser->id(), Chris@0: 'type' => 'article', Chris@0: 'nid' => $test_nid, Chris@0: ]; Chris@0: /** @var \Drupal\node\NodeInterface $node */ Chris@0: $node = Node::create($node); Chris@0: $node->enforceIsNew(); Chris@0: Chris@0: $this->assertEqual($node->getOwnerId(), $this->webUser->id()); Chris@0: Chris@0: $node->save(); Chris@0: // Test the import. Chris@0: $node_by_nid = Node::load($test_nid); Chris@0: $this->assertTrue($node_by_nid, 'Node load by node ID.'); Chris@0: Chris@0: $node_by_title = $this->drupalGetNodeByTitle($title); Chris@0: $this->assertTrue($node_by_title, 'Node load by node title.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies accuracy of the "created" and "changed" timestamp functionality. Chris@0: */ Chris@0: public function testTimestamps() { Chris@0: // Use the default timestamps. Chris@0: $edit = [ Chris@0: 'uid' => $this->webUser->id(), Chris@0: 'type' => 'article', Chris@0: 'title' => $this->randomMachineName(8), Chris@0: ]; Chris@0: Chris@0: Node::create($edit)->save(); Chris@0: $node = $this->drupalGetNodeByTitle($edit['title']); Chris@0: $this->assertEqual($node->getCreatedTime(), REQUEST_TIME, 'Creating a node sets default "created" timestamp.'); Chris@0: $this->assertEqual($node->getChangedTime(), REQUEST_TIME, 'Creating a node sets default "changed" timestamp.'); Chris@0: Chris@0: // Store the timestamps. Chris@0: $created = $node->getCreatedTime(); Chris@0: Chris@0: $node->save(); Chris@0: $node = $this->drupalGetNodeByTitle($edit['title'], TRUE); Chris@0: $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.'); Chris@0: Chris@0: // Programmatically set the timestamps using hook_ENTITY_TYPE_presave(). Chris@0: $node->title = 'testing_node_presave'; Chris@0: Chris@0: $node->save(); Chris@0: $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE); Chris@0: $this->assertEqual($node->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.'); Chris@0: $this->assertEqual($node->getChangedTime(), 979534800, 'Saving a node uses "changed" timestamp set in presave hook.'); Chris@0: Chris@0: // Programmatically set the timestamps on the node. Chris@0: $edit = [ Chris@0: 'uid' => $this->webUser->id(), Chris@0: 'type' => 'article', Chris@0: 'title' => $this->randomMachineName(8), Chris@0: // Sun, 19 Nov 1978 05:00:00 GMT. Chris@0: 'created' => 280299600, Chris@0: // Drupal 1.0 release. Chris@0: 'changed' => 979534800, Chris@0: ]; Chris@0: Chris@0: Node::create($edit)->save(); Chris@0: $node = $this->drupalGetNodeByTitle($edit['title']); Chris@0: $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.'); Chris@0: $this->assertEqual($node->getChangedTime(), 979534800, 'Creating a node programmatically uses programmatically set "changed" timestamp.'); Chris@0: Chris@0: // Update the timestamps. Chris@0: $node->setCreatedTime(979534800); Chris@0: $node->changed = 280299600; Chris@0: Chris@0: $node->save(); Chris@0: $node = $this->drupalGetNodeByTitle($edit['title'], TRUE); Chris@0: $this->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.'); Chris@0: // Allowing setting changed timestamps is required, see Chris@0: // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp) Chris@0: // for example. Chris@0: $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests node presave and static node load cache. Chris@0: * Chris@0: * This test determines changes in hook_ENTITY_TYPE_presave() and verifies Chris@0: * that the static node load cache is cleared upon save. Chris@0: */ Chris@0: public function testDeterminingChanges() { Chris@0: // Initial creation. Chris@0: $node = Node::create([ Chris@0: 'uid' => $this->webUser->id(), Chris@0: 'type' => 'article', Chris@0: 'title' => 'test_changes', Chris@0: ]); Chris@0: $node->save(); Chris@0: Chris@0: // Update the node without applying changes. Chris@0: $node->save(); Chris@0: $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.'); Chris@0: Chris@0: // Apply changes. Chris@0: $node->title = 'updated'; Chris@0: $node->save(); Chris@0: Chris@0: // The hook implementations node_test_node_presave() and Chris@0: // node_test_node_update() determine changes and change the title. Chris@0: $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.'); Chris@0: Chris@0: // Test the static node load cache to be cleared. Chris@0: $node = Node::load($node->id()); Chris@0: $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests saving a node on node insert. Chris@0: * Chris@0: * This test ensures that a node has been fully saved when Chris@0: * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again Chris@0: * in a hook implementation without errors. Chris@0: * Chris@0: * @see node_test_node_insert() Chris@0: */ Chris@0: public function testNodeSaveOnInsert() { Chris@0: // node_test_node_insert() triggers a save on insert if the title equals Chris@0: // 'new'. Chris@0: $node = $this->drupalCreateNode(['title' => 'new']); Chris@0: $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.'); Chris@0: } Chris@0: Chris@0: }