Projet

Général

Profil

Paste
Télécharger (13,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_mapper_file.test @ 41cc1b08

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsMapperFileTestCase.
6
 */
7

    
8
/**
9
 * Test case for Filefield mapper mappers/filefield.inc.
10
 */
11
class FeedsMapperFileTestCase extends FeedsMapperTestCase {
12

    
13
  public static function getInfo() {
14
    return array(
15
      'name' => 'Mapper: File field',
16
      'description' => 'Test Feeds Mapper support for file fields. <strong>Requires SimplePie library</strong>.',
17
      'group' => 'Feeds',
18
    );
19
  }
20

    
21
  public function setUp() {
22
    parent::setUp(array('dblog'));
23
  }
24

    
25
  /**
26
   * Basic test loading a single entry CSV file.
27
   */
28
  public function test() {
29
    // If this is unset (or FALSE) http_request.inc will use curl, and will
30
    // generate a 404 for this feel url provided by feeds_tests. However, if
31
    // feeds_tests was enabled in your site before running the test, it will
32
    // work fine. Since it is truly screwy, lets just force it to use
33
    // drupal_http_request for this test case.
34
    variable_set('feeds_never_use_curl', TRUE);
35

    
36
    // Only download simplepie if the plugin doesn't already exist somewhere.
37
    // People running tests locally might have it.
38
    if (!feeds_simplepie_exists()) {
39
      $this->downloadExtractSimplePie('1.3');
40
      $this->assertTrue(feeds_simplepie_exists());
41
      // Reset all the caches!
42
      $this->resetAll();
43
    }
44
    $typename = $this->createContentType(array(), array(
45
      'files' => array(
46
        'type' => 'file',
47
        'instance_settings' => array(
48
          'instance[settings][file_extensions]' => 'png, gif, jpg, jpeg',
49
        ),
50
      ),
51
    ));
52

    
53
    // 1) Test mapping remote resources to file field.
54

    
55
    // Create importer configuration.
56
    $this->createImporterConfiguration();
57
    $this->setPlugin('syndication', 'FeedsSimplePieParser');
58
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('bundle' => $typename));
59
    $this->addMappings('syndication', array(
60
      0 => array(
61
        'source' => 'title',
62
        'target' => 'title',
63
      ),
64
      1 => array(
65
        'source' => 'timestamp',
66
        'target' => 'created',
67
      ),
68
      2 => array(
69
        'source' => 'enclosures',
70
        'target' => 'field_files:uri',
71
      ),
72
    ));
73
    $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/testing/feeds/flickr.xml');
74
    $this->assertText('Created 5 nodes');
75

    
76
    $files = $this->listTestFiles();
77
    $entities = db_select('feeds_item')
78
      ->fields('feeds_item', array('entity_id'))
79
      ->condition('id', 'syndication')
80
      ->execute();
81
    foreach ($entities as $entity) {
82
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
83
      $this->assertText(str_replace(' ', '_', array_shift($files)));
84
    }
85

    
86
    // 2) Test mapping local resources to file field.
87

    
88
    // Copy directory of files, CSV file expects them in public://images, point
89
    // file field to a 'resources' directory. Feeds should copy files from
90
    // images/ to resources/ on import.
91
    $this->copyDir($this->absolutePath() . '/tests/feeds/assets', 'public://images');
92
    $edit = array(
93
      'instance[settings][file_directory]' => 'resources',
94
    );
95
    $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
96

    
97
    // Create a CSV importer configuration.
98
    $this->createImporterConfiguration('Node import from CSV', 'node');
99
    $this->setPlugin('node', 'FeedsCSVParser');
100
    $this->setSettings('node', 'FeedsNodeProcessor', array('bundle' => $typename));
101
    $this->setSettings('node', NULL, array('content_type' => ''));
102
    $this->addMappings('node', array(
103
      0 => array(
104
        'source' => 'title',
105
        'target' => 'title',
106
      ),
107
      1 => array(
108
        'source' => 'file',
109
        'target' => 'field_files:uri',
110
      ),
111
    ));
112

    
113
    // Import.
114
    $edit = array(
115
      'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files.csv', array('absolute' => TRUE)),
116
    );
117
    $this->drupalPost('import/node', $edit, 'Import');
118
    $this->assertText('Created 5 nodes');
119

    
120
    // Assert: files should be in resources/.
121
    $files = $this->listTestFiles();
122
    $entities = db_select('feeds_item')
123
      ->fields('feeds_item', array('entity_id'))
124
      ->condition('id', 'node')
125
      ->execute();
126

    
127
    foreach ($entities as $entity) {
128
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
129
      $this->assertRaw('resources/' . rawurlencode(array_shift($files)));
130
    }
131

    
132
    // 3) Test mapping of local resources, this time leave files in place.
133
    $this->drupalPost('import/node/delete-items', array(), 'Delete');
134
    // Setting the fields file directory to images will make copying files
135
    // obsolete.
136
    $edit = array(
137
      'instance[settings][file_directory]' => 'images',
138
    );
139
    $this->drupalPost('admin/structure/types/manage/' . $typename . '/fields/field_files', $edit, t('Save settings'));
140
    $edit = array(
141
      'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv',
142
    );
143
    $this->drupalPost('import/node', $edit, 'Import');
144
    $this->assertText('Created 5 nodes');
145

    
146
    // Assert: files should be in images/ now.
147
    $files = $this->listTestFiles();
148
    $entities = db_select('feeds_item')
149
      ->fields('feeds_item', array('entity_id'))
150
      ->condition('id', 'node')
151
      ->execute();
152

    
153
    foreach ($entities as $entity) {
154
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
155
      $this->assertRaw('images/' . rawurlencode(array_shift($files)));
156
    }
157

    
158
    // Deleting all imported items will delete the files from the images/ dir.
159
    $this->drupalPost('import/node/delete-items', array(), 'Delete');
160
    foreach ($this->listTestFiles() as $file) {
161
      $this->assertFalse(is_file("public://images/$file"));
162
    }
163
  }
