1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
<?php
// $Id$
class AggregatorTestCase extends DrupalWebTestCase {
private static $prefix = 'simpletest_aggregator_';
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp('aggregator');
$web_user = $this->drupalCreateUser(array('administer news feeds', 'access news feeds'));
$this->drupalLogin($web_user);
}
/**
* Create an aggregator feed (simulate form submission on admin/content/aggregator/add/feed).
*
* @return $feed Full feed object if possible.
*/
function createFeed() {
$edit = $this->getFeedEditArray();
$this->drupalPost('admin/content/aggregator/add/feed', $edit, t('Save'));
$this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title'])));
$feed = db_fetch_object(db_query("SELECT * FROM {aggregator_feed} WHERE title = '%s' AND url='%s'", $edit['title'], $edit['url']));
$this->assertTrue(!empty($feed), t('The feed found in database.'));
return $feed;
}
/**
* Delete an aggregator feed.
*
* @param object $feed Feed object representing the feed.
*/
function deleteFeed($feed) {
$this->drupalPost('admin/content/aggregator/edit/feed/' . $feed->fid, array(), t('Delete'));
$this->assertRaw(t('The feed %title has been deleted.', array('%title' => $feed->title)), t('Feed deleted successfully.'));
}
/**
* Return a randomly generated feed edit array.
*
* @return array Feed array.
*/
function getFeedEditArray() {
$feed_name = $this->randomName(10, self::$prefix);
$feed_url = url(NULL, array('absolute' => TRUE)) . 'rss.xml?feed=' . $feed_name;
$edit = array(
'title' => $feed_name,
'url' => $feed_url,
'refresh' => '900',
);
return $edit;
}
/**
* Update feed items (simulate click to admin/content/aggregator/update/$fid).
*
* @param object $feed Feed object representing the feed.
*/
function updateFeedItems(&$feed) {
// First, let's ensure we can get to the rss xml.
$this->drupalGet('rss.xml');
$this->assertResponse(200, t('rss.xml is reachable.'));
// Our tests are based off of rss.xml, so let's find out how many elements should be related.
$feed_count = db_result(db_query_range(db_rewrite_sql('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1'), 0, variable_get('feed_default_items', 10)));
$feed_count = $feed_count > 10 ? 10 : $feed_count;
// Refresh the feed (simulated link click).
$this->drupalGet('admin/content/aggregator/update/' . $feed->fid);
// Ensure we have the right number of items.
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $feed->fid);
$items = array();
$feed->items = array();
while ($item = db_fetch_object($result)) {
$feed->items[] = $item->iid;
}
$feed->item_count = count($feed->items);
$this->assertEqual($feed_count, $feed->item_count, t('Total items in feed equal to the total items in database (!val1 != !val2)', array('!val1' => $feed_count, '!val2' => $feed->item_count)));
}
/**
* Confirm item removal from a feed.
*
* @param object $feed Feed object representing the feed.
*/
function removeFeedItems($feed) {
$this->drupalPost('admin/content/aggregator/remove/' . $feed->fid, array(), t('Remove items'));
$this->assertRaw(t('The news items from %title have been removed.', array('%title' => $feed->title)), t('Feed items removed.'));
}
/**
* Pull feed categories from aggregator_category_feed table.
*
* @param object $feed Feed object representing the feed.
*/
function getFeedCategories($feed) {
// add the categories to the feed so we can use them
$result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $feed->fid);
while ($category = db_fetch_object($result)) {
$feed->categories[] = $category->cid;
}
}
/**
* Check if the feed name and url is unique.
*
* @param string $feed_name Feed name to check.
* @param string $feed_url Feed url to check.
* @return boolean Feed is unique.
*/
function uniqueFeed($feed_name, $feed_url) {
$result = db_result(db_query("SELECT count(*) FROM {aggregator_feed} WHERE title = '%s' AND url='%s'", $feed_name, $feed_url));
return (1 == $result);
}
}
class AddFeedTestCase extends AggregatorTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Add feed functionality'),
'description' => t('Add feed test.'),
'group' => t('Aggregator')
);
}
/**
* Create a feed, ensure that it is unique, check the source, and delete the feed.
*/
function testAddFeed() {
$feed = $this->createFeed();
// Check feed data.
$this->assertEqual($this->getUrl(), url('admin/content/aggregator/add/feed', array('absolute' => TRUE)), t('Directed to correct url.'));
$this->assertTrue($this->uniqueFeed($feed->title, $feed->url), t('The feed is unique.'));
// Check feed source.
$this->drupalGet('aggregator/sources/' . $feed->fid);
$this->assertResponse(200, t('Feed source exists.'));
$this->assertText($feed->title, t('Page title'));
// Delete feed.
$this->deleteFeed($feed);
}
}
class UpdateFeedTestCase extends AggregatorTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Update feed functionality'),
'description' => t('Update feed test.'),
'group' => t('Aggregator')
);
}
/**
* Create a feed and attempt to update it.
*/
function testUpdateFeed() {
$feed = $this->createFeed();
// Get new feed data array and modify newly created feed.
$edit = $this->getFeedEditArray();
$edit['refresh'] = 1800; // Change refresh value.
$this->drupalPost('admin/content/aggregator/edit/feed/' . $feed->fid, $edit, t('Save'));
$this->assertRaw(t('The feed %name has been updated.', array('%name' => $edit['title'])), t('The feed %name has been updated.', array('%name' => $edit['title'])));
// Check feed data.
$this->assertEqual($this->getUrl(), url('admin/content/aggregator/', array('absolute' => TRUE)));
$this->assertTrue($this->uniqueFeed($edit['title'], $edit['url']), t('The feed is unique.'));
// Check feed source.
$this->drupalGet('aggregator/sources/' . $feed->fid);
$this->assertResponse(200, t('Feed source exists.'));
$this->assertText($edit['title'], t('Page title'));
// Delete feed.
$feed->title = $edit['title']; // Set correct title so deleteFeed() will work.
$this->deleteFeed($feed);
}
}
class RemoveFeedTestCase extends AggregatorTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Remove feed functionality'),
'description' => t('Remove feed test.'),
'group' => t('Aggregator')
);
}
/**
* Remove a feed and ensure that all it services are removed.
*/
function testRemoveFeed() {
$feed = $this->createFeed();
// Delete feed.
$this->deleteFeed($feed);
// Check feed source.
$this->drupalGet('aggregator/sources/' . $feed->fid);
$this->assertResponse(404, t('Deleted feed source does not exists.'));
// Check database for feed.
$result = db_result(db_query("SELECT count(*) FROM {aggregator_feed} WHERE title = '%s' AND url='%s'", $feed->title, $feed->url));
$this->assertFalse($result, t('Feed not found in database'));
}
}
class UpdateFeedItemTestCase extends AggregatorTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Update feed item functionality'),
'description' => t('Update feed items from a feed.'),
'group' => t('Aggregator')
);
}
/**
* Test running "update items" from the 'admin/content/aggregator' page.
*/
function testUpdateFeedItem() {
// Create a feed and test updating feed items if possible.
$feed = $this->createFeed();
if (!empty($feed)) {
$this->updateFeedItems($feed);
$this->removeFeedItems($feed);
}
// Delete feed.
$this->deleteFeed($feed);
}
}
class RemoveFeedItemTestCase extends AggregatorTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Remove feed item functionality'),
'description' => t('Remove feed items from a feed.'),
'group' => t('Aggregator')
);
}
/**
* Test running "remove items" from the 'admin/content/aggregator' page.
*/
function testRemoveFeedItem() {
$feed = $this->createFeed();
// Add and remove feed items and ensure that the count is zero.
$this->updateFeedItems($feed);
$this->removeFeedItems($feed);
$count = db_result(db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = %d', $feed->fid));
$this->assertTrue($count == 0);
// Delete feed.
$this->deleteFeed($feed);
}
}
class CategorizeFeedItemTestCase extends AggregatorTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Categorize feed item functionality'),
'description' => t('Test feed item categorization.'),
'group' => t('Aggregator')
);
}
/**
* If a feed has a category, make sure that the children inherit that
* categorization.
*/
function testCategorizeFeedItem() {
// TODO: Need to add categories to the feed on creation.
$feed = $this->createFeed();
$this->updateFeedItems($feed);
$this->getFeedCategories($feed);
// For each category of a feed, ensure feed items have that category, too.
if (!empty($feed->categories) && !empty($feed->items)) {
foreach ($feed->categories as $category) {
$items_str = implode(', ', $feed->items);
$categorized_count = db_result(db_query('SELECT COUNT(*) FROM {aggregator_category_item} WHERE iid IN (' . $items_str . ')'));
$this->assertEqual($feed->item_count, $categorized_count, t('Total items in feed equal to the total categorized feed items in database'));
}
}
// Delete feed.
$this->deleteFeed($feed);
}
}
|