You are here

NodeOwnerTest.php in Zircon Profile 8

Same filename and directory in other branches
  1. 8.0 core/modules/node/src/Tests/NodeOwnerTest.php

Namespace

Drupal\node\Tests

File

core/modules/node/src/Tests/NodeOwnerTest.php
View source
<?php

/**
 * @file
 * Contains \Drupal\node\Tests\NodeOwnerTest.
 */
namespace Drupal\node\Tests;

use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\system\Tests\Entity\EntityUnitTestBase;

/**
 * Tests node owner functionality.
 *
 * @group Entity
 */
class NodeOwnerTest extends EntityUnitTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'node',
    'language',
  );
  protected function setUp() {
    parent::setUp();

    // Create the node bundles required for testing.
    $type = NodeType::create(array(
      'type' => 'page',
      'name' => 'page',
    ));
    $type
      ->save();

    // Enable two additional languages.
    ConfigurableLanguage::createFromLangcode('de')
      ->save();
    ConfigurableLanguage::createFromLangcode('it')
      ->save();
    $this
      ->installSchema('node', 'node_access');
  }

  /**
   * Tests node owner functionality.
   */
  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());
  }

}

Classes

Namesort descending Description
NodeOwnerTest Tests node owner functionality.