feeds_mapper_date.test 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperDateTestCase.
  5. */
  6. /**
  7. * Test case for CCK date field mapper mappers/date.inc.
  8. *
  9. * @todo: Add test method iCal
  10. * @todo: Add test method for end date
  11. */
  12. class FeedsMapperDateTestCase extends FeedsMapperTestCase {
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Mapper: Date',
  16. 'description' => 'Test Feeds Mapper support for CCK Date fields.',
  17. 'group' => 'Feeds',
  18. 'dependencies' => array('date'),
  19. );
  20. }
  21. public function setUp() {
  22. parent::setUp(array('date_api', 'date'));
  23. variable_set('date_default_timezone', 'UTC');
  24. }
  25. /**
  26. * Basic test loading a single entry CSV file.
  27. */
  28. public function test() {
  29. $this->drupalGet('admin/config/regional/settings');
  30. // Create content type.
  31. $typename = $this->createContentType(array(), array(
  32. 'date' => 'date',
  33. 'datestamp' => 'datestamp',
  34. //'datetime' => 'datetime', // REMOVED because the field is broken ATM.
  35. ));
  36. // Hack to get date fields to not round to every 15 minutes.
  37. foreach (array('date', 'datestamp') as $field) {
  38. $field = 'field_' . $field;
  39. $edit = array(
  40. 'widget_type' => 'date_select',
  41. );
  42. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
  43. $edit = array(
  44. 'instance[widget][settings][increment]' => 1,
  45. );
  46. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
  47. $edit = array(
  48. 'widget_type' => 'date_text',
  49. );
  50. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
  51. }
  52. // Create and configure importer.
  53. $this->createImporterConfiguration('Date RSS', 'daterss');
  54. $this->setSettings('daterss', NULL, array(
  55. 'content_type' => '',
  56. 'import_period' => FEEDS_SCHEDULE_NEVER,
  57. ));
  58. $this->setPlugin('daterss', 'FeedsFileFetcher');
  59. $this->setSettings('daterss', 'FeedsNodeProcessor', array(
  60. 'bundle' => $typename,
  61. ));
  62. $this->addMappings('daterss', array(
  63. 0 => array(
  64. 'source' => 'title',
  65. 'target' => 'title',
  66. ),
  67. 1 => array(
  68. 'source' => 'description',
  69. 'target' => 'body',
  70. ),
  71. 2 => array(
  72. 'source' => 'timestamp',
  73. 'target' => 'field_date:start',
  74. ),
  75. 3 => array(
  76. 'source' => 'timestamp',
  77. 'target' => 'field_datestamp:start',
  78. ),
  79. ));
  80. $edit = array(
  81. 'allowed_extensions' => 'rss2',
  82. );
  83. $this->drupalPost('admin/structure/feeds/daterss/settings/FeedsFileFetcher', $edit, 'Save');
  84. // Import CSV file.
  85. $this->importFile('daterss', $this->absolutePath() . '/tests/feeds/googlenewstz.rss2');
  86. $this->assertText('Created 6 nodes');
  87. // Check the imported nodes.
  88. $values = array(
  89. '01/06/2010 - 19:26',
  90. '01/06/2010 - 10:21',
  91. '01/06/2010 - 13:42',
  92. '01/06/2010 - 06:05',
  93. '01/06/2010 - 11:26',
  94. '01/07/2010 - 00:26',
  95. );
  96. for ($i = 1; $i <= 6; $i++) {
  97. $this->drupalGet("node/$i/edit");
  98. $this->assertNodeFieldValue('date', $values[$i-1]);
  99. $this->assertNodeFieldValue('datestamp', $values[$i-1]);
  100. }
  101. }
  102. protected function getFormFieldsNames($field_name, $index) {
  103. if (in_array($field_name, array('date', 'datetime', 'datestamp'))) {
  104. return array("field_{$field_name}[und][{$index}][value][date]");
  105. }
  106. else {
  107. return parent::getFormFieldsNames($field_name, $index);
  108. }
  109. }
  110. /**
  111. * Tests if values are cleared out when an empty value is provided.
  112. */
  113. public function testClearOutValues() {
  114. // Create content type.
  115. $typename = $this->createContentType(array(), array(
  116. 'date' => 'date',
  117. 'datestamp' => 'datestamp',
  118. 'datetime' => 'datetime',
  119. ));
  120. // Hack to get date fields to not round to every 15 minutes.
  121. foreach (array('date', 'datestamp', 'datetime') as $field) {
  122. $field = 'field_' . $field;
  123. $edit = array(
  124. 'widget_type' => 'date_select',
  125. );
  126. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
  127. $edit = array(
  128. 'instance[widget][settings][increment]' => 1,
  129. 'field[settings][enddate_get]' => 1,
  130. );
  131. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field, $edit, 'Save settings');
  132. $edit = array(
  133. 'widget_type' => 'date_text',
  134. );
  135. $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/' . $field . '/widget-type', $edit, 'Continue');
  136. }
  137. // Create and configure importer.
  138. $this->createImporterConfiguration('Content CSV', 'csv');
  139. $this->setSettings('csv', NULL, array(
  140. 'content_type' => '',
  141. 'import_period' => FEEDS_SCHEDULE_NEVER,
  142. ));
  143. $this->setPlugin('csv', 'FeedsFileFetcher');
  144. $this->setPlugin('csv', 'FeedsCSVParser');
  145. $this->setSettings('csv', 'FeedsNodeProcessor', array(
  146. 'bundle' => $typename,
  147. 'update_existing' => 1,
  148. ));
  149. $this->addMappings('csv', array(
  150. 0 => array(
  151. 'source' => 'title',
  152. 'target' => 'title',
  153. 'unique' => TRUE,
  154. ),
  155. 1 => array(
  156. 'source' => 'created',
  157. 'target' => 'field_date:start',
  158. ),
  159. 2 => array(
  160. 'source' => 'end',
  161. 'target' => 'field_date:end',
  162. ),
  163. 3 => array(
  164. 'source' => 'created',
  165. 'target' => 'field_datestamp:start',
  166. ),
  167. 4 => array(
  168. 'source' => 'end',
  169. 'target' => 'field_datestamp:end',
  170. ),
  171. 5 => array(
  172. 'source' => 'created',
  173. 'target' => 'field_datetime:start',
  174. ),
  175. 6 => array(
  176. 'source' => 'end',
  177. 'target' => 'field_datetime:end',
  178. ),
  179. ));
  180. // Import CSV file.
  181. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
  182. $this->assertText('Created 2 nodes');
  183. // Check the imported nodes.
  184. $date_values = array(
  185. 1 => array(
  186. 'created' => '09/03/2009 - 00:12',
  187. 'end' => '11/03/2012 - 09:58',
  188. ),
  189. 2 => array(
  190. 'created' => '09/02/2009 - 22:59',
  191. 'end' => '11/03/2012 - 08:46',
  192. ),
  193. );
  194. for ($i = 1; $i <= 2; $i++) {
  195. $this->drupalGet("node/$i/edit");
  196. $this->assertNodeFieldValue('date', $date_values[$i]['created']);
  197. $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
  198. $this->assertNodeFieldValue('datestamp', $date_values[$i]['created']);
  199. $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
  200. $this->assertNodeFieldValue('datetime', $date_values[$i]['created']);
  201. $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
  202. }
  203. // Import CSV file with empty values.
  204. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
  205. $this->assertText('Updated 2 nodes');
  206. // Check if all values were cleared out for both nodes.
  207. for ($i = 1; $i <= 2; $i++) {
  208. $this->drupalGet("node/$i/edit");
  209. $this->assertNoNodeFieldValue('date', $date_values[$i]['created']);
  210. $this->assertNoFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
  211. $this->assertNoNodeFieldValue('datestamp', $date_values[$i]['created']);
  212. $this->assertNoFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
  213. $this->assertNoNodeFieldValue('datetime', $date_values[$i]['created']);
  214. $this->assertNoFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
  215. $this->drupalGet("node/$i");
  216. $this->assertNoText('date_label');
  217. $this->assertNoText('datestamp_label');
  218. $this->assertNoText('datetime_label');
  219. }
  220. // Re-import the first file again and check if the values returned.
  221. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_date.csv');
  222. $this->assertText('Updated 2 nodes');
  223. for ($i = 1; $i <= 2; $i++) {
  224. $this->drupalGet("node/$i/edit");
  225. $this->assertNodeFieldValue('date', $date_values[$i]['created']);
  226. $this->assertFieldByName('field_date[und][0][value2][date]', $date_values[$i]['end']);
  227. $this->assertNodeFieldValue('datestamp', $date_values[$i]['created']);
  228. $this->assertFieldByName('field_datestamp[und][0][value2][date]', $date_values[$i]['end']);
  229. $this->assertNodeFieldValue('datetime', $date_values[$i]['created']);
  230. $this->assertFieldByName('field_datetime[und][0][value2][date]', $date_values[$i]['end']);
  231. }
  232. // Import CSV file with non-existent values.
  233. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
  234. $this->assertText('Updated 2 nodes');
  235. // Check if all values were cleared out for node 1.
  236. $this->drupalGet('node/1/edit');
  237. $this->assertNoNodeFieldValue('date', $date_values[1]['created']);
  238. $this->assertNoFieldByName('field_date[und][0][value2][date]', $date_values[1]['end']);
  239. $this->assertNoNodeFieldValue('datestamp', $date_values[1]['created']);
  240. $this->assertNoFieldByName('field_datestamp[und][0][value2][date]', $date_values[1]['end']);
  241. $this->assertNoNodeFieldValue('datetime', $date_values[1]['created']);
  242. $this->assertNoFieldByName('field_datetime[und][0][value2][date]', $date_values[1]['end']);
  243. // Check if labels for fields that should be cleared out are not shown.
  244. $this->drupalGet('node/1');
  245. $this->assertNoText('date_label');
  246. $this->assertNoText('datestamp_label');
  247. $this->assertNoText('datetime_label');
  248. }
  249. }