feeds_mapper_summary.test 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperNodeSummaryTestCase.
  5. */
  6. /**
  7. * Test case for mapping to node summary.
  8. */
  9. class FeedsMapperNodeSummaryTestCase extends FeedsMapperTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Mapper: Text with summary',
  13. 'description' => 'Test Feeds Mapper support for text with summary fields.',
  14. 'group' => 'Feeds',
  15. );
  16. }
  17. /**
  18. * Tests importing CSV files for text fields with summary.
  19. */
  20. public function test() {
  21. // Create and configure importer.
  22. $this->createImporterWithSummaryMapper();
  23. // Create a new filter format.
  24. $format = drupal_strtolower($this->randomName());
  25. $edit = array(
  26. 'format' => $format,
  27. 'name' => $this->randomName(),
  28. // Authenticated users.
  29. 'roles[2]' => TRUE,
  30. );
  31. $this->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
  32. // The "update existing" and "skip hash check" are turned on so we can test
  33. // later if the summaries of the nodes get overwritten with the values from
  34. // the source.
  35. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  36. 'update_existing' => 2,
  37. 'skip_hash_check' => TRUE,
  38. 'input_format' => $format,
  39. ));
  40. // Import CSV file.
  41. $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/node_summary.csv');
  42. $this->assertText('Created 3 nodes');
  43. $this->assertNodeSummaryValues();
  44. // Check that text format is applied correctly.
  45. $this->drupalGet('node/1/edit');
  46. $this->assertNodeFieldValue('format', $format);
  47. // Check the teasers of the three imported nodes, assumed to be all present
  48. // on the front page.
  49. $this->assertNodeTeaserValues();
  50. // Set a summary and a text for each imported node.
  51. $edit = array(
  52. 'body[und][0][summary]' => 'Nam liber tempor summary',
  53. 'body[und][0][value]' => 'Nam liber tempor body',
  54. );
  55. $this->drupalPost('node/1/edit', $edit, t('Save'));
  56. $this->drupalPost('node/2/edit', $edit, t('Save'));
  57. $this->drupalPost('node/3/edit', $edit, t('Save'));
  58. // Import the same CSV file again.
  59. $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/node_summary.csv');
  60. $this->assertText('Updated 3 nodes');
  61. $this->assertNodeSummaryValues();
  62. $this->assertNodeTeaserValues();
  63. // The previous texts of the nodes should no longer be visible.
  64. $this->assertNoText('Nam liber tempor summary');
  65. $this->assertNoText('Nam liber tempor body');
  66. // Check that text format is applied correctly.
  67. $this->drupalGet('node/1/edit');
  68. $this->assertNodeFieldValue('format', $format);
  69. $this->drupalGet('node/2/edit');
  70. $this->assertNodeFieldValue('format', $format);
  71. $this->drupalGet('node/3/edit');
  72. $this->assertNodeFieldValue('format', $format);
  73. // Remove the body mapping to check that the text format doesn't get updated
  74. // from the summary.
  75. $this->removeMappings('syndication', array(
  76. 2 => array(
  77. 'source' => 'body',
  78. 'target' => 'body',
  79. ),
  80. ));
  81. // Change the text format and remove the body mapping to ensure that the
  82. // text format doesn't change.
  83. $this->setSettings('syndication', 'FeedsNodeProcessor', array(
  84. 'input_format' => 'plain_text',
  85. ));
  86. $this->importFile('syndication', $this->absolutePath() . '/tests/feeds/node_summary.csv');
  87. $this->assertText('Updated 3 nodes');
  88. // Check that text format remains at its previous value.
  89. $this->drupalGet('node/1/edit');
  90. $this->assertNodeFieldValue('format', $format);
  91. $this->drupalGet('node/2/edit');
  92. $this->assertNodeFieldValue('format', $format);
  93. $this->drupalGet('node/3/edit');
  94. $this->assertNodeFieldValue('format', $format);
  95. }
  96. /**
  97. * Creates an importer with a summary mapper.
  98. *
  99. * @param $name
  100. * The natural name of the feed.
  101. * @param $id
  102. * The persistent id of the feed.
  103. *
  104. * @return void
  105. */
  106. protected function createImporterWithSummaryMapper($name = 'Syndication', $id = 'syndication') {
  107. // Create content type. A field named "body" which is of type "Long text and summary"
  108. // will be created by default, so we don't need to create a field of that type here.
  109. $typename = $this->createContentType(array());
  110. // Create and configure importer.
  111. $this->createImporterConfiguration($name, $id);
  112. $this->setSettings('syndication', NULL, array(
  113. 'content_type' => '',
  114. 'import_period' => FEEDS_SCHEDULE_NEVER,
  115. ));
  116. $this->setPlugin('syndication', 'FeedsFileFetcher');
  117. $this->setPlugin('syndication', 'FeedsCSVParser');
  118. $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
  119. $this->addMappings('syndication', array(
  120. 0 => array(
  121. 'source' => 'title',
  122. 'target' => 'title',
  123. ),
  124. 1 => array(
  125. 'source' => 'summary',
  126. 'target' => 'body:summary',
  127. ),
  128. 2 => array(
  129. 'source' => 'body',
  130. 'target' => 'body',
  131. ),
  132. 3 => array(
  133. 'source' => 'guid',
  134. 'target' => 'guid',
  135. 'unique' => TRUE,
  136. ),
  137. ));
  138. }
  139. /**
  140. * Overrides FeedsMapperTestCase::getFormFieldsNames().
  141. *
  142. * Returns different form field names for:
  143. * - body
  144. * This field doesn't have the "field_" prefix.
  145. * - summary
  146. * Which is part of the body field.
  147. * - format
  148. * The format of the body field.
  149. */
  150. protected function getFormFieldsNames($field_name, $index) {
  151. switch ($field_name) {
  152. case 'body':
  153. return array("body[und][{$index}][value]");
  154. case 'summary':
  155. return array("body[und][{$index}][summary]");
  156. case 'format':
  157. return array("body[und][{$index}][format]");
  158. }
  159. return parent::getFormFieldsNames($field_name, $index);
  160. }
  161. /**
  162. * Checks that the nodes match the imported values.
  163. */
  164. protected function assertNodeSummaryValues() {
  165. // Check the three imported nodes.
  166. $this->drupalGet('node/1/edit');
  167. $this->assertNodeFieldValue('summary', 'Lorem ipsum summary');
  168. $this->assertNodeFieldValue('body', 'Lorem ipsum body');
  169. $this->drupalGet('node/2/edit');
  170. $this->assertNodeFieldValue('summary', '');
  171. $this->assertNodeFieldValue('body', 'Ut wisi enim ad minim veniam body');
  172. $this->drupalGet('node/3/edit');
  173. $this->assertNodeFieldValue('summary', '');
  174. $this->assertNodeFieldValue('body', '');
  175. }
  176. /**
  177. * Checks the frontpage for teaser values.
  178. */
  179. protected function assertNodeTeaserValues() {
  180. $this->drupalGet('');
  181. $this->assertText('Lorem ipsum summary');
  182. $this->assertNoText('Lorem ipsum body');
  183. $this->assertText('Ut wisi enim ad minim veniam body');
  184. }
  185. }