ced995714218e4a9a1bb9e848045d3220a85becb
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeSaveTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 use Drupal\node\Entity\Node;
6
7 /**
8  * Tests $node->save() for saving content.
9  *
10  * @group node
11  */
12 class NodeSaveTest extends NodeTestBase {
13
14   /**
15    * A normal logged in user.
16    *
17    * @var \Drupal\user\UserInterface
18    */
19   protected $webUser;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['node_test'];
27
28   protected function setUp() {
29     parent::setUp();
30
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;
35   }
36
37   /**
38    * Checks whether custom node IDs are saved properly during an import operation.
39    *
40    * Workflow:
41    *  - first create a piece of content
42    *  - save the content
43    *  - check if node exists
44    */
45   public function testImport() {
46     // Node ID must be a number that is not in the database.
47     $nids = \Drupal::entityManager()->getStorage('node')->getQuery()
48       ->sort('nid', 'DESC')
49       ->range(0, 1)
50       ->execute();
51     $max_nid = reset($nids);
52     $test_nid = $max_nid + mt_rand(1000, 1000000);
53     $title = $this->randomMachineName(8);
54     $node = [
55       'title' => $title,
56       'body' => [['value' => $this->randomMachineName(32)]],
57       'uid' => $this->webUser->id(),
58       'type' => 'article',
59       'nid' => $test_nid,
60     ];
61     /** @var \Drupal\node\NodeInterface $node */
62     $node = Node::create($node);
63     $node->enforceIsNew();
64
65     $this->assertEqual($node->getOwnerId(), $this->webUser->id());
66
67     $node->save();
68     // Test the import.
69     $node_by_nid = Node::load($test_nid);
70     $this->assertTrue($node_by_nid, 'Node load by node ID.');
71
72     $node_by_title = $this->drupalGetNodeByTitle($title);
73     $this->assertTrue($node_by_title, 'Node load by node title.');
74   }
75
76   /**
77    * Verifies accuracy of the "created" and "changed" timestamp functionality.
78    */
79   public function testTimestamps() {
80     // Use the default timestamps.
81     $edit = [
82       'uid' => $this->webUser->id(),
83       'type' => 'article',
84       'title' => $this->randomMachineName(8),
85     ];
86
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.');
91
92     // Store the timestamps.
93     $created = $node->getCreatedTime();
94
95     $node->save();
96     $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
97     $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');
98
99     // Programmatically set the timestamps using hook_ENTITY_TYPE_presave().
100     $node->title = 'testing_node_presave';
101
102     $node->save();
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.');
106
107     // Programmatically set the timestamps on the node.
108     $edit = [
109       'uid' => $this->webUser->id(),
110       'type' => 'article',
111       'title' => $this->randomMachineName(8),
112       'created' => 280299600, // Sun, 19 Nov 1978 05:00:00 GMT
113       'changed' => 979534800, // Drupal 1.0 release.
114     ];
115
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.');
120
121     // Update the timestamps.
122     $node->setCreatedTime(979534800);
123     $node->changed = 280299600;
124
125     $node->save();
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)
130     // for example.
131     $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.');
132   }
133
134   /**
135    * Tests node presave and static node load cache.
136    *
137    * This test determines changes in hook_ENTITY_TYPE_presave() and verifies
138    * that the static node load cache is cleared upon save.
139    */
140   public function testDeterminingChanges() {
141     // Initial creation.
142     $node = Node::create([
143       'uid' => $this->webUser->id(),
144       'type' => 'article',
145       'title' => 'test_changes',
146     ]);
147     $node->save();
148
149     // Update the node without applying changes.
150     $node->save();
151     $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
152
153     // Apply changes.
154     $node->title = 'updated';
155     $node->save();
156
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.');
160
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.');
164   }
165
166   /**
167    * Tests saving a node on node insert.
168    *
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.
172    *
173    * @see node_test_node_insert()
174    */
175   public function testNodeSaveOnInsert() {
176     // node_test_node_insert() triggers a save on insert if the title equals
177     // 'new'.
178     $node = $this->drupalCreateNode(['title' => 'new']);
179     $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
180   }
181
182 }