Chris@0: drupalCreateUser(['create page content', 'edit own page content']); Chris@0: $this->drupalLogin($web_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a "Basic page" node and verifies its consistency in the database. Chris@0: */ Chris@0: public function testNodeCreation() { Chris@0: $node_type_storage = \Drupal::entityManager()->getStorage('node_type'); Chris@0: Chris@0: // Test /node/add page with only one content type. Chris@0: $node_type_storage->load('article')->delete(); Chris@0: $this->drupalGet('node/add'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertUrl('node/add/page'); Chris@0: // Create a node. Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $this->randomMachineName(8); Chris@0: $edit['body[0][value]'] = $this->randomMachineName(16); Chris@0: $this->drupalPostForm('node/add/page', $edit, t('Save')); Chris@0: Chris@0: // Check that the Basic page has been created. Chris@0: $this->assertText(t('@post @title has been created.', ['@post' => 'Basic page', '@title' => $edit['title[0][value]']]), 'Basic page created.'); Chris@0: Chris@0: // Verify that the creation message contains a link to a node. Chris@0: $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']); Chris@0: $this->assert(isset($view_link), 'The message area contains a link to a node'); Chris@0: Chris@0: // Check that the node exists in the database. Chris@0: $node = $this->drupalGetNodeByTitle($edit['title[0][value]']); Chris@0: $this->assertTrue($node, 'Node found in database.'); Chris@0: Chris@0: // Verify that pages do not show submitted information by default. Chris@0: $this->drupalGet('node/' . $node->id()); Chris@18: $this->assertNoText($node->getOwner()->getAccountName()); Chris@18: $this->assertNoText($this->container->get('date.formatter')->format($node->getCreatedTime())); Chris@0: Chris@0: // Change the node type setting to show submitted by information. Chris@0: /** @var \Drupal\node\NodeTypeInterface $node_type */ Chris@0: $node_type = $node_type_storage->load('page'); Chris@0: $node_type->setDisplaySubmitted(TRUE); Chris@0: $node_type->save(); Chris@0: Chris@0: $this->drupalGet('node/' . $node->id()); Chris@18: $this->assertText($node->getOwner()->getAccountName()); Chris@18: $this->assertText($this->container->get('date.formatter')->format($node->getCreatedTime())); Chris@0: Chris@0: // Check if the node revision checkbox is not rendered on node creation form. Chris@0: $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->drupalGet('node/add/page'); Chris@0: $this->assertNoFieldById('edit-revision', NULL, 'The revision checkbox is not present.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies that a transaction rolls back the failed creation. Chris@0: */ Chris@0: public function testFailedPageCreation() { Chris@0: // Create a node. Chris@0: $edit = [ Chris@0: 'uid' => $this->loggedInUser->id(), Chris@0: 'name' => $this->loggedInUser->name, Chris@0: 'type' => 'page', Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: 'title' => 'testing_transaction_exception', Chris@0: ]; Chris@0: Chris@0: try { Chris@0: // An exception is generated by node_test_exception_node_insert() if the Chris@0: // title is 'testing_transaction_exception'. Chris@0: Node::create($edit)->save(); Chris@0: $this->fail(t('Expected exception has not been thrown.')); Chris@0: } Chris@0: catch (\Exception $e) { Chris@0: $this->pass(t('Expected exception has been thrown.')); Chris@0: } Chris@0: Chris@0: if (Database::getConnection()->supportsTransactions()) { Chris@0: // Check that the node does not exist in the database. Chris@0: $node = $this->drupalGetNodeByTitle($edit['title']); Chris@0: $this->assertFalse($node, 'Transactions supported, and node not found in database.'); Chris@0: } Chris@0: else { Chris@0: // Check that the node exists in the database. Chris@0: $node = $this->drupalGetNodeByTitle($edit['title']); Chris@0: $this->assertTrue($node, 'Transactions not supported, and node found in database.'); Chris@0: Chris@0: // Check that the failed rollback was logged. Chris@0: $records = static::getWatchdogIdsForFailedExplicitRollback(); Chris@0: $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.'); Chris@0: } Chris@0: Chris@0: // Check that the rollback error was logged. Chris@0: $records = static::getWatchdogIdsForTestExceptionRollback(); Chris@0: $this->assertTrue(count($records) > 0, 'Rollback explanatory error logged to watchdog.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates an unpublished node and confirms correct redirect behavior. Chris@0: */ Chris@0: public function testUnpublishedNodeCreation() { Chris@0: // Set the front page to the test page. Chris@0: $this->config('system.site')->set('page.front', '/test-page')->save(); Chris@0: Chris@0: // Set "Basic page" content type to be unpublished by default. Chris@0: $fields = \Drupal::entityManager()->getFieldDefinitions('node', 'page'); Chris@0: $fields['status']->getConfig('page') Chris@0: ->setDefaultValue(FALSE) Chris@0: ->save(); Chris@0: Chris@0: // Create a node. Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $this->randomMachineName(8); Chris@0: $edit['body[0][value]'] = $this->randomMachineName(16); Chris@0: $this->drupalPostForm('node/add/page', $edit, t('Save')); Chris@0: Chris@0: // Check that the user was redirected to the home page. Chris@0: $this->assertUrl(''); Chris@0: $this->assertText(t('Test page text')); Chris@0: Chris@0: // Confirm that the node was created. Chris@0: $this->assertText(t('@post @title has been created.', ['@post' => 'Basic page', '@title' => $edit['title[0][value]']])); Chris@0: Chris@0: // Verify that the creation message contains a link to a node. Chris@0: $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']); Chris@0: $this->assert(isset($view_link), 'The message area contains a link to a node'); Chris@0: } Chris@0: Chris@0: /** Chris@18: * Creates nodes with different authored dates. Chris@18: */ Chris@18: public function testAuthoredDate() { Chris@18: $now = \Drupal::time()->getRequestTime(); Chris@18: $admin = $this->drupalCreateUser([], NULL, TRUE); Chris@18: $this->drupalLogin($admin); Chris@18: Chris@18: // Create a node with the default creation date. Chris@18: $edit = [ Chris@18: 'title[0][value]' => $this->randomMachineName(8), Chris@18: 'body[0][value]' => $this->randomMachineName(16), Chris@18: ]; Chris@18: $this->drupalPostForm('node/add/page', $edit, 'Save'); Chris@18: $node = $this->drupalGetNodeByTitle($edit['title[0][value]']); Chris@18: $this->assertNotNull($node->getCreatedTime()); Chris@18: Chris@18: // Create a node with the custom creation date in the past. Chris@18: $date = $now - 86400; Chris@18: $edit = [ Chris@18: 'title[0][value]' => $this->randomMachineName(8), Chris@18: 'body[0][value]' => $this->randomMachineName(16), Chris@18: 'created[0][value][date]' => date('Y-m-d', $date), Chris@18: 'created[0][value][time]' => date('H:i:s', $date), Chris@18: ]; Chris@18: $this->drupalPostForm('node/add/page', $edit, 'Save'); Chris@18: $node = $this->drupalGetNodeByTitle($edit['title[0][value]']); Chris@18: $this->assertEquals($date, $node->getCreatedTime()); Chris@18: Chris@18: // Create a node with the custom creation date in the future. Chris@18: $date = $now + 86400; Chris@18: $edit = [ Chris@18: 'title[0][value]' => $this->randomMachineName(8), Chris@18: 'body[0][value]' => $this->randomMachineName(16), Chris@18: 'created[0][value][date]' => date('Y-m-d', $date), Chris@18: 'created[0][value][time]' => date('H:i:s', $date), Chris@18: ]; Chris@18: $this->drupalPostForm('node/add/page', $edit, 'Save'); Chris@18: $node = $this->drupalGetNodeByTitle($edit['title[0][value]']); Chris@18: $this->assertEquals($date, $node->getCreatedTime()); Chris@18: Chris@18: // Test an invalid date. Chris@18: $edit = [ Chris@18: 'title[0][value]' => $this->randomMachineName(8), Chris@18: 'body[0][value]' => $this->randomMachineName(16), Chris@18: 'created[0][value][date]' => '2013-13-13', Chris@18: 'created[0][value][time]' => '11:00:00', Chris@18: ]; Chris@18: $this->drupalPostForm('node/add/page', $edit, 'Save'); Chris@18: $this->assertSession()->pageTextContains('The Authored on date is invalid.'); Chris@18: $this->assertFalse($this->drupalGetNodeByTitle($edit['title[0][value]'])); Chris@18: Chris@18: // Test an invalid time. Chris@18: $edit = [ Chris@18: 'title[0][value]' => $this->randomMachineName(8), Chris@18: 'body[0][value]' => $this->randomMachineName(16), Chris@18: 'created[0][value][date]' => '2012-01-01', Chris@18: 'created[0][value][time]' => '30:00:00', Chris@18: ]; Chris@18: $this->drupalPostForm('node/add/page', $edit, 'Save'); Chris@18: $this->assertSession()->pageTextContains('The Authored on date is invalid.'); Chris@18: $this->assertFalse($this->drupalGetNodeByTitle($edit['title[0][value]'])); Chris@18: } Chris@18: Chris@18: /** Chris@0: * Tests the author autocompletion textfield. Chris@0: */ Chris@0: public function testAuthorAutocomplete() { Chris@0: $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: $this->drupalGet('node/add/page'); Chris@0: Chris@0: $result = $this->xpath('//input[@id="edit-uid-0-value" and contains(@data-autocomplete-path, "user/autocomplete")]'); Chris@0: $this->assertEqual(count($result), 0, 'No autocompletion without access user profiles.'); Chris@0: Chris@0: $admin_user = $this->drupalCreateUser(['administer nodes', 'create page content', 'access user profiles']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: $this->drupalGet('node/add/page'); Chris@0: Chris@0: $result = $this->xpath('//input[@id="edit-uid-0-target-id" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/user/default")]'); Chris@0: $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check node/add when no node types exist. Chris@0: */ Chris@0: public function testNodeAddWithoutContentTypes() { Chris@0: $this->drupalGet('node/add'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertNoLinkByHref('/admin/structure/types/add'); Chris@0: Chris@0: // Test /node/add page without content types. Chris@0: foreach (\Drupal::entityManager()->getStorage('node_type')->loadMultiple() as $entity) { Chris@0: $entity->delete(); Chris@0: } Chris@0: Chris@0: $this->drupalGet('node/add'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: $admin_content_types = $this->drupalCreateUser(['administer content types']); Chris@0: $this->drupalLogin($admin_content_types); Chris@0: Chris@0: $this->drupalGet('node/add'); Chris@0: Chris@0: $this->assertLinkByHref('/admin/structure/types/add'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the watchdog IDs of the records with the rollback exception message. Chris@0: * Chris@0: * @return int[] Chris@0: * Array containing the IDs of the log records with the rollback exception Chris@0: * message. Chris@0: */ Chris@0: protected static function getWatchdogIdsForTestExceptionRollback() { Chris@0: // PostgreSQL doesn't support bytea LIKE queries, so we need to unserialize Chris@0: // first to check for the rollback exception message. Chris@0: $matches = []; Chris@0: $query = db_query("SELECT wid, variables FROM {watchdog}"); Chris@0: foreach ($query as $row) { Chris@0: $variables = (array) unserialize($row->variables); Chris@0: if (isset($variables['@message']) && $variables['@message'] === 'Test exception for rollback.') { Chris@0: $matches[] = $row->wid; Chris@0: } Chris@0: } Chris@0: return $matches; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the log records with the explicit rollback failed exception message. Chris@0: * Chris@0: * @return \Drupal\Core\Database\StatementInterface Chris@0: * A prepared statement object (already executed), which contains the log Chris@0: * records with the explicit rollback failed exception message. Chris@0: */ Chris@0: protected static function getWatchdogIdsForFailedExplicitRollback() { Chris@0: return db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll(); Chris@0: } Chris@0: Chris@0: }