From d08fdac59197f107a529b1cf9b298a866fff1cc0 Mon Sep 17 00:00:00 2001 From: febbraro <febbraro@43670.no-reply.drupal.org> Date: Wed, 4 Jan 2012 13:28:04 -0500 Subject: [PATCH] Issue #686470 by johnv, wojtha, febbraro | rjbrown99: Fixed Filefield mapper: URLs with spaces not parsed properly. --- plugins/FeedsParser.inc | 32 +++++++++++++++++++++++--- tests/feeds/feeds-tests-files.tpl.php | 1 + tests/feeds/feeds-tests-flickr.tpl.php | 32 ++++++++++++++++++++++++++ tests/feeds_mapper_file.test | 17 ++++++++------ tests/feeds_tests.module | 2 ++ 5 files changed, 74 insertions(+), 10 deletions(-) diff --git a/plugins/FeedsParser.inc b/plugins/FeedsParser.inc index 0c5477e1..ac53bc41 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 5c8aaa1a..9674abb2 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 c16606d5..92c7ab9c 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 7d9cb0b3..d642e949 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 7c5e7bc4..e1e5f97a 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"; -- GitLab