node.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the node destination plugin.
  5. */
  6. /**
  7. * Test node migration.
  8. */
  9. class MigrateNodeUnitTest extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Node migration',
  13. 'description' => 'Test migration of node data',
  14. 'group' => 'Migrate',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('list', 'number', 'taxonomy', 'image', 'migrate', 'migrate_example');
  19. }
  20. function testNodeImport() {
  21. $migration = Migration::getInstance('WineVariety');
  22. $result = $migration->processImport();
  23. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  24. t('Variety term import returned RESULT_COMPLETED'));
  25. $migration = Migration::getInstance('WineRegion');
  26. $result = $migration->processImport();
  27. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  28. t('Region term import returned RESULT_COMPLETED'));
  29. $migration = Migration::getInstance('WineBestWith');
  30. $result = $migration->processImport();
  31. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  32. t('"Best With" term import returned RESULT_COMPLETED'));
  33. $migration = Migration::getInstance('WineFileCopy');
  34. $result = $migration->processImport();
  35. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  36. t('File import returned RESULT_COMPLETED'));
  37. $migration = Migration::getInstance('WineRole');
  38. $result = $migration->processImport();
  39. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  40. t('Role import returned RESULT_COMPLETED'));
  41. $migration = Migration::getInstance('WineUser');
  42. $result = $migration->processImport();
  43. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  44. t('User import returned RESULT_COMPLETED'));
  45. $migration = Migration::getInstance('WineProducer');
  46. $result = $migration->processImport();
  47. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  48. t('Producer node import returned RESULT_COMPLETED'));
  49. $migration = Migration::getInstance('WineWine');
  50. $result = $migration->processImport();
  51. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  52. t('Wine node import returned RESULT_COMPLETED'));
  53. // Gather wine and producer nodes, and their corresponding input data
  54. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_producer'), TRUE);
  55. // Index by title
  56. $producer_nodes = array();
  57. foreach ($rawnodes as $node) {
  58. $producer_nodes[$node->title] = $node;
  59. }
  60. $query = db_select('migrate_example_wine_producer', 'p')
  61. ->fields('p', array('producerid', 'name', 'body', 'excerpt', 'accountid'));
  62. // Region term is singletons, handled straighforwardly
  63. $query->leftJoin('migrate_example_wine_category_producer', 'reg',
  64. "p.producerid = reg.producerid");
  65. $query->addField('reg', 'categoryid', 'region');
  66. $result = $query->execute();
  67. $producer_rows = array();
  68. foreach ($result as $row) {
  69. $producer_rows[$row->name] = $row;
  70. }
  71. $this->assertEqual(count($producer_nodes), count($producer_rows),
  72. t('Counts of producer nodes and input rows match'));
  73. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  74. // Index by title
  75. $wine_nodes = array();
  76. foreach ($rawnodes as $node) {
  77. $wine_nodes[$node->title] = $node;
  78. }
  79. $query = db_select('migrate_example_wine', 'w')
  80. ->fields('w', array('wineid', 'name', 'body', 'excerpt', 'accountid',
  81. 'posted', 'last_changed', 'variety', 'region'));
  82. $query->leftJoin('migrate_example_wine_category_wine', 'cwbw',
  83. "w.wineid = cwbw.wineid");
  84. $query->leftJoin('migrate_example_wine_categories', 'bw',
  85. "cwbw.categoryid = bw.categoryid AND bw.type = 'best_with'");
  86. // Gives a single comma-separated list of related terms
  87. $query->groupBy('cwbw.wineid');
  88. $query->addExpression('GROUP_CONCAT(bw.categoryid)', 'best_with');
  89. $result = $query->execute();
  90. $wine_rows = array();
  91. foreach ($result as $row) {
  92. $wine_rows[$row->name] = $row;
  93. }
  94. $this->assertEqual(count($wine_nodes), count($wine_rows),
  95. t('Counts of wine nodes and input rows match'));
  96. // Test each base node field
  97. $producer_node = $producer_nodes['Montes'];
  98. $producer_row = $producer_rows['Montes'];
  99. $wine_node = $wine_nodes['Montes Classic Cabernet Sauvignon'];
  100. $wine_row = $wine_rows['Montes Classic Cabernet Sauvignon'];
  101. $user_migration = MigrationBase::getInstance('WineUser');
  102. $mapped_uid = $user_migration->getMap()->lookupDestinationID(array($producer_row->accountid));
  103. if (is_array($mapped_uid)) {
  104. $this->assertEqual($producer_node->uid, reset($mapped_uid),
  105. t('uid properly migrated'));
  106. }
  107. else {
  108. $this->error(t('Account ID !id not migrated', array('!id' => $producer_row->accountid)));
  109. }
  110. $this->assertEqual($wine_node->created, $wine_row->posted,
  111. t('created properly migrated'));
  112. $this->assertEqual($wine_node->changed, $wine_row->last_changed,
  113. t('changed properly migrated'));
  114. // Test Field API fields of all types
  115. // body_with_summary
  116. $body = field_get_items('node', $wine_node, 'body');
  117. $this->assertEqual($body[0]['value'], 'REVIEW: ' . drupal_strtoupper($wine_row->body),
  118. t('body properly migrated'));
  119. $this->assertEqual($body[0]['summary'], $wine_row->excerpt,
  120. t('summary properly migrated'));
  121. // taxonomy_term_reference - single and multiple
  122. $variety = field_get_items('node', $wine_node, 'migrate_example_wine_varieties');
  123. $variety_migration = MigrationBase::getInstance('WineVariety');
  124. $mapped_tid = $variety_migration->getMap()->lookupDestinationID(array($wine_row->variety));
  125. if (is_array($mapped_tid)) {
  126. $this->assertEqual($variety[0]['tid'], reset($mapped_tid),
  127. t('Single taxonomy_term_reference properly migrated'));
  128. }
  129. else {
  130. $this->error(t('Variety !var not migrated', array('!var' => $wine_row->variety)));
  131. }
  132. $best_with = field_get_items('node', $wine_node, 'migrate_example_wine_best_with');
  133. $best_with_migration = MigrationBase::getInstance('WineBestWith');
  134. $source_ids = explode(',', $wine_row->best_with);
  135. $mapped_tids = array();
  136. foreach ($source_ids as $source_id) {
  137. $tid = $best_with_migration->getMap()->lookupDestinationID(array($source_id));
  138. if ($tid) {
  139. $mapped_tids[reset($tid)] = reset($tid);
  140. }
  141. }
  142. $this->assertEqual(count($best_with), count($mapped_tids),
  143. t('Counts of Best With match'));
  144. foreach ($best_with as $current) {
  145. $this->assertNotNull($mapped_tids[$current['tid']],
  146. t('Multiple value taxonomy_term_reference properly migrated'));
  147. }
  148. // Test the vintages field (demonstrating prepareRow() works) - we know
  149. // the valid vintages for this node are 2006 and 2007
  150. $expected = array(array('value' => 2006), array('value' => 2007));
  151. $this->assertEqual($wine_node->field_migrate_example_top_vintag[LANGUAGE_NONE], $expected,
  152. t('Vintages match (prepareRow works)'));
  153. // Test updates
  154. // Capture original nodes
  155. $original_nodes = node_load_multiple(array(), array('type' => 'migrate_example_wine'));
  156. $update_migration = Migration::getInstance('WineUpdates');
  157. $result = $update_migration->processImport();
  158. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  159. t('Wine updates import returned RESULT_COMPLETED'));
  160. $final_nodes = node_load_multiple(array(), array('type' => 'migrate_example_wine'), TRUE);
  161. foreach ($original_nodes as $nid => $original_node) {
  162. foreach ($original_node as $field => $value) {
  163. if ($field == 'field_migrate_example_wine_ratin' || $field == 'changed' || $field == 'revision_timestamp') {
  164. if ($value == $final_nodes[$nid]->$field) {
  165. $this->error(t('Field !field should have changed but did not, value=!value',
  166. array('!field' => $field, '!value' => print_r($value, TRUE))));
  167. }
  168. }
  169. else {
  170. if ($value != $final_nodes[$nid]->$field) {
  171. $this->error(t('Field !field mismatch: original !value1, result !value2',
  172. array('!field' => $field, '!value1' => print_r($value, TRUE),
  173. '!value2' => print_r($final_nodes[$nid]->$field, TRUE))));
  174. }
  175. }
  176. }
  177. }
  178. // Test highwater marks - add new wines, modify an old one, and see what changes
  179. $fields = array('wineid', 'name', 'body', 'excerpt', 'accountid',
  180. 'posted', 'last_changed', 'variety', 'region', 'rating');
  181. $query = db_insert('migrate_example_wine')
  182. ->fields($fields);
  183. $data = array(
  184. // Timestamps 1284008523, 1284120550
  185. array(3, 'Schloss Muhlenhof Dornfelder', 'Juicy black & red berry fruits', 'Pretty good', 9,
  186. strtotime('2010-09-09 01:02:03'), strtotime('2010-09-10 08:09:10'), 25, 17, 95),
  187. // Timestamps 1286122209, 1286122209
  188. array(4, 'Gachot-Monot Bourgogne Rge 06', 'Funky', 'Pair with white sauced dishes', 3,
  189. strtotime('2010-10-03 12:10:09'), strtotime('2010-10-03 12:10:09'), 26, 2, 85),
  190. );
  191. foreach ($data as $row) {
  192. $query->values(array_combine($fields, $row));
  193. }
  194. $query->execute();
  195. db_update('migrate_example_wine')
  196. ->fields(array(
  197. 'body' => 'Not so much berry character',
  198. // Timestamp 1285058521
  199. 'last_changed' => strtotime('2010-10-21 04:42:01'),
  200. ))
  201. ->condition('wineid', 2)
  202. ->execute();
  203. $migration = Migration::getInstance('WineWine');
  204. $result = $migration->processImport();
  205. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  206. t('Wine node import returned RESULT_COMPLETED'));
  207. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  208. if (!$this->assertEqual(count($rawnodes), 4, t('Now 4 wine nodes exist'))) {
  209. $this->error(t('There are now !count nodes', array('!count' => count($rawnodes))));
  210. }
  211. $nodes = node_load_multiple(FALSE, array('title' => 'Archeo Ruggero di Tasso Nero d\'Avola'), TRUE);
  212. $node = reset($nodes);
  213. $body = $node->body[LANGUAGE_NONE][0]['value'];
  214. if (!$this->assertEqual($body, 'REVIEW: NOT SO MUCH BERRY CHARACTER', t('Body updated'))) {
  215. $this->error(t('Actual body !body', array('!body' => $body)));
  216. }
  217. // Test rollback
  218. $result = $migration->processRollback(array('force' => TRUE));
  219. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  220. t('Wine node rollback returned RESULT_COMPLETED'));
  221. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  222. $this->assertEqual(count($rawnodes), 0, t('All nodes deleted'));
  223. $count = db_select('migrate_map_winewine', 'map')
  224. ->fields('map', array('sourceid1'))
  225. ->countQuery()
  226. ->execute()
  227. ->fetchField();
  228. $this->assertEqual($count, 0, t('Map cleared'));
  229. $count = db_select('migrate_message_winewine', 'msg')
  230. ->fields('msg', array('sourceid1'))
  231. ->countQuery()
  232. ->execute()
  233. ->fetchField();
  234. $this->assertEqual($count, 0, t('Messages cleared'));
  235. // Now test highwater with unjoined map table
  236. $migration->getSource()->setMapJoinable(FALSE);
  237. $result = $migration->processImport(array('limit' =>
  238. array('value' => 2, 'unit' => 'items')));
  239. db_update('migrate_example_wine')
  240. ->fields(array(
  241. 'body' => 'Very berry',
  242. // Timestamp 1286008921
  243. 'last_changed' => strtotime('2010-10-02 04:42:01'),
  244. ))
  245. ->condition('wineid', 1)
  246. ->execute();
  247. $result = $migration->processImport();
  248. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  249. t('Wine node import returned RESULT_COMPLETED'));
  250. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  251. if (!$this->assertEqual(count($rawnodes), 4, t('Now 4 wine nodes exist'))) {
  252. $this->error(t('There are now !count nodes', array('!count' => count($rawnodes))));
  253. }
  254. $nodes = node_load_multiple(FALSE, array('title' => 'Montes Classic Cabernet Sauvignon'), TRUE);
  255. $node = reset($nodes);
  256. $body = $node->body[LANGUAGE_NONE][0]['value'];
  257. if (!$this->assertEqual($body, 'REVIEW: VERY BERRY', t('Body updated'))) {
  258. $this->error(t('Actual body !body', array('!body' => $body)));
  259. }
  260. // Test itemlimit (joined map table)
  261. $result = $migration->processRollback(array('force' => TRUE));
  262. $migration->getSource()->setMapJoinable(TRUE);
  263. $result = $migration->processImport(array('limit' =>
  264. array('value' => 1, 'unit' => 'item')));
  265. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  266. t('Wine node import with itemlimit returned RESULT_COMPLETED'));
  267. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  268. $this->assertEqual(count($rawnodes), 1, t('One node imported'));
  269. // Test idlist (joined map table)
  270. $result = $migration->processRollback(array('force' => TRUE));
  271. $result = $migration->processImport(array('idlist' => 2));
  272. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  273. t('Wine node import with idlist returned RESULT_COMPLETED'));
  274. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  275. $this->assertEqual(count($rawnodes), 1, t('One node imported'));
  276. $node = reset($rawnodes);
  277. $this->assertEqual($node->title, 'Archeo Ruggero di Tasso Nero d\'Avola',
  278. t('Single specified node imported'));
  279. // Test itemlimit (unjoined map table)
  280. $result = $migration->processRollback(array('force' => TRUE));
  281. $migration->getSource()->setMapJoinable(FALSE);
  282. $result = $migration->processImport(array('limit' =>
  283. array('value' => 1, 'unit' => 'item')));
  284. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  285. t('Wine node import with itemlimit returned RESULT_COMPLETED'));
  286. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  287. $this->assertEqual(count($rawnodes), 1, t('One node imported'));
  288. // Test idlist (unjoined map table)
  289. $result = $migration->processRollback(array('force' => TRUE));
  290. $result = $migration->processImport(array('idlist' => 2));
  291. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  292. t('Wine node import with idlist returned RESULT_COMPLETED'));
  293. $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_wine'), TRUE);
  294. $this->assertEqual(count($rawnodes), 1, t('One node imported'));
  295. $node = reset($rawnodes);
  296. $this->assertEqual($node->title, 'Archeo Ruggero di Tasso Nero d\'Avola',
  297. t('Single specified node imported'));
  298. // Test integer highwater marks (http://drupal.org/node/1161612)
  299. $result = $migration->processRollback(array('force' => TRUE));
  300. db_update('migrate_example_wine')
  301. ->fields(array('last_changed' => 100000000))
  302. ->condition('wineid', 1)
  303. ->execute();
  304. db_update('migrate_example_wine')
  305. ->fields(array('last_changed' => 200000000))
  306. ->condition('wineid', 2)
  307. ->execute();
  308. db_update('migrate_example_wine')
  309. ->fields(array('last_changed' => 300000000))
  310. ->condition('wineid', 3)
  311. ->execute();
  312. db_update('migrate_example_wine')
  313. ->fields(array('last_changed' => 400000000))
  314. ->condition('wineid', 4)
  315. ->execute();
  316. $result = $migration->processImport();
  317. // Just a quick check to make sure we got four nodes with the right changed values
  318. $count = db_query("SELECT COUNT(nid)
  319. FROM {node} n
  320. INNER JOIN {migrate_map_winewine} map ON n.nid=map.destid1
  321. WHERE n.changed = map.sourceid1*100000000")->fetchField();
  322. $this->assertEqual($count, 4, t('Four nodes with updated changed values imported'));
  323. // We mark two nodes with higher updated values. If these end up being treated
  324. // as strings in saveHighwater(), the saved highwater mark will end up as
  325. // 500000000 instead of 1000000000.
  326. db_update('migrate_example_wine')
  327. ->fields(array('last_changed' => 1000000000))
  328. ->condition('wineid', 2)
  329. ->execute();
  330. db_update('migrate_example_wine')
  331. ->fields(array('last_changed' => 500000000))
  332. ->condition('wineid', 3)
  333. ->execute();
  334. $result = $migration->processImport();
  335. $newHighwater = db_select('migrate_status', 'ms')
  336. ->fields('ms', array('highwater'))
  337. ->condition('machine_name', 'WineWine')
  338. ->execute()
  339. ->fetchField();
  340. if (!$this->assertEqual($newHighwater, 1000000000, t('Correct highwater mark set'))) {
  341. $this->error(t('Unexpected highwater mark !highwater', array('!highwater' => $newHighwater)));
  342. }
  343. // Test for http://drupal.org/node/1037872 - updating with nid mapped and idlist
  344. $migration = Migration::getInstance('BeerTerm');
  345. $result = $migration->processImport();
  346. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  347. t('Beer term import returned RESULT_COMPLETED'));
  348. $migration = Migration::getInstance('BeerUser');
  349. $result = $migration->processImport();
  350. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  351. t('Beer user import returned RESULT_COMPLETED'));
  352. $migration = Migration::getInstance('BeerNode');
  353. $result = $migration->processImport();
  354. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  355. t('Beer node import returned RESULT_COMPLETED'));
  356. db_update('migrate_map_beernode')
  357. ->fields(array('needs_update' => 1))
  358. ->execute();
  359. $result = $migration->processImport(array('idlist' => 99999999));
  360. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  361. t('Beer node update import returned RESULT_COMPLETED'));
  362. $result = db_select('migrate_message_beernode', 'msg')
  363. ->fields('msg', array('message'))
  364. ->execute();
  365. foreach ($result as $row) {
  366. $this->error($row->message);
  367. }
  368. }
  369. }