Projet

Général

Profil

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

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

1
<?php
2

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

    
8
/**
9
 * Test cron scheduling.
10
 */
11
class FeedsSchedulerTestCase extends FeedsWebTestCase {
12

    
13
  public static function getInfo() {
14
    return array(
15
      'name' => 'Scheduler',
16
      'description' => 'Tests for feeds scheduler.',
17
      'group' => 'Feeds',
18
    );
19
  }
20

    
21
  /**
22
   * Test scheduling on cron.
23
   */
24
  public function testScheduling() {
25
    // Create importer configuration.
26
    $this->createImporterConfiguration();
27
    $this->addMappings('syndication',
28
      array(
29
        0 => array(
30
          'source' => 'title',
31
          'target' => 'title',
32
          'unique' => FALSE,
33
        ),
34
        1 => array(
35
          'source' => 'description',
36
          'target' => 'body',
37
        ),
38
        2 => array(
39
          'source' => 'timestamp',
40
          'target' => 'created',
41
        ),
42
        3 => array(
43
          'source' => 'url',
44
          'target' => 'url',
45
          'unique' => TRUE,
46
        ),
47
        4 => array(
48
          'source' => 'guid',
49
          'target' => 'guid',
50
          'unique' => TRUE,
51
        ),
52
      )
53
    );
54

    
55
    // Create 10 feed nodes. Turn off import on create before doing that.
56
    $edit = array(
57
      'import_on_create' => FALSE,
58
    );
59
    $this->drupalPost('admin/structure/feeds/syndication/settings', $edit, 'Save');
60
    $this->assertText('Do not import on submission');
61

    
62
    $nids = $this->createFeedNodes();
63
    // This implicitly tests the import_on_create node setting being 0.
64
    $this->assertTrue($nids[0] == 1 && $nids[1] == 2, 'Node ids sequential.');
65

    
66
    // Check whether feed got properly added to scheduler.
67
    foreach ($nids as $nid) {
68
      $this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND id = :nid AND name = 'feeds_source_import' AND last <> 0 AND scheduled = 0 AND period = 1800 AND periodic = 1", array(':nid' => $nid))->fetchField());
69
    }
70

    
71
    // Take time for comparisons.
72
    $time = time();
73
    sleep(1);
74

    
75
    // Log out and run cron, no changes.
76
    $this->drupalLogout();
77
    $this->cronRun();
78
    $count = db_query("SELECT COUNT(*) FROM {job_schedule} WHERE last > :time", array(':time' => $time))->fetchField();
79
    $this->assertEqual($count, 0, '0 feeds refreshed on cron.');
80

    
81
    // Set next time to 0 to simulate updates.
82
    // There should be 2 x job_schedule_num (= 10) feeds updated now.
83
    db_query("UPDATE {job_schedule} SET next = 0");
84
    $this->cronRun();
85
    $this->cronRun();
86

    
87
    // There should be feeds_schedule_num X 2 (= 20) feeds updated now.
88
    $schedule = array();
89
    $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(':time' => $time));
90
    foreach ($rows as $row) {
91
      $schedule[$row->id] = $row;
92
    }
93
    $this->assertEqual(count($schedule), 20, '20 feeds refreshed on cron.' . $count);
94

    
95
    // There should be 200 article nodes in the database.
96
    $count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article' AND status = 1")->fetchField();
97
    $this->assertEqual($count, 200, 'There are 200 article nodes aggregated.' . $count);
98

    
99
    // There shouldn't be any items with scheduled = 1 now, if so, this would
100
    // mean they are stuck.
101
    $count = db_query("SELECT COUNT(*) FROM {job_schedule} WHERE scheduled = 1")->fetchField();
102
    $this->assertEqual($count, 0, 'All items are unscheduled (schedule flag = 0).' . $count);
103

    
104
    // Hit cron again twice.
105
    $this->cronRun();
106
    $this->cronRun();
107

    
108
    // The import_period setting of the feed configuration is 1800, there
109
    // shouldn't be any change to the database now.
110
    $equal = TRUE;
111
    $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(':time' => $time));
112
    foreach ($rows as $row) {
113
      $equal = $equal && ($row->last == $schedule[$row->id]->last);
114
    }
115
    $this->assertTrue($equal, 'Schedule did not change.');
116

    
117
    // Log back in and set refreshing to as often as possible.
118
    $this->drupalLogin($this->admin_user);
119
    $edit = array(
120
      'import_period' => 0,
121
    );
122
    $this->drupalPost('admin/structure/feeds/syndication/settings', $edit, 'Save');
123
    $this->assertText('Periodic import: as often as possible');
124
    $this->drupalLogout();
125

    
126
    // Hit cron once, this should cause Feeds to reschedule all entries.
127
    $this->cronRun();
128
    $equal = FALSE;
129
    $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(':time' => $time));
130
    foreach ($rows as $row) {
131
      $equal = $equal && ($row->last == $schedule[$row->id]->last);
132
      $schedule[$row->id] = $row;
133
    }
134
    $this->assertFalse($equal, 'Every feed schedule time changed.');
135

    
136
    // Hit cron again, 4 times now, every item should change again.
137
    for ($i = 0; $i < 4; $i++) {
138
      $this->cronRun();
139
    }
140
    $equal = FALSE;
141
    $rows = db_query("SELECT id, last, scheduled FROM {job_schedule} WHERE last > :time", array(':time' => $time));
