NodeOwnerTest.php in Drupal 10
File
core/modules/node/tests/src/Kernel/NodeOwnerTest.php
View source
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\User;
class NodeOwnerTest extends EntityKernelTestBase {
protected static $modules = [
'node',
'language',
];
protected function setUp() : void {
parent::setUp();
$type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$type
->save();
ConfigurableLanguage::createFromLangcode('de')
->save();
ConfigurableLanguage::createFromLangcode('it')
->save();
$this
->installSchema('node', 'node_access');
}
public function testOwner() {
$user = $this
->createUser();
$container = \Drupal::getContainer();
$container
->get('current_user')
->setAccount($user);
$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();
$english
->set('uid', [
'target_id' => NULL,
]);
$german
->set('uid', [
'target_id' => NULL,
]);
$italian
->set('uid', [
'target_id' => NULL,
]);
$italian
->save();
$this
->assertEquals(0, $english
->getOwnerId());
$this
->assertEquals(0, $german
->getOwnerId());
$this
->assertEquals(0, $italian
->getOwnerId());
}
public function testUnsavedNodeOwner() {
$user = User::create([
'name' => 'foo',
]);
$node = Node::create([
'type' => 'page',
'title' => $this
->randomMachineName(),
]);
$node
->setOwner($user);
$user
->save();
$node
->save();
$this
->assertEquals($user
->id(), $node
->getOwnerId());
}
}