You are here

function NodeSaveTest::testImport in Zircon Profile 8

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

Checks whether custom node IDs are saved properly during an import operation.

Workflow:

  • first create a piece of content
  • save the content
  • check if node exists

File

core/modules/node/src/Tests/NodeSaveTest.php, line 50
Contains \Drupal\node\Tests\NodeSaveTest.

Class

NodeSaveTest
Tests $node->save() for saving content.

Namespace

Drupal\node\Tests

Code

function testImport() {

  // Node ID must be a number that is not in the database.
  $nids = \Drupal::entityManager()
    ->getStorage('node')
    ->getQuery()
    ->sort('nid', 'DESC')
    ->range(0, 1)
    ->execute();
  $max_nid = reset($nids);
  $test_nid = $max_nid + mt_rand(1000, 1000000);
  $title = $this
    ->randomMachineName(8);
  $node = array(
    'title' => $title,
    'body' => array(
      array(
        'value' => $this
          ->randomMachineName(32),
      ),
    ),
    'uid' => $this->webUser
      ->id(),
    'type' => 'article',
    'nid' => $test_nid,
  );

  /** @var \Drupal\node\NodeInterface $node */
  $node = entity_create('node', $node);
  $node
    ->enforceIsNew();
  $this
    ->assertEqual($node
    ->getOwnerId(), $this->webUser
    ->id());
  $node
    ->save();

  // Test the import.
  $node_by_nid = Node::load($test_nid);
  $this
    ->assertTrue($node_by_nid, 'Node load by node ID.');
  $node_by_title = $this
    ->drupalGetNodeByTitle($title);
  $this
    ->assertTrue($node_by_title, 'Node load by node title.');
}