Spike PHPCoverage Details: aggregator.test

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