feeds_mapper_field.test 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperFieldTestCase.
  5. */
  6. /**
  7. * Test case for simple CCK field mapper mappers/content.inc.
  8. */
  9. class FeedsMapperFieldTestCase extends FeedsMapperTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Mapper: Fields',
  13. 'description' => 'Test Feeds Mapper support for fields.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp(array('number'));
  19. }
  20. /**
  21. * Basic test loading a double entry CSV file.
  22. */
  23. public function test() {
  24. // Create content type.
  25. $typename = $this->createContentType(array(), array(
  26. 'alpha' => 'text',
  27. 'beta' => 'number_integer',
  28. 'gamma' => 'number_decimal',
  29. 'delta' => 'number_float',
  30. ));
  31. // Create and configure importer.
  32. $this->createImporterConfiguration('Content CSV', 'csv');
  33. $this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
  34. $this->setPlugin('csv', 'FeedsFileFetcher');
  35. $this->setPlugin('csv', 'FeedsCSVParser');
  36. $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename));
  37. $this->addMappings('csv', array(
  38. 0 => array(
  39. 'source' => 'title',
  40. 'target' => 'title',
  41. ),
  42. 1 => array(
  43. 'source' => 'created',
  44. 'target' => 'created',
  45. ),
  46. 2 => array(
  47. 'source' => 'body',
  48. 'target' => 'body',
  49. ),
  50. 3 => array(
  51. 'source' => 'alpha',
  52. 'target' => 'field_alpha',
  53. ),
  54. 4 => array(
  55. 'source' => 'beta',
  56. 'target' => 'field_beta',
  57. ),
  58. 5 => array(
  59. 'source' => 'gamma',
  60. 'target' => 'field_gamma',
  61. ),
  62. 6 => array(
  63. 'source' => 'delta',
  64. 'target' => 'field_delta',
  65. ),
  66. ));
  67. // Import CSV file.
  68. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
  69. $this->assertText('Created 2 nodes');
  70. // Check the two imported files.
  71. $this->drupalGet('node/1/edit');
  72. $this->assertNodeFieldValue('alpha', 'Lorem');
  73. $this->assertNodeFieldValue('beta', '42');
  74. $this->assertNodeFieldValue('gamma', '4.20');
  75. $this->assertNodeFieldValue('delta', '3.14159');
  76. $this->drupalGet('node/2/edit');
  77. $this->assertNodeFieldValue('alpha', 'Ut wisi');
  78. $this->assertNodeFieldValue('beta', '32');
  79. $this->assertNodeFieldValue('gamma', '1.20');
  80. $this->assertNodeFieldValue('delta', '5.62951');
  81. }
  82. /**
  83. * Tests if values are cleared out when an empty value is provided.
  84. */
  85. public function testClearOutValues() {
  86. // Create content type.
  87. $typename = $this->createContentType(array(), array(
  88. 'alpha' => 'text',
  89. 'beta' => 'number_integer',
  90. 'gamma' => 'number_decimal',
  91. 'delta' => 'number_float',
  92. ));
  93. // Create and configure importer.
  94. $this->createImporterConfiguration('Content CSV', 'csv');
  95. $this->setSettings('csv', NULL, array('content_type' => '', 'import_period' => FEEDS_SCHEDULE_NEVER));
  96. $this->setPlugin('csv', 'FeedsFileFetcher');
  97. $this->setPlugin('csv', 'FeedsCSVParser');
  98. $this->setSettings('csv', 'FeedsNodeProcessor', array('bundle' => $typename, 'update_existing' => 1));
  99. $this->addMappings('csv', array(
  100. array(
  101. 'source' => 'guid',
  102. 'target' => 'guid',
  103. 'unique' => TRUE,
  104. ),
  105. array(
  106. 'source' => 'title',
  107. 'target' => 'title',
  108. ),
  109. array(
  110. 'source' => 'created',
  111. 'target' => 'created',
  112. ),
  113. array(
  114. 'source' => 'body',
  115. 'target' => 'body',
  116. ),
  117. array(
  118. 'source' => 'alpha',
  119. 'target' => 'field_alpha',
  120. ),
  121. array(
  122. 'source' => 'beta',
  123. 'target' => 'field_beta',
  124. ),
  125. array(
  126. 'source' => 'gamma',
  127. 'target' => 'field_gamma',
  128. ),
  129. array(
  130. 'source' => 'delta',
  131. 'target' => 'field_delta',
  132. ),
  133. ));
  134. // Import CSV file.
  135. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
  136. $this->assertText('Created 2 nodes');
  137. // Check the two imported nodes.
  138. $this->drupalGet('node/1/edit');
  139. $this->assertNodeFieldValue('alpha', 'Lorem');
  140. $this->assertNodeFieldValue('beta', '42');
  141. $this->assertNodeFieldValue('gamma', '4.20');
  142. $this->assertNodeFieldValue('delta', '3.14159');
  143. $this->assertFieldByName('body[und][0][value]', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.');
  144. $this->drupalGet('node/2/edit');
  145. $this->assertNodeFieldValue('alpha', 'Ut wisi');
  146. $this->assertNodeFieldValue('beta', '32');
  147. $this->assertNodeFieldValue('gamma', '1.20');
  148. $this->assertNodeFieldValue('delta', '5.62951');
  149. $this->assertFieldByName('body[und][0][value]', 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.');
  150. // Import CSV file with empty values.
  151. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
  152. $this->assertText('Updated 2 nodes');
  153. // Check if all values were cleared out for node 1.
  154. $this->drupalGet('node/1/edit');
  155. $this->assertNoNodeFieldValue('alpha', 'Lorem');
  156. $this->assertNoNodeFieldValue('beta', '42');
  157. $this->assertNoNodeFieldValue('gamma', '4.20');
  158. $this->assertNoNodeFieldValue('delta', '3.14159');
  159. $this->assertFieldByName('body[und][0][value]', '');
  160. // Check if labels for fields that should be cleared out are not shown.
  161. $this->drupalGet('node/1');
  162. $this->assertNoText('alpha_text_label');
  163. $this->assertNoText('beta_number_integer_label');
  164. $this->assertNoText('gamma_number_decimal_label');
  165. $this->assertNoText('delta_number_float_label');
  166. // Check if zero's didn't cleared out values for node 2.
  167. $this->drupalGet('node/2/edit');
  168. $this->assertNodeFieldValue('alpha', 0);
  169. $this->assertNodeFieldValue('beta', 0);
  170. $this->assertNodeFieldValue('gamma', 0);
  171. $this->assertNodeFieldValue('delta', 0);
  172. $this->assertFieldByName('body[und][0][value]', 0);
  173. // Check if labels for fields of node 2 are still shown.
  174. $this->drupalGet('node/2');
  175. $this->assertText('alpha_text_label');
  176. $this->assertText('beta_number_integer_label');
  177. $this->assertText('gamma_number_decimal_label');
  178. $this->assertText('delta_number_float_label');
  179. // Re-import the first file again.
  180. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv');
  181. $this->assertText('Updated 2 nodes');
  182. // Check if the two imported nodes have content again.
  183. $this->drupalGet('node/1/edit');
  184. $this->assertNodeFieldValue('alpha', 'Lorem');
  185. $this->assertNodeFieldValue('beta', '42');
  186. $this->assertNodeFieldValue('gamma', '4.20');
  187. $this->assertNodeFieldValue('delta', '3.14159');
  188. $this->assertFieldByName('body[und][0][value]', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.');
  189. $this->drupalGet('node/2/edit');
  190. $this->assertNodeFieldValue('alpha', 'Ut wisi');
  191. $this->assertNodeFieldValue('beta', '32');
  192. $this->assertNodeFieldValue('gamma', '1.20');
  193. $this->assertNodeFieldValue('delta', '5.62951');
  194. $this->assertFieldByName('body[und][0][value]', 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.');
  195. // Import CSV file with non-existent values.
  196. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
  197. $this->assertText('Updated 2 nodes');
  198. // Check if all values were cleared out for node 1.
  199. $this->drupalGet('node/1/edit');
  200. $this->assertNoNodeFieldValue('alpha', 'Lorem');
  201. $this->assertNoNodeFieldValue('beta', '42');
  202. $this->assertNoNodeFieldValue('gamma', '4.20');
  203. $this->assertNoNodeFieldValue('delta', '3.14159');
  204. $this->assertFieldByName('body[und][0][value]', '');
  205. // Check if labels for fields that should be cleared out are not shown.
  206. $this->drupalGet('node/1');
  207. $this->assertNoText('alpha_text_label');
  208. $this->assertNoText('beta_number_integer_label');
  209. $this->assertNoText('gamma_number_decimal_label');
  210. $this->assertNoText('delta_number_float_label');
  211. }
  212. }