PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/node/src/Tests/NodeSaveTest.php

https://gitlab.com/guillaumev/alkarama
PHP | 182 lines | 93 code | 28 blank | 61 comment | 0 complexity | 8aaf52d4731c6f009c71710de79b9e4c MD5 | raw file
  1. <?php
  2. namespace Drupal\node\Tests;
  3. use Drupal\node\Entity\Node;
  4. /**
  5. * Tests $node->save() for saving content.
  6. *
  7. * @group node
  8. */
  9. class NodeSaveTest extends NodeTestBase {
  10. /**
  11. * A normal logged in user.
  12. *
  13. * @var \Drupal\user\UserInterface
  14. */
  15. protected $webUser;
  16. /**
  17. * Modules to enable.
  18. *
  19. * @var array
  20. */
  21. public static $modules = array('node_test');
  22. protected function setUp() {
  23. parent::setUp();
  24. // Create a user that is allowed to post; we'll use this to test the submission.
  25. $web_user = $this->drupalCreateUser(array('create article content'));
  26. $this->drupalLogin($web_user);
  27. $this->webUser = $web_user;
  28. }
  29. /**
  30. * Checks whether custom node IDs are saved properly during an import operation.
  31. *
  32. * Workflow:
  33. * - first create a piece of content
  34. * - save the content
  35. * - check if node exists
  36. */
  37. function testImport() {
  38. // Node ID must be a number that is not in the database.
  39. $nids = \Drupal::entityManager()->getStorage('node')->getQuery()
  40. ->sort('nid', 'DESC')
  41. ->range(0, 1)
  42. ->execute();
  43. $max_nid = reset($nids);
  44. $test_nid = $max_nid + mt_rand(1000, 1000000);
  45. $title = $this->randomMachineName(8);
  46. $node = array(
  47. 'title' => $title,
  48. 'body' => array(array('value' => $this->randomMachineName(32))),
  49. 'uid' => $this->webUser->id(),
  50. 'type' => 'article',
  51. 'nid' => $test_nid,
  52. );
  53. /** @var \Drupal\node\NodeInterface $node */
  54. $node = Node::create($node);
  55. $node->enforceIsNew();
  56. $this->assertEqual($node->getOwnerId(), $this->webUser->id());
  57. $node->save();
  58. // Test the import.
  59. $node_by_nid = Node::load($test_nid);
  60. $this->assertTrue($node_by_nid, 'Node load by node ID.');
  61. $node_by_title = $this->drupalGetNodeByTitle($title);
  62. $this->assertTrue($node_by_title, 'Node load by node title.');
  63. }
  64. /**
  65. * Verifies accuracy of the "created" and "changed" timestamp functionality.
  66. */
  67. function testTimestamps() {
  68. // Use the default timestamps.
  69. $edit = array(
  70. 'uid' => $this->webUser->id(),
  71. 'type' => 'article',
  72. 'title' => $this->randomMachineName(8),
  73. );
  74. Node::create($edit)->save();
  75. $node = $this->drupalGetNodeByTitle($edit['title']);
  76. $this->assertEqual($node->getCreatedTime(), REQUEST_TIME, 'Creating a node sets default "created" timestamp.');
  77. $this->assertEqual($node->getChangedTime(), REQUEST_TIME, 'Creating a node sets default "changed" timestamp.');
  78. // Store the timestamps.
  79. $created = $node->getCreatedTime();
  80. $node->save();
  81. $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
  82. $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');
  83. // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
  84. $node->title = 'testing_node_presave';
  85. $node->save();
  86. $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE);
  87. $this->assertEqual($node->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.');
  88. $this->assertEqual($node->getChangedTime(), 979534800, 'Saving a node uses "changed" timestamp set in presave hook.');
  89. // Programmatically set the timestamps on the node.
  90. $edit = array(
  91. 'uid' => $this->webUser->id(),
  92. 'type' => 'article',
  93. 'title' => $this->randomMachineName(8),
  94. 'created' => 280299600, // Sun, 19 Nov 1978 05:00:00 GMT
  95. 'changed' => 979534800, // Drupal 1.0 release.
  96. );
  97. Node::create($edit)->save();
  98. $node = $this->drupalGetNodeByTitle($edit['title']);
  99. $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.');
  100. $this->assertEqual($node->getChangedTime(), 979534800, 'Creating a node programmatically uses programmatically set "changed" timestamp.');
  101. // Update the timestamps.
  102. $node->setCreatedTime(979534800);
  103. $node->changed = 280299600;
  104. $node->save();
  105. $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
  106. $this->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.');
  107. // Allowing setting changed timestamps is required, see
  108. // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
  109. // for example.
  110. $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.');
  111. }
  112. /**
  113. * Tests node presave and static node load cache.
  114. *
  115. * This test determines changes in hook_ENTITY_TYPE_presave() and verifies
  116. * that the static node load cache is cleared upon save.
  117. */
  118. function testDeterminingChanges() {
  119. // Initial creation.
  120. $node = Node::create([
  121. 'uid' => $this->webUser->id(),
  122. 'type' => 'article',
  123. 'title' => 'test_changes',
  124. ]);
  125. $node->save();
  126. // Update the node without applying changes.
  127. $node->save();
  128. $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
  129. // Apply changes.
  130. $node->title = 'updated';
  131. $node->save();
  132. // The hook implementations node_test_node_presave() and
  133. // node_test_node_update() determine changes and change the title.
  134. $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.');
  135. // Test the static node load cache to be cleared.
  136. $node = Node::load($node->id());
  137. $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
  138. }
  139. /**
  140. * Tests saving a node on node insert.
  141. *
  142. * This test ensures that a node has been fully saved when
  143. * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again
  144. * in a hook implementation without errors.
  145. *
  146. * @see node_test_node_insert()
  147. */
  148. function testNodeSaveOnInsert() {
  149. // node_test_node_insert() triggers a save on insert if the title equals
  150. // 'new'.
  151. $node = $this->drupalCreateNode(array('title' => 'new'));
  152. $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
  153. }
  154. }