feeds_mapper_taxonomy.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperTaxonomyTestCase.
  5. */
  6. /**
  7. * Test case for taxonomy mapper mappers/taxonomy.inc.
  8. */
  9. class FeedsMapperTaxonomyTestCase extends FeedsMapperTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Mapper: Taxonomy',
  13. 'description' => 'Test Feeds Mapper support for Taxonomy.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp();
  19. // Add Tags vocabulary
  20. $edit = array(
  21. 'name' => 'Tags',
  22. 'machine_name' => 'tags',
  23. );
  24. $this->drupalPost('admin/structure/taxonomy/add', $edit, 'Save');
  25. $edit = array(
  26. 'name' => 'term1',
  27. );
  28. $this->drupalPost('admin/structure/taxonomy/tags/add', $edit, t('Save'));
  29. $this->assertText('Created new term term1.');
  30. // Create term reference field.
  31. $field = array(
  32. 'field_name' => 'field_tags',
  33. 'type' => 'taxonomy_term_reference',
  34. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  35. 'settings' => array(
  36. 'allowed_values' => array(
  37. array(
  38. 'vocabulary' => 'tags',
  39. 'parent' => 0,
  40. ),
  41. ),
  42. ),
  43. );
  44. field_create_field($field);
  45. // Add term reference field to feed item bundle.
  46. $this->instance = array(
  47. 'field_name' => 'field_tags',
  48. 'bundle' => 'article',
  49. 'entity_type' => 'node',
  50. 'widget' => array(
  51. 'type' => 'options_select',
  52. ),
  53. 'display' => array(
  54. 'default' => array(
  55. 'type' => 'taxonomy_term_reference_link',
  56. ),
  57. ),
  58. );
  59. field_create_instance($this->instance);
  60. // Add term reference field to feed node bundle.
  61. $this->instance = array(
  62. 'field_name' => 'field_tags',
  63. 'bundle' => 'page',
  64. 'entity_type' => 'node',
  65. 'widget' => array(
  66. 'type' => 'options_select',
  67. ),
  68. 'display' => array(
  69. 'default' => array(
  70. 'type' => 'taxonomy_term_reference_link',
  71. ),
  72. ),
  73. );
  74. field_create_instance($this->instance);
  75. // Create an importer configuration with basic mapping.
  76. $this->createImporterConfiguration('Syndication', 'syndication');
  77. $this->addMappings('syndication',
  78. array(
  79. 0 => array(
  80. 'source' => 'title',
  81. 'target' => 'title',
  82. ),
  83. 1 => array(
  84. 'source' => 'description',
  85. 'target' => 'body',
  86. ),
  87. 2 => array(
  88. 'source' => 'timestamp',
  89. 'target' => 'created',
  90. ),
  91. 3 => array(
  92. 'source' => 'url',
  93. 'target' => 'url',
  94. 'unique' => TRUE,
  95. ),
  96. 4 => array(
  97. 'source' => 'guid',
  98. 'target' => 'guid',
  99. 'unique' => TRUE,
  100. ),
  101. )
  102. );
  103. }
  104. /**
  105. * Tests inheriting taxonomy from the feed node.
  106. */
  107. public function testInheritTaxonomy() {
  108. // Adjust importer settings
  109. $this->setSettings('syndication', NULL, array('import_period' => FEEDS_SCHEDULE_NEVER));
  110. $this->setSettings('syndication', NULL, array('import_on_create' => FALSE));
  111. $this->assertText('Do not import on submission');
  112. // Map feed node's taxonomy to feed item node's taxonomy.
  113. $mappings = array(
  114. 5 => array(
  115. 'source' => 'parent:taxonomy:tags',
  116. 'target' => 'field_tags',
  117. ),
  118. );
  119. $this->addMappings('syndication', $mappings);
  120. // Create feed node and add term term1.
  121. $langcode = LANGUAGE_NONE;
  122. $nid = $this->createFeedNode('syndication', NULL, 'Syndication');
  123. $term = taxonomy_get_term_by_name('term1');
  124. $term = reset($term);
  125. $edit = array(
  126. 'field_tags' . '[' . $langcode . '][]' => $term->tid,
  127. );
  128. $this->drupalPost("node/$nid/edit", $edit, t('Save'));
  129. $this->assertTaxonomyTerm($term->name);
  130. // Import nodes.
  131. $this->drupalPost("node/$nid/import", array(), 'Import');
  132. $this->assertText('Created 10 nodes.');
  133. $count = db_query("SELECT COUNT(*) FROM {taxonomy_index}")->fetchField();
  134. // There should be one term for each node imported plus the term on the feed node.
  135. $this->assertEqual(11, $count, 'Found correct number of tags for all feed nodes and feed items.');
  136. }
  137. /**
  138. * Tests searching taxonomy terms by name.
  139. */
  140. public function testSearchByName() {
  141. $terms = array(
  142. 'Drupal',
  143. 'localization',
  144. 'localization client',
  145. 'localization server',
  146. 'open atrium',
  147. 'translation',
  148. 'translation server',
  149. 'Drupal planet',
  150. );
  151. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  152. 'skip_hash_check' => TRUE,
  153. 'update_existing' => 2,
  154. ));
  155. $mappings = array(
  156. 5 => array(
  157. 'source' => 'tags',
  158. 'target' => 'field_tags',
  159. 'term_search' => 0,
  160. ),
  161. );
  162. $this->addMappings('syndication', $mappings);
  163. $nid = $this->createFeedNode('syndication', NULL, 'Syndication');
  164. $this->assertText('Created 10 nodes.');
  165. // Check that terms we not auto-created.
  166. $this->drupalGet('node/2');
  167. foreach ($terms as $term) {
  168. $this->assertNoTaxonomyTerm($term);
  169. }
  170. $this->drupalGet('node/3');
  171. $this->assertNoTaxonomyTerm('Washington DC');
  172. // Change the mapping configuration.
  173. $this->removeMappings('syndication', $mappings);
  174. // Turn on autocreate.
  175. $mappings[5]['autocreate'] = TRUE;
  176. $this->addMappings('syndication', $mappings);
  177. $this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
  178. $this->assertText('Updated 10 nodes.');
  179. $this->drupalGet('node/2');
  180. foreach ($terms as $term) {
  181. $this->assertTaxonomyTerm($term);
  182. }
  183. $this->drupalGet('node/3');
  184. $this->assertTaxonomyTerm('Washington DC');
  185. $names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol();
  186. $this->assertEqual(count($names), 31, 'Found correct number of terms in the database.');
  187. // Run import again. This verifys that the terms we found by name.
  188. $this->drupalPost('node/' . $nid . '/import', array(), t('Import'));
  189. $this->assertText('Updated 10 nodes.');
  190. $names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol();
  191. $this->assertEqual(count($names), 31, 'Found correct number of terms in the database.');
  192. }
  193. /**
  194. * Tests mapping to taxonomy terms by tid.
  195. */
  196. public function testSearchByID() {
  197. // Create 10 terms. The first one was created in setup.
  198. $terms = array(1);
  199. foreach (range(2, 10) as $i) {
  200. $term = (object) array(
  201. 'name' => 'term' . $i,
  202. 'vid' => 1,
  203. );
  204. taxonomy_term_save($term);
  205. $terms[] = $term->tid;
  206. }
  207. FeedsPlugin::loadMappers();
  208. $entity = new stdClass();
  209. $target = 'field_tags';
  210. $mapping = array(
  211. 'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_ID,
  212. 'language' => LANGUAGE_NONE,
  213. );
  214. $source = FeedsSource::instance('tmp', 0);
  215. taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping);
  216. $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
  217. // Test a second mapping with a bogus term id.
  218. taxonomy_feeds_set_target($source, $entity, $target, array(1234), $mapping);
  219. $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
  220. }
  221. /**
  222. * Tests mapping to a taxonomy term's guid.
  223. */
  224. public function testSearchByGUID() {
  225. // Create 10 terms. The first one was created in setup.
  226. $tids = array(1);
  227. foreach (range(2, 10) as $i) {
  228. $term = (object) array(
  229. 'name' => 'term' . $i,
  230. 'vid' => 1,
  231. );
  232. taxonomy_term_save($term);
  233. $tids[] = $term->tid;
  234. }
  235. // Create a bunch of bogus imported terms.
  236. $guids = array();
  237. foreach ($tids as $tid) {
  238. $guid = 100 * $tid;
  239. $guids[] = $guid;
  240. $record = array(
  241. 'entity_type' => 'taxonomy_term',
  242. 'entity_id' => $tid,
  243. 'id' => 'does_not_exist',
  244. 'feed_nid' => 0,
  245. 'imported' => REQUEST_TIME,
  246. 'url' => '',
  247. 'guid' => $guid,
  248. );
  249. drupal_write_record('feeds_item', $record);
  250. }
  251. FeedsPlugin::loadMappers();
  252. $entity = new stdClass();
  253. $target = 'field_tags';
  254. $mapping = array(
  255. 'term_search' => FEEDS_TAXONOMY_SEARCH_TERM_GUID,
  256. 'language' => LANGUAGE_NONE,
  257. );
  258. $source = FeedsSource::instance('tmp', 0);
  259. taxonomy_feeds_set_target($source, $entity, $target, $guids, $mapping);
  260. $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
  261. foreach ($entity->field_tags[LANGUAGE_NONE] as $delta => $values) {
  262. $this->assertEqual($tids[$delta], $values['tid'], 'Correct term id foud.');
  263. }
  264. // Test a second mapping with a bogus term id.
  265. taxonomy_feeds_set_target($source, $entity, $target, array(1234), $mapping);
  266. $this->assertEqual(count($entity->field_tags[LANGUAGE_NONE]), 10);
  267. foreach ($entity->field_tags[LANGUAGE_NONE] as $delta => $values) {
  268. $this->assertEqual($tids[$delta], $values['tid'], 'Correct term id foud.');
  269. }
  270. }
  271. /**
  272. * Tests importing empty values
  273. */
  274. public function testBlankSourceValues() {
  275. // Create a CSV importer configuration.
  276. $this->createImporterConfiguration('Node import from CSV', 'node');
  277. $this->setPlugin('node', 'FeedsFileFetcher');
  278. $this->setPlugin('node', 'FeedsCSVParser');
  279. $this->setSettings('node', 'FeedsNodeProcessor', array('bundle' => 'article'));
  280. $this->setSettings('node', NULL, array('content_type' => ''));
  281. $this->addMappings('node', array(
  282. 0 => array(
  283. 'source' => 'title',
  284. 'target' => 'title',
  285. ),
  286. 1 => array(
  287. 'source' => 'tags',
  288. 'target' => 'field_tags',
  289. 'term_search' => 0,
  290. 'autocreate' => 1,
  291. ),
  292. 2 => array(
  293. 'source' => 'guid',
  294. 'target' => 'guid',
  295. 'unique' => TRUE,
  296. ),
  297. ));
  298. // Verify that there are 5 nodes total.
  299. $this->importFile('node', $this->absolutePath() . '/tests/feeds/taxonomy_empty_terms.csv');
  300. $this->assertText('Created 5 nodes');
  301. // Make sure only two terms were added
  302. $names = db_query('SELECT name FROM {taxonomy_term_data}')->fetchCol();
  303. $this->assertEqual(count($names), 2, 'Found correct number of terms in the database.');
  304. // Make sure the correct terms were created
  305. $terms = array(
  306. 'term1',
  307. '0',
  308. );
  309. foreach ($terms as $term_name) {
  310. $this->assertTrue(in_array($term_name, $names), 'Correct term created');
  311. }
  312. }
  313. /**
  314. * Tests that there are no errors when trying to map to an invalid vocabulary.
  315. */
  316. public function testMissingVocabulary() {
  317. $this->addMappings('syndication', array(
  318. 5 => array(
  319. 'source' => 'tags',
  320. 'target' => 'field_tags',
  321. 'term_search' => 0,
  322. 'autocreate' => TRUE,
  323. ),
  324. ));
  325. // Create an invalid configuration.
  326. db_delete('taxonomy_vocabulary')->execute();
  327. $this->createFeedNode('syndication', NULL, 'Syndication');
  328. $this->assertText('Created 10 nodes.');
  329. }
  330. /**
  331. * Tests if values are cleared out when an empty value or no value
  332. * is provided.
  333. */
  334. public function testClearOutValues() {
  335. // Create a CSV importer configuration.
  336. $this->createImporterConfiguration('Node import from CSV', 'node');
  337. $this->setSettings('node', NULL, array(
  338. 'content_type' => '',
  339. ));
  340. $this->setPlugin('node', 'FeedsFileFetcher');
  341. $this->setPlugin('node', 'FeedsCSVParser');
  342. $this->setSettings('node', 'FeedsNodeProcessor', array(
  343. 'bundle' => 'article',
  344. 'update_existing' => 1,
  345. ));
  346. $this->addMappings('node', array(
  347. 0 => array(
  348. 'source' => 'title',
  349. 'target' => 'title',
  350. ),
  351. 1 => array(
  352. 'source' => 'alpha',
  353. 'target' => 'field_tags',
  354. 'term_search' => 0,
  355. 'autocreate' => 1,
  356. ),
  357. 2 => array(
  358. 'source' => 'guid',
  359. 'target' => 'guid',
  360. 'unique' => TRUE,
  361. ),
  362. ));
  363. $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
  364. $this->assertText('Created 2 nodes');
  365. // Check the imported nodes.
  366. $terms1 = taxonomy_get_term_by_name('Lorem');
  367. $term1 = reset($terms1);
  368. $terms2 = taxonomy_get_term_by_name('Ut wisi');
  369. $term2 = reset($terms2);
  370. $taxonomy_values = array(
  371. 1 => $term1->tid,
  372. 2 => $term2->tid,
  373. );
  374. for ($i = 1; $i <= 2; $i++) {
  375. $this->drupalGet("node/$i/edit");
  376. $this->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]);
  377. }
  378. // Import CSV file with empty values.
  379. $this->importFile('node', $this->absolutePath() . '/tests/feeds/content_empty.csv');
  380. $this->assertText('Updated 2 nodes');
  381. // Check if the taxonomy reference field was cleared out for node 1.
  382. $this->drupalGet('node/1/edit');
  383. $this->assertFieldByName('field_tags[und][]', '_none');
  384. $this->drupalGet('node/1');
  385. $this->assertNoText('field_tags');
  386. // Check if zero's didn't cleared out the taxonomy reference field for
  387. // node 2.
  388. $terms0 = taxonomy_get_term_by_name('0');
  389. $term0 = reset($terms0);
  390. $this->drupalGet('node/2/edit');
  391. $this->assertFieldByName('field_tags[und][]', $term0->tid);
  392. $this->drupalGet('node/2');
  393. $this->assertText('field_tags');
  394. // Re-import the first file again and check if the values returned.
  395. $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
  396. $this->assertText('Updated 2 nodes');
  397. for ($i = 1; $i <= 2; $i++) {
  398. $this->drupalGet("node/$i/edit");
  399. $this->assertFieldByName('field_tags[und][]', $taxonomy_values[$i]);
  400. }
  401. // Import CSV file with non-existent values.
  402. $this->importFile('node', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
  403. $this->assertText('Updated 2 nodes');
  404. // Check if the taxonomy reference field was cleared out for node 1.
  405. $this->drupalGet('node/1/edit');
  406. $this->assertFieldByName('field_tags[und][]', '_none');
  407. $this->drupalGet('node/1');
  408. $this->assertNoText('field_tags');
  409. }
  410. /**
  411. * Finds node style taxonomy term markup in DOM.
  412. */
  413. public function assertTaxonomyTerm($term) {
  414. $term = check_plain($term);
  415. $this->assertPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Found ' . $term);
  416. }
  417. /**
  418. * Asserts that the term does not exist on a node page.
  419. */
  420. public function assertNoTaxonomyTerm($term) {
  421. $term = check_plain($term);
  422. $this->assertNoPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Did not find ' . $term);
  423. }
  424. }