diff --git a/plugins/FeedsParser.inc b/plugins/FeedsParser.inc index 0c5477e169402ab24dbbcae2eb8c13603f957d21..ac53bc41cb7306a10fd2c1a85c52ceb8c13030eb 100644 --- a/plugins/FeedsParser.inc +++ b/plugins/FeedsParser.inc @@ -263,15 +263,41 @@ class FeedsEnclosure extends FeedsElement { return $this->mime_type; } + /** + * Use this method instead of FeedsElement::getValue() when fetching the file + * from the URL. + * + * @return + * Value with encoded space characters to safely fetch the file from the URL. + * + * @see FeedsElement::getValue() + */ + public function getUrlEncodedValue() { + return str_replace(' ', '%20', $this->getValue()); + } + + /** + * Use this method instead of FeedsElement::getValue() to get the file name + * transformed for better local saving (underscores instead of spaces) + * + * @return + * Value with space characters changed to underscores. + * + * @see FeedsElement::getValue() + */ + public function getLocalValue() { + return str_replace(' ', '_', $this->getValue()); + } + /** * @return * The content of the referenced resource. */ public function getContent() { feeds_include_library('http_request.inc', 'http_request'); - $result = http_request_get($this->getValue()); + $result = http_request_get($this->getUrlEncodedValue()); if ($result->code != 200) { - throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->getValue(), '!code' => $result->code))); + throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->getUrlEncodedValue(), '!code' => $result->code))); } return $result->data; } @@ -318,7 +344,7 @@ class FeedsEnclosure extends FeedsElement { } } else { - $filename = basename($this->getValue()); + $filename = basename($this->getLocalValue()); if (module_exists('transliteration')) { require_once drupal_get_path('module', 'transliteration') . '/transliteration.inc'; $filename = transliteration_clean_filename($filename); diff --git a/tests/feeds/feeds-tests-files.tpl.php b/tests/feeds/feeds-tests-files.tpl.php index 5c8aaa1abc4a5053c029ed0624c9a2ccd5fc5b11..9674abb2d46b3fb06cc4fe25cbc8220752937f71 100644 --- a/tests/feeds/feeds-tests-files.tpl.php +++ b/tests/feeds/feeds-tests-files.tpl.php @@ -3,3 +3,4 @@ Title,published,file,GUID "Jeff vs Tom",428112720,<?php print $files[1]; ?>,1 "Attersee",1151766000,<?php print $files[2]; ?>,2 "H Street NE",1256326995,<?php print $files[3]; ?>,3 +"La Fayette Park",1256326995,<?php print $files[4]; ?>,4 diff --git a/tests/feeds/feeds-tests-flickr.tpl.php b/tests/feeds/feeds-tests-flickr.tpl.php index c16606d56604f8dcf6452305a0b6532628a89e2e..92c7ab9cd506996630759e54554cede0adf3d043 100644 --- a/tests/feeds/feeds-tests-flickr.tpl.php +++ b/tests/feeds/feeds-tests-flickr.tpl.php @@ -168,4 +168,36 @@ print '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'; <category term="hstreetbynight" scheme="http://www.flickr.com/photos/tags/" /> <category term="forlaia" scheme="http://www.flickr.com/photos/tags/" /> </entry> + <entry> + <title>La Fayette Park</title> + <link rel="alternate" type="text/html" href="http://www.flickr.com/photos/a-barth/4209685951/in/set-72157603970496952/"/> + <id>tag:flickr.com,2005:/photo/4209685951/in/set-72157603970496952</id> + <published>2009-07-09T21:48:04Z</published> + <updated>2009-07-09T21:48:04Z</updated> + <dc:date.Taken>2009-05-01T00:00:00-08:00</dc:date.Taken> + <content type="html"><p><a href="http://www.flickr.com/people/a-barth/">Alex Barth</a> posted a photo:</p> + + <p><a href="http://www.flickr.com/photos/a-barth/3596408735/" title="Tubing is fun"><img src="http://farm3.staticflickr.com/2675/4209685951_cb073de96f_m.jpg" width="239" height="240" alt="La Fayette park" /></a></p> + + + <p>Virginia, 2009</p></content> + <author> + <name>Alex Barth</name> + <uri>http://www.flickr.com/people/a-barth/</uri> + </author> + <link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-nc/2.0/deed.en" /> + <link rel="enclosure" type="image/jpeg" href="<?php print $image_urls[4]; ?>" /> + + <category term="color" scheme="http://www.flickr.com/photos/tags/" /> + <category term="film" scheme="http://www.flickr.com/photos/tags/" /> + <category term="virginia" scheme="http://www.flickr.com/photos/tags/" /> + <category term="awesome" scheme="http://www.flickr.com/photos/tags/" /> + <category term="ishootfilm" scheme="http://www.flickr.com/photos/tags/" /> + <category term="va" scheme="http://www.flickr.com/photos/tags/" /> + <category term="badge" scheme="http://www.flickr.com/photos/tags/" /> + <category term="tubing" scheme="http://www.flickr.com/photos/tags/" /> + <category term="fuji160c" scheme="http://www.flickr.com/photos/tags/" /> + <category term="anfamiliebarth" scheme="http://www.flickr.com/photos/tags/" /> + <category term="canon24l" scheme="http://www.flickr.com/photos/tags/" /> + </entry> </feed> \ No newline at end of file diff --git a/tests/feeds_mapper_file.test b/tests/feeds_mapper_file.test index 7d9cb0b392948d07e8021d7b1635fdd9c9620b63..d642e9491a05afb987e69c2c3302be98226f3232 100644 --- a/tests/feeds_mapper_file.test +++ b/tests/feeds_mapper_file.test @@ -54,7 +54,7 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { ), )); $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] . '/testing/feeds/flickr.xml'); - $this->assertText('Created 4 nodes'); + $this->assertText('Created 5 nodes'); $files = $this->testFiles(); $entities = db_select('feeds_item') @@ -63,7 +63,8 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { ->execute(); foreach ($entities as $entity) { $this->drupalGet('node/' . $entity->entity_id . '/edit'); - $this->assertText(array_shift($files)); + $f = new FeedsEnclosure(array_shift($files), NULL); + $this->assertText($f->getLocalValue()); } // 2) Test mapping local resources to file field. @@ -101,7 +102,7 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { 'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv', ); $this->drupalPost('import/node', $edit, 'Import'); - $this->assertText('Created 4 nodes'); + $this->assertText('Created 5 nodes'); // Assert: files should be in resources/. $files = $this->testFiles(); @@ -111,7 +112,8 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { ->execute(); foreach ($entities as $entity) { $this->drupalGet('node/' . $entity->entity_id . '/edit'); - $this->assertRaw('resources/' . array_shift($files)); + $f = new FeedsEnclosure(array_shift($files), NULL); + $this->assertRaw('resources/' . $f->getUrlEncodedValue()); } // 3) Test mapping of local resources, this time leave files in place. @@ -126,7 +128,7 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { 'feeds[FeedsHTTPFetcher][source]' => $GLOBALS['base_url'] . '/testing/feeds/files.csv', ); $this->drupalPost('import/node', $edit, 'Import'); - $this->assertText('Created 4 nodes'); + $this->assertText('Created 5 nodes'); // Assert: files should be in images/ now. $files = $this->testFiles(); @@ -136,7 +138,8 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { ->execute(); foreach ($entities as $entity) { $this->drupalGet('node/' . $entity->entity_id . '/edit'); - $this->assertRaw('images/' . array_shift($files)); + $f = new FeedsEnclosure(array_shift($files), NULL); + $this->assertRaw('images/' . $f->getUrlEncodedValue()); } // Deleting all imported items will delete the files from the images/ dir. @@ -151,7 +154,7 @@ class FeedsMapperFileTestCase extends FeedsMapperTestCase { * Lists test files. */ public function testFiles() { - return array('tubing.jpeg', 'foosball.jpeg', 'attersee.jpeg', 'hstreet.jpeg'); + return array('tubing.jpeg', 'foosball.jpeg', 'attersee.jpeg', 'hstreet.jpeg', 'la fayette.jpeg'); } /** diff --git a/tests/feeds_tests.module b/tests/feeds_tests.module index 7c5e7bc4b664ef78b305cff69647009dc5b3a96a..e1e5f97a304cd5cbc8a878bd34852056c48495e4 100644 --- a/tests/feeds_tests.module +++ b/tests/feeds_tests.module @@ -44,6 +44,7 @@ function feeds_tests_flickr() { 1 => "foosball.jpeg", 2 => "attersee.jpeg", 3 => "hstreet.jpeg", + 4 => "la fayette.jpeg", ); $path = drupal_get_path('module', 'feeds_tests') . '/feeds/assets'; foreach ($images as &$image) { @@ -62,6 +63,7 @@ function feeds_tests_files() { 1 => "foosball.jpeg", 2 => "attersee.jpeg", 3 => "hstreet.jpeg", + 4 => "la fayette.jpeg", ); foreach ($images as &$image) { $image = "public://images/$image";