feeds_mapper_taxonomy.test 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @file
  4. * Test case for taxonomy mapper mappers/taxonomy.inc.
  5. */
  6. /**
  7. * Class for testing Feeds <em>content</em> mapper.
  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. 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. * Test inheriting taxonomy from the feed node.
  106. */
  107. 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. * Test aggregating RSS categories to taxonomy.
  139. */
  140. /*
  141. function testRSSCategoriesToTaxonomy() {
  142. // Add mapping to tags vocabulary.
  143. $this->addMappings('syndication',
  144. array(
  145. array(
  146. 'source' => 'tags',
  147. 'target' => 'taxonomy:1',
  148. ),
  149. )
  150. );
  151. // Aggregate feed node with "Tag" vocabulary.
  152. $nid = $this->createFeedNode();
  153. // Assert 10 items aggregated after creation of the node.
  154. $this->assertText('Created 10 nodes');
  155. // There should be 30 terms and 44 term-node relations.
  156. $this->assertEqual(30, db_query("SELECT count(*) FROM {term_data}")->fetchField(), "Found correct number of terms.");
  157. $this->assertEqual(44, db_query("SELECT count(*) FROM {term_node}")->fetchField(), "Found correct number of term-node relations.");
  158. // Take a look at the actual terms on frontpage.
  159. $this->drupalGet('node');
  160. $terms = array(
  161. 'authentication',
  162. 'custom mapping',
  163. 'data visualization',
  164. 'Drupal',
  165. 'Drupal planet',
  166. 'faceted search',
  167. 'GeoDC',
  168. 'graphs',
  169. 'interface',
  170. 'intranet',
  171. 'localization',
  172. 'localization client',
  173. 'localization server',
  174. 'map-basec browser',
  175. 'mapbox',
  176. 'microfinance',
  177. 'MIX Market',
  178. 'open atrium',
  179. 'open data',
  180. 'open source',
  181. 'Peru',
  182. 'salesforce',
  183. 'siteminder',
  184. 'siteminder module',
  185. 'software freedom day',
  186. 'translation',
  187. 'translation server',
  188. 'usability',
  189. 'Washington DC',
  190. 'World Bank',
  191. );
  192. foreach ($terms as $term) {
  193. $this->assertTaxonomyTerm($term);
  194. }
  195. // Delete all items, all associations are gone.
  196. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  197. $this->assertText('Deleted 10 nodes');
  198. $this->assertEqual(30, db_query("SELECT count(*) FROM {term_data}")->fetchField(), "Found correct number of terms.");
  199. $this->assertEqual(0, db_query("SELECT count(*) FROM {term_node}")->fetchField(), "Found correct number of term-node relations.");
  200. // Remove "Tag" setting, import again.
  201. $edit = array(
  202. 'tags' => FALSE,
  203. );
  204. $this->drupalPost('admin/content/taxonomy/edit/vocabulary/1', $edit, 'Save');
  205. $this->drupalPost("node/$nid/import", array(), 'Import');
  206. $this->assertText('Created 10 nodes');
  207. // We should only get one term-node association per node.
  208. $this->assertEqual(30, db_query("SELECT count(*) FROM {term_data}")->fetchField(), "Found correct number of terms.");
  209. $this->assertEqual(10, db_query("SELECT count(*) FROM {term_node}")->fetchField(), "Found correct number of term-node relations.");
  210. // Delete all items.
  211. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  212. // Set vocabulary to multiple terms, import again.
  213. $edit = array(
  214. 'multiple' => TRUE,
  215. );
  216. $this->drupalPost('admin/content/taxonomy/edit/vocabulary/1', $edit, 'Save');
  217. $this->drupalPost("node/$nid/import", array(), 'Import');
  218. $this->assertText('Created 10 nodes');
  219. // We should get all term-node associations again.
  220. $this->assertEqual(30, db_query("SELECT count(*) FROM {term_data}")->fetchField(), "Found correct number of terms.");
  221. $this->assertEqual(44, db_query("SELECT count(*) FROM {term_node}")->fetchField(), "Found correct number of term-node relations.");
  222. // Delete all items.
  223. $this->drupalPost("node/$nid/delete-items", array(), 'Delete');
  224. // Remove a term, import again.
  225. $this->drupalPost('admin/content/taxonomy/edit/term/1', array(), 'Delete');
  226. $this->drupalPost(NULL, array(), 'Delete');
  227. $this->assertText('Deleted term');
  228. $this->drupalPost("node/$nid/import", array(), 'Import');
  229. $this->assertText('Created 10 nodes');
  230. // This term should now be missing from term-node associations.
  231. $this->assertEqual(29, db_query("SELECT count(*) FROM {term_data}")->fetchField(), "Found correct number of terms.");
  232. $this->assertEqual(39, db_query("SELECT count(*) FROM {term_node}")->fetchField(), "Found correct number of term-node relations.");
  233. }
  234. */
  235. /**
  236. * Helper, finds node style taxonomy term markup in DOM.
  237. */
  238. public function assertTaxonomyTerm($term) {
  239. $term = check_plain($term);
  240. $this->assertPattern('/<a href="\/.*taxonomy\/term\/[0-9]+">' . $term . '<\/a>/', 'Found ' . $term);
  241. }
  242. }