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