function 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

drupal/core/modules/node/lib/Drupal/node/Tests/NodeSaveTest.php, line 47
Definition of Drupal\node\Tests\NodeSaveTest.

Class

NodeSaveTest
Tests node save related functionality, including import-save.

Namespace

Drupal\node\Tests

Code

function testImport() {

  // Node ID must be a number that is not in the database.
  $max_nid = db_query('SELECT MAX(nid) FROM {node}')
    ->fetchField();
  $test_nid = $max_nid + mt_rand(1000, 1000000);
  $title = $this
    ->randomName(8);
  $node = array(
    'title' => $title,
    'body' => array(
      array(
        'value' => $this
          ->randomName(32),
      ),
    ),
    'uid' => $this->web_user->uid,
    'type' => 'article',
    'nid' => $test_nid,
  );
  $node = node_submit(entity_create('node', $node));
  $node
    ->enforceIsNew();

  // Verify that node_submit did not overwrite the user ID.
  $this
    ->assertEqual($node->uid, $this->web_user->uid, 'Function node_submit() preserves user 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.');
}