Projet

Général

Profil

Paste
Télécharger (6,27 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / tests / feeds_fetcher_file.test @ 651307cd

1
<?php
2

    
3
/**
4
 * @file
5
 * File fetcher tests.
6
 */
7

    
8
/**
9
 * File fetcher test class.
10
 */
11
class FeedsFileFetcherTestCase extends FeedsWebTestCase {
12
  public static function getInfo() {
13
    return array(
14
      'name' => 'File fetcher',
15
      'description' => 'Tests for file fetcher plugin.',
16
      'group' => 'Feeds',
17
    );
18
  }
19

    
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function setUp() {
24
    parent::setUp();
25

    
26
    // Set up an importer.
27
    $this->createImporterConfiguration('Node import', 'node');
28
    // Set and configure plugins and mappings.
29
    $this->setSettings('node', NULL, array('content_type' => ''));
30
    $this->setPlugin('node', 'FeedsFileFetcher');
31
    $this->setPlugin('node', 'FeedsCSVParser');
32
    $this->addMappings('node', array(
33
      '0' => array(
34
        'source' => 'title',
35
        'target' => 'title',
36
      ),
37
    ));
38
  }
39

    
40
  /**
41
   * Test scheduling on cron.
42
   */
43
  public function testPublicFiles() {
44
    // Straight up upload is covered in other tests, focus on direct mode and
45
    // file batching here.
46
    $settings = array(
47
      'direct' => TRUE,
48
      'directory' => 'public://feeds',
49
    );
50
    $this->setSettings('node', 'FeedsFileFetcher', $settings);
51

    
52
    // Verify that invalid paths are not accepted.
53
    foreach (array('/tmp/') as $path) {
54
      $edit = array(
55
        'feeds[FeedsFileFetcher][source]' => $path,
56
      );
57
      $this->drupalPost('import/node', $edit, t('Import'));
58
      $this->assertText("The file needs to reside within the site's files directory, its path needs to start with scheme://. Available schemes:");
59
      $count = db_query("SELECT COUNT(*) FROM {feeds_source} WHERE feed_nid = 0")->fetchField();
60
      $this->assertEqual($count, 0);
61
    }
62

    
63
    // Verify batching through directories.
64
    // Copy directory of files.
65
    $dir = 'public://batchtest';
66
    $this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);
67

    
68
    // Ingest directory of files. Set limit to 5 to force processor to batch,
69
    // too.
70
    variable_set('feeds_process_limit', 5);
71
    $edit = array(
72
      'feeds[FeedsFileFetcher][source]' => $dir,
73
    );
74
    $this->drupalPost('import/node', $edit, t('Import'));
75
    $this->assertText('Created 18 nodes');
76
  }
77

    
78
  /**
79
   * Test uploading private files.
80
   */
81
  public function testPrivateFiles() {
82
    // Straight up upload is covered in other tests, focus on direct mode and
83
    // file batching here.
84
    $settings = array(
85
      'direct' => TRUE,
86
      'directory' => 'private://feeds',
87
    );
88
    $this->setSettings('node', 'FeedsFileFetcher', $settings);
89

    
90
    // Verify batching through directories.
91
    // Copy directory of files.
92
    $dir = 'private://batchtest';
93
    $this->copyDir($this->absolutePath() . '/tests/feeds/batch', $dir);
94

    
95
    // Ingest directory of files. Set limit to 5 to force processor to batch,
96
    // too.
97
    variable_set('feeds_process_limit', 5);
98
    $edit = array(
99
      'feeds[FeedsFileFetcher][source]' => $dir,
100
    );
101
    $this->drupalPost('import/node', $edit, t('Import'));
102
    $this->assertText('Created 18 nodes');
103
  }
104

    
105
  /**
106
   * Tests if files can be removed after the import has finished.
107
   */
108
  public function testRemoveFileAfterImport() {
109
    $this->setSettings('node', 'FeedsFileFetcher', array(
110
      'delete_uploaded_file' => TRUE,
111
      'directory' => 'private://feeds',
112
    ));
113

    
114
    // Import the file.
115
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/content.csv');
116
    $this->assertText('Created 2 nodes');
117

    
118
    // Assert that the file no longer exists.
119
    $this->assertFalse(file_exists('private://feeds/content.csv'), 'The imported file no longer exists.');
120

    
121
    // Assert that the file is no longer shown on the import form.
122
    $this->drupalGet('import/node');
123
    $this->assertNoText('nodes.csv');
124
  }
125

    
126
  /**
127
   * Tests if files can be removed after import when running the import in
128
   * background.
129
   */
130
  public function testRemoveFileAfterImportInBackground() {
131
    // Configure to import in background and import as often as possible.
132
    $this->setSettings('node', NULL, array(
133
      'import_period' => 0,
134
      'import_on_create' => FALSE,
135
      'process_in_background' => TRUE,
136
    ));
137
    $this->setSettings('node', 'FeedsFileFetcher', array(
138
      'delete_uploaded_file' => TRUE,
139
      'directory' => 'private://feeds',
140
    ));
141

    
142
    // Make sure that the import cannot be completed in one run.
143
    variable_set('feeds_process_limit', 5);
144

    
145
    // Set variable to enforce that only five items get imported per cron run.
146
    // @see feeds_tests_cron_queue_alter()
147
    // @see feeds_tests_feeds_after_save()
148
    variable_set('feeds_tests_feeds_source_import_queue_time', 5);
149
    variable_set('feeds_tests_feeds_after_save_sleep', 1);
150

    
151
    // Import a file with 9 nodes.
152
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/nodes.csv');
153

    
154
    // Assert that the file has been created.
155
    $this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file is created.');
156

    
157
    // Run cron and assert that five nodes have been created.
158
    $this->cronRun();
159
    $node_count = db_select('node')
160
      ->fields('node', array())
161
      ->countQuery()
162
      ->execute()
163
      ->fetchField();
164
    $this->assertEqual(5, $node_count, format_string('Five nodes have been created (actual: @count).', array(
165
      '@count' => $node_count,
166
    )));
167

    
168
    // Assert that the file to import still exists as the import hasn't finished
169
    // yet.
170
    drupal_flush_all_caches();
171
    $this->assertTrue(file_exists('private://feeds/nodes.csv'), 'The imported file still exists.');
172

    
173
    // Run cron again to import the remaining 4 nodes and assert that 9 nodes
174
    // exist in total.
175
    $this->cronRun();
176
    $node_count = db_select('node')
177
      ->fields('node', array())
178
      ->countQuery()
179
      ->execute()
180
      ->fetchField();
181
    $this->assertEqual(9, $node_count, format_string('Nine nodes have been created (actual: @count).', array(
182
      '@count' => $node_count,
183
    )));
184

    
185
    // Assert that the file to import finally has been removed now.
186
    drupal_flush_all_caches();
187
    $this->assertFalse(file_exists('private://feeds/nodes.csv'), 'The imported file no longer exists.');
188

    
189
    // Assert that running a second import does not result into errors.
190
    $this->cronRun();
191

    
192
    // Assert that the file is no longer shown on the import form.
193
    $this->drupalGet('import/node');
194
    $this->assertNoText('nodes.csv');
195
  }
196

    
197
}