NodeCreationTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\Core\Language\LanguageInterface;
  5. use Drupal\node\Entity\Node;
  6. /**
  7. * Create a node and test saving it.
  8. *
  9. * @group node
  10. */
  11. class NodeCreationTest extends NodeTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * Enable dummy module that implements hook_ENTITY_TYPE_insert() for
  16. * exceptions (function node_test_exception_node_insert() ).
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['node_test_exception', 'dblog', 'test_page_test'];
  21. protected function setUp() {
  22. parent::setUp();
  23. $web_user = $this->drupalCreateUser(['create page content', 'edit own page content']);
  24. $this->drupalLogin($web_user);
  25. }
  26. /**
  27. * Creates a "Basic page" node and verifies its consistency in the database.
  28. */
  29. public function testNodeCreation() {
  30. $node_type_storage = \Drupal::entityManager()->getStorage('node_type');
  31. // Test /node/add page with only one content type.
  32. $node_type_storage->load('article')->delete();
  33. $this->drupalGet('node/add');
  34. $this->assertResponse(200);
  35. $this->assertUrl('node/add/page');
  36. // Create a node.
  37. $edit = [];
  38. $edit['title[0][value]'] = $this->randomMachineName(8);
  39. $edit['body[0][value]'] = $this->randomMachineName(16);
  40. $this->drupalPostForm('node/add/page', $edit, t('Save'));
  41. // Check that the Basic page has been created.
  42. $this->assertText(t('@post @title has been created.', ['@post' => 'Basic page', '@title' => $edit['title[0][value]']]), 'Basic page created.');
  43. // Verify that the creation message contains a link to a node.
  44. $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
  45. $this->assert(isset($view_link), 'The message area contains a link to a node');
  46. // Check that the node exists in the database.
  47. $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
  48. $this->assertTrue($node, 'Node found in database.');
  49. // Verify that pages do not show submitted information by default.
  50. $this->drupalGet('node/' . $node->id());
  51. $this->assertNoText($node->getOwner()->getUsername());
  52. $this->assertNoText(format_date($node->getCreatedTime()));
  53. // Change the node type setting to show submitted by information.
  54. /** @var \Drupal\node\NodeTypeInterface $node_type */
  55. $node_type = $node_type_storage->load('page');
  56. $node_type->setDisplaySubmitted(TRUE);
  57. $node_type->save();
  58. $this->drupalGet('node/' . $node->id());
  59. $this->assertText($node->getOwner()->getUsername());
  60. $this->assertText(format_date($node->getCreatedTime()));
  61. // Check if the node revision checkbox is not rendered on node creation form.
  62. $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']);
  63. $this->drupalLogin($admin_user);
  64. $this->drupalGet('node/add/page');
  65. $this->assertNoFieldById('edit-revision', NULL, 'The revision checkbox is not present.');
  66. }
  67. /**
  68. * Verifies that a transaction rolls back the failed creation.
  69. */
  70. public function testFailedPageCreation() {
  71. // Create a node.
  72. $edit = [
  73. 'uid' => $this->loggedInUser->id(),
  74. 'name' => $this->loggedInUser->name,
  75. 'type' => 'page',
  76. 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  77. 'title' => 'testing_transaction_exception',
  78. ];
  79. try {
  80. // An exception is generated by node_test_exception_node_insert() if the
  81. // title is 'testing_transaction_exception'.
  82. Node::create($edit)->save();
  83. $this->fail(t('Expected exception has not been thrown.'));
  84. }
  85. catch (\Exception $e) {
  86. $this->pass(t('Expected exception has been thrown.'));
  87. }
  88. if (Database::getConnection()->supportsTransactions()) {
  89. // Check that the node does not exist in the database.
  90. $node = $this->drupalGetNodeByTitle($edit['title']);
  91. $this->assertFalse($node, 'Transactions supported, and node not found in database.');
  92. }
  93. else {
  94. // Check that the node exists in the database.
  95. $node = $this->drupalGetNodeByTitle($edit['title']);
  96. $this->assertTrue($node, 'Transactions not supported, and node found in database.');
  97. // Check that the failed rollback was logged.
  98. $records = static::getWatchdogIdsForFailedExplicitRollback();
  99. $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
  100. }
  101. // Check that the rollback error was logged.
  102. $records = static::getWatchdogIdsForTestExceptionRollback();
  103. $this->assertTrue(count($records) > 0, 'Rollback explanatory error logged to watchdog.');
  104. }
  105. /**
  106. * Creates an unpublished node and confirms correct redirect behavior.
  107. */
  108. public function testUnpublishedNodeCreation() {
  109. // Set the front page to the test page.
  110. $this->config('system.site')->set('page.front', '/test-page')->save();
  111. // Set "Basic page" content type to be unpublished by default.
  112. $fields = \Drupal::entityManager()->getFieldDefinitions('node', 'page');
  113. $fields['status']->getConfig('page')
  114. ->setDefaultValue(FALSE)
  115. ->save();
  116. // Create a node.
  117. $edit = [];
  118. $edit['title[0][value]'] = $this->randomMachineName(8);
  119. $edit['body[0][value]'] = $this->randomMachineName(16);
  120. $this->drupalPostForm('node/add/page', $edit, t('Save'));
  121. // Check that the user was redirected to the home page.
  122. $this->assertUrl('');
  123. $this->assertText(t('Test page text'));
  124. // Confirm that the node was created.
  125. $this->assertText(t('@post @title has been created.', ['@post' => 'Basic page', '@title' => $edit['title[0][value]']]));
  126. // Verify that the creation message contains a link to a node.
  127. $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
  128. $this->assert(isset($view_link), 'The message area contains a link to a node');
  129. }
  130. /**
  131. * Tests the author autocompletion textfield.
  132. */
  133. public function testAuthorAutocomplete() {
  134. $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']);
  135. $this->drupalLogin($admin_user);
  136. $this->drupalGet('node/add/page');
  137. $result = $this->xpath('//input[@id="edit-uid-0-value" and contains(@data-autocomplete-path, "user/autocomplete")]');
  138. $this->assertEqual(count($result), 0, 'No autocompletion without access user profiles.');
  139. $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content', 'access user profiles']);
  140. $this->drupalLogin($admin_user);
  141. $this->drupalGet('node/add/page');
  142. $result = $this->xpath('//input[@id="edit-uid-0-target-id" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/user/default")]');
  143. $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
  144. }
  145. /**
  146. * Check node/add when no node types exist.
  147. */
  148. public function testNodeAddWithoutContentTypes() {
  149. $this->drupalGet('node/add');
  150. $this->assertResponse(200);
  151. $this->assertNoLinkByHref('/admin/structure/types/add');
  152. // Test /node/add page without content types.
  153. foreach (\Drupal::entityManager()->getStorage('node_type')->loadMultiple() as $entity) {
  154. $entity->delete();
  155. }
  156. $this->drupalGet('node/add');
  157. $this->assertResponse(403);
  158. $admin_content_types = $this->drupalCreateUser(['administer content types']);
  159. $this->drupalLogin($admin_content_types);
  160. $this->drupalGet('node/add');
  161. $this->assertLinkByHref('/admin/structure/types/add');
  162. }
  163. /**
  164. * Gets the watchdog IDs of the records with the rollback exception message.
  165. *
  166. * @return int[]
  167. * Array containing the IDs of the log records with the rollback exception
  168. * message.
  169. */
  170. protected static function getWatchdogIdsForTestExceptionRollback() {
  171. // PostgreSQL doesn't support bytea LIKE queries, so we need to unserialize
  172. // first to check for the rollback exception message.
  173. $matches = [];
  174. $query = db_query("SELECT wid, variables FROM {watchdog}");
  175. foreach ($query as $row) {
  176. $variables = (array) unserialize($row->variables);
  177. if (isset($variables['@message']) && $variables['@message'] === 'Test exception for rollback.') {
  178. $matches[] = $row->wid;
  179. }
  180. }
  181. return $matches;
  182. }
  183. /**
  184. * Gets the log records with the explicit rollback failed exception message.
  185. *
  186. * @return \Drupal\Core\Database\StatementInterface
  187. * A prepared statement object (already executed), which contains the log
  188. * records with the explicit rollback failed exception message.
  189. */
  190. protected static function getWatchdogIdsForFailedExplicitRollback() {
  191. return db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
  192. }
  193. }