X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fnode%2Ftests%2Fsrc%2FFunctional%2FNodeSaveTest.php;fp=web%2Fcore%2Fmodules%2Fnode%2Ftests%2Fsrc%2FFunctional%2FNodeSaveTest.php;h=ced995714218e4a9a1bb9e848045d3220a85becb;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/node/tests/src/Functional/NodeSaveTest.php b/web/core/modules/node/tests/src/Functional/NodeSaveTest.php new file mode 100644 index 000000000..ced995714 --- /dev/null +++ b/web/core/modules/node/tests/src/Functional/NodeSaveTest.php @@ -0,0 +1,182 @@ +save() for saving content. + * + * @group node + */ +class NodeSaveTest extends NodeTestBase { + + /** + * A normal logged in user. + * + * @var \Drupal\user\UserInterface + */ + protected $webUser; + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = ['node_test']; + + protected function setUp() { + parent::setUp(); + + // Create a user that is allowed to post; we'll use this to test the submission. + $web_user = $this->drupalCreateUser(['create article content']); + $this->drupalLogin($web_user); + $this->webUser = $web_user; + } + + /** + * Checks whether custom node IDs are saved properly during an import operation. + * + * Workflow: + * - first create a piece of content + * - save the content + * - check if node exists + */ + public function testImport() { + // Node ID must be a number that is not in the database. + $nids = \Drupal::entityManager()->getStorage('node')->getQuery() + ->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, + ]; + /** @var \Drupal\node\NodeInterface $node */ + $node = Node::create($node); + $node->enforceIsNew(); + + $this->assertEqual($node->getOwnerId(), $this->webUser->id()); + + $node->save(); + // Test the import. + $node_by_nid = Node::load($test_nid); + $this->assertTrue($node_by_nid, 'Node load by node ID.'); + + $node_by_title = $this->drupalGetNodeByTitle($title); + $this->assertTrue($node_by_title, 'Node load by node title.'); + } + + /** + * Verifies accuracy of the "created" and "changed" timestamp functionality. + */ + 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->assertEqual($node->getCreatedTime(), REQUEST_TIME, 'Creating a node sets default "created" timestamp.'); + $this->assertEqual($node->getChangedTime(), REQUEST_TIME, 'Creating a node sets default "changed" timestamp.'); + + // Store the timestamps. + $created = $node->getCreatedTime(); + + $node->save(); + $node = $this->drupalGetNodeByTitle($edit['title'], TRUE); + $this->assertEqual($node->getCreatedTime(), $created, '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->assertEqual($node->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.'); + $this->assertEqual($node->getChangedTime(), 979534800, '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), + 'created' => 280299600, // Sun, 19 Nov 1978 05:00:00 GMT + 'changed' => 979534800, // Drupal 1.0 release. + ]; + + Node::create($edit)->save(); + $node = $this->drupalGetNodeByTitle($edit['title']); + $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.'); + $this->assertEqual($node->getChangedTime(), 979534800, '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->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.'); + // Allowing setting changed timestamps is required, see + // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp) + // for example. + $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.'); + } + + /** + * Tests node presave and static node load cache. + * + * This test determines changes in hook_ENTITY_TYPE_presave() and verifies + * that the static node load cache is cleared upon save. + */ + public function testDeterminingChanges() { + // Initial creation. + $node = Node::create([ + 'uid' => $this->webUser->id(), + 'type' => 'article', + 'title' => 'test_changes', + ]); + $node->save(); + + // Update the node without applying changes. + $node->save(); + $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.'); + + // Apply changes. + $node->title = 'updated'; + $node->save(); + + // The hook implementations node_test_node_presave() and + // node_test_node_update() determine changes and change the title. + $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.'); + + // Test the static node load cache to be cleared. + $node = Node::load($node->id()); + $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.'); + } + + /** + * Tests saving a node on node insert. + * + * This test ensures that a node has been fully saved when + * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again + * in a hook implementation without errors. + * + * @see node_test_node_insert() + */ + public function testNodeSaveOnInsert() { + // node_test_node_insert() triggers a save on insert if the title equals + // 'new'. + $node = $this->drupalCreateNode(['title' => 'new']); + $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.'); + } + +}