You are here

public function NodeSaveTest::testImport in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeSaveTest.php \Drupal\Tests\node\Functional\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/tests/src/Functional/NodeSaveTest.php, line 50

Class

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

Namespace

Drupal\Tests\node\Functional

Code

public function testImport() {

  // Node ID must be a number that is not in the database.
  $nids = \Drupal::entityTypeManager()
    ->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 = [
    'title' => $title,
    'body' => [
      [
        'value' => $this
          ->randomMachineName(32),
      ],
    ],
    'uid' => $this->webUser
      ->id(),
    'type' => 'article',
    'nid' => $test_nid,
  ];

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

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