View source
<?php
namespace Drupal\Tests\node\Functional;
use Drupal\node\NodeInterface;
use Drupal\user\Entity\User;
class NodeEditFormTest extends NodeTestBase {
protected $defaultTheme = 'stark';
protected $webUser;
protected $adminUser;
protected $nodeStorage;
public static $modules = [
'block',
'node',
'datetime',
];
protected function setUp() {
parent::setUp();
$this->webUser = $this
->drupalCreateUser([
'edit own page content',
'create page content',
]);
$this->adminUser = $this
->drupalCreateUser([
'bypass node access',
'administer nodes',
]);
$this
->drupalPlaceBlock('local_tasks_block');
$this->nodeStorage = $this->container
->get('entity_type.manager')
->getStorage('node');
}
public function testNodeEdit() {
$this
->drupalLogin($this->webUser);
$title_key = 'title[0][value]';
$body_key = 'body[0][value]';
$edit = [];
$edit[$title_key] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit[$title_key]);
$this
->assertNotEmpty($node, 'Node found in database.');
$this
->clickLink(t('Edit'));
$this
->assertUrl($node
->toUrl('edit-form', [
'absolute' => TRUE,
])
->toString());
$this
->assertRaw('Edit<span class="visually-hidden">(active tab)</span>', 'Edit tab found and marked active.');
$this
->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
$this
->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
$edit = [];
$edit[$title_key] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText($edit[$title_key], 'Title displayed.');
$this
->assertText($edit[$body_key], 'Body displayed.');
$second_web_user = $this
->drupalCreateUser([
'administer nodes',
'edit any page content',
]);
$this
->drupalLogin($second_web_user);
$this
->drupalGet("node/" . $node
->id() . "/edit");
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$edit['revision'] = TRUE;
$this
->drupalPostForm(NULL, $edit, t('Save'));
$revised_node = $this
->drupalGetNodeByTitle($edit['title[0][value]'], TRUE);
$this
->assertNotIdentical($node
->getRevisionId(), $revised_node
->getRevisionId(), 'A new revision has been created.');
$this
->assertIdentical($node
->getOwnerId(), $revised_node
->getOwnerId(), 'The node author has been preserved.');
$first_node_version = node_revision_load($node
->getRevisionId());
$second_node_version = node_revision_load($revised_node
->getRevisionId());
$this
->assertNotIdentical($first_node_version
->getRevisionUser()
->id(), $second_node_version
->getRevisionUser()
->id(), 'Each revision has a distinct user.');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertFieldById('edit-revision', NULL, 'The revision field is present.');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$edit = [];
$edit['created[0][value][date]'] = $this
->randomMachineName(8);
$open_details_elements = count($this
->cssSelect('details[open="open"]'));
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertRaw('<details class="node-form-author js-form-wrapper form-wrapper" data-drupal-selector="edit-author" id="edit-author" open="open">');
$open_details_elements++;
$this
->assertEqual(count($this
->cssSelect('details[open="open"]')), $open_details_elements, 'Exactly one extra open <details> element found.');
$this
->drupalGet("node/" . $node
->id() . "/edit");
$edit = [
'status[value]' => FALSE,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertFalse($node
->isPublished(), 'Node is unpublished');
}
public function testNodeEditAuthoredBy() {
$this
->drupalLogin($this->adminUser);
$body_key = 'body[0][value]';
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit[$body_key] = $this
->randomMachineName(16);
$this
->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertIdentical($node
->getOwnerId(), $this->adminUser
->id(), 'Node authored by admin user.');
$this
->checkVariousAuthoredByValues($node, 'uid[0][target_id]');
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertNoFieldByName('uid[0][target_id]');
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.page.default');
$widget = $form_display
->getComponent('uid');
$widget['type'] = 'entity_reference_autocomplete_tags';
$widget['settings'] = [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
];
$form_display
->setComponent('uid', $widget);
$form_display
->save();
$this
->drupalLogin($this->adminUser);
$this
->drupalPostForm('node/' . $node
->id() . '/edit', [], t('Save'));
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertIdentical($this->webUser
->id(), $node
->getOwner()
->id());
$this
->checkVariousAuthoredByValues($node, 'uid[target_id]');
$form_display
->removeComponent('uid')
->save();
$this
->drupalPostForm('node/' . $node
->id() . '/edit', [], t('Save'));
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertIdentical($this->webUser
->id(), $node
->getOwner()
->id());
}
public function testNodeMetaInformation() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/page');
$this
->assertNoText('Not saved yet');
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->drupalGet("node/" . $node
->id() . "/edit");
$this
->assertNoText('Published');
$this
->assertNoText($this->container
->get('date.formatter')
->format($node
->getChangedTime(), 'short'));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/add/page');
$this
->assertText('Not saved yet');
$edit['title[0][value]'] = $this
->randomMachineName(8);
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->drupalGet("node/" . $node
->id() . "/edit");
$this
->assertText('Published');
$this
->assertText($this->container
->get('date.formatter')
->format($node
->getChangedTime(), 'short'));
}
protected function checkVariousAuthoredByValues(NodeInterface $node, $form_element_name) {
$edit = [
$form_element_name => 'invalid-name',
];
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this
->assertRaw(t('There are no entities matching "%name".', [
'%name' => 'invalid-name',
]));
$edit[$form_element_name] = '';
$this
->drupalPostForm('node/' . $node
->id() . '/edit', $edit, t('Save'));
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$uid = $node
->getOwnerId();
$this
->assertTrue($uid === 0 || $uid === '0', 'Node authored by anonymous user.');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$anonymous_user = User::getAnonymousUser();
$expected = $anonymous_user
->label() . ' (' . $anonymous_user
->id() . ')';
$this
->assertFieldByName($form_element_name, $expected, 'Authored by field displays the correct value for the anonymous user.');
$edit[$form_element_name] = $this->webUser
->getAccountName();
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this->nodeStorage
->resetCache([
$node
->id(),
]);
$node = $this->nodeStorage
->load($node
->id());
$this
->assertIdentical($node
->getOwnerId(), $this->webUser
->id(), 'Node authored by normal user.');
}
}