function NodeOwnerTest::testUnsavedNodeOwner

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()
  3. 11.x 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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.