View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\LanguageInterface;
use Drupal\node\Entity\Node;
class NodeCreationTest extends NodeTestBase {
public static $modules = [
'node_test_exception',
'dblog',
'test_page_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'create page content',
'edit own page content',
]);
$this
->drupalLogin($web_user);
}
public function testNodeCreation() {
$node_type_storage = \Drupal::entityTypeManager()
->getStorage('node_type');
$node_type_storage
->load('article')
->delete();
$this
->drupalGet('node/add');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertUrl('node/add/page');
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$this
->assertText(t('@post @title has been created.', [
'@post' => 'Basic page',
'@title' => $edit['title[0][value]'],
]), 'Basic page created.');
$view_link = $this
->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
':href' => 'node/',
]);
$this
->assert(isset($view_link), 'The message area contains a link to a node');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotEmpty($node, 'Node found in database.');
$this
->drupalGet('node/' . $node
->id());
$this
->assertNoText($node
->getOwner()
->getAccountName());
$this
->assertNoText($this->container
->get('date.formatter')
->format($node
->getCreatedTime()));
$node_type = $node_type_storage
->load('page');
$node_type
->setDisplaySubmitted(TRUE);
$node_type
->save();
$this
->drupalGet('node/' . $node
->id());
$this
->assertText($node
->getOwner()
->getAccountName());
$this
->assertText($this->container
->get('date.formatter')
->format($node
->getCreatedTime()));
$admin_user = $this
->drupalCreateUser([
'administer nodes',
'create page content',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('node/add/page');
$this
->assertNoFieldById('edit-revision', NULL, 'The revision checkbox is not present.');
}
public function testFailedPageCreation() {
$edit = [
'uid' => $this->loggedInUser
->id(),
'name' => $this->loggedInUser->name,
'type' => 'page',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'title' => 'testing_transaction_exception',
];
try {
Node::create($edit)
->save();
$this
->fail('Expected exception has not been thrown.');
} catch (\Exception $e) {
}
if (Database::getConnection()
->supportsTransactions()) {
$node = $this
->drupalGetNodeByTitle($edit['title']);
$this
->assertFalse($node, 'Transactions supported, and node not found in database.');
}
else {
$node = $this
->drupalGetNodeByTitle($edit['title']);
$this
->assertTrue($node, 'Transactions not supported, and node found in database.');
$records = static::getWatchdogIdsForFailedExplicitRollback();
$this
->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
}
$records = static::getWatchdogIdsForTestExceptionRollback();
$this
->assertTrue(count($records) > 0, 'Rollback explanatory error logged to watchdog.');
}
public function testUnpublishedNodeCreation() {
$this
->config('system.site')
->set('page.front', '/test-page')
->save();
$fields = \Drupal::service('entity_field.manager')
->getFieldDefinitions('node', 'page');
$fields['status']
->getConfig('page')
->setDefaultValue(FALSE)
->save();
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$this
->assertUrl('');
$this
->assertText(t('Test page text'));
$this
->assertText(t('@post @title has been created.', [
'@post' => 'Basic page',
'@title' => $edit['title[0][value]'],
]));
$view_link = $this
->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [
':href' => 'node/',
]);
$this
->assert(isset($view_link), 'The message area contains a link to a node');
}
public function testAuthoredDate() {
$now = \Drupal::time()
->getRequestTime();
$admin = $this
->drupalCreateUser([], NULL, TRUE);
$this
->drupalLogin($admin);
$edit = [
'title[0][value]' => $this
->randomMachineName(8),
'body[0][value]' => $this
->randomMachineName(16),
];
$this
->drupalPostForm('node/add/page', $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotNull($node
->getCreatedTime());
$date = $now - 86400;
$edit = [
'title[0][value]' => $this
->randomMachineName(8),
'body[0][value]' => $this
->randomMachineName(16),
'created[0][value][date]' => date('Y-m-d', $date),
'created[0][value][time]' => date('H:i:s', $date),
];
$this
->drupalPostForm('node/add/page', $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertEquals($date, $node
->getCreatedTime());
$date = $now + 86400;
$edit = [
'title[0][value]' => $this
->randomMachineName(8),
'body[0][value]' => $this
->randomMachineName(16),
'created[0][value][date]' => date('Y-m-d', $date),
'created[0][value][time]' => date('H:i:s', $date),
];
$this
->drupalPostForm('node/add/page', $edit, 'Save');
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertEquals($date, $node
->getCreatedTime());
$edit = [
'title[0][value]' => $this
->randomMachineName(8),
'body[0][value]' => $this
->randomMachineName(16),
'created[0][value][date]' => '2013-13-13',
'created[0][value][time]' => '11:00:00',
];
$this
->drupalPostForm('node/add/page', $edit, 'Save');
$this
->assertSession()
->pageTextContains('The Authored on date is invalid.');
$this
->assertFalse($this
->drupalGetNodeByTitle($edit['title[0][value]']));
$edit = [
'title[0][value]' => $this
->randomMachineName(8),
'body[0][value]' => $this
->randomMachineName(16),
'created[0][value][date]' => '2012-01-01',
'created[0][value][time]' => '30:00:00',
];
$this
->drupalPostForm('node/add/page', $edit, 'Save');
$this
->assertSession()
->pageTextContains('The Authored on date is invalid.');
$this
->assertFalse($this
->drupalGetNodeByTitle($edit['title[0][value]']));
}
public function testAuthorAutocomplete() {
$admin_user = $this
->drupalCreateUser([
'administer nodes',
'create page content',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('node/add/page');
$result = $this
->xpath('//input[@id="edit-uid-0-value" and contains(@data-autocomplete-path, "user/autocomplete")]');
$this
->assertCount(0, $result, 'No autocompletion without access user profiles.');
$admin_user = $this
->drupalCreateUser([
'administer nodes',
'create page content',
'access user profiles',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('node/add/page');
$result = $this
->xpath('//input[@id="edit-uid-0-target-id" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/user/default")]');
$this
->assertCount(1, $result, 'Ensure that the user does have access to the autocompletion');
}
public function testNodeAddWithoutContentTypes() {
$this
->drupalGet('node/add');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertNoLinkByHref('/admin/structure/types/add');
foreach (\Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple() as $entity) {
$entity
->delete();
}
$this
->drupalGet('node/add');
$this
->assertSession()
->statusCodeEquals(403);
$admin_content_types = $this
->drupalCreateUser([
'administer content types',
]);
$this
->drupalLogin($admin_content_types);
$this
->drupalGet('node/add');
$this
->assertLinkByHref('/admin/structure/types/add');
}
protected static function getWatchdogIdsForTestExceptionRollback() {
$matches = [];
$query = Database::getConnection()
->select('watchdog', 'w')
->fields('w', [
'wid',
'variables',
])
->execute();
foreach ($query as $row) {
$variables = (array) unserialize($row->variables);
if (isset($variables['@message']) && $variables['@message'] === 'Test exception for rollback.') {
$matches[] = $row->wid;
}
}
return $matches;
}
protected static function getWatchdogIdsForFailedExplicitRollback() {
return Database::getConnection()
->query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")
->fetchAll();
}
}