<?php
/**
 * @file
 * Node export/import tests.
 */

define('ORIGINAL_TITLE', 'Original node title');
define('CHANGED_TITLE', 'Changed node title');
define('ORIGINAL_BODY', 'This is some original test text for the node body.');
define('CHANGED_BODY', 'Changed test text for the node body.');

/**
 * Test XML export and import.
 */
class NodeExportXMLTestCase extends DrupalWebTestCase {
  protected $user;

  public static function getInfo() {
    return array(
      'name' => 'Node Export XML export/import test',
      'description' => 'Test exporting a node as XML, changing the exported XML, then importing the changed XML.',
      'group' => 'Node Export',
    );
  }

  function setUp() {
    parent::setUp('node_export');
    // Make sure the export gives back a file.
    variable_set('node_export_code', 'file');
    // We want to test XML export.
    variable_set('node_export_format', array('xml'));
    variable_set('node_export_existing', 'revision');
    $this->user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access', 'export nodes', 'export own nodes', 'use PHP to import nodes'));
    $this->drupalLogin($this->user);
  }

  /**
   * Test XML export and import.
   */
  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.");
  }
}
