3 namespace Drupal\aggregator\Tests;
5 use Drupal\aggregator\Entity\Feed;
6 use Drupal\Component\Utility\Html;
7 use Drupal\simpletest\WebTestBase;
8 use Drupal\aggregator\FeedInterface;
11 * Defines a base class for testing the Aggregator module.
13 * @deprecated Scheduled for removal in Drupal 9.0.0.
14 * Use \Drupal\Tests\aggregator\Functional\AggregatorTestBase instead.
16 abstract class AggregatorTestBase extends WebTestBase {
19 * A user with permission to administer feeds and create content.
21 * @var \Drupal\user\Entity\User
30 public static $modules = ['block', 'node', 'aggregator', 'aggregator_test', 'views'];
35 protected function setUp() {
38 // Create an Article node type.
39 if ($this->profile != 'standard') {
40 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
43 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer news feeds', 'access news feeds', 'create article content']);
44 $this->drupalLogin($this->adminUser);
45 $this->drupalPlaceBlock('local_tasks_block');
49 * Creates an aggregator feed.
51 * This method simulates the form submission on path aggregator/sources/add.
53 * @param string $feed_url
54 * (optional) If given, feed will be created with this URL, otherwise
55 * /rss.xml will be used. Defaults to NULL.
57 * Array with additional form fields.
59 * @return \Drupal\aggregator\FeedInterface
60 * Full feed object if possible.
62 * @see getFeedEditArray()
64 public function createFeed($feed_url = NULL, array $edit = []) {
65 $edit = $this->getFeedEditArray($feed_url, $edit);
66 $this->drupalPostForm('aggregator/sources/add', $edit, t('Save'));
67 $this->assertText(t('The feed @name has been added.', ['@name' => $edit['title[0][value]']]), format_string('The feed @name has been added.', ['@name' => $edit['title[0][value]']]));
69 // Verify that the creation message contains a link to a feed.
70 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'aggregator/sources/']);
71 $this->assert(isset($view_link), 'The message area contains a link to a feed');
73 $fids = \Drupal::entityQuery('aggregator_feed')->condition('title', $edit['title[0][value]'])->condition('url', $edit['url[0][value]'])->execute();
74 $this->assertNotEmpty($fids, 'The feed found in database.');
75 return Feed::load(array_values($fids)[0]);
79 * Deletes an aggregator feed.
81 * @param \Drupal\aggregator\FeedInterface $feed
82 * Feed object representing the feed.
84 public function deleteFeed(FeedInterface $feed) {
85 $this->drupalPostForm('aggregator/sources/' . $feed->id() . '/delete', [], t('Delete'));
86 $this->assertRaw(t('The feed %title has been deleted.', ['%title' => $feed->label()]), 'Feed deleted successfully.');
90 * Returns a randomly generated feed edit array.
92 * @param string $feed_url
93 * (optional) If given, feed will be created with this URL, otherwise
94 * /rss.xml will be used. Defaults to NULL.
96 * Array with additional form fields.
101 public function getFeedEditArray($feed_url = NULL, array $edit = []) {
102 $feed_name = $this->randomMachineName(10);
104 $feed_url = \Drupal::url('view.frontpage.feed_1', [], [
105 'query' => ['feed' => $feed_name],
110 'title[0][value]' => $feed_name,
111 'url[0][value]' => $feed_url,
118 * Returns a randomly generated feed edit object.
120 * @param string $feed_url
121 * (optional) If given, feed will be created with this URL, otherwise
122 * /rss.xml will be used. Defaults to NULL.
123 * @param array $values
124 * (optional) Default values to initialize object properties with.
126 * @return \Drupal\aggregator\FeedInterface
129 public function getFeedEditObject($feed_url = NULL, array $values = []) {
130 $feed_name = $this->randomMachineName(10);
132 $feed_url = \Drupal::url('view.frontpage.feed_1', [
133 'query' => ['feed' => $feed_name],
138 'title' => $feed_name,
142 return Feed::create($values);
146 * Returns the count of the randomly created feed array.
149 * Number of feed items on default feed created by createFeed().
151 public function getDefaultFeedItemCount() {
152 // Our tests are based off of rss.xml, so let's find out how many elements should be related.
153 $feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField();
154 return $feed_count > 10 ? 10 : $feed_count;
158 * Updates the feed items.
160 * This method simulates a click to
161 * admin/config/services/aggregator/update/$fid.
163 * @param \Drupal\aggregator\FeedInterface $feed
164 * Feed object representing the feed.
165 * @param int|null $expected_count
166 * Expected number of feed items. If omitted no check will happen.
168 public function updateFeedItems(FeedInterface $feed, $expected_count = NULL) {
169 // First, let's ensure we can get to the rss xml.
170 $this->drupalGet($feed->getUrl());
171 $this->assertResponse(200, format_string(':url is reachable.', [':url' => $feed->getUrl()]));
173 // Attempt to access the update link directly without an access token.
174 $this->drupalGet('admin/config/services/aggregator/update/' . $feed->id());
175 $this->assertResponse(403);
177 // Refresh the feed (simulated link click).
178 $this->drupalGet('admin/config/services/aggregator');
179 $this->clickLink('Update items');
181 // Ensure we have the right number of items.
182 $iids = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->execute();
184 foreach ($iids as $iid) {
185 $feed->items[] = $iid;
188 if ($expected_count !== NULL) {
189 $feed->item_count = count($feed->items);
190 $this->assertEqual($expected_count, $feed->item_count, format_string('Total items in feed equal to the total items in database (@val1 != @val2)', ['@val1' => $expected_count, '@val2' => $feed->item_count]));
195 * Confirms an item removal from a feed.
197 * @param \Drupal\aggregator\FeedInterface $feed
198 * Feed object representing the feed.
200 public function deleteFeedItems(FeedInterface $feed) {
201 $this->drupalPostForm('admin/config/services/aggregator/delete/' . $feed->id(), [], t('Delete items'));
202 $this->assertRaw(t('The news items from %title have been deleted.', ['%title' => $feed->label()]), 'Feed items deleted.');
206 * Adds and deletes feed items and ensure that the count is zero.
208 * @param \Drupal\aggregator\FeedInterface $feed
209 * Feed object representing the feed.
210 * @param int $expected_count
211 * Expected number of feed items.
213 public function updateAndDelete(FeedInterface $feed, $expected_count) {
214 $count_query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count();
215 $this->updateFeedItems($feed, $expected_count);
216 $count = $count_query->execute();
217 $this->assertTrue($count);
218 $this->deleteFeedItems($feed);
219 $count = $count_query->execute();
220 $this->assertTrue($count == 0);
224 * Checks whether the feed name and URL are unique.
226 * @param string $feed_name
227 * String containing the feed name to check.
228 * @param string $feed_url
229 * String containing the feed url to check.
232 * TRUE if feed is unique.
234 public function uniqueFeed($feed_name, $feed_url) {
235 $result = \Drupal::entityQuery('aggregator_feed')->condition('title', $feed_name)->condition('url', $feed_url)->count()->execute();
236 return (1 == $result);
240 * Creates a valid OPML file from an array of feeds.
242 * @param array $feeds
246 * Path to valid OPML file.
248 public function getValidOpml(array $feeds) {
249 // Properly escape URLs so that XML parsers don't choke on them.
250 foreach ($feeds as &$feed) {
251 $feed['url[0][value]'] = Html::escape($feed['url[0][value]']);
254 * Does not have an XML declaration, must pass the parser.
260 <!-- First feed to be imported. -->
261 <outline text="{$feeds[0]['title[0][value]']}" xmlurl="{$feeds[0]['url[0][value]']}" />
263 <!-- Second feed. Test string delimitation and attribute order. -->
264 <outline xmlurl='{$feeds[1]['url[0][value]']}' text='{$feeds[1]['title[0][value]']}'/>
266 <!-- Test for duplicate URL and title. -->
267 <outline xmlurl="{$feeds[0]['url[0][value]']}" text="Duplicate URL"/>
268 <outline xmlurl="http://duplicate.title" text="{$feeds[1]['title[0][value]']}"/>
270 <!-- Test that feeds are only added with required attributes. -->
271 <outline text="{$feeds[2]['title[0][value]']}" />
272 <outline xmlurl="{$feeds[2]['url[0][value]']}" />
277 $path = 'public://valid-opml.xml';
278 // Add the UTF-8 byte order mark.
279 return file_unmanaged_save_data(chr(239) . chr(187) . chr(191) . $opml, $path);
283 * Creates an invalid OPML file.
286 * Path to invalid OPML file.
288 public function getInvalidOpml() {
295 $path = 'public://invalid-opml.xml';
296 return file_unmanaged_save_data($opml, $path);
300 * Creates a valid but empty OPML file.
303 * Path to empty OPML file.
305 public function getEmptyOpml() {
307 <?xml version="1.0" encoding="utf-8"?>
311 <outline text="Sample text" />
312 <outline text="Sample text" url="Sample URL" />
317 $path = 'public://empty-opml.xml';
318 return file_unmanaged_save_data($opml, $path);
322 * Returns a example RSS091 feed.
327 public function getRSS091Sample() {
328 return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_rss091.xml';
332 * Returns a example Atom feed.
337 public function getAtomSample() {
338 // The content of this sample ATOM feed is based directly off of the
339 // example provided in RFC 4287.
340 return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_atom.xml';
344 * Returns a example feed.
349 public function getHtmlEntitiesSample() {
350 return $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'aggregator') . '/tests/modules/aggregator_test/aggregator_test_title_entities.xml';
354 * Creates sample article nodes.
357 * (optional) The number of nodes to generate. Defaults to five.
359 public function createSampleNodes($count = 5) {
360 // Post $count article nodes.
361 for ($i = 0; $i < $count; $i++) {
363 $edit['title[0][value]'] = $this->randomMachineName();
364 $edit['body[0][value]'] = $this->randomMachineName();
365 $this->drupalPostForm('node/add/article', $edit, t('Save'));
370 * Enable the plugins coming with aggregator_test module.
372 public function enableTestPlugins() {
373 $this->config('aggregator.settings')
374 ->set('fetcher', 'aggregator_test_fetcher')
375 ->set('parser', 'aggregator_test_parser')
376 ->set('processors', [
377 'aggregator_test_processor' => 'aggregator_test_processor',
378 'aggregator' => 'aggregator',