Projet

Général

Profil

Paste
Télécharger (12,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_tests.module @ b5aa1857

1
<?php
2

    
3
/**
4
 * @file
5
 * Helper module for Feeds tests.
6
 */
7

    
8
/**
9
 * Implements hook_menu().
10
 */
11
function feeds_tests_menu() {
12
  $items['testing/feeds/flickr.xml'] = array(
13
    'page callback' => 'feeds_tests_flickr',
14
    'access arguments' => array('access content'),
15
    'type' => MENU_CALLBACK,
16
  );
17
  $items['testing/feeds/files.csv'] = array(
18
    'page callback' => 'feeds_tests_files',
19
    'access arguments' => array('access content'),
20
    'type' => MENU_CALLBACK,
21
  );
22
  $items['testing/feeds/files-remote.csv'] = array(
23
    'page callback' => 'feeds_tests_files_remote',
24
    'access arguments' => array('access content'),
25
    'type' => MENU_CALLBACK,
26
  );
27
  $items['testing/feeds/files-empty-alt-title.csv'] = array(
28
    'page callback' => 'feeds_tests_files_empty_alt_title',
29
    'access arguments' => array('access content'),
30
    'type' => MENU_CALLBACK,
31
  );
32
  $items['testing/feeds/files-empty.csv'] = array(
33
    'page callback' => 'feeds_tests_files_empty',
34
    'access arguments' => array('access content'),
35
    'type' => MENU_CALLBACK,
36
  );
37
  return $items;
38
}
39

    
40
/**
41
 * Implements hook_theme().
42
 */
43
function feeds_tests_theme() {
44
  return array(
45
    'feeds_tests_flickr' => array(
46
      'variables' => array('image_urls' => array()),
47
      'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
48
      'template' => 'feeds-tests-flickr',
49
    ),
50
    'feeds_tests_files' => array(
51
      'variables' => array('files' => array()),
52
      'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
53
      'template' => 'feeds-tests-files',
54
    ),
55
    'feeds_tests_files_empty' => array(
56
      'variables' => array('files' => array()),
57
      'path' => drupal_get_path('module', 'feeds_tests') . '/feeds',
58
      'template' => 'feeds-tests-files-empty',
59
    ),
60
  );
61
}
62

    
63
/**
64
 * Implements hook_node_load().
65
 */
66
function feeds_tests_node_load($nodes) {
67
  // Eventually keep track of nodes that get loaded.
68
  if (variable_get('feeds_track_node_loads', FALSE)) {
69
    $loads = variable_get('feeds_loaded_nodes', array());
70
    $loads = array_merge($loads, array_keys($nodes));
71
    variable_set('feeds_loaded_nodes', $loads);
72
  }
73
}
74

    
75
/**
76
 * Implements hook_cron_queue_alter().
77
 *
78
 * Changes runtime limit for feeds_source_import queue.
79
 *
80
 * @see FeedsFileFetcherTestCase::testRemoveFileAfterImportInBackground()
81
 */
82
function feeds_tests_cron_queue_info_alter(&$queues) {
83
  $feeds_source_import_queue_time = variable_get('feeds_tests_feeds_source_import_queue_time', FALSE);
84
  if ($feeds_source_import_queue_time && isset($queues['feeds_source_import'])) {
85
    $queues['feeds_source_import']['time'] = $feeds_source_import_queue_time;
86
  }
87
}
88

    
89
/**
90
 * Outputs flickr test feed.
91
 */
92
function feeds_tests_flickr() {
93
  $images = array(
94
    0 => "tubing.jpeg",
95
    1 => "foosball.jpeg",
96
    2 => "attersee.jpeg",
97
    3 => "hstreet.jpeg",
98
    4 => "la fayette.jpeg",
99
  );
100
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
101
  foreach ($images as &$image) {
102
    $image = file_create_url("$path/$image");
103
  }
104
  drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
105
  print theme('feeds_tests_flickr', array('image_urls' => $images));
106
}
107

    
108
/**
109
 * Outputs a CSV file pointing to files.
110
 */
111
function feeds_tests_files() {
112
  $images = array(
113
    0 => "tubing.jpeg",
114
    1 => "foosball.jpeg",
115
    2 => "attersee.jpeg",
116
    3 => "hstreet.jpeg",
117
    4 => "la fayette.jpeg",
118
  );
119
  foreach ($images as &$image) {
120
    $image = "public://images/$image";
121
  }
122
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
123
  print theme('feeds_tests_files', array('files' => $images));
124
}
125

    
126
/**
127
 * Outputs a CSV file pointing to files to download.
128
 */
129
function feeds_tests_files_remote() {
130
  $images = array(
131
    0 => 'tubing.jpeg',
132
    1 => 'foosball.jpeg',
133
    2 => 'attersee.jpeg',
134
    3 => 'hstreet.jpeg',
135
    4 => 'la fayette.jpeg',
136
  );
137
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
138
  foreach ($images as &$image) {
139
    $image = file_create_url("$path/$image");
140
  }
141
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
142
  print theme('feeds_tests_files', array('files' => $images));
143
}
144

    
145
/**
146
 * Outputs a CSV file pointing to files without alt/title.
147
 *
148
 * This is used to test if alt/title attributes are removed on a second import.
149
 */
150
function feeds_tests_files_empty_alt_title() {
151
  $images = array(
152
    0 => 'tubing.jpeg',
153
    1 => 'foosball.jpeg',
154
    2 => 'attersee.jpeg',
155
    3 => 'hstreet.jpeg',
156
    4 => 'la fayette.jpeg',
157
  );
158
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
159
  foreach ($images as &$image) {
160
    $image = file_create_url("$path/$image");
161
  }
162
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
163
  print theme('feeds_tests_files_empty', array('files' => $images));
164
}
165

    
166
/**
167
 * Outputs a CSV file pointing to no files.
168
 *
169
 * This is used to test if files are removed on a second import.
170
 */
171
function feeds_tests_files_empty() {
172
  $images = array(
173
    0 => '',
174
    1 => '',
175
    2 => '',
176
    3 => '',
177
    4 => '',
178
  );
179
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
180
  print theme('feeds_tests_files_empty', array('files' => $images));
181
}
182

    
183
/**
184
 * Implements hook_feeds_processor_targets().
185
 */
186
function feeds_tests_feeds_processor_targets($entity_type, $bundle) {
187
  $targets = array();
188

    
189
  // Tests that old keys still work.
190
  $targets['test_target_compat'] = array(
191
    'name' => t('Old style target'),
192
    'callback' => 'feeds_tests_mapper_set_target',
193
    'summary_callback' => 'feeds_tests_mapper_summary',
194
    'form_callback' => 'feeds_tests_mapper_form',
195
  );
196

    
197
  $targets['test_target'] = array(
198
    'name' => t('Test Target'),
199
    'description' => t('This is a test target.'),
200
    'callback' => 'feeds_tests_mapper_set_target',
201
    'summary_callbacks' => array('feeds_tests_mapper_summary', 'feeds_tests_mapper_summary_2'),
202
    'form_callbacks' => array('feeds_tests_mapper_form', 'feeds_tests_mapper_form_2'),
203
    'preprocess_callbacks' => array(array(new FeedsTestsPreprocess(), 'callback')),
204
  );
205

    
206
  $targets['test_unique_target'] = array(
207
    'name' => t('Test unique target'),
208
    'description' => t('This is a unique test target.'),
209
    'callback' => 'feeds_tests_mapper_set_target',
210
    'optional_unique' => TRUE,
211
    'unique_callbacks' => array('feeds_tests_mapper_unique'),
212
    'preprocess_callbacks' => array(
213
      array('FeedsTestsPreprocess', 'callback'),
214
      // Make sure that invalid callbacks are filtered.
215
      '__feeds_tests_invalid_callback',
216
    ),
217
  );
218

    
219
  return $targets;
220
}
221

    
222
/**
223
 * Implements hook_feeds_processor_targets_alter().
224
 */
225
function feeds_tests_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
226
  if (!isset($targets['test_target'])) {
227
    return;
228
  }
229

    
230
  $targets['test_target']['description'] = t('The target description was altered.');
231
}
232

    
233
/**
234
 * Preprocess callback for test_target.
235
 *
236
 * @see feeds_tests_feeds_processor_targets()
237
 */
238
function feeds_tests_preprocess_callback(array $target, array &$mapping) {
239
  $mapping['required_value'] = TRUE;
240
}
241

    
242
/**
243
 * Set target value on entity.
244
 *
245
 * @see my_module_set_target()
246
 */
247
function feeds_tests_mapper_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
248
  if (empty($mapping['required_value'])) {
249
    trigger_error('The required value was not set.', E_USER_ERROR);
250
  }
251

    
252
  $entity->body['und'][0]['value'] = serialize($mapping);
253
}
254

    
255
/**
256
 * Provides setting summary for the mapper.
257
 *
258
 * @see my_module_summary_callback()
259
 */
260
function feeds_tests_mapper_summary(array $mapping, $target, array $form, array $form_state) {
261
  $options = array(
262
    'option1' => t('Option 1'),
263
    'option2' => t('Another Option'),
264
    'option3' => t('Option for select'),
265
    'option4' => t('Another One'),
266
  );
267

    
268
  $items = array();
269
  if (!empty($mapping['checkbox']) && $mapping['checkbox']) {
270
    $items[] = t('Checkbox active.');
271
  }
272
  else {
273
    $items[] = t('Checkbox inactive.');
274
  }
275
  if (!empty($mapping['textfield'])) {
276
    $items[] = t('<strong>Textfield value</strong>: %textfield', array('%textfield' => $mapping['textfield']));
277
  }
278
  if (!empty($mapping['textarea'])) {
279
    $items[] = t('<strong>Textarea value</strong>: %textarea', array('%textarea' => $mapping['textarea']));
280
  }
281
  if (!empty($mapping['radios'])) {
282
    $items[] = t('<strong>Radios value</strong>: %radios', array('%radios' => $options[$mapping['radios']]));
283
  }
284
  if (!empty($mapping['select'])) {
285
    $items[] = t('<strong>Select value</strong>: %select', array('%select' => $options[$mapping['select']]));
286
  }
287
  $list = array(
288
    '#type' => 'ul',
289
    '#theme' => 'item_list',
290
    '#items' => $items,
291
  );
292
  return drupal_render($list);
293
}
294

    
295
/**
296
 * Provides a second summary callback.
297
 *
298
 * @see my_module_summary_callback()
299
 */
300
function feeds_tests_mapper_summary_2(array $mapping, $target, array $form, array $form_state) {
301
  $mapping += array('second_value' => '');
302
  return t('Second summary: @value', array('@value' => $mapping['second_value']));
303
}
304

    
305
/**
306
 * Provides the form with mapper settings.
307
 */
308
function feeds_tests_mapper_form(array $mapping, $target, array $form, array $form_state) {
309
  $mapping += array(
310
    'checkbox' => FALSE,
311
    'textfield' => '',
312
    'textarea' => '',
313
    'radios' => NULL,
314
    'select' => NULL,
315
  );
316
  return array(
317
    'checkbox' => array(
318
      '#type' => 'checkbox',
319
      '#title' => t('A checkbox'),
320
      '#default_value' => !empty($mapping['checkbox']),
321
    ),
322
    'textfield' => array(
323
      '#type' => 'textfield',
324
      '#title' => t('A text field'),
325
      '#default_value' => $mapping['textfield'],
326
      '#required' => TRUE,
327
    ),
328
    'textarea' => array(
329
      '#type' => 'textarea',
330
      '#title' => t('A textarea'),
331
      '#default_value' => $mapping['textarea'],
332
    ),
333
    'radios' => array(
334
      '#type' => 'radios',
335
      '#title' => t('Some radios'),
336
      '#options' => array('option1' => t('Option 1'), 'option2' => t('Another Option')),
337
      '#default_value' => $mapping['radios'],
338
    ),
339
    'select' => array(
340
      '#type' => 'select',
341
      '#title' => t('A select list'),
342
      '#options' => array('option3' => t('Option for select'), 'option4' => t('Another One')),
343
      '#default_value' => $mapping['select'],
344
    ),
345
  );
346
}
347

    
348
/**
349
 * Provides a second settings form.
350
 */
351
function feeds_tests_mapper_form_2(array $mapping, $target, array $form, array $form_state) {
352
  return array(
353
    'second_value' => array(
354
      '#type' => 'textfield',
355
      '#title' => t('The second callback value'),
356
      '#default_value' => !empty($mapping['second_value']) ? $mapping['second_value'] : '',
357
    ),
358
  );
359
}
360

    
361
/**
362
 * Callback for unique_callbacks for test_target mapper.
363
 *
364
 * @see feeds_tests_feeds_processor_targets()
365
 */
366
function feeds_tests_mapper_unique(FeedsSource $source, $entity_type, $bundle, $target, array $values) {
367
  $query = new EntityFieldQuery();
368
  $result = $query
369
    ->entityCondition('entity_type', $entity_type)
370
    ->entityCondition('bundle', $bundle)
371
    ->fieldCondition('field_alpha', 'value', $values)
372
    ->execute();
373

    
374
  if (!empty($result[$entity_type])) {
375
    return key($result[$entity_type]);
376
  }
377
}
378

    
379
/**
380
 * Implements hook_feeds_after_parse().
381
 */
382
function feeds_tests_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
383
  // Empties the list of items to import in case the test says that there are
384
  // items in there with encoding issues. These items can not be processed
385
  // during tests without having a test failure because in < PHP 5.4 that would
386
  // produce the following warning:
387
  //   htmlspecialchars(): Invalid multibyte sequence in argument
388
  // @see FeedsCSVParserTestCase::testMbstringExtensionDisabled()
389
  if (variable_get('feeds_tests_feeds_after_parse_empty_items', FALSE)) {
390
    // Remove all items. No items will be processed.
391
    $result->items = array();
392
  }
393

    
394
  if ($source->id == 'user_import') {
395
    foreach ($result->items as &$item) {
396
      if (!empty($item['roles']) && strpos($item['roles'], '|')) {
397
        // Convert roles value to multiple values.
398
        // @see FeedsCSVtoUsersTest::testRoleTargetWithoutRoleCreation()
399
        // @see FeedsCSVtoUsersTest::testRoleTargetWithRoleCreation()
400
        // @see FeedsCSVtoUsersTest::testRoleTargetWithAllowedRoles()
401
        $item['roles'] = explode('|', $item['roles']);
402
      }
403
      if (!empty($item['rids']) && strpos($item['rids'], '|')) {
404
        // Convert roles value to multiple values.
405
        // @see FeedsCSVtoUsersTest::testRoleTargetRids()
406
        $item['rids'] = explode('|', $item['rids']);
407
      }
408
    }
409
  }
410
}
411

    
412
/**
413
 * Implements hook_feeds_after_save().
414
 *
415
 * @see FeedsFileFetcherTestCase::testRemoveFileAfterImportInBackground()
416
 */
417
function feeds_tests_feeds_after_save() {
418
  $sleep = variable_get('feeds_tests_feeds_after_save_sleep', FALSE);
419
  if ($sleep) {
420
    sleep($sleep);
421
  }
422
}
423

    
424
/**
425
 * Helper class to ensure callbacks can be objects.
426
 */
427
class FeedsTestsPreprocess {
428

    
429
/**
430
 * Preprocess callback for test_target.
431
 *
432
 * @see feeds_tests_feeds_processor_targets()
433
 */
434
  public static function callback(array $target, array &$mapping) {
435
    $mapping['required_value'] = TRUE;
436
  }
437

    
438
}