Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\node\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Database\Database;
|
Chris@0
|
6 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
7 use Drupal\node\Entity\Node;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Create a node and test saving it.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @group node
|
Chris@0
|
13 */
|
Chris@0
|
14 class NodeCreationTest extends NodeTestBase {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Modules to enable.
|
Chris@0
|
18 *
|
Chris@0
|
19 * Enable dummy module that implements hook_ENTITY_TYPE_insert() for
|
Chris@0
|
20 * exceptions (function node_test_exception_node_insert() ).
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 public static $modules = ['node_test_exception', 'dblog', 'test_page_test'];
|
Chris@0
|
25
|
Chris@0
|
26 protected function setUp() {
|
Chris@0
|
27 parent::setUp();
|
Chris@0
|
28
|
Chris@0
|
29 $web_user = $this->drupalCreateUser(['create page content', 'edit own page content']);
|
Chris@0
|
30 $this->drupalLogin($web_user);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * Creates a "Basic page" node and verifies its consistency in the database.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function testNodeCreation() {
|
Chris@0
|
37 $node_type_storage = \Drupal::entityManager()->getStorage('node_type');
|
Chris@0
|
38
|
Chris@0
|
39 // Test /node/add page with only one content type.
|
Chris@0
|
40 $node_type_storage->load('article')->delete();
|
Chris@0
|
41 $this->drupalGet('node/add');
|
Chris@0
|
42 $this->assertResponse(200);
|
Chris@0
|
43 $this->assertUrl('node/add/page');
|
Chris@0
|
44 // Create a node.
|
Chris@0
|
45 $edit = [];
|
Chris@0
|
46 $edit['title[0][value]'] = $this->randomMachineName(8);
|
Chris@0
|
47 $edit['body[0][value]'] = $this->randomMachineName(16);
|
Chris@0
|
48 $this->drupalPostForm('node/add/page', $edit, t('Save'));
|
Chris@0
|
49
|
Chris@0
|
50 // Check that the Basic page has been created.
|
Chris@0
|
51 $this->assertText(t('@post @title has been created.', ['@post' => 'Basic page', '@title' => $edit['title[0][value]']]), 'Basic page created.');
|
Chris@0
|
52
|
Chris@0
|
53 // Verify that the creation message contains a link to a node.
|
Chris@0
|
54 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
|
Chris@0
|
55 $this->assert(isset($view_link), 'The message area contains a link to a node');
|
Chris@0
|
56
|
Chris@0
|
57 // Check that the node exists in the database.
|
Chris@0
|
58 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@0
|
59 $this->assertTrue($node, 'Node found in database.');
|
Chris@0
|
60
|
Chris@0
|
61 // Verify that pages do not show submitted information by default.
|
Chris@0
|
62 $this->drupalGet('node/' . $node->id());
|
Chris@18
|
63 $this->assertNoText($node->getOwner()->getAccountName());
|
Chris@18
|
64 $this->assertNoText($this->container->get('date.formatter')->format($node->getCreatedTime()));
|
Chris@0
|
65
|
Chris@0
|
66 // Change the node type setting to show submitted by information.
|
Chris@0
|
67 /** @var \Drupal\node\NodeTypeInterface $node_type */
|
Chris@0
|
68 $node_type = $node_type_storage->load('page');
|
Chris@0
|
69 $node_type->setDisplaySubmitted(TRUE);
|
Chris@0
|
70 $node_type->save();
|
Chris@0
|
71
|
Chris@0
|
72 $this->drupalGet('node/' . $node->id());
|
Chris@18
|
73 $this->assertText($node->getOwner()->getAccountName());
|
Chris@18
|
74 $this->assertText($this->container->get('date.formatter')->format($node->getCreatedTime()));
|
Chris@0
|
75
|
Chris@0
|
76 // Check if the node revision checkbox is not rendered on node creation form.
|
Chris@0
|
77 $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']);
|
Chris@0
|
78 $this->drupalLogin($admin_user);
|
Chris@0
|
79 $this->drupalGet('node/add/page');
|
Chris@0
|
80 $this->assertNoFieldById('edit-revision', NULL, 'The revision checkbox is not present.');
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Verifies that a transaction rolls back the failed creation.
|
Chris@0
|
85 */
|
Chris@0
|
86 public function testFailedPageCreation() {
|
Chris@0
|
87 // Create a node.
|
Chris@0
|
88 $edit = [
|
Chris@0
|
89 'uid' => $this->loggedInUser->id(),
|
Chris@0
|
90 'name' => $this->loggedInUser->name,
|
Chris@0
|
91 'type' => 'page',
|
Chris@0
|
92 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
Chris@0
|
93 'title' => 'testing_transaction_exception',
|
Chris@0
|
94 ];
|
Chris@0
|
95
|
Chris@0
|
96 try {
|
Chris@0
|
97 // An exception is generated by node_test_exception_node_insert() if the
|
Chris@0
|
98 // title is 'testing_transaction_exception'.
|
Chris@0
|
99 Node::create($edit)->save();
|
Chris@0
|
100 $this->fail(t('Expected exception has not been thrown.'));
|
Chris@0
|
101 }
|
Chris@0
|
102 catch (\Exception $e) {
|
Chris@0
|
103 $this->pass(t('Expected exception has been thrown.'));
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 if (Database::getConnection()->supportsTransactions()) {
|
Chris@0
|
107 // Check that the node does not exist in the database.
|
Chris@0
|
108 $node = $this->drupalGetNodeByTitle($edit['title']);
|
Chris@0
|
109 $this->assertFalse($node, 'Transactions supported, and node not found in database.');
|
Chris@0
|
110 }
|
Chris@0
|
111 else {
|
Chris@0
|
112 // Check that the node exists in the database.
|
Chris@0
|
113 $node = $this->drupalGetNodeByTitle($edit['title']);
|
Chris@0
|
114 $this->assertTrue($node, 'Transactions not supported, and node found in database.');
|
Chris@0
|
115
|
Chris@0
|
116 // Check that the failed rollback was logged.
|
Chris@0
|
117 $records = static::getWatchdogIdsForFailedExplicitRollback();
|
Chris@0
|
118 $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 // Check that the rollback error was logged.
|
Chris@0
|
122 $records = static::getWatchdogIdsForTestExceptionRollback();
|
Chris@0
|
123 $this->assertTrue(count($records) > 0, 'Rollback explanatory error logged to watchdog.');
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * Creates an unpublished node and confirms correct redirect behavior.
|
Chris@0
|
128 */
|
Chris@0
|
129 public function testUnpublishedNodeCreation() {
|
Chris@0
|
130 // Set the front page to the test page.
|
Chris@0
|
131 $this->config('system.site')->set('page.front', '/test-page')->save();
|
Chris@0
|
132
|
Chris@0
|
133 // Set "Basic page" content type to be unpublished by default.
|
Chris@0
|
134 $fields = \Drupal::entityManager()->getFieldDefinitions('node', 'page');
|
Chris@0
|
135 $fields['status']->getConfig('page')
|
Chris@0
|
136 ->setDefaultValue(FALSE)
|
Chris@0
|
137 ->save();
|
Chris@0
|
138
|
Chris@0
|
139 // Create a node.
|
Chris@0
|
140 $edit = [];
|
Chris@0
|
141 $edit['title[0][value]'] = $this->randomMachineName(8);
|
Chris@0
|
142 $edit['body[0][value]'] = $this->randomMachineName(16);
|
Chris@0
|
143 $this->drupalPostForm('node/add/page', $edit, t('Save'));
|
Chris@0
|
144
|
Chris@0
|
145 // Check that the user was redirected to the home page.
|
Chris@0
|
146 $this->assertUrl('');
|
Chris@0
|
147 $this->assertText(t('Test page text'));
|
Chris@0
|
148
|
Chris@0
|
149 // Confirm that the node was created.
|
Chris@0
|
150 $this->assertText(t('@post @title has been created.', ['@post' => 'Basic page', '@title' => $edit['title[0][value]']]));
|
Chris@0
|
151
|
Chris@0
|
152 // Verify that the creation message contains a link to a node.
|
Chris@0
|
153 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
|
Chris@0
|
154 $this->assert(isset($view_link), 'The message area contains a link to a node');
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@18
|
158 * Creates nodes with different authored dates.
|
Chris@18
|
159 */
|
Chris@18
|
160 public function testAuthoredDate() {
|
Chris@18
|
161 $now = \Drupal::time()->getRequestTime();
|
Chris@18
|
162 $admin = $this->drupalCreateUser([], NULL, TRUE);
|
Chris@18
|
163 $this->drupalLogin($admin);
|
Chris@18
|
164
|
Chris@18
|
165 // Create a node with the default creation date.
|
Chris@18
|
166 $edit = [
|
Chris@18
|
167 'title[0][value]' => $this->randomMachineName(8),
|
Chris@18
|
168 'body[0][value]' => $this->randomMachineName(16),
|
Chris@18
|
169 ];
|
Chris@18
|
170 $this->drupalPostForm('node/add/page', $edit, 'Save');
|
Chris@18
|
171 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@18
|
172 $this->assertNotNull($node->getCreatedTime());
|
Chris@18
|
173
|
Chris@18
|
174 // Create a node with the custom creation date in the past.
|
Chris@18
|
175 $date = $now - 86400;
|
Chris@18
|
176 $edit = [
|
Chris@18
|
177 'title[0][value]' => $this->randomMachineName(8),
|
Chris@18
|
178 'body[0][value]' => $this->randomMachineName(16),
|
Chris@18
|
179 'created[0][value][date]' => date('Y-m-d', $date),
|
Chris@18
|
180 'created[0][value][time]' => date('H:i:s', $date),
|
Chris@18
|
181 ];
|
Chris@18
|
182 $this->drupalPostForm('node/add/page', $edit, 'Save');
|
Chris@18
|
183 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@18
|
184 $this->assertEquals($date, $node->getCreatedTime());
|
Chris@18
|
185
|
Chris@18
|
186 // Create a node with the custom creation date in the future.
|
Chris@18
|
187 $date = $now + 86400;
|
Chris@18
|
188 $edit = [
|
Chris@18
|
189 'title[0][value]' => $this->randomMachineName(8),
|
Chris@18
|
190 'body[0][value]' => $this->randomMachineName(16),
|
Chris@18
|
191 'created[0][value][date]' => date('Y-m-d', $date),
|
Chris@18
|
192 'created[0][value][time]' => date('H:i:s', $date),
|
Chris@18
|
193 ];
|
Chris@18
|
194 $this->drupalPostForm('node/add/page', $edit, 'Save');
|
Chris@18
|
195 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
Chris@18
|
196 $this->assertEquals($date, $node->getCreatedTime());
|
Chris@18
|
197
|
Chris@18
|
198 // Test an invalid date.
|
Chris@18
|
199 $edit = [
|
Chris@18
|
200 'title[0][value]' => $this->randomMachineName(8),
|
Chris@18
|
201 'body[0][value]' => $this->randomMachineName(16),
|
Chris@18
|
202 'created[0][value][date]' => '2013-13-13',
|
Chris@18
|
203 'created[0][value][time]' => '11:00:00',
|
Chris@18
|
204 ];
|
Chris@18
|
205 $this->drupalPostForm('node/add/page', $edit, 'Save');
|
Chris@18
|
206 $this->assertSession()->pageTextContains('The Authored on date is invalid.');
|
Chris@18
|
207 $this->assertFalse($this->drupalGetNodeByTitle($edit['title[0][value]']));
|
Chris@18
|
208
|
Chris@18
|
209 // Test an invalid time.
|
Chris@18
|
210 $edit = [
|
Chris@18
|
211 'title[0][value]' => $this->randomMachineName(8),
|
Chris@18
|
212 'body[0][value]' => $this->randomMachineName(16),
|
Chris@18
|
213 'created[0][value][date]' => '2012-01-01',
|
Chris@18
|
214 'created[0][value][time]' => '30:00:00',
|
Chris@18
|
215 ];
|
Chris@18
|
216 $this->drupalPostForm('node/add/page', $edit, 'Save');
|
Chris@18
|
217 $this->assertSession()->pageTextContains('The Authored on date is invalid.');
|
Chris@18
|
218 $this->assertFalse($this->drupalGetNodeByTitle($edit['title[0][value]']));
|
Chris@18
|
219 }
|
Chris@18
|
220
|
Chris@18
|
221 /**
|
Chris@0
|
222 * Tests the author autocompletion textfield.
|
Chris@0
|
223 */
|
Chris@0
|
224 public function testAuthorAutocomplete() {
|
Chris@0
|
225 $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']);
|
Chris@0
|
226 $this->drupalLogin($admin_user);
|
Chris@0
|
227
|
Chris@0
|
228 $this->drupalGet('node/add/page');
|
Chris@0
|
229
|
Chris@0
|
230 $result = $this->xpath('//input[@id="edit-uid-0-value" and contains(@data-autocomplete-path, "user/autocomplete")]');
|
Chris@0
|
231 $this->assertEqual(count($result), 0, 'No autocompletion without access user profiles.');
|
Chris@0
|
232
|
Chris@0
|
233 $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content', 'access user profiles']);
|
Chris@0
|
234 $this->drupalLogin($admin_user);
|
Chris@0
|
235
|
Chris@0
|
236 $this->drupalGet('node/add/page');
|
Chris@0
|
237
|
Chris@0
|
238 $result = $this->xpath('//input[@id="edit-uid-0-target-id" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/user/default")]');
|
Chris@0
|
239 $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * Check node/add when no node types exist.
|
Chris@0
|
244 */
|
Chris@0
|
245 public function testNodeAddWithoutContentTypes() {
|
Chris@0
|
246 $this->drupalGet('node/add');
|
Chris@0
|
247 $this->assertResponse(200);
|
Chris@0
|
248 $this->assertNoLinkByHref('/admin/structure/types/add');
|
Chris@0
|
249
|
Chris@0
|
250 // Test /node/add page without content types.
|
Chris@0
|
251 foreach (\Drupal::entityManager()->getStorage('node_type')->loadMultiple() as $entity) {
|
Chris@0
|
252 $entity->delete();
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 $this->drupalGet('node/add');
|
Chris@0
|
256 $this->assertResponse(403);
|
Chris@0
|
257
|
Chris@0
|
258 $admin_content_types = $this->drupalCreateUser(['administer content types']);
|
Chris@0
|
259 $this->drupalLogin($admin_content_types);
|
Chris@0
|
260
|
Chris@0
|
261 $this->drupalGet('node/add');
|
Chris@0
|
262
|
Chris@0
|
263 $this->assertLinkByHref('/admin/structure/types/add');
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 /**
|
Chris@0
|
267 * Gets the watchdog IDs of the records with the rollback exception message.
|
Chris@0
|
268 *
|
Chris@0
|
269 * @return int[]
|
Chris@0
|
270 * Array containing the IDs of the log records with the rollback exception
|
Chris@0
|
271 * message.
|
Chris@0
|
272 */
|
Chris@0
|
273 protected static function getWatchdogIdsForTestExceptionRollback() {
|
Chris@0
|
274 // PostgreSQL doesn't support bytea LIKE queries, so we need to unserialize
|
Chris@0
|
275 // first to check for the rollback exception message.
|
Chris@0
|
276 $matches = [];
|
Chris@0
|
277 $query = db_query("SELECT wid, variables FROM {watchdog}");
|
Chris@0
|
278 foreach ($query as $row) {
|
Chris@0
|
279 $variables = (array) unserialize($row->variables);
|
Chris@0
|
280 if (isset($variables['@message']) && $variables['@message'] === 'Test exception for rollback.') {
|
Chris@0
|
281 $matches[] = $row->wid;
|
Chris@0
|
282 }
|
Chris@0
|
283 }
|
Chris@0
|
284 return $matches;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Gets the log records with the explicit rollback failed exception message.
|
Chris@0
|
289 *
|
Chris@0
|
290 * @return \Drupal\Core\Database\StatementInterface
|
Chris@0
|
291 * A prepared statement object (already executed), which contains the log
|
Chris@0
|
292 * records with the explicit rollback failed exception message.
|
Chris@0
|
293 */
|
Chris@0
|
294 protected static function getWatchdogIdsForFailedExplicitRollback() {
|
Chris@0
|
295 return db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 }
|