entityreference.feeds.test 8.7 KB

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