annotate core/modules/node/tests/src/Functional/NodeCreationTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
rev   line source
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@0 63 $this->assertNoText($node->getOwner()->getUsername());
Chris@0 64 $this->assertNoText(format_date($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@0 73 $this->assertText($node->getOwner()->getUsername());
Chris@0 74 $this->assertText(format_date($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@0 158 * Tests the author autocompletion textfield.
Chris@0 159 */
Chris@0 160 public function testAuthorAutocomplete() {
Chris@0 161 $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']);
Chris@0 162 $this->drupalLogin($admin_user);
Chris@0 163
Chris@0 164 $this->drupalGet('node/add/page');
Chris@0 165
Chris@0 166 $result = $this->xpath('//input[@id="edit-uid-0-value" and contains(@data-autocomplete-path, "user/autocomplete")]');
Chris@0 167 $this->assertEqual(count($result), 0, 'No autocompletion without access user profiles.');
Chris@0 168
Chris@0 169 $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content', 'access user profiles']);
Chris@0 170 $this->drupalLogin($admin_user);
Chris@0 171
Chris@0 172 $this->drupalGet('node/add/page');
Chris@0 173
Chris@0 174 $result = $this->xpath('//input[@id="edit-uid-0-target-id" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/user/default")]');
Chris@0 175 $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Check node/add when no node types exist.
Chris@0 180 */
Chris@0 181 public function testNodeAddWithoutContentTypes() {
Chris@0 182 $this->drupalGet('node/add');
Chris@0 183 $this->assertResponse(200);
Chris@0 184 $this->assertNoLinkByHref('/admin/structure/types/add');
Chris@0 185
Chris@0 186 // Test /node/add page without content types.
Chris@0 187 foreach (\Drupal::entityManager()->getStorage('node_type')->loadMultiple() as $entity) {
Chris@0 188 $entity->delete();
Chris@0 189 }
Chris@0 190
Chris@0 191 $this->drupalGet('node/add');
Chris@0 192 $this->assertResponse(403);
Chris@0 193
Chris@0 194 $admin_content_types = $this->drupalCreateUser(['administer content types']);
Chris@0 195 $this->drupalLogin($admin_content_types);
Chris@0 196
Chris@0 197 $this->drupalGet('node/add');
Chris@0 198
Chris@0 199 $this->assertLinkByHref('/admin/structure/types/add');
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Gets the watchdog IDs of the records with the rollback exception message.
Chris@0 204 *
Chris@0 205 * @return int[]
Chris@0 206 * Array containing the IDs of the log records with the rollback exception
Chris@0 207 * message.
Chris@0 208 */
Chris@0 209 protected static function getWatchdogIdsForTestExceptionRollback() {
Chris@0 210 // PostgreSQL doesn't support bytea LIKE queries, so we need to unserialize
Chris@0 211 // first to check for the rollback exception message.
Chris@0 212 $matches = [];
Chris@0 213 $query = db_query("SELECT wid, variables FROM {watchdog}");
Chris@0 214 foreach ($query as $row) {
Chris@0 215 $variables = (array) unserialize($row->variables);
Chris@0 216 if (isset($variables['@message']) && $variables['@message'] === 'Test exception for rollback.') {
Chris@0 217 $matches[] = $row->wid;
Chris@0 218 }
Chris@0 219 }
Chris@0 220 return $matches;
Chris@0 221 }
Chris@0 222
Chris@0 223 /**
Chris@0 224 * Gets the log records with the explicit rollback failed exception message.
Chris@0 225 *
Chris@0 226 * @return \Drupal\Core\Database\StatementInterface
Chris@0 227 * A prepared statement object (already executed), which contains the log
Chris@0 228 * records with the explicit rollback failed exception message.
Chris@0 229 */
Chris@0 230 protected static function getWatchdogIdsForFailedExplicitRollback() {
Chris@0 231 return db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
Chris@0 232 }
Chris@0 233
Chris@0 234 }