feeds_tests.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for Feeds tests.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function feeds_tests_menu() {
  10. $items['testing/feeds/flickr.xml'] = array(
  11. 'page callback' => 'feeds_tests_flickr',
  12. 'access arguments' => array('access content'),
  13. 'type' => MENU_CALLBACK,
  14. );
  15. $items['testing/feeds/files.csv'] = array(
  16. 'page callback' => 'feeds_tests_files',
  17. 'access arguments' => array('access content'),
  18. 'type' => MENU_CALLBACK,
  19. );
  20. $items['testing/feeds/files-remote.csv'] = array(
  21. 'page callback' => 'feeds_tests_files_remote',
  22. 'access arguments' => array('access content'),
  23. 'type' => MENU_CALLBACK,
  24. );
  25. $items['testing/feeds/files-empty-alt-title.csv'] = array(
  26. 'page callback' => 'feeds_tests_files_empty_alt_title',
  27. 'access arguments' => array('access content'),
  28. 'type' => MENU_CALLBACK,
  29. );
  30. $items['testing/feeds/files-empty.csv'] = array(
  31. 'page callback' => 'feeds_tests_files_empty',
  32. 'access arguments' => array('access content'),
  33. 'type' => MENU_CALLBACK,
  34. );
  35. return $items;
  36. }
  37. /**
  38. * Implements hook_theme().
  39. */
  40. function feeds_tests_theme() {
  41. return array(
  42. 'feeds_tests_flickr' => array(
  43. 'variables' => array('image_urls' => array()),
  44. 'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
  45. 'template' => 'feeds-tests-flickr',
  46. ),
  47. 'feeds_tests_files' => array(
  48. 'variables' => array('files' => array()),
  49. 'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
  50. 'template' => 'feeds-tests-files',
  51. ),
  52. 'feeds_tests_files_empty' => array(
  53. 'variables' => array('files' => array()),
  54. 'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
  55. 'template' => 'feeds-tests-files-empty',
  56. ),
  57. );
  58. }
  59. /**
  60. * Outputs flickr test feed.
  61. */
  62. function feeds_tests_flickr() {
  63. $images = array(
  64. 0 => "tubing.jpeg",
  65. 1 => "foosball.jpeg",
  66. 2 => "attersee.jpeg",
  67. 3 => "hstreet.jpeg",
  68. 4 => "la fayette.jpeg",
  69. );
  70. $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
  71. foreach ($images as &$image) {
  72. $image = file_create_url("$path/$image");
  73. }
  74. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  75. print theme('feeds_tests_flickr', array('image_urls' => $images));
  76. }
  77. /**
  78. * Outputs a CSV file pointing to files.
  79. */
  80. function feeds_tests_files() {
  81. $images = array(
  82. 0 => "tubing.jpeg",
  83. 1 => "foosball.jpeg",
  84. 2 => "attersee.jpeg",
  85. 3 => "hstreet.jpeg",
  86. 4 => "la fayette.jpeg",
  87. );
  88. foreach ($images as &$image) {
  89. $image = "public://images/$image";
  90. }
  91. drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
  92. print theme('feeds_tests_files', array('files' => $images));
  93. }
  94. /**
  95. * Outputs a CSV file pointing to files to download.
  96. */
  97. function feeds_tests_files_remote() {
  98. $images = array(
  99. 0 => 'tubing.jpeg',
  100. 1 => 'foosball.jpeg',
  101. 2 => 'attersee.jpeg',
  102. 3 => 'hstreet.jpeg',
  103. 4 => 'la fayette.jpeg',
  104. );
  105. $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
  106. foreach ($images as &$image) {
  107. $image = file_create_url("$path/$image");
  108. }
  109. drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
  110. print theme('feeds_tests_files', array('files' => $images));
  111. }
  112. /**
  113. * Outputs a CSV file pointing to files without alt/title.
  114. *
  115. * This is used to test if alt/title attributes are removed on a second import.
  116. */
  117. function feeds_tests_files_empty_alt_title() {
  118. $images = array(
  119. 0 => 'tubing.jpeg',
  120. 1 => 'foosball.jpeg',
  121. 2 => 'attersee.jpeg',
  122. 3 => 'hstreet.jpeg',
  123. 4 => 'la fayette.jpeg',
  124. );
  125. $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
  126. foreach ($images as &$image) {
  127. $image = file_create_url("$path/$image");
  128. }
  129. drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
  130. print theme('feeds_tests_files_empty', array('files' => $images));
  131. }
  132. /**
  133. * Outputs a CSV file pointing to no files.
  134. *
  135. * This is used to test if files are removed on a second import.
  136. */
  137. function feeds_tests_files_empty() {
  138. $images = array(
  139. 0 => '',
  140. 1 => '',
  141. 2 => '',
  142. 3 => '',
  143. 4 => '',
  144. );
  145. drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
  146. print theme('feeds_tests_files_empty', array('files' => $images));
  147. }
  148. /**
  149. * Implements hook_feeds_processor_targets().
  150. */
  151. function feeds_tests_feeds_processor_targets($entity_type, $bundle) {
  152. $targets = array();
  153. // Tests that old keys still work.
  154. $targets['test_target_compat'] = array(
  155. 'name' => t('Old style target'),
  156. 'callback' => 'feeds_tests_mapper_set_target',
  157. 'summary_callback' => 'feeds_tests_mapper_summary',
  158. 'form_callback' => 'feeds_tests_mapper_form',
  159. );
  160. $targets['test_target'] = array(
  161. 'name' => t('Test Target'),
  162. 'description' => t('This is a test target.'),
  163. 'callback' => 'feeds_tests_mapper_set_target',
  164. 'summary_callbacks' => array('feeds_tests_mapper_summary', 'feeds_tests_mapper_summary_2'),
  165. 'form_callbacks' => array('feeds_tests_mapper_form', 'feeds_tests_mapper_form_2'),
  166. 'preprocess_callbacks' => array(array(new FeedsTestsPreprocess(), 'callback')),
  167. );
  168. $targets['test_unique_target'] = array(
  169. 'name' => t('Test unique target'),
  170. 'description' => t('This is a unique test target.'),
  171. 'callback' => 'feeds_tests_mapper_set_target',
  172. 'optional_unique' => TRUE,
  173. 'unique_callbacks' => array('feeds_tests_mapper_unique'),
  174. 'preprocess_callbacks' => array(
  175. array('FeedsTestsPreprocess', 'callback'),
  176. // Make sure that invalid callbacks are filtered.
  177. '__feeds_tests_invalid_callback',
  178. ),
  179. );
  180. return $targets;
  181. }
  182. /**
  183. * Implements hook_feeds_processor_targets_alter().
  184. */
  185. function feeds_tests_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
  186. if (!isset($targets['test_target'])) {
  187. return;
  188. }
  189. $targets['test_target']['description'] = t('The target description was altered.');
  190. }
  191. /**
  192. * Preprocess callback for test_target.
  193. *
  194. * @see feeds_tests_feeds_processor_targets()
  195. */
  196. function feeds_tests_preprocess_callback(array $target, array &$mapping) {
  197. $mapping['required_value'] = TRUE;
  198. }
  199. /**
  200. * Set target value on entity.
  201. *
  202. * @see my_module_set_target()
  203. */
  204. function feeds_tests_mapper_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  205. if (empty($mapping['required_value'])) {
  206. trigger_error('The required value was not set.', E_USER_ERROR);
  207. }
  208. $entity->body['und'][0]['value'] = serialize($mapping);
  209. }
  210. /**
  211. * Provides setting summary for the mapper.
  212. *
  213. * @see my_module_summary_callback()
  214. */
  215. function feeds_tests_mapper_summary(array $mapping, $target, array $form, array $form_state) {
  216. $options = array(
  217. 'option1' => t('Option 1'),
  218. 'option2' => t('Another Option'),
  219. 'option3' => t('Option for select'),
  220. 'option4' => t('Another One'),
  221. );
  222. $items = array();
  223. if (!empty($mapping['checkbox']) && $mapping['checkbox']) {
  224. $items[] = t('Checkbox active.');
  225. }
  226. else {
  227. $items[] = t('Checkbox inactive.');
  228. }
  229. if (!empty($mapping['textfield'])) {
  230. $items[] = t('<strong>Textfield value</strong>: %textfield', array('%textfield' => $mapping['textfield']));
  231. }
  232. if (!empty($mapping['textarea'])) {
  233. $items[] = t('<strong>Textarea value</strong>: %textarea', array('%textarea' => $mapping['textarea']));
  234. }
  235. if (!empty($mapping['radios'])) {
  236. $items[] = t('<strong>Radios value</strong>: %radios', array('%radios' => $options[$mapping['radios']]));
  237. }
  238. if (!empty($mapping['select'])) {
  239. $items[] = t('<strong>Select value</strong>: %select', array('%select' => $options[$mapping['select']]));
  240. }
  241. $list = array(
  242. '#type' => 'ul',
  243. '#theme' => 'item_list',
  244. '#items' => $items,
  245. );
  246. return drupal_render($list);
  247. }
  248. /**
  249. * Provides a second summary callback.
  250. *
  251. * @see my_module_summary_callback()
  252. */
  253. function feeds_tests_mapper_summary_2(array $mapping, $target, array $form, array $form_state) {
  254. $mapping += array('second_value' => '');
  255. return t('Second summary: @value', array('@value' => $mapping['second_value']));
  256. }
  257. /**
  258. * Provides the form with mapper settings.
  259. */
  260. function feeds_tests_mapper_form(array $mapping, $target, array $form, array $form_state) {
  261. $mapping += array(
  262. 'checkbox' => FALSE,
  263. 'textfield' => '',
  264. 'textarea' => '',
  265. 'radios' => NULL,
  266. 'select' => NULL,
  267. );
  268. return array(
  269. 'checkbox' => array(
  270. '#type' => 'checkbox',
  271. '#title' => t('A checkbox'),
  272. '#default_value' => !empty($mapping['checkbox']),
  273. ),
  274. 'textfield' => array(
  275. '#type' => 'textfield',
  276. '#title' => t('A text field'),
  277. '#default_value' => $mapping['textfield'],
  278. '#required' => TRUE,
  279. ),
  280. 'textarea' => array(
  281. '#type' => 'textarea',
  282. '#title' => t('A textarea'),
  283. '#default_value' => $mapping['textarea'],
  284. ),
  285. 'radios' => array(
  286. '#type' => 'radios',
  287. '#title' => t('Some radios'),
  288. '#options' => array('option1' => t('Option 1'), 'option2' => t('Another Option')),
  289. '#default_value' => $mapping['radios'],
  290. ),
  291. 'select' => array(
  292. '#type' => 'select',
  293. '#title' => t('A select list'),
  294. '#options' => array('option3' => t('Option for select'), 'option4' => t('Another One')),
  295. '#default_value' => $mapping['select'],
  296. ),
  297. );
  298. }
  299. /**
  300. * Provides a second settings form.
  301. */
  302. function feeds_tests_mapper_form_2(array $mapping, $target, array $form, array $form_state) {
  303. return array(
  304. 'second_value' => array(
  305. '#type' => 'textfield',
  306. '#title' => t('The second callback value'),
  307. '#default_value' => !empty($mapping['second_value']) ? $mapping['second_value'] : '',
  308. ),
  309. );
  310. }
  311. /**
  312. * Callback for unique_callbacks for test_target mapper.
  313. *
  314. * @see feeds_tests_feeds_processor_targets()
  315. */
  316. function feeds_tests_mapper_unique(FeedsSource $source, $entity_type, $bundle, $target, array $values) {
  317. $query = new EntityFieldQuery();
  318. $result = $query
  319. ->entityCondition('entity_type', $entity_type)
  320. ->entityCondition('bundle', $bundle)
  321. ->fieldCondition('field_alpha', 'value', $values)
  322. ->execute();
  323. if (!empty($result[$entity_type])) {
  324. return key($result[$entity_type]);
  325. }
  326. }
  327. /**
  328. * Implements hook_feeds_after_parse().
  329. *
  330. * Empties the list of items to import in case the test says that there are
  331. * items in there with encoding issues. These items can not be processed during
  332. * tests without having a test failure because in < PHP 5.4 that would produce
  333. * the following warning:
  334. * htmlspecialchars(): Invalid multibyte sequence in argument
  335. *
  336. * @see FeedsCSVParserTestCase::testMbstringExtensionDisabled()
  337. */
  338. function feeds_tests_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  339. if (variable_get('feeds_tests_feeds_after_parse_empty_items', FALSE)) {
  340. // Remove all items. No items will be processed.
  341. $result->items = array();
  342. }
  343. }
  344. /**
  345. * Helper class to ensure callbacks can be objects.
  346. */
  347. class FeedsTestsPreprocess {
  348. /**
  349. * Preprocess callback for test_target.
  350. *
  351. * @see feeds_tests_feeds_processor_targets()
  352. */
  353. public static function callback(array $target, array &$mapping) {
  354. $mapping['required_value'] = TRUE;
  355. }
  356. }