You are here

public function NodeOwnerTest::testOwner in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Tests/NodeOwnerTest.php \Drupal\node\Tests\NodeOwnerTest::testOwner()

Tests node owner functionality.

File

core/modules/node/src/Tests/NodeOwnerTest.php, line 49
Contains \Drupal\node\Tests\NodeOwnerTest.

Class

NodeOwnerTest
Tests node owner functionality.

Namespace

Drupal\node\Tests

Code

public function testOwner() {
  $user = $this
    ->createUser();
  $container = \Drupal::getContainer();
  $container
    ->get('current_user')
    ->setAccount($user);

  // Create a test node.
  $english = Node::create(array(
    'type' => 'page',
    'title' => $this
      ->randomMachineName(),
    'language' => 'en',
  ));
  $english
    ->save();
  $this
    ->assertEqual($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,
  ]);

  // Entity::save() saves all translations!
  $italian
    ->save();
  $this
    ->assertEqual(0, $english
    ->getOwnerId());
  $this
    ->assertEqual(0, $german
    ->getOwnerId());
  $this
    ->assertEqual(0, $italian
    ->getOwnerId());
}