function NodeOwnerTest::testOwner

Same name in other branches
  1. 9 core/modules/node/tests/src/Kernel/NodeOwnerTest.php \Drupal\Tests\node\Kernel\NodeOwnerTest::testOwner()
  2. 8.9.x core/modules/node/tests/src/Kernel/NodeOwnerTest.php \Drupal\Tests\node\Kernel\NodeOwnerTest::testOwner()
  3. 10 core/modules/node/tests/src/Kernel/NodeOwnerTest.php \Drupal\Tests\node\Kernel\NodeOwnerTest::testOwner()

Tests node owner functionality.

File

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

Class

NodeOwnerTest
Tests node owner functionality.

Namespace

Drupal\Tests\node\Kernel

Code

public function testOwner() : void {
    $user = $this->createUser();
    $container = \Drupal::getContainer();
    $container->get('current_user')
        ->setAccount($user);
    // Create a test node.
    $english = Node::create([
        'type' => 'page',
        'title' => $this->randomMachineName(),
        'language' => 'en',
    ]);
    $english->save();
    $this->assertEquals($user->id(), $english->getOwnerId());
    $german = $english->addTranslation('de');
    $german->title = $this->randomString();
    $italian = $english->addTranslation('it');
    $italian->title = $this->randomString();
    // Node::preSave() sets owner to anonymous user if owner is nor set.
    $english->set('uid', [
        'target_id' => NULL,
    ]);
    $german->set('uid', [
        'target_id' => NULL,
    ]);
    $italian->set('uid', [
        'target_id' => NULL,
    ]);
    // This saves all translations!
    $italian->save();
    $this->assertEquals(0, $english->getOwnerId());
    $this->assertEquals(0, $german->getOwnerId());
    $this->assertEquals(0, $italian->getOwnerId());
}

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