feeds_processor_term.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * @file
  4. * Tests for plugins/FeedsTermProcessor.inc
  5. */
  6. /**
  7. * Test aggregating a feed as data records.
  8. */
  9. class FeedsCSVtoTermsTest extends FeedsWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Processor: Taxonomy',
  13. 'description' => 'Tests a standalone import configuration that uses file fetcher and CSV parser to import taxonomy terms from a CSV file.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. /**
  18. * Set up test.
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. // Create an importer.
  23. $this->createImporterConfiguration('Term import', 'term_import');
  24. // Set and configure plugins and mappings.
  25. $this->setPlugin('term_import', 'FeedsFileFetcher');
  26. $this->setPlugin('term_import', 'FeedsCSVParser');
  27. $this->setPlugin('term_import', 'FeedsTermProcessor');
  28. // Create vocabulary.
  29. $edit = array(
  30. 'name' => 'Addams vocabulary',
  31. 'machine_name' => 'addams',
  32. );
  33. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  34. $this->setSettings('term_import', 'FeedsTermProcessor', array('bundle' => 'addams'));
  35. // Use standalone form.
  36. $this->setSettings('term_import', NULL, array('content_type' => ''));
  37. }
  38. /**
  39. * Test term creation, refreshing/deleting feeds and feed items.
  40. */
  41. public function test() {
  42. $mappings = array(
  43. 0 => array(
  44. 'source' => 'name',
  45. 'target' => 'name',
  46. 'unique' => 1,
  47. ),
  48. );
  49. $this->addMappings('term_import', $mappings);
  50. // Import and assert.
  51. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
  52. $this->assertText('Created 5 terms');
  53. $this->drupalGet('admin/structure/taxonomy/addams');
  54. $this->assertText('Morticia');
  55. $this->assertText('Fester');
  56. $this->assertText('Gomez');
  57. $this->assertText('Pugsley');
  58. // Import again.
  59. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
  60. $this->assertText('There are no new terms.');
  61. // Force update.
  62. $this->setSettings('term_import', 'FeedsTermProcessor', array(
  63. 'skip_hash_check' => TRUE,
  64. 'update_existing' => 2,
  65. ));
  66. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
  67. $this->assertText('Updated 5 terms.');
  68. // Add a term manually, delete all terms, this term should still stand.
  69. $edit = array(
  70. 'name' => 'Cousin Itt',
  71. );
  72. $this->drupalPost('admin/structure/taxonomy/addams/add', $edit, t('Save'));
  73. $this->drupalPost('import/term_import/delete-items', array(), t('Delete'));
  74. $this->drupalGet('admin/structure/taxonomy/addams');
  75. $this->assertText('Cousin Itt');
  76. $this->assertNoText('Morticia');
  77. $this->assertNoText('Fester');
  78. $this->assertNoText('Gomez');
  79. $this->assertNoText('Pugsley');
  80. }
  81. /**
  82. * Test that saving an invalid vocabulary throws an exception.
  83. */
  84. public function testInvalidVocabulary() {
  85. $mappings = array(
  86. 0 => array(
  87. 'source' => 'name',
  88. 'target' => 'name',
  89. 'unique' => 1,
  90. ),
  91. );
  92. $this->addMappings('term_import', $mappings);
  93. // Force configuration to be invalid.
  94. $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'term_import'))->fetchField());
  95. $config['processor']['config']['bundle'] = 'does_not_exist';
  96. db_update('feeds_importer')
  97. ->fields(array('config' => serialize($config)))
  98. ->condition('id', 'term_import')
  99. ->execute();
  100. // Import and assert.
  101. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
  102. $this->assertText(t('No vocabulary defined for Taxonomy Term processor.'));
  103. }
  104. /**
  105. * Tests that terms mapped to their parent by GUID are from the same vocabulary.
  106. */
  107. public function testParentTargetByGUID() {
  108. // Create an other vocabulary.
  109. $vocabulary1 = 'addams';
  110. $vocabulary2 = strtolower($this->randomName());
  111. $edit = array(
  112. 'name' => $this->randomString(),
  113. 'machine_name' => $vocabulary2,
  114. );
  115. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  116. // Add mappings for the first importer.
  117. $this->addMappings('term_import',
  118. array(
  119. 0 => array(
  120. 'source' => 'guid',
  121. 'target' => 'guid',
  122. 'unique' => TRUE,
  123. ),
  124. 1 => array(
  125. 'source' => 'name',
  126. 'target' => 'name',
  127. ),
  128. 2 => array(
  129. 'source' => 'parentguid',
  130. 'target' => 'parentguid',
  131. ),
  132. )
  133. );
  134. // Create a second importer.
  135. $this->createImporterConfiguration('Term import 2', 'term_import2');
  136. $this->setSettings('term_import2', NULL, array('content_type' => ''));
  137. // Set and configure plugins and mappings.
  138. $this->setPlugin('term_import2', 'FeedsFileFetcher');
  139. $this->setPlugin('term_import2', 'FeedsCSVParser');
  140. $this->setPlugin('term_import2', 'FeedsTermProcessor');
  141. $this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
  142. // Add mappings for the second importer.
  143. $this->addMappings('term_import2',
  144. array(
  145. 0 => array(
  146. 'source' => 'guid',
  147. 'target' => 'guid',
  148. 'unique' => TRUE,
  149. ),
  150. 1 => array(
  151. 'source' => 'name',
  152. 'target' => 'name',
  153. ),
  154. 2 => array(
  155. 'source' => 'parentguid',
  156. 'target' => 'parentguid',
  157. ),
  158. )
  159. );
  160. $values = array(
  161. 1 => 'Europe',
  162. 2 => 'Belgium',
  163. );
  164. // Import file using the first importer.
  165. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
  166. $this->assertText('Created 2 terms.');
  167. // Assert that two terms were created in the first vocabulary.
  168. $terms = entity_load('taxonomy_term', array_keys($values));
  169. foreach ($terms as $tid => $term) {
  170. $this->assertEqual($values[$tid], $term->name);
  171. $this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
  172. }
  173. // Assert that the second term's parent is the first term.
  174. $parents = taxonomy_get_parents($terms[2]->tid);
  175. $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
  176. if (!empty($parents)) {
  177. $parent = current($parents);
  178. $this->assertEqual(1, $parent->tid, $message);
  179. }
  180. else {
  181. $this->fail($message);
  182. }
  183. $values = array(
  184. 3 => 'Europe',
  185. 4 => 'Belgium',
  186. );
  187. // Now import the file using the second importer.
  188. $this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
  189. $this->assertText('Created 2 terms.');
  190. // Assert that two terms were created in the second vocabulary.
  191. $terms = entity_load('taxonomy_term', array_keys($values));
  192. foreach ($terms as $tid => $term) {
  193. $this->assertEqual($values[$tid], $term->name);
  194. $this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
  195. }
  196. // Assert that the second term's parent is the first term.
  197. $parents = taxonomy_get_parents($terms[4]->tid);
  198. $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
  199. if (!empty($parents)) {
  200. $parent = current($parents);
  201. $this->assertEqual(3, $parent->tid, $message);
  202. }
  203. else {
  204. $this->fail($message);
  205. }
  206. }
  207. /**
  208. * Tests that terms mapped to their parent by GUID are from the same vocabulary.
  209. */
  210. public function testParentTargetByName() {
  211. // Create an other vocabulary.
  212. $vocabulary1 = 'addams';
  213. $vocabulary2 = strtolower($this->randomName());
  214. $edit = array(
  215. 'name' => $this->randomString(),
  216. 'machine_name' => $vocabulary2,
  217. );
  218. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  219. // Add mappings for the first importer.
  220. $this->addMappings('term_import',
  221. array(
  222. 0 => array(
  223. 'source' => 'guid',
  224. 'target' => 'guid',
  225. 'unique' => TRUE,
  226. ),
  227. 1 => array(
  228. 'source' => 'name',
  229. 'target' => 'name',
  230. ),
  231. 2 => array(
  232. 'source' => 'parent',
  233. 'target' => 'parent',
  234. ),
  235. )
  236. );
  237. // Create a second importer.
  238. $this->createImporterConfiguration('Term import 2', 'term_import2');
  239. $this->setSettings('term_import2', NULL, array('content_type' => ''));
  240. // Set and configure plugins and mappings.
  241. $this->setPlugin('term_import2', 'FeedsFileFetcher');
  242. $this->setPlugin('term_import2', 'FeedsCSVParser');
  243. $this->setPlugin('term_import2', 'FeedsTermProcessor');
  244. $this->setSettings('term_import2', 'FeedsTermProcessor', array('bundle' => $vocabulary2));
  245. // Add mappings for the second importer.
  246. $this->addMappings('term_import2',
  247. array(
  248. 0 => array(
  249. 'source' => 'guid',
  250. 'target' => 'guid',
  251. 'unique' => TRUE,
  252. ),
  253. 1 => array(
  254. 'source' => 'name',
  255. 'target' => 'name',
  256. ),
  257. 2 => array(
  258. 'source' => 'parent',
  259. 'target' => 'parent',
  260. ),
  261. )
  262. );
  263. $values = array(
  264. 1 => 'Europe',
  265. 2 => 'Belgium',
  266. );
  267. // Import file using the first importer.
  268. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/terms.csv');
  269. $this->assertText('Created 2 terms.');
  270. // Assert that two terms were created in the first vocabulary.
  271. $terms = entity_load('taxonomy_term', array_keys($values));
  272. foreach ($terms as $tid => $term) {
  273. $this->assertEqual($values[$tid], $term->name);
  274. $this->assertEqual($vocabulary1, $term->vocabulary_machine_name);
  275. }
  276. // Assert that the second term's parent is the first term.
  277. $parents = taxonomy_get_parents($terms[2]->tid);
  278. $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[2]->name));
  279. if (!empty($parents)) {
  280. $parent = current($parents);
  281. $this->assertEqual(1, $parent->tid, $message);
  282. }
  283. else {
  284. $this->fail($message);
  285. }
  286. $values = array(
  287. 3 => 'Europe',
  288. 4 => 'Belgium',
  289. );
  290. // Now import the file using the second importer.
  291. $this->importFile('term_import2', $this->absolutePath() . '/tests/feeds/terms.csv');
  292. $this->assertText('Created 2 terms.');
  293. // Assert that two terms were created in the second vocabulary.
  294. $terms = entity_load('taxonomy_term', array_keys($values));
  295. foreach ($terms as $tid => $term) {
  296. $this->assertEqual($values[$tid], $term->name);
  297. $this->assertEqual($vocabulary2, $term->vocabulary_machine_name);
  298. }
  299. // Assert that the second term's parent is the first term.
  300. $parents = taxonomy_get_parents($terms[4]->tid);
  301. $message = format_string('The term @term is correctly linked to its parent.', array('@term' => $terms[4]->name));
  302. if (!empty($parents)) {
  303. $parent = current($parents);
  304. $this->assertEqual(3, $parent->tid, $message);
  305. }
  306. else {
  307. $this->fail($message);
  308. }
  309. }
  310. /**
  311. * Test replacing terms on subsequent imports.
  312. */
  313. public function testReplaceTerms() {
  314. $mappings = array(
  315. 0 => array(
  316. 'source' => 'name',
  317. 'target' => 'name',
  318. 'unique' => 1,
  319. ),
  320. );
  321. $this->addMappings('term_import', $mappings);
  322. // Configure the processor to "Replace existing terms".
  323. $this->setSettings('term_import', 'FeedsTermProcessor', array(
  324. 'skip_hash_check' => TRUE,
  325. 'update_existing' => 1,
  326. ));
  327. // Import first time.
  328. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
  329. $this->assertText('Created 5 terms');
  330. // Import again to replace terms.
  331. $this->importFile('term_import', $this->absolutePath() . '/tests/feeds/users.csv');
  332. $this->assertText('Updated 5 terms.');
  333. }
  334. }