164

    
165
  /**
166
   * Tests mapping to an image field.
167
   */
168
  public function testImages() {
169
    variable_set('feeds_never_use_curl', TRUE);
170

    
171
    $typename = $this->createContentType(array(), array('images' => 'image'));
172

    
173
    // Enable title and alt mapping.
174
    $edit = array(
175
      'instance[settings][alt_field]' => 1,
176
      'instance[settings][title_field]' => 1,
177
    );
178
    $this->drupalPost("admin/structure/types/manage/$typename/fields/field_images", $edit, t('Save settings'));
179

    
180
    // Create a CSV importer configuration.
181
    $this->createImporterConfiguration('Node import from CSV', 'image_test');
182
    $this->setPlugin('image_test', 'FeedsCSVParser');
183
    $this->setSettings('image_test', 'FeedsNodeProcessor', array('bundle' => $typename));
184
    $this->setSettings('image_test', NULL, array('content_type' => ''));
185
    $this->addMappings('image_test', array(
186
      0 => array(
187
        'source' => 'title',
188
        'target' => 'title',
189
      ),
190
      1 => array(
191
        'source' => 'file',
192
        'target' => 'field_images:uri',
193
      ),
194
      2 => array(
195
        'source' => 'title2',
196
        'target' => 'field_images:title',
197
      ),
198
      3 => array(
199
        'source' => 'alt',
200
        'target' => 'field_images:alt',
201
      ),
202
    ));
203

    
204
    // Import.
205
    $edit = array(
206
      'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-remote.csv', array('absolute' => TRUE)),
207
    );
208
    $this->drupalPost('import/image_test', $edit, 'Import');
209
    $this->assertText('Created 5 nodes');
210

    
211
    // Assert files exist.
212
    $files = $this->listTestFiles();
213
    $entities = db_select('feeds_item')
214
      ->fields('feeds_item', array('entity_id'))
215
      ->condition('id', 'image_test')
216
      ->execute();
217

    
218
    foreach ($entities as $i => $entity) {
219
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
220
      $this->assertRaw(str_replace(' ', '_', array_shift($files)));
221
      $this->assertRaw("Alt text $i");
222
      $this->assertRaw("Title text $i");
223
    }
224
  }
225

    
226
  public function testInvalidFileExtension() {
227
    variable_set('feeds_never_use_curl', TRUE);
228

    
229
    $typename = $this->createContentType(array(), array(
230
      'files' => array(
231
        'type' => 'file',
232
        'instance_settings' => array(
233
          'instance[settings][file_extensions]' => 'txt',
234
        ),
235
      ),
236
    ));
237

    
238
    // Create a CSV importer configuration.
239
    $this->createImporterConfiguration('Node import from CSV', 'invalid_extension');
240
    $this->setPlugin('invalid_extension', 'FeedsCSVParser');
241
    $this->setSettings('invalid_extension', 'FeedsNodeProcessor', array('bundle' => $typename));
242
    $this->setSettings('invalid_extension', NULL, array('content_type' => ''));
243
    $this->addMappings('invalid_extension', array(
244
      0 => array(
245
        'source' => 'title',
246
        'target' => 'title',
247
      ),
248
      1 => array(
249
        'source' => 'file',
250
        'target' => 'field_files:uri',
251
      ),
252
    ));
253

    
254
    // Import.
255
    $edit = array(
256
      'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-remote.csv', array('absolute' => TRUE)),
257
    );
258
    $this->drupalPost('import/invalid_extension', $edit, 'Import');
259
    $this->assertText('Created 5 nodes');
260

    
261
    foreach (range(1, 5) as $nid) {
262
      $node = node_load($nid);
263
      $this->assertTrue(empty($node->field_files));
264
    }
265

    
266
    foreach ($this->listTestFiles() as $filename) {
267
      $message = t('The file @file has an invalid extension.', array('@file' => $filename));
268
      $this->assertTrue(db_query("SELECT 1 FROM {watchdog} WHERE message = :message", array(':message' => $message))->fetchField());
269
    }
270

    
271
    // Test that query string and fragments are removed.
272
    $enclosure = new FeedsEnclosure('http://example.com/image.jpg?thing=stuff', 'text/plain');
273
    $this->assertEqual($enclosure->getLocalValue(), 'image.jpg');
274
    $enclosure = new FeedsEnclosure('http://example.com/image.jpg#stuff', 'text/plain');
275
    $this->assertEqual($enclosure->getLocalValue(), 'image.jpg');
276
    $enclosure = new FeedsEnclosure('http://example.com/image.JPG?thing=stuff#stuff', 'text/plain');
277
    $this->assertEqual($enclosure->getLocalValue(), 'image.JPG');
278
  }
279

    
280
  /**
281
   * Tests if values are cleared out when an empty value or no value
282
   * is provided.
283
   */
284
  public function testClearOutValues() {
285
    variable_set('feeds_never_use_curl', TRUE);
286

    
287
    $this->createContentType(array(), array('files' => 'file'));
288
    $typename = $this->createContentType(array(), array(
289
      'images' => 'image',
290
    ));
291

    
292
    // Enable title and alt mapping.
293
    $edit = array(
294
      'instance[settings][alt_field]' => 1,
295
      'instance[settings][title_field]' => 1,
296
    );
297
    $this->drupalPost("admin/structure/types/manage/$typename/fields/field_images", $edit, t('Save settings'));
298

    
299
    // Create and configure importer.
300
    $this->createImporterConfiguration('Content CSV', 'csv');
301
    $this->setSettings('csv', NULL, array(
302
      'content_type' => '',
303
      'import_period' => FEEDS_SCHEDULE_NEVER,
304
    ));
305
    $this->setPlugin('csv', 'FeedsCSVParser');
306
    $this->setSettings('csv', 'FeedsNodeProcessor', array(
307
      'bundle' => $typename,
308
      'update_existing' => 1,
309
    ));
310
    $this->addMappings('csv', array(
311
      0 => array(
312
        'source' => 'guid',
313
        'target' => 'guid',
314
        'unique' => TRUE,
315
      ),
316
      1 => array(
317
        'source' => 'title',
318
        'target' => 'title',
319
      ),
320
      2 => array(
321
        'source' => 'file',
322
        'target' => 'field_images:uri',
323
      ),
324
      3 => array(
325
        'source' => 'title2',
326
        'target' => 'field_images:title',
327
      ),
328
      4 => array(
329
        'source' => 'alt',
330
        'target' => 'field_images:alt',
331
      ),
332
    ));
333

    
334
    // Import.
335
    $edit = array(
336
      'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-remote.csv', array('absolute' => TRUE)),
337
    );
338
    $this->drupalPost('import/csv', $edit, 'Import');
339
    $this->assertText('Created 5 nodes');
340

    
341
    // Assert files exist.
342
    $files = $this->listTestFiles();
343
    foreach ($files as $file) {
344
      $file_path = drupal_realpath('public://') . '/' . str_replace(' ', '_', $file);
345
      $this->assertTrue(file_exists($file_path), format_string('The file %file exists.', array(
346
        '%file' => $file_path,
347
      )));
348
    }
349

    
350
    // Assert files exists with the expected alt/title on node edit form.
351
    $entities = db_select('feeds_item')
352
      ->fields('feeds_item', array('entity_id'))
353
      ->condition('id', 'csv')
354
      ->execute()
355
      ->fetchAll();
356

    
357
    foreach ($entities as $i => $entity) {
358
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
359
      $this->assertRaw(str_replace(' ', '_', array_shift($files)));
360
      $this->assertRaw("Alt text $i");
361
      $this->assertRaw("Title text $i");
362
    }
363

    
364
    // Import CSV with empty alt/title fields and check if these are removed.
365
    $edit = array(
366
      'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-empty-alt-title.csv', array('absolute' => TRUE)),
367
    );
368
    $this->drupalPost('import/csv', $edit, 'Import');
369
    $this->assertText('Updated 5 nodes');
370

    
371
    $files = $this->listTestFiles();
372
    foreach ($entities as $i => $entity) {
373
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
374
      $this->assertRaw(str_replace(' ', '_', array_shift($files)));
375
      $this->assertNoRaw("Alt text $i");
376
      $this->assertNoRaw("Title text $i");
377
    }
378

    
379
    // Import CSV with empty file fields and check if all files are removed.
380
    $edit = array(
381
      'feeds[FeedsHTTPFetcher][source]' => url('testing/feeds/files-empty.csv', array('absolute' => TRUE)),
382
    );
383
    $this->drupalPost('import/csv', $edit, 'Import');
384
    $this->assertText('Updated 5 nodes');
385

    
386
    // Assert files are removed.
387
    $files = $this->listTestFiles();
388
    foreach ($files as $file) {
389
      $file_path = drupal_realpath('public://') . '/' . str_replace(' ', '_', $file);
390
      $this->assertFalse(file_exists($file_path), format_string('The file %file no longer exists.', array(
391
        '%file' => $file_path,
392
      )));
393
    }
394
    // Check if the files are removed from the node edit form as well.
395
    foreach ($entities as $i => $entity) {
396
      $this->drupalGet('node/' . $entity->entity_id . '/edit');
397
      $this->assertNoRaw(str_replace(' ', '_', array_shift($files)));
398
    }
399
  }
400

    
401
  /**
402
   * Lists test files.
403
   */
404
  protected function listTestFiles() {
405
    return array(
406
      'tubing.jpeg',
407
      'foosball.jpeg',
408
      'attersee.jpeg',
409
      'hstreet.jpeg',
410
      'la fayette.jpeg',
411
    );
412
  }
413

    
414
}