You are here

function NodeExportXMLTestCase::testNodeExportXMLExportImport in Node export 7.3

Test XML export and import.

File

./node_export.test, line 40
Node export/import tests.

Class

NodeExportXMLTestCase
Test XML export and import.

Code

function testNodeExportXMLExportImport() {
  $langcode = LANGUAGE_NONE;
  $title_key = 'title';
  $body_key = "body[{$langcode}][0][value]";
  $original_title = ORIGINAL_TITLE;
  $changed_title = CHANGED_TITLE;
  $original_body = ORIGINAL_BODY;
  $changed_body = CHANGED_BODY;
  $settings = array(
    'type' => 'page',
    'title' => $original_title,
    'body' => array(
      $langcode => array(
        array(
          'value' => $original_body,
        ),
      ),
    ),
  );
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->verbose('Node created: ' . var_export($node, TRUE));
  $this
    ->drupalGet("node/{$node->nid}/edit");
  $this
    ->assertFieldByName($title_key, $original_title, "Found original title in edit form.");
  $this
    ->assertFieldByName($body_key, $original_body, "Found original body in edit form.");

  // Test export.
  $xml_file = $this
    ->drupalGet("node/{$node->nid}/node_export");

  // Check if the export is valid XML.
  $this
    ->assertResponse(200, 'Export was successful.');
  $this
    ->assertTrue(simplexml_load_string($xml_file), 'XML is valid.');
  $node_export = simplexml_load_string($xml_file);
  debug($node_export
    ->asXml(), 'Before changing the XML');

  // Find the original title in the XML.
  $original_title_value = $node_export
    ->xpath("node[1]/title[text() = '{$original_title}']");
  $title_found = !empty($original_title_value);
  $this
    ->assertTrue($title_found, 'Original title was found in the XML.');
  if ($title_found) {

    // Change the title value.
    $original_title_value[0][0] = $changed_title;
  }

  // Find the original body in the XML.
  $original_body_value = $node_export
    ->xpath("node[1]//body//n0//*[text() = '{$original_body}']");
  $body_found = !empty($original_body_value);
  $this
    ->assertTrue($body_found, 'Original body was found in the XML.');
  if ($body_found) {

    // Change the body value.
    $original_body_value[0][0] = $changed_body;
  }

  // Find the changed title in the XML.
  $changed_title_value = $node_export
    ->xpath("node[1]/title[text() = '{$changed_title}']");
  $this
    ->assertTrue(!empty($changed_title_value), 'Changed title was found in the XML.');

  // Find the changed body in the XML.
  $changed_body_value = $node_export
    ->xpath("node[1]//body//n0//*[text() = '{$changed_body}']");
  $this
    ->assertTrue(!empty($changed_body_value), 'Changed body was found in the XML.');
  if ($title_found || $body_found) {
    debug($node_export
      ->asXml(), 'After changing the XML');
  }

  // Test import.
  $import_info = node_export_import($node_export
    ->asXml());
  $this
    ->assertTrue($import_info['success'], 'Import was succesful.');
  if ($import_info['success']) {
    $this
      ->assertEqual($import_info['format'], 'xml', 'XML was imported.');
  }
  $this
    ->drupalGet("node/{$node->nid}/edit");
  $this
    ->assertFieldByName($title_key, $changed_title, "Found changed title in edit form.");
  $this
    ->assertFieldByName($body_key, $changed_body, "Found changed body in edit form.");
}