diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index 84a657b0b0e152341ff459114f4a4cc02cc50d77..1e5e5ef8210e05870c04d838c6527d208adba8c8 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -91,9 +91,9 @@ class Node extends ContentEntityBase implements NodeInterface { public function preSave(EntityStorageInterface $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()) { - $this->setOwnerId(\Drupal::currentUser()->id()); + $this->setOwnerId(0); } // If no revision author has been set explicitly, make the node owner the // revision author. diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php index ec0ad4122c2da3ac1658a1ed2b187cba42c51359..36cfc74c504fa005831ad6688e0f53d9bbe23974 100644 --- a/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -353,24 +353,6 @@ public function preview(array $form, FormStateInterface $form_state) { )); } - /** - * {@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} */ diff --git a/core/modules/node/src/NodeTranslationHandler.php b/core/modules/node/src/NodeTranslationHandler.php index 41c6909535219432a6e1fdf2218d40639d2d825a..e6d2a8d2b9e0a80905dd3e489a760297b34d7c4c 100644 --- a/core/modules/node/src/NodeTranslationHandler.php +++ b/core/modules/node/src/NodeTranslationHandler.php @@ -78,10 +78,8 @@ public function entityFormEntityBuild($entity_type, EntityInterface $entity, arr if ($form_state->hasValue('content_translation')) { $translation = &$form_state->getValue('content_translation'); $translation['status'] = $entity->isPublished(); - // $form['content_translation']['name'] is the equivalent field - // for translation author uid. $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'); } parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state); diff --git a/core/modules/node/src/Tests/PageEditTest.php b/core/modules/node/src/Tests/NodeEditFormTest.php similarity index 65% rename from core/modules/node/src/Tests/PageEditTest.php rename to core/modules/node/src/Tests/NodeEditFormTest.php index 31d6ea9925ce505f6214fe208358afcf1946107f..79fa5eabf2092d05aa2f7f4ec08ccc125b25fbeb 100644 --- a/core/modules/node/src/Tests/PageEditTest.php +++ b/core/modules/node/src/Tests/NodeEditFormTest.php @@ -2,20 +2,41 @@ /** * @file - * Contains \Drupal\node\Tests\PageEditTest. + * Contains \Drupal\node\Tests\NodeEditFormTest. */ namespace Drupal\node\Tests; +use Drupal\node\NodeInterface; + /** * Create a node and test node edit functionality. * * @group node */ -class PageEditTest extends NodeTestBase { +class NodeEditFormTest extends NodeTestBase { + + /** + * A normal logged in user. + * + * @var \Drupal\user\UserInterface + */ protected $webUser; + + /** + * A user with permission to bypass content access checks. + * + * @var \Drupal\user\UserInterface + */ protected $adminUser; + /** + * The node storage. + * + * @var \Drupal\node\NodeStorageInterface + */ + protected $nodeStorage; + /** * Modules to enable. * @@ -29,12 +50,14 @@ protected function setUp() { $this->webUser = $this->drupalCreateUser(array('edit own page content', 'create page content')); $this->adminUser = $this->drupalCreateUser(array('bypass node access', 'administer nodes')); $this->drupalPlaceBlock('local_tasks_block'); + + $this->nodeStorage = $this->container->get('entity.manager')->getStorage('node'); } /** * Checks node edit functionality. */ - function testPageEdit() { + public function testNodeEdit() { $this->drupalLogin($this->webUser); $title_key = 'title[0][value]'; @@ -98,8 +121,7 @@ function testPageEdit() { /** * Tests changing a node's "authored by" field. */ - function testPageAuthoredBy() { - $node_storage = $this->container->get('entity.manager')->getStorage('node'); + public function testNodeEditAuthoredBy() { $this->drupalLogin($this->adminUser); // Create node to edit. @@ -113,18 +135,69 @@ function testPageAuthoredBy() { $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]'); + + // 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. $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->assertRaw(t('There are no entities matching "%name".', array('%name' => 'invalid-name'))); - // Change the authored by field to the anonymous user (uid 0). - $edit['uid[0][target_id]'] = 'Anonymous (0)'; + // Change the authored by field to an empty string, which should assign + // authorship to the anonymous user (uid 0). + $edit[$form_element_name] = ''; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save and keep published')); - $node_storage->resetCache(array($node->id())); - $node = $node_storage->load($node->id()); + $this->nodeStorage->resetCache(array($node->id())); + $node = $this->nodeStorage->load($node->id()); $uid = $node->getOwnerId(); // Most SQL database drivers stringify fetches but entities are not // necessarily stored in a SQL database. At the same time, NULL/FALSE/"" @@ -133,15 +206,11 @@ function testPageAuthoredBy() { // Change the authored by field to another user's name (that is not // 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')); - $node_storage->resetCache(array($node->id())); - $node = $node_storage->load($node->id()); + $this->nodeStorage->resetCache(array($node->id())); + $node = $this->nodeStorage->load($node->id()); $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]'); } + }