node_export.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Node export/import tests.
  5. */
  6. define('ORIGINAL_TITLE', 'Original node title');
  7. define('CHANGED_TITLE', 'Changed node title');
  8. define('ORIGINAL_BODY', 'This is some original test text for the node body.');
  9. define('CHANGED_BODY', 'Changed test text for the node body.');
  10. /**
  11. * Test XML export and import.
  12. */
  13. class NodeExportXMLTestCase extends DrupalWebTestCase {
  14. protected $user;
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Node Export XML export/import test',
  18. 'description' => 'Test exporting a node as XML, changing the exported XML, then importing the changed XML.',
  19. 'group' => 'Node Export',
  20. );
  21. }
  22. function setUp() {
  23. parent::setUp('node_export');
  24. // Make sure the export gives back a file.
  25. variable_set('node_export_code', 'file');
  26. // We want to test XML export.
  27. variable_set('node_export_format', array('xml'));
  28. variable_set('node_export_existing', 'revision');
  29. $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'));
  30. $this->drupalLogin($this->user);
  31. }
  32. /**
  33. * Test XML export and import.
  34. */
  35. function testNodeExportXMLExportImport() {
  36. $langcode = LANGUAGE_NONE;
  37. $title_key = 'title';
  38. $body_key = "body[{$langcode}][0][value]";
  39. $original_title = ORIGINAL_TITLE;
  40. $changed_title = CHANGED_TITLE;
  41. $original_body = ORIGINAL_BODY;
  42. $changed_body = CHANGED_BODY;
  43. $settings = array(
  44. 'type' => 'page',
  45. 'title' => $original_title,
  46. 'body' => array($langcode => array(array('value' => $original_body))),
  47. );
  48. $node = $this->drupalCreateNode($settings);
  49. $this->verbose('Node created: ' . var_export($node, TRUE));
  50. $this->drupalGet("node/{$node->nid}/edit");
  51. $this->assertFieldByName($title_key, $original_title, "Found original title in edit form.");
  52. $this->assertFieldByName($body_key, $original_body, "Found original body in edit form.");
  53. // Test export.
  54. $xml_file = $this->drupalGet("node/{$node->nid}/node_export");
  55. // Check if the export is valid XML.
  56. $this->assertResponse(200, 'Export was successful.');
  57. $this->assertTrue(simplexml_load_string($xml_file), 'XML is valid.');
  58. $node_export = simplexml_load_string($xml_file);
  59. debug($node_export->asXml(), 'Before changing the XML');
  60. // Find the original title in the XML.
  61. $original_title_value = $node_export->xpath("node[1]/title[text() = '{$original_title}']");
  62. $title_found = !empty($original_title_value);
  63. $this->assertTrue($title_found, 'Original title was found in the XML.');
  64. if ($title_found) {
  65. // Change the title value.
  66. $original_title_value[0][0] = $changed_title;
  67. }
  68. // Find the original body in the XML.
  69. $original_body_value = $node_export->xpath("node[1]//body//n0//*[text() = '{$original_body}']");
  70. $body_found = !empty($original_body_value);
  71. $this->assertTrue($body_found, 'Original body was found in the XML.');
  72. if ($body_found) {
  73. // Change the body value.
  74. $original_body_value[0][0] = $changed_body;
  75. }
  76. // Find the changed title in the XML.
  77. $changed_title_value = $node_export->xpath("node[1]/title[text() = '{$changed_title}']");
  78. $this->assertTrue(!empty($changed_title_value), 'Changed title was found in the XML.');
  79. // Find the changed body in the XML.
  80. $changed_body_value = $node_export->xpath("node[1]//body//n0//*[text() = '{$changed_body}']");
  81. $this->assertTrue(!empty($changed_body_value), 'Changed body was found in the XML.');
  82. if ($title_found || $body_found) {
  83. debug($node_export->asXml(), 'After changing the XML');
  84. }
  85. // Test import.
  86. $import_info = node_export_import($node_export->asXml());
  87. $this->assertTrue($import_info['success'], 'Import was succesful.');
  88. if ($import_info['success']) {
  89. $this->assertEqual($import_info['format'], 'xml', 'XML was imported.');
  90. }
  91. $this->drupalGet("node/{$node->nid}/edit");
  92. $this->assertFieldByName($title_key, $changed_title, "Found changed title in edit form.");
  93. $this->assertFieldByName($body_key, $changed_body, "Found changed body in edit form.");
  94. }
  95. }