diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php index 4775873..5839877 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Plugin/aggregator/fetcher/DefaultFetcher.php @@ -12,6 +12,7 @@ use Drupal\Core\Annotation\Plugin; use Drupal\Core\Annotation\Translation; use Guzzle\Http\Exception\BadResponseException; +use Guzzle\Http\Exception\RequestException; /** * Defines a default fetcher implementation. @@ -48,6 +49,12 @@ function fetch(Feed $feed) { $feed->modified = strtotime($response->getLastModified()); $feed->http_headers = $response->getHeaders(); + // Update the feed URL in case of a 301 redirect. + if ($previous_response = $response->getPreviousResponse()) { + if ($previous_response->getStatusCode() == 301 && $location = $response->getPreviousResponse()->getLocation()) { + $feed->url->value = $location; + } + } return TRUE; } catch (BadResponseException $e) { @@ -56,5 +63,10 @@ function fetch(Feed $feed) { drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase()))); return FALSE; } + catch (RequestException $e) { + watchdog('aggregator', 'The feed from %site seems to be broken due to "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()), WATCHDOG_WARNING); + drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()))); + return FALSE; + } } } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php b/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php index f2e1cf0..b573092 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Tests/FeedParserTest.php @@ -73,4 +73,33 @@ function testHtmlEntitiesSample() { $this->assertResponse(200, format_string('Feed %name exists.', array('%name' => $feed->label()))); $this->assertRaw("Quote" Amp&"); } + + /** + * Tests error handling when an invalid feed is added. + */ + function testRedirectFeed() { + // Simulate a typo in the URL to force a curl exception. + $invalid_url = url('aggregator/redirect', array('absolute' => TRUE)); + $feed = entity_create('aggregator_feed', array('url' => $invalid_url)); + $feed->save(); + aggregator_refresh($feed); + + // Make sure that the feed URL was updated correctly. + $this->assertEqual($feed->url->value, url('aggregator/test-feed', array('absolute' => TRUE))); + } + + /** + * Tests error handling when an invalid feed is added. + */ + function testInvalidFeed() { + // Simulate a typo in the URL to force a curl exception. + $invalid_url = 'http:/www.drupal.org'; + $feed = entity_create('aggregator_feed', array('url' => $invalid_url, 'title' => $this->randomName())); + $feed->save(); + + // Update the feed. Use the UI to be able to check the message easily. + $this->drupalGet('admin/config/services/aggregator'); + $this->clickLink(t('Update items')); + $this->assertRaw(t('The feed from %title seems to be broken because of error "%error"', array('%title' => $feed->label(), '%error' => "[curl] 6: Couldn't resolve host 'http' [url] /"))); + } } diff --git a/core/modules/aggregator/tests/aggregator_test.module b/core/modules/aggregator/tests/aggregator_test.module index 2d26a5d..09190fa 100644 --- a/core/modules/aggregator/tests/aggregator_test.module +++ b/core/modules/aggregator/tests/aggregator_test.module @@ -11,6 +11,13 @@ function aggregator_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['aggregator/redirect'] = array( + 'title' => 'Test feed with a redirect', + 'description' => "A feed that redirects to another one", + 'page callback' => 'aggregator_test_redirect', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -56,3 +63,10 @@ function aggregator_test_feed($use_last_modified = FALSE, $use_etag = FALSE) { print $feed; } + +/** + * Page callback that redirects to another feed. + */ +function aggregator_test_redirect() { + drupal_goto('aggregator/test-feed', array(), 301); +}