You are here

public function NodeOwnerTest::testUnsavedNodeOwner in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Kernel/NodeOwnerTest.php \Drupal\Tests\node\Kernel\NodeOwnerTest::testUnsavedNodeOwner()
  2. 10 core/modules/node/tests/src/Kernel/NodeOwnerTest.php \Drupal\Tests\node\Kernel\NodeOwnerTest::testUnsavedNodeOwner()

Test an unsaved node owner.

File

core/modules/node/tests/src/Kernel/NodeOwnerTest.php, line 82

Class

NodeOwnerTest
Tests node owner functionality.

Namespace

Drupal\Tests\node\Kernel

Code

public function testUnsavedNodeOwner() {
  $user = User::create([
    'name' => 'foo',
  ]);
  $node = Node::create([
    'type' => 'page',
    'title' => $this
      ->randomMachineName(),
  ]);

  // Set the node owner while the user is unsaved and then immediately save
  // the user and node.
  $node
    ->setOwner($user);
  $user
    ->save();
  $node
    ->save();

  // The ID assigned to the newly saved user will now be the owner ID of the
  // node.
  $this
    ->assertEquals($user
    ->id(), $node
    ->getOwnerId());
}