entityreference.feeds.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @file
  4. * Test case for simple CCK field mapper mappers/content.inc.
  5. */
  6. /**
  7. * Class for testing Feeds field mapper.
  8. */
  9. class FeedsMapperFieldTestCase extends DrupalWebTestCase{
  10. /**
  11. * Test info function.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Feeds integration (field mapper)',
  16. 'description' => 'Test Feeds Mapper support for fields.',
  17. 'group' => 'Entity Reference',
  18. );
  19. }
  20. /**
  21. * Set-up function.
  22. */
  23. public function setUp() {
  24. parent::setUp();
  25. module_enable(array('entityreference_feeds_test'), TRUE);
  26. $this->resetAll();
  27. if (!module_exists('feeds')) {
  28. return;
  29. }
  30. $permissions[] = 'access content';
  31. $permissions[] = 'administer site configuration';
  32. $permissions[] = 'administer content types';
  33. $permissions[] = 'administer nodes';
  34. $permissions[] = 'bypass node access';
  35. $permissions[] = 'administer taxonomy';
  36. $permissions[] = 'administer users';
  37. $permissions[] = 'administer feeds';
  38. // Create an admin user and log in.
  39. $this->admin_user = $this->drupalCreateUser($permissions);
  40. $this->drupalLogin($this->admin_user);
  41. }
  42. /**
  43. * Check if mapping exists.
  44. *
  45. * @param string $id
  46. * ID of the importer.
  47. * @param integer $i
  48. * The key of the mapping.
  49. * @param string $source
  50. * The source field.
  51. * @param string $target
  52. * The target field.
  53. *
  54. * @return integer
  55. * -1 if the mapping doesn't exist, the key of the mapping otherwise.
  56. */
  57. public function mappingExists($id, $i, $source, $target) {
  58. $current_mappings = $this->getCurrentMappings($id);
  59. if ($current_mappings) {
  60. foreach ($current_mappings as $key => $mapping) {
  61. if ($mapping['source'] == $source && $mapping['target'] == $target && $key == $i) {
  62. return $key;
  63. }
  64. }
  65. }
  66. return -1;
  67. }
  68. /**
  69. * Adds mappings to a given configuration.
  70. *
  71. * @param string $id
  72. * ID of the importer.
  73. * @param array $mappings
  74. * An array of mapping arrays. Each mapping array must have a source and
  75. * an target key and can have a unique key.
  76. * @param bool $test_mappings
  77. * (optional) TRUE to automatically test mapping configs. Defaults to TRUE.
  78. */
  79. public function addMappings($id, $mappings, $test_mappings = TRUE) {
  80. $path = "admin/structure/feeds/$id/mapping";
  81. // Iterate through all mappings and add the mapping via the form.
  82. foreach ($mappings as $i => $mapping) {
  83. if ($test_mappings) {
  84. $current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
  85. $this->assertEqual($current_mapping_key, -1, 'Mapping does not exist before addition.');
  86. }
  87. // Get unique flag and unset it. Otherwise, drupalPost will complain that
  88. // Split up config and mapping.
  89. $config = $mapping;
  90. unset($config['source'], $config['target']);
  91. $mapping = array('source' => $mapping['source'], 'target' => $mapping['target']);
  92. // Add mapping.
  93. $this->drupalPost($path, $mapping, t('Add'));
  94. // If there are other configuration options, set them.
  95. if ($config) {
  96. $this->drupalPostAJAX(NULL, array(), 'mapping_settings_edit_' . $i);
  97. // Set some settings.
  98. $edit = array();
  99. foreach ($config as $key => $value) {
  100. $edit["config[$i][settings][$key]"] = $value;
  101. }
  102. $this->drupalPostAJAX(NULL, $edit, 'mapping_settings_update_' . $i);
  103. $this->drupalPost(NULL, array(), t('Save'));
  104. }
  105. if ($test_mappings) {
  106. $current_mapping_key = $this->mappingExists($id, $i, $mapping['source'], $mapping['target']);
  107. $this->assertTrue($current_mapping_key >= 0, 'Mapping exists after addition.');
  108. }
  109. }
  110. }
  111. /**
  112. * Gets an array of current mappings from the feeds_importer config.
  113. *
  114. * @param string $id
  115. * ID of the importer.
  116. *
  117. * @return bool|array
  118. * FALSE if the importer has no mappings, or an an array of mappings.
  119. */
  120. public function getCurrentMappings($id) {
  121. $config = db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => $id))->fetchField();
  122. $config = unserialize($config);
  123. // We are very specific here. 'mappings' can either be an array or not
  124. // exist.
  125. if (array_key_exists('mappings', $config['processor']['config'])) {
  126. $this->assertTrue(is_array($config['processor']['config']['mappings']), 'Mappings is an array.');
  127. return $config['processor']['config']['mappings'];
  128. }
  129. return FALSE;
  130. }
  131. /**
  132. * Basic test loading a double entry CSV file.
  133. */
  134. public function test() {
  135. if (!module_exists('feeds')) {
  136. return;
  137. }
  138. $this->drupalLogin($this->admin_user);
  139. $this->drupalGet('admin/structure/types/manage/article/fields');
  140. $this->assertText('Ref - entity ID', t('Found Entity reference field %field.', array('%field' => 'field_er_id')));
  141. $this->assertText('Ref - entity label', t('Found Entity reference field %field.', array('%field' => 'field_er_label')));
  142. $this->assertText('Ref - feeds GUID', t('Found Entity reference field %field.', array('%field' => 'field_er_guid')));
  143. $this->assertText('Ref - feeds URL', t('Found Entity reference field %field.', array('%field' => 'field_er_url')));
  144. // Add feeds importer
  145. $this->drupalGet('admin/structure/feeds');
  146. $this->clickLink('Add importer');
  147. $this->drupalPost('admin/structure/feeds/create', array('name' => 'Nodes', 'id' => 'nodes'), 'Create');
  148. $this->assertText('Your configuration has been created with default settings.');
  149. $this->drupalPost('admin/structure/feeds/nodes/settings/', array('content_type' => '', 'import_period' => -1), 'Save');
  150. $this->assertText('Your changes have been saved.');
  151. $this->drupalPost("admin/structure/feeds/nodes/fetcher", array('plugin_key' => 'FeedsFileFetcher'), 'Save');
  152. $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
  153. $this->assertEqual($config['fetcher']['plugin_key'], 'FeedsFileFetcher', 'Verified correct fetcher (FeedsFileFetcher).');
  154. $this->drupalPost("admin/structure/feeds/nodes/parser", array('plugin_key' => 'FeedsCSVParser'), 'Save');
  155. $config = unserialize(db_query("SELECT config FROM {feeds_importer} WHERE id = :id", array(':id' => 'nodes'))->fetchField());
  156. $this->assertEqual($config['parser']['plugin_key'], 'FeedsCSVParser', 'Verified correct parser (FeedsCSVParser).');
  157. $this->drupalPost('admin/structure/feeds/nodes/settings/FeedsNodeProcessor', array('content_type' => 'article'), 'Save');
  158. $this->assertText('Your changes have been saved.');
  159. $this->addMappings('nodes', array(
  160. 0 => array(
  161. 'source' => 'title',
  162. 'target' => 'title',
  163. ),
  164. 1 => array(
  165. 'source' => 'nid',
  166. 'target' => 'nid',
  167. 'unique' => TRUE,
  168. ),
  169. 2 => array(
  170. 'source' => 'permalink',
  171. 'target' => 'url',
  172. 'unique' => TRUE,
  173. ),
  174. 3 => array(
  175. 'source' => 'nid',
  176. 'target' => 'guid',
  177. 'unique' => TRUE,
  178. ),
  179. 4 => array(
  180. 'source' => 'parent_nid',
  181. 'target' => 'field_er_id:etid',
  182. ),
  183. 5 => array(
  184. 'source' => 'parent_title',
  185. 'target' => 'field_er_label:label',
  186. ),
  187. 6 => array(
  188. 'source' => 'parent_url',
  189. 'target' => 'field_er_url:url',
  190. ),
  191. 7 => array(
  192. 'source' => 'parent_guid',
  193. 'target' => 'field_er_guid',
  194. ),
  195. )
  196. );
  197. $file = realpath(getcwd()) . '/' . drupal_get_path('module', 'entityreference') . '/tests/feeds_test.csv';
  198. $this->assertTrue(file_exists($file), 'Source file exists');
  199. $this->drupalPost('import/nodes', array('files[feeds]' => $file), 'Import');
  200. $this->assertText('Created 2 nodes');
  201. $parent = node_load(1);
  202. $this->assertTrue(empty($parent->field_er_id['und'][0]['target_id']), t('Parent node: Import by entity ID OK.'));
  203. $this->assertTrue(empty($parent->field_er_label['und'][0]['target_id']), t('Parent node: Import by entity label OK.'));
  204. $this->assertTrue(empty($parent->field_er_guid['und'][0]['target_id']), t('Parent node: Import by feeds GUID OK.'));
  205. $this->assertTrue(empty($parent->field_er_url['und'][0]['target_id']), t('Parent node: Import by feeds URL OK.'));
  206. $child = node_load(2);
  207. $this->assertTrue($child->field_er_id['und'][0]['target_id'] == 1, t('Child node: Import by entity ID OK.'));
  208. $this->assertTrue($child->field_er_label['und'][0]['target_id'] == 1, t('Child node: Import by entity label OK.'));
  209. $this->assertTrue($child->field_er_guid['und'][0]['target_id'] == 1, t('Child node: Import by feeds GUID OK.'));
  210. $this->assertTrue($child->field_er_url['und'][0]['target_id'] == 1, t('Child node: Import by feeds URL OK.'));
  211. }
  212. }