142
    foreach ($rows as $row) {
143
      $equal = $equal && ($row->last == $schedule[$row->id]->last);
144
    }
145
    $this->assertFalse($equal, 'Every feed schedule time changed.');
146

    
147
    // There should be 200 article nodes in the database.
148
    $count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article' AND status = 1")->fetchField();
149
    $this->assertEqual($count, 200, 'The total of 200 article nodes has not changed.');
150

    
151
    // Set expire settings, check rescheduling.
152
    $max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")->fetchField();
153
    $min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")->fetchField();
154
    $this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire'")->fetchField());
155
    $this->drupalLogin($this->admin_user);
156
    $this->setSettings('syndication', 'FeedsNodeProcessor', array('expire' => 86400));
157
    $this->drupalLogout();
158
    sleep(1);
159
    $this->cronRun();
160
    // There should be 20 feeds_source_expire jobs now, and all last fields should be reset.
161
    $this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire' AND last <> 0 AND scheduled = 0 AND period = 3600")->fetchField());
162
    $new_max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")->fetchField();
163
    $new_min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 0")->fetchField();
164
    $this->assertNotEqual($new_max_last, $max_last);
165
    $this->assertNotEqual($new_min_last, $min_last);
166
    $this->assertEqual($new_max_last, $new_min_last);
167
    $max_last = $new_max_last;
168
    $min_last = $new_min_last;
169

    
170
    // Set import settings, check rescheduling.
171
    $this->drupalLogin($this->admin_user);
172
    $this->setSettings('syndication', '', array('import_period' => 3600));
173
    $this->drupalLogout();
174
    sleep(1);
175
    $this->cronRun();
176
    $new_max_last = db_query("SELECT MAX(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 3600")->fetchField();
177
    $new_min_last = db_query("SELECT MIN(last) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period = 3600")->fetchField();
178
    $this->assertNotEqual($new_max_last, $max_last);
179
    $this->assertNotEqual($new_min_last, $min_last);
180
    $this->assertEqual($new_max_last, $new_min_last);
181
    $this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND period <> 3600")->fetchField());
182
    $this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire' AND period = 3600 AND last = :last", array(':last' => $new_min_last))->fetchField());
183

    
184
    // Delete source, delete importer, check schedule.
185
    $this->drupalLogin($this->admin_user);
186
    $nid = array_shift($nids);
187
    $this->drupalPost("node/$nid/delete", array(), t('Delete'));
188
    $this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import' AND id = :nid", array(':nid' => $nid))->fetchField());
189
    $this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire' AND id = :nid", array(':nid' => $nid))->fetchField());
190
    $this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import'")->fetchField());
191
    $this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire'")->fetchField());
192

    
193
    $this->drupalPost('admin/structure/feeds/syndication/delete', array(), t('Delete'));
194
    $this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_expire'")->fetchField());
195
    $this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import'")->fetchField());
196
  }
197

    
198
  /**
199
   * Test batching on cron.
200
   */
201
  function testBatching() {
202
    // Set up an importer.
203
    $this->createImporterConfiguration('Node import', 'node');
204
    // Set and configure plugins and mappings.
205
    $edit = array(
206
      'content_type' => '',
207
    );
208
    $this->drupalPost('admin/structure/feeds/node/settings', $edit, 'Save');
209
    $this->setPlugin('node', 'FeedsFileFetcher');
210
    $this->setPlugin('node', 'FeedsCSVParser');
211
    $mappings = array(
212
      0 => array(
213
        'source' => 'title',
214
        'target' => 'title',
215
      ),
216
    );
217
    $this->addMappings('node', $mappings);
218

    
219
    // Verify that there are 86 nodes total.
220
    $this->importFile('node', $this->absolutePath() . '/tests/feeds/many_nodes.csv');
221
    $this->assertText('Created 86 nodes');
222

    
223
    // Run batch twice with two different process limits.
224
    // 50 = FEEDS_PROCESS_LIMIT.
225
    foreach (array(10, 50) as $limit) {
226
      variable_set('feeds_process_limit', $limit);
227

    
228
      db_query("UPDATE {job_schedule} SET next = 0");
229
      $this->drupalPost('import/node/delete-items', array(), 'Delete');
230
      $node_count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField();
231
      $this->assertEqual(0, $node_count);
232

    
233
      // Hit cron for importing, until we have all items.
234
      while ($node_count < 86) {
235
        $this->cronRun();
236
        $node_count = db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField();
237
      }
238
      $this->assertEqual(86, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField(), 'Number of nodes is correct after batched importing via cron.');
239
      // Import should be rescheduled for 1800 seconds.
240
      $this->assertEqual(1800, db_query("SELECT period FROM {job_schedule} WHERE type = 'node' AND id = 0")->fetchField());
241
    }
242

    
243
    // Delete a couple of nodes, then hit cron again. They should not be replaced
244
    // as the minimum update time is 30 minutes.
245
    $nodes = db_query_range("SELECT nid FROM {node} WHERE type = 'article'", 0, 2);
246
    foreach ($nodes as $node) {
247
      $this->drupalPost("node/{$node->nid}/delete", array(), 'Delete');
248
    }
249
    $this->assertEqual(84, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField());
250
    $this->cronRun();
251
    $this->assertEqual(84, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField());
252
  }
253
}