diff --git a/includes/FeedsHTTPCache.inc b/includes/FeedsHTTPCache.inc index 944a2d1..1f01f8f 100644 --- a/includes/FeedsHTTPCache.inc +++ b/includes/FeedsHTTPCache.inc @@ -90,11 +90,26 @@ class FeedsHTTPCache extends DrupalDatabaseCache { * * @return string|null * The name of the file that was created or NULL if no file was created. + * + * @throws Exception + * In case the cache dir is not writable. */ public function saveFile($cid, $response) { if (isset($response->data)) { $file_cache_dir = $this->getCacheDir(); - file_prepare_directory($file_cache_dir, FILE_CREATE_DIRECTORY); + if (!file_prepare_directory($file_cache_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { + // Cache directory is not writeable. + if (user_access('administer feeds')) { + $message = t("The feeds cache directory (@dir) either cannot be created or is not writable. You can change the cache directory by setting the '@variable' variable.", array( + '@dir' => $file_cache_dir, + '@variable' => 'feeds_http_file_cache_dir', + )); + } + else { + $message = t('The feeds cache directory either cannot be created or is not writable. Please contact your site administrator.'); + } + throw new Exception($message); + } $filename = $this->constructFilePath($cid); file_put_contents($filename, $response->data); diff --git a/tests/FeedsHTTPCacheTest.test b/tests/FeedsHTTPCacheTest.test index 2577afc..2ae8d5e 100644 --- a/tests/FeedsHTTPCacheTest.test +++ b/tests/FeedsHTTPCacheTest.test @@ -510,4 +510,34 @@ class FeedsHTTPCacheTest extends FeedsWebTestCase { // And assert that the cache class reports that the cache is not empty. $this->assertFalse($this->cache->isEmpty()); } + + /** + * @covers FeedsHTTPCache::saveFile(). + */ + public function testSaveFile() { + $cid = static::randomName(); + $response = $this->createHttpResponse(); + $this->cache->saveFile($cid, $response); + $this->assertCacheFileExists($cid); + } + + /** + * @covers FeedsHTTPCache::saveFile(). + * + * Tests failing to create cache directory. + */ + public function testSaveFileException() { + variable_set('feeds_http_file_cache_dir', 'file://non-writeable-dir/feeds'); + + $cid = static::randomName(); + $response = $this->createHttpResponse(); + try { + $this->cache->saveFile($cid, $response); + } + catch (Exception $e) {} + $this->assertTrue(isset($e), 'An exception is thrown.'); + + $this->assertNoCacheFileExists($cid); + } + } diff --git a/tests/feeds_fetcher_http.test b/tests/feeds_fetcher_http.test index 32618ad..cb7d637 100644 --- a/tests/feeds_fetcher_http.test +++ b/tests/feeds_fetcher_http.test @@ -384,4 +384,26 @@ class FeedsFileHTTPTestCase extends FeedsWebTestCase { $this->assertNotEqual('CHANGED IN CACHED FILE', $node->body[LANGUAGE_NONE][0]['value']); } + /** + * Tests that a non-writable cache directory does not result into fatal + * errors. + */ + public function testNonWritableCacheDirectory() { + // Set the cache directory to a non-writable directory. + variable_set('feeds_http_file_cache_dir', 'file://non-writeable-dir/feeds'); + + $this->setUpImporter(); + + $source_url = url('testing/feeds/nodes.csv', array('absolute' => TRUE)); + $edit = array( + 'feeds[FeedsHTTPFetcher][source]' => $source_url, + ); + $this->drupalPost('import/node', $edit, t('Import')); + + // Assert that a message is being displayed and that we are back on the + // import form. + $this->assertText("The feeds cache directory (file://non-writeable-dir/feeds) either cannot be created or is not writable. You can change the cache directory by setting the 'feeds_http_file_cache_dir' variable."); + $this->assertFieldByName('feeds[FeedsHTTPFetcher][source]', $source_url); + } + }