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_importer_expire' AND last <> 0 AND scheduled = 0")->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 a feeds_importer_expire job now, and all last fields should be reset.
|
161
|
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_importer_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(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_importer_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(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import'")->fetchField());
|
190
|
$this->assertEqual(1, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_importer_expire' AND id = 0")->fetchField());
|
191
|
|
192
|
$this->drupalPost('admin/structure/feeds/syndication/delete', array(), t('Delete'));
|
193
|
$this->assertEqual(0, db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_importer_expire' AND id = 0")->fetchField());
|
194
|
$this->assertEqual(count($nids), db_query("SELECT COUNT(*) FROM {job_schedule} WHERE type = 'syndication' AND name = 'feeds_source_import'")->fetchField());
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Test batching on cron.
|
199
|
*/
|
200
|
function testBatching() {
|
201
|
// Set up an importer.
|
202
|
$this->createImporterConfiguration('Node import', 'node');
|
203
|
// Set and configure plugins and mappings.
|
204
|
$edit = array(
|
205
|
'content_type' => '',
|
206
|
);
|
207
|
$this->drupalPost('admin/structure/feeds/node/settings', $edit, 'Save');
|
208
|
$this->setPlugin('node', 'FeedsFileFetcher');
|
209
|
$this->setPlugin('node', 'FeedsCSVParser');
|
210
|
$mappings = array(
|
211
|
0 => array(
|
212
|
'source' => 'title',
|
213
|
'target' => 'title',
|
214
|
),
|
215
|
);
|
216
|
$this->addMappings('node', $mappings);
|
217
|
|
218
|
// Verify that there are 86 nodes total.
|
219
|
$this->importFile('node', $this->absolutePath() . '/tests/feeds/many_nodes.csv');
|
220
|
$this->assertText('Created 86 nodes');
|
221
|
|
222
|
// Run batch twice with two different process limits.
|
223
|
// 50 = FEEDS_PROCESS_LIMIT.
|
224
|
foreach (array(10, 50) as $limit) {
|
225
|
variable_set('feeds_process_limit', $limit);
|
226
|
|
227
|
db_query("UPDATE {job_schedule} SET next = 0");
|
228
|
$this->drupalPost('import/node/delete-items', array(), 'Delete');
|
229
|
$this->assertEqual(0, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField());
|
230
|
|
231
|
// Hit cron (item count / limit) times, assert correct number of articles.
|
232
|
for ($i = 0; $i < ceil(86 / $limit); $i++) {
|
233
|
$this->cronRun();
|
234
|
sleep(1);
|
235
|
if ($limit * ($i + 1) < 86) {
|
236
|
$count = $limit * ($i + 1);
|
237
|
$period = 0; // Import should be rescheduled for ASAP.
|
238
|
}
|
239
|
else {
|
240
|
$count = 86; // We've reached our total of 86.
|
241
|
$period = 1800; // Hence we should find the Source's default period.
|
242
|
}
|
243
|
$this->assertEqual($count, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField());
|
244
|
$this->assertEqual($period, db_query("SELECT period FROM {job_schedule} WHERE type = 'node' AND id = 0")->fetchField());
|
245
|
}
|
246
|
}
|
247
|
|
248
|
// Delete a couple of nodes, then hit cron again. They should not be replaced
|
249
|
// as the minimum update time is 30 minutes.
|
250
|
$nodes = db_query_range("SELECT nid FROM {node} WHERE type = 'article'", 0, 2);
|
251
|
foreach ($nodes as $node) {
|
252
|
$this->drupalPost("node/{$node->nid}/delete", array(), 'Delete');
|
253
|
}
|
254
|
$this->assertEqual(84, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField());
|
255
|
$this->cronRun();
|
256
|
$this->assertEqual(84, db_query("SELECT COUNT(*) FROM {node} WHERE type = 'article'")->fetchField());
|
257
|
}
|
258
|
}
|