Issue #2566419 by amateescu, dawehner, jhedstrom, hchonov, hussainweb, jibran: Using the "Autocomplete (Tags style)" widget for the author field does not save the submitted user
parent
b42b41ac4d
commit
dbf74b3724
|
@ -91,9 +91,9 @@ class Node extends ContentEntityBase implements NodeInterface {
|
||||||
public function preSave(EntityStorageInterface $storage) {
|
public function preSave(EntityStorageInterface $storage) {
|
||||||
parent::preSave($storage);
|
parent::preSave($storage);
|
||||||
|
|
||||||
// If no owner has been set explicitly, make the current user the owner.
|
// If no owner has been set explicitly, make the anonymous user the owner.
|
||||||
if (!$this->getOwner()) {
|
if (!$this->getOwner()) {
|
||||||
$this->setOwnerId(\Drupal::currentUser()->id());
|
$this->setOwnerId(0);
|
||||||
}
|
}
|
||||||
// If no revision author has been set explicitly, make the node owner the
|
// If no revision author has been set explicitly, make the node owner the
|
||||||
// revision author.
|
// revision author.
|
||||||
|
|
|
@ -353,24 +353,6 @@ class NodeForm extends ContentEntityForm {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function buildEntity(array $form, FormStateInterface $form_state) {
|
|
||||||
/** @var \Drupal\node\NodeInterface $entity */
|
|
||||||
$entity = parent::buildEntity($form, $form_state);
|
|
||||||
// A user might assign the node author by entering a user name in the node
|
|
||||||
// form, which we then need to translate to a user ID.
|
|
||||||
// @todo: Remove it when https://www.drupal.org/node/2322525 is pushed.
|
|
||||||
if (!empty($form_state->getValue('uid')[0]['target_id']) && $account = User::load($form_state->getValue('uid')[0]['target_id'])) {
|
|
||||||
$entity->setOwnerId($account->id());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$entity->setOwnerId(0);
|
|
||||||
}
|
|
||||||
return $entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -78,10 +78,8 @@ class NodeTranslationHandler extends ContentTranslationHandler {
|
||||||
if ($form_state->hasValue('content_translation')) {
|
if ($form_state->hasValue('content_translation')) {
|
||||||
$translation = &$form_state->getValue('content_translation');
|
$translation = &$form_state->getValue('content_translation');
|
||||||
$translation['status'] = $entity->isPublished();
|
$translation['status'] = $entity->isPublished();
|
||||||
// $form['content_translation']['name'] is the equivalent field
|
|
||||||
// for translation author uid.
|
|
||||||
$account = $entity->uid->entity;
|
$account = $entity->uid->entity;
|
||||||
$translation['name'] = $account ? $account->getUsername() : '';
|
$translation['uid'] = $account ? $account->id() : 0;
|
||||||
$translation['created'] = format_date($entity->created->value, 'custom', 'Y-m-d H:i:s O');
|
$translation['created'] = format_date($entity->created->value, 'custom', 'Y-m-d H:i:s O');
|
||||||
}
|
}
|
||||||
parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
|
parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
|
||||||
|
|
|
@ -2,20 +2,41 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
* Contains \Drupal\node\Tests\PageEditTest.
|
* Contains \Drupal\node\Tests\NodeEditFormTest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Drupal\node\Tests;
|
namespace Drupal\node\Tests;
|
||||||
|
|
||||||
|
use Drupal\node\NodeInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a node and test node edit functionality.
|
* Create a node and test node edit functionality.
|
||||||
*
|
*
|
||||||
* @group node
|
* @group node
|
||||||
*/
|
*/
|
||||||
class PageEditTest extends NodeTestBase {
|
class NodeEditFormTest extends NodeTestBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A normal logged in user.
|
||||||
|
*
|
||||||
|
* @var \Drupal\user\UserInterface
|
||||||
|
*/
|
||||||
protected $webUser;
|
protected $webUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A user with permission to bypass content access checks.
|
||||||
|
*
|
||||||
|
* @var \Drupal\user\UserInterface
|
||||||
|
*/
|
||||||
protected $adminUser;
|
protected $adminUser;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The node storage.
|
||||||
|
*
|
||||||
|
* @var \Drupal\node\NodeStorageInterface
|
||||||
|
*/
|
||||||
|
protected $nodeStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modules to enable.
|
* Modules to enable.
|
||||||
*
|
*
|
||||||
|
@ -29,12 +50,14 @@ class PageEditTest extends NodeTestBase {
|
||||||
$this->webUser = $this->drupalCreateUser(array('edit own page content', 'create page content'));
|
$this->webUser = $this->drupalCreateUser(array('edit own page content', 'create page content'));
|
||||||
$this->adminUser = $this->drupalCreateUser(array('bypass node access', 'administer nodes'));
|
$this->adminUser = $this->drupalCreateUser(array('bypass node access', 'administer nodes'));
|
||||||
$this->drupalPlaceBlock('local_tasks_block');
|
$this->drupalPlaceBlock('local_tasks_block');
|
||||||
|
|
||||||
|
$this->nodeStorage = $this->container->get('entity.manager')->getStorage('node');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks node edit functionality.
|
* Checks node edit functionality.
|
||||||
*/
|
*/
|
||||||
function testPageEdit() {
|
public function testNodeEdit() {
|
||||||
$this->drupalLogin($this->webUser);
|
$this->drupalLogin($this->webUser);
|
||||||
|
|
||||||
$title_key = 'title[0][value]';
|
$title_key = 'title[0][value]';
|
||||||
|
@ -98,8 +121,7 @@ class PageEditTest extends NodeTestBase {
|
||||||
/**
|
/**
|
||||||
* Tests changing a node's "authored by" field.
|
* Tests changing a node's "authored by" field.
|
||||||
*/
|
*/
|
||||||
function testPageAuthoredBy() {
|
public function testNodeEditAuthoredBy() {
|
||||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
|
||||||
$this->drupalLogin($this->adminUser);
|
$this->drupalLogin($this->adminUser);
|
||||||
|
|
||||||
// Create node to edit.
|
// Create node to edit.
|
||||||
|
@ -113,18 +135,69 @@ class PageEditTest extends NodeTestBase {
|
||||||
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
|
||||||
$this->assertIdentical($node->getOwnerId(), $this->adminUser->id(), 'Node authored by admin user.');
|
$this->assertIdentical($node->getOwnerId(), $this->adminUser->id(), 'Node authored by admin user.');
|
||||||
|
|
||||||
|
$this->checkVariousAuthoredByValues($node, 'uid[0][target_id]');
|
||||||
|
|
||||||
|
// Check that normal users cannot change the authored by information.
|
||||||
|
$this->drupalLogin($this->webUser);
|
||||||
|
$this->drupalGet('node/' . $node->id() . '/edit');
|
||||||
|
$this->assertNoFieldByName('uid[0][target_id]');
|
||||||
|
|
||||||
|
// Now test with the Autcomplete (Tags) field widget.
|
||||||
|
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
|
||||||
|
$form_display = \Drupal::entityManager()->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);
|
||||||
|
|
||||||
|
// Save the node without making any changes.
|
||||||
|
$this->drupalPostForm('node/' . $node->id() . '/edit', [], t('Save and keep published'));
|
||||||
|
$this->nodeStorage->resetCache(array($node->id()));
|
||||||
|
$node = $this->nodeStorage->load($node->id());
|
||||||
|
$this->assertIdentical($this->webUser->id(), $node->getOwner()->id());
|
||||||
|
|
||||||
|
$this->checkVariousAuthoredByValues($node, 'uid[target_id]');
|
||||||
|
|
||||||
|
// Hide the 'authored by' field from the form.
|
||||||
|
$form_display->removeComponent('uid')->save();
|
||||||
|
|
||||||
|
// Check that saving the node without making any changes keeps the proper
|
||||||
|
// author ID.
|
||||||
|
$this->drupalPostForm('node/' . $node->id() . '/edit', [], t('Save and keep published'));
|
||||||
|
$this->nodeStorage->resetCache(array($node->id()));
|
||||||
|
$node = $this->nodeStorage->load($node->id());
|
||||||
|
$this->assertIdentical($this->webUser->id(), $node->getOwner()->id());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that the "authored by" works correctly with various values.
|
||||||
|
*
|
||||||
|
* @param \Drupal\node\NodeInterface $node
|
||||||
|
* A node object.
|
||||||
|
* @param string $form_element_name
|
||||||
|
* The name of the form element to populate.
|
||||||
|
*/
|
||||||
|
protected function checkVariousAuthoredByValues(NodeInterface $node, $form_element_name) {
|
||||||
// Try to change the 'authored by' field to an invalid user name.
|
// Try to change the 'authored by' field to an invalid user name.
|
||||||
$edit = array(
|
$edit = array(
|
||||||
'uid[0][target_id]' => 'invalid-name',
|
$form_element_name => 'invalid-name',
|
||||||
);
|
);
|
||||||
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
|
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
|
||||||
$this->assertRaw(t('There are no entities matching "%name".', array('%name' => 'invalid-name')));
|
$this->assertRaw(t('There are no entities matching "%name".', array('%name' => 'invalid-name')));
|
||||||
|
|
||||||
// Change the authored by field to the anonymous user (uid 0).
|
// Change the authored by field to an empty string, which should assign
|
||||||
$edit['uid[0][target_id]'] = 'Anonymous (0)';
|
// authorship to the anonymous user (uid 0).
|
||||||
|
$edit[$form_element_name] = '';
|
||||||
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
|
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
|
||||||
$node_storage->resetCache(array($node->id()));
|
$this->nodeStorage->resetCache(array($node->id()));
|
||||||
$node = $node_storage->load($node->id());
|
$node = $this->nodeStorage->load($node->id());
|
||||||
$uid = $node->getOwnerId();
|
$uid = $node->getOwnerId();
|
||||||
// Most SQL database drivers stringify fetches but entities are not
|
// Most SQL database drivers stringify fetches but entities are not
|
||||||
// necessarily stored in a SQL database. At the same time, NULL/FALSE/""
|
// necessarily stored in a SQL database. At the same time, NULL/FALSE/""
|
||||||
|
@ -133,15 +206,11 @@ class PageEditTest extends NodeTestBase {
|
||||||
|
|
||||||
// Change the authored by field to another user's name (that is not
|
// Change the authored by field to another user's name (that is not
|
||||||
// logged in).
|
// logged in).
|
||||||
$edit['uid[0][target_id]'] = $this->webUser->getUsername();
|
$edit[$form_element_name] = $this->webUser->getUsername();
|
||||||
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
|
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published'));
|
||||||
$node_storage->resetCache(array($node->id()));
|
$this->nodeStorage->resetCache(array($node->id()));
|
||||||
$node = $node_storage->load($node->id());
|
$node = $this->nodeStorage->load($node->id());
|
||||||
$this->assertIdentical($node->getOwnerId(), $this->webUser->id(), 'Node authored by normal user.');
|
$this->assertIdentical($node->getOwnerId(), $this->webUser->id(), 'Node authored by normal user.');
|
||||||
|
}
|
||||||
|
|
||||||
// Check that normal users cannot change the authored by information.
|
|
||||||
$this->drupalLogin($this->webUser);
|
|
||||||
$this->drupalGet('node/' . $node->id() . '/edit');
|
|
||||||
$this->assertNoFieldByName('uid[0][target_id]');
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue