Projet

Général

Profil

Paste
Télécharger (11 ko) Statistiques
| Branche: | Révision:

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

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
 * Outputs flickr test feed.
65
 */
66
function feeds_tests_flickr() {
67
  $images = array(
68
    0 => "tubing.jpeg",
69
    1 => "foosball.jpeg",
70
    2 => "attersee.jpeg",
71
    3 => "hstreet.jpeg",
72
    4 => "la fayette.jpeg",
73
  );
74
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
75
  foreach ($images as &$image) {
76
    $image = file_create_url("$path/$image");
77
  }
78
  drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
79
  print theme('feeds_tests_flickr', array('image_urls' => $images));
80
}
81

    
82
/**
83
 * Outputs a CSV file pointing to files.
84
 */
85
function feeds_tests_files() {
86
  $images = array(
87
    0 => "tubing.jpeg",
88
    1 => "foosball.jpeg",
89
    2 => "attersee.jpeg",
90
    3 => "hstreet.jpeg",
91
    4 => "la fayette.jpeg",
92
  );
93
  foreach ($images as &$image) {
94
    $image = "public://images/$image";
95
  }
96
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
97
  print theme('feeds_tests_files', array('files' => $images));
98
}
99

    
100
/**
101
 * Outputs a CSV file pointing to files to download.
102
 */
103
function feeds_tests_files_remote() {
104
  $images = array(
105
    0 => 'tubing.jpeg',
106
    1 => 'foosball.jpeg',
107
    2 => 'attersee.jpeg',
108
    3 => 'hstreet.jpeg',
109
    4 => 'la fayette.jpeg',
110
  );
111
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
112
  foreach ($images as &$image) {
113
    $image = file_create_url("$path/$image");
114
  }
115
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
116
  print theme('feeds_tests_files', array('files' => $images));
117
}
118

    
119
/**
120
 * Outputs a CSV file pointing to files without alt/title.
121
 *
122
 * This is used to test if alt/title attributes are removed on a second import.
123
 */
124
function feeds_tests_files_empty_alt_title() {
125
  $images = array(
126
    0 => 'tubing.jpeg',
127
    1 => 'foosball.jpeg',
128
    2 => 'attersee.jpeg',
129
    3 => 'hstreet.jpeg',
130
    4 => 'la fayette.jpeg',
131
  );
132
  $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets';
133
  foreach ($images as &$image) {
134
    $image = file_create_url("$path/$image");
135
  }
136
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
137
  print theme('feeds_tests_files_empty', array('files' => $images));
138
}
139

    
140
/**
141
 * Outputs a CSV file pointing to no files.
142
 *
143
 * This is used to test if files are removed on a second import.
144
 */
145
function feeds_tests_files_empty() {
146
  $images = array(
147
    0 => '',
148
    1 => '',
149
    2 => '',
150
    3 => '',
151
    4 => '',
152
  );
153
  drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
154
  print theme('feeds_tests_files_empty', array('files' => $images));
155
}
156

    
157
/**
158
 * Implements hook_feeds_processor_targets().
159
 */
160
function feeds_tests_feeds_processor_targets($entity_type, $bundle) {
161
  $targets = array();
162

    
163
  // Tests that old keys still work.
164
  $targets['test_target_compat'] = array(
165
    'name' => t('Old style target'),
166
    'callback' => 'feeds_tests_mapper_set_target',
167
    'summary_callback' => 'feeds_tests_mapper_summary',
168
    'form_callback' => 'feeds_tests_mapper_form',
169
  );
170

    
171
  $targets['test_target'] = array(
172
    'name' => t('Test Target'),
173
    'description' => t('This is a test target.'),
174
    'callback' => 'feeds_tests_mapper_set_target',
175
    'summary_callbacks' => array('feeds_tests_mapper_summary', 'feeds_tests_mapper_summary_2'),
176
    'form_callbacks' => array('feeds_tests_mapper_form', 'feeds_tests_mapper_form_2'),
177
    'preprocess_callbacks' => array(array(new FeedsTestsPreprocess(), 'callback')),
178
  );
179

    
180
  $targets['test_unique_target'] = array(
181
    'name' => t('Test unique target'),
182
    'description' => t('This is a unique test target.'),
183
    'callback' => 'feeds_tests_mapper_set_target',
184
    'optional_unique' => TRUE,
185
    'unique_callbacks' => array('feeds_tests_mapper_unique'),
186
    'preprocess_callbacks' => array(
187
      array('FeedsTestsPreprocess', 'callback'),
188
      // Make sure that invalid callbacks are filtered.
189
      '__feeds_tests_invalid_callback',
190
    ),
191
  );
192

    
193
  return $targets;
194
}
195

    
196
/**
197
 * Implements hook_feeds_processor_targets_alter().
198
 */
199
function feeds_tests_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
200
  if (!isset($targets['test_target'])) {
201
    return;
202
  }
203

    
204
  $targets['test_target']['description'] = t('The target description was altered.');
205
}
206

    
207
/**
208
 * Preprocess callback for test_target.
209
 *
210
 * @see feeds_tests_feeds_processor_targets()
211
 */
212
function feeds_tests_preprocess_callback(array $target, array &$mapping) {
213
  $mapping['required_value'] = TRUE;
214
}
215

    
216
/**
217
 * Set target value on entity.
218
 *
219
 * @see my_module_set_target()
220
 */
221
function feeds_tests_mapper_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
222
  if (empty($mapping['required_value'])) {
223
    trigger_error('The required value was not set.', E_USER_ERROR);
224
  }
225

    
226
  $entity->body['und'][0]['value'] = serialize($mapping);
227
}
228

    
229
/**
230
 * Provides setting summary for the mapper.
231
 *
232
 * @see my_module_summary_callback()
233
 */
234
function feeds_tests_mapper_summary(array $mapping, $target, array $form, array $form_state) {
235
  $options = array(
236
    'option1' => t('Option 1'),
237
    'option2' => t('Another Option'),
238
    'option3' => t('Option for select'),
239
    'option4' => t('Another One'),
240
  );
241

    
242
  $items = array();
243
  if (!empty($mapping['checkbox']) && $mapping['checkbox']) {
244
    $items[] = t('Checkbox active.');
245
  }
246
  else {
247
    $items[] = t('Checkbox inactive.');
248
  }
249
  if (!empty($mapping['textfield'])) {
250
    $items[] = t('<strong>Textfield value</strong>: %textfield', array('%textfield' => $mapping['textfield']));
251
  }
252
  if (!empty($mapping['textarea'])) {
253
    $items[] = t('<strong>Textarea value</strong>: %textarea', array('%textarea' => $mapping['textarea']));
254
  }
255
  if (!empty($mapping['radios'])) {
256
    $items[] = t('<strong>Radios value</strong>: %radios', array('%radios' => $options[$mapping['radios']]));
257
  }
258
  if (!empty($mapping['select'])) {
259
    $items[] = t('<strong>Select value</strong>: %select', array('%select' => $options[$mapping['select']]));
260
  }
261
  $list = array(
262
    '#type' => 'ul',
263
    '#theme' => 'item_list',
264
    '#items' => $items,
265
  );
266
  return drupal_render($list);
267
}
268

    
269
/**
270
 * Provides a second summary callback.
271
 *
272
 * @see my_module_summary_callback()
273
 */
274
function feeds_tests_mapper_summary_2(array $mapping, $target, array $form, array $form_state) {
275
  $mapping += array('second_value' => '');
276
  return t('Second summary: @value', array('@value' => $mapping['second_value']));
277
}
278

    
279
/**
280
 * Provides the form with mapper settings.
281
 */
282
function feeds_tests_mapper_form(array $mapping, $target, array $form, array $form_state) {
283
  $mapping += array(
284
    'checkbox' => FALSE,
285
    'textfield' => '',
286
    'textarea' => '',
287
    'radios' => NULL,
288
    'select' => NULL,
289
  );
290
  return array(
291
    'checkbox' => array(
292
      '#type' => 'checkbox',
293
      '#title' => t('A checkbox'),
294
      '#default_value' => !empty($mapping['checkbox']),
295
    ),
296
    'textfield' => array(
297
      '#type' => 'textfield',
298
      '#title' => t('A text field'),
299
      '#default_value' => $mapping['textfield'],
300
      '#required' => TRUE,
301
    ),
302
    'textarea' => array(
303
      '#type' => 'textarea',
304
      '#title' => t('A textarea'),
305
      '#default_value' => $mapping['textarea'],
306
    ),
307
    'radios' => array(
308
      '#type' => 'radios',
309
      '#title' => t('Some radios'),
310
      '#options' => array('option1' => t('Option 1'), 'option2' => t('Another Option')),
311
      '#default_value' => $mapping['radios'],
312
    ),
313
    'select' => array(
314
      '#type' => 'select',
315
      '#title' => t('A select list'),
316
      '#options' => array('option3' => t('Option for select'), 'option4' => t('Another One')),
317
      '#default_value' => $mapping['select'],
318
    ),
319
  );
320
}
321

    
322
/**
323
 * Provides a second settings form.
324
 */
325
function feeds_tests_mapper_form_2(array $mapping, $target, array $form, array $form_state) {
326
  return array(
327
    'second_value' => array(
328
      '#type' => 'textfield',
329
      '#title' => t('The second callback value'),
330
      '#default_value' => !empty($mapping['second_value']) ? $mapping['second_value'] : '',
331
    ),
332
  );
333
}
334

    
335
/**
336
 * Callback for unique_callbacks for test_target mapper.
337
 *
338
 * @see feeds_tests_feeds_processor_targets()
339
 */
340
function feeds_tests_mapper_unique(FeedsSource $source, $entity_type, $bundle, $target, array $values) {
341
  $query = new EntityFieldQuery();
342
  $result = $query
343
    ->entityCondition('entity_type', $entity_type)
344
    ->entityCondition('bundle', $bundle)
345
    ->fieldCondition('field_alpha', 'value', $values)
346
    ->execute();
347

    
348
  if (!empty($result[$entity_type])) {
349
    return key($result[$entity_type]);
350
  }
351
}
352

    
353
/**
354
 * Implements hook_feeds_after_parse().
355
 *
356
 * Empties the list of items to import in case the test says that there are
357
 * items in there with encoding issues. These items can not be processed during
358
 * tests without having a test failure because in < PHP 5.4 that would produce
359
 * the following warning:
360
 *   htmlspecialchars(): Invalid multibyte sequence in argument
361
 *
362
 * @see FeedsCSVParserTestCase::testMbstringExtensionDisabled()
363
 */
364
function feeds_tests_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
365
  if (variable_get('feeds_tests_feeds_after_parse_empty_items', FALSE)) {
366
    // Remove all items. No items will be processed.
367
    $result->items = array();
368
  }
369
}
370

    
371
/**
372
 * Helper class to ensure callbacks can be objects.
373
 */
374
class FeedsTestsPreprocess {
375

    
376
/**
377
 * Preprocess callback for test_target.
378
 *
379
 * @see feeds_tests_feeds_processor_targets()
380
 */
381
  public static function callback(array $target, array &$mapping) {
382
    $mapping['required_value'] = TRUE;
383
  }
384

    
385
}