Pull merge.
[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       // Sun, 19 Nov 1978 05:00:00 GMT.
113       'created' => 280299600,
114       // Drupal 1.0 release.
115       'changed' => 979534800,
116     ];
117
118     Node::create($edit)->save();
119     $node = $this->drupalGetNodeByTitle($edit['title']);
120     $this->assertEqual($node->getCreatedTime(), 280299600, 'Creating a node programmatically uses programmatically set "created" timestamp.');
121     $this->assertEqual($node->getChangedTime(), 979534800, 'Creating a node programmatically uses programmatically set "changed" timestamp.');
122
123     // Update the timestamps.
124     $node->setCreatedTime(979534800);
125     $node->changed = 280299600;
126
127     $node->save();
128     $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
129     $this->assertEqual($node->getCreatedTime(), 979534800, 'Updating a node uses user-set "created" timestamp.');
130     // Allowing setting changed timestamps is required, see
131     // Drupal\content_translation\ContentTranslationMetadataWrapper::setChangedTime($timestamp)
132     // for example.
133     $this->assertEqual($node->getChangedTime(), 280299600, 'Updating a node uses user-set "changed" timestamp.');
134   }
135
136   /**
137    * Tests node presave and static node load cache.
138    *
139    * This test determines changes in hook_ENTITY_TYPE_presave() and verifies
140    * that the static node load cache is cleared upon save.
141    */
142   public function testDeterminingChanges() {
143     // Initial creation.
144     $node = Node::create([
145       'uid' => $this->webUser->id(),
146       'type' => 'article',
147       'title' => 'test_changes',
148     ]);
149     $node->save();
150
151     // Update the node without applying changes.
152     $node->save();
153     $this->assertEqual($node->label(), 'test_changes', 'No changes have been determined.');
154
155     // Apply changes.
156     $node->title = 'updated';
157     $node->save();
158
159     // The hook implementations node_test_node_presave() and
160     // node_test_node_update() determine changes and change the title.
161     $this->assertEqual($node->label(), 'updated_presave_update', 'Changes have been determined.');
162
163     // Test the static node load cache to be cleared.
164     $node = Node::load($node->id());
165     $this->assertEqual($node->label(), 'updated_presave', 'Static cache has been cleared.');
166   }
167
168   /**
169    * Tests saving a node on node insert.
170    *
171    * This test ensures that a node has been fully saved when
172    * hook_ENTITY_TYPE_insert() is invoked, so that the node can be saved again
173    * in a hook implementation without errors.
174    *
175    * @see node_test_node_insert()
176    */
177   public function testNodeSaveOnInsert() {
178     // node_test_node_insert() triggers a save on insert if the title equals
179     // 'new'.
180     $node = $this->drupalCreateNode(['title' => 'new']);
181     $this->assertEqual($node->getTitle(), 'Node ' . $node->id(), 'Node saved on node insert.');
182   }
183
184 }