feeds_mapper_link.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * @file
  4. * Contains FeedsMapperLinkTestCase.
  5. */
  6. /**
  7. * Test case for CCK link mapper mappers/date.inc.
  8. */
  9. class FeedsMapperLinkTestCase extends FeedsMapperTestCase {
  10. /**
  11. * Title for link fields with a static title.
  12. *
  13. * @var string
  14. */
  15. private $staticTitle;
  16. /**
  17. * Name of created content type.
  18. *
  19. * @var string
  20. */
  21. private $contentType;
  22. public static function getInfo() {
  23. return array(
  24. 'name' => 'Mapper: Link',
  25. 'description' => 'Test Feeds Mapper support for Link fields.',
  26. 'group' => 'Feeds',
  27. 'dependencies' => array('link'),
  28. );
  29. }
  30. public function setUp() {
  31. parent::setUp(array('link'));
  32. $this->staticTitle = $this->randomName();
  33. // Create content type.
  34. $this->contentType = $this->createContentType(array(), array(
  35. 'alpha' => array(
  36. 'type' => 'link_field',
  37. 'instance_settings' => array(
  38. 'instance[settings][title]' => 'required',
  39. ),
  40. ),
  41. 'beta' => array(
  42. 'type' => 'link_field',
  43. 'instance_settings' => array(
  44. 'instance[settings][title]' => 'none',
  45. ),
  46. ),
  47. 'gamma' => array(
  48. 'type' => 'link_field',
  49. 'instance_settings' => array(
  50. 'instance[settings][title]' => 'optional',
  51. ),
  52. ),
  53. 'omega' => array(
  54. 'type' => 'link_field',
  55. 'instance_settings' => array(
  56. 'instance[settings][title]' => 'value',
  57. 'instance[settings][title_value]' => $this->staticTitle,
  58. ),
  59. ),
  60. ));
  61. }
  62. /**
  63. * Basic test loading a single entry CSV file.
  64. */
  65. public function test() {
  66. // Create importer configuration.
  67. $this->createImporterConfiguration(); //Create a default importer configuration
  68. $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $this->contentType)); //Processor settings
  69. $this->addMappings('syndication', array(
  70. 0 => array(
  71. 'source' => 'title',
  72. 'target' => 'title'
  73. ),
  74. 1 => array(
  75. 'source' => 'timestamp',
  76. 'target' => 'created'
  77. ),
  78. 2 => array(
  79. 'source' => 'description',
  80. 'target' => 'body'
  81. ),
  82. 3 => array(
  83. 'source' => 'url',
  84. 'target' => 'field_alpha:url'
  85. ),
  86. 4 => array(
  87. 'source' => 'title',
  88. 'target' => 'field_alpha:title'
  89. ),
  90. 5 => array(
  91. 'source' => 'url',
  92. 'target' => 'field_beta:url'
  93. ),
  94. 6 => array(
  95. 'source' => 'url',
  96. 'target' => 'field_gamma:url'
  97. ),
  98. 7 => array(
  99. 'source' => 'title',
  100. 'target' => 'field_gamma:title'
  101. ),
  102. 8 => array(
  103. 'source' => 'url',
  104. 'target' => 'field_omega:url'
  105. ),
  106. ));
  107. // Import RSS file.
  108. $nid = $this->createFeedNode();
  109. // Assert 10 items aggregated after creation of the node.
  110. $this->assertText('Created 10 nodes');
  111. // Edit the imported node.
  112. $this->drupalGet('node/2/edit');
  113. $url = 'http://developmentseed.org/blog/2009/oct/06/open-atrium-translation-workflow-two-way-updating';
  114. $title = 'Open Atrium Translation Workflow: Two Way Translation Updates';
  115. $this->assertNodeFieldValue('alpha', array('url' => $url, 'static' => $title));
  116. $this->assertNodeFieldValue('beta', array('url' => $url));
  117. $this->assertNodeFieldValue('gamma', array('url' => $url, 'static' => $title));
  118. $this->assertNodeFieldValue('omega', array('url' => $url, 'static' => $this->staticTitle));
  119. // Test the static title.
  120. $this->drupalGet('node/2');
  121. $this->assertText($this->staticTitle, 'Static title link found.');
  122. }
  123. /**
  124. * Tests if values are cleared out when an empty value or no value
  125. * is provided.
  126. */
  127. public function testClearOutValues() {
  128. // Create and configure importer.
  129. $this->createImporterConfiguration('Content CSV', 'csv');
  130. $this->setSettings('csv', NULL, array(
  131. 'content_type' => '',
  132. 'import_period' => FEEDS_SCHEDULE_NEVER,
  133. ));
  134. $this->setPlugin('csv', 'FeedsFileFetcher');
  135. $this->setPlugin('csv', 'FeedsCSVParser');
  136. $this->setSettings('csv', 'FeedsNodeProcessor', array(
  137. 'bundle' => $this->contentType,
  138. 'update_existing' => 1,
  139. ));
  140. $this->addMappings('csv', array(
  141. 0 => array(
  142. 'source' => 'guid',
  143. 'target' => 'guid',
  144. 'unique' => TRUE,
  145. ),
  146. 1 => array(
  147. 'source' => 'title',
  148. 'target' => 'title'
  149. ),
  150. 2 => array(
  151. 'source' => 'url',
  152. 'target' => 'field_alpha:url'
  153. ),
  154. 3 => array(
  155. 'source' => 'link_title',
  156. 'target' => 'field_alpha:title'
  157. ),
  158. 4 => array(
  159. 'source' => 'url',
  160. 'target' => 'field_beta:url'
  161. ),
  162. 5 => array(
  163. 'source' => 'link_title',
  164. 'target' => 'field_beta:title'
  165. ),
  166. 6 => array(
  167. 'source' => 'url',
  168. 'target' => 'field_gamma:url'
  169. ),
  170. 7 => array(
  171. 'source' => 'link_title',
  172. 'target' => 'field_gamma:title'
  173. ),
  174. 8 => array(
  175. 'source' => 'url',
  176. 'target' => 'field_omega:url'
  177. ),
  178. 9 => array(
  179. 'source' => 'link_title',
  180. 'target' => 'field_omega:title'
  181. ),
  182. ));
  183. // Import CSV file.
  184. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_link.csv');
  185. $this->assertText('Created 2 nodes');
  186. // Check the imported nodes.
  187. $link_values = array(
  188. 1 => array(
  189. 'title' => 'Feeds',
  190. 'url' => 'https://www.drupal.org/project/feeds',
  191. ),
  192. 2 => array(
  193. 'title' => 'Framework for expected behavior when importing empty/blank values',
  194. 'url' => 'https://www.drupal.org/node/1107522',
  195. ),
  196. );
  197. for ($i = 1; $i <= 2; $i++) {
  198. $this->drupalGet("node/$i/edit");
  199. $this->assertNodeFieldValue('alpha', array(
  200. 'url' => $link_values[$i]['url'],
  201. 'title' => $link_values[$i]['title'],
  202. ));
  203. $this->assertNodeFieldValue('beta', array(
  204. 'url' => $link_values[$i]['url'],
  205. ));
  206. $this->assertNodeFieldValue('gamma', array(
  207. 'url' => $link_values[$i]['url'],
  208. 'title' => $link_values[$i]['title'],
  209. ));
  210. $this->assertNodeFieldValue('omega', array(
  211. 'url' => $link_values[$i]['url'],
  212. ));
  213. // Test static title.
  214. $this->drupalGet("node/$i");
  215. $this->assertText($this->staticTitle, 'Static title link found.');
  216. }
  217. // Import CSV file with empty values.
  218. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_empty.csv');
  219. $this->assertText('Updated 2 nodes');
  220. // Check if all values were cleared out for both nodes.
  221. for ($i = 1; $i <= 2; $i++) {
  222. $this->drupalGet("node/$i/edit");
  223. $this->assertNoNodeFieldValue('alpha', array(
  224. 'url' => $link_values[$i]['url'],
  225. 'title' => $link_values[$i]['title'],
  226. ));
  227. $this->assertNoNodeFieldValue('beta', array(
  228. 'url' => $link_values[$i]['url'],
  229. ));
  230. $this->assertNoNodeFieldValue('gamma', array(
  231. 'url' => $link_values[$i]['url'],
  232. 'title' => $link_values[$i]['title'],
  233. ));
  234. $this->assertNoNodeFieldValue('omega', array(
  235. 'url' => $link_values[$i]['url'],
  236. ));
  237. // Check labels.
  238. $this->drupalGet('node/' . $i);
  239. $this->assertNoText('alpha_link_field_label');
  240. $this->assertNoText('beta_link_field_label');
  241. $this->assertNoText('gamma_link_field_label');
  242. $this->assertNoText('omega_link_field_label');
  243. // Assert that the static title is no longer shown.
  244. $this->assertNoText($this->staticTitle, 'Static title link not found.');
  245. }
  246. // Re-import the first file again and check if the values returned.
  247. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_link.csv');
  248. $this->assertText('Updated 2 nodes');
  249. for ($i = 1; $i <= 2; $i++) {
  250. $this->drupalGet("node/$i/edit");
  251. $this->assertNodeFieldValue('alpha', array(
  252. 'url' => $link_values[$i]['url'],
  253. 'title' => $link_values[$i]['title'],
  254. ));
  255. $this->assertNodeFieldValue('beta', array(
  256. 'url' => $link_values[$i]['url'],
  257. ));
  258. $this->assertNodeFieldValue('gamma', array(
  259. 'url' => $link_values[$i]['url'],
  260. 'title' => $link_values[$i]['title'],
  261. ));
  262. $this->assertNodeFieldValue('omega', array(
  263. 'url' => $link_values[$i]['url'],
  264. ));
  265. }
  266. // Import CSV file with non-existent values.
  267. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_non_existent.csv');
  268. $this->assertText('Updated 2 nodes');
  269. // Check if all values were cleared out for node 1.
  270. $this->drupalGet("node/1/edit");
  271. $this->assertNoNodeFieldValue('alpha', array(
  272. 'url' => $link_values[1]['url'],
  273. 'title' => $link_values[1]['title'],
  274. ));
  275. $this->assertNoNodeFieldValue('beta', array(
  276. 'url' => $link_values[1]['url'],
  277. ));
  278. $this->assertNoNodeFieldValue('gamma', array(
  279. 'url' => $link_values[1]['url'],
  280. 'title' => $link_values[1]['title'],
  281. ));
  282. $this->assertNoNodeFieldValue('omega', array(
  283. 'url' => $link_values[1]['url'],
  284. ));
  285. $this->drupalGet('node/1');
  286. $this->assertNoText('alpha_link_field_label');
  287. $this->assertNoText('beta_link_field_label');
  288. $this->assertNoText('gamma_link_field_label');
  289. $this->assertNoText('omega_link_field_label');
  290. // Assert that the static title is no longer shown.
  291. $this->assertNoText($this->staticTitle, 'Static title link not found.');
  292. }
  293. /**
  294. * Override parent::getFormFieldsNames().
  295. */
  296. protected function getFormFieldsNames($field_name, $index) {
  297. if (in_array($field_name, array('alpha', 'beta', 'gamma', 'omega'))) {
  298. $fields = array("field_{$field_name}[und][{$index}][url]");
  299. if (in_array($field_name, array('alpha', 'gamma'))) {
  300. $fields[] = "field_{$field_name}[und][{$index}][title]";
  301. }
  302. return $fields;
  303. }
  304. else {
  305. return parent::getFormFieldsNames($field_name, $index);
  306. }
  307. }
  308. /**
  309. * Override parent::getFormFieldsValues().
  310. */
  311. protected function getFormFieldsValues($field_name, $value) {
  312. if (in_array($field_name, array('alpha', 'beta', 'gamma', 'omega'))) {
  313. if (!is_array($value)) {
  314. $value = array('url' => $value);
  315. }
  316. $values = array($value['url']);
  317. if (in_array($field_name, array('alpha', 'gamma'))) {
  318. $values[] = isset($value['title']) ? $value['title'] : '';
  319. }
  320. return $values;
  321. }
  322. else {
  323. return parent::getFormFieldsValues($field_name, $index);
  324. }
  325. }
  326. }