Skip to content

Commit

Permalink
Merge pull request #923 from Codeinwp/fix/image_import
Browse files Browse the repository at this point in the history
fix: image import for images with spaces in url
  • Loading branch information
vytisbulkevicius authored Apr 10, 2024
2 parents e507184 + be1b512 commit c957b08
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,8 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title

if ( ! $id ) {

if ( filter_var( $img_source_url, FILTER_VALIDATE_URL ) === false ) {
// We escape the URL to ensure that valid URLs are passed by the filter.
if ( filter_var( esc_url( $img_source_url ), FILTER_VALIDATE_URL ) === false ) {
$import_errors[] = 'Invalid Featured Image URL: ' . $img_source_url;
return false;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/test-image-import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* WordPress unit test plugin.
*
* @package feedzy-rss-feeds-pro
* @subpackage Tests
* @copyright Copyright (c) 2024, Bogdan Preda
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 4.4.6
*/
class Test_Image_Import extends WP_UnitTestCase {

/**
* Test that the image import allows valid image URLs and logs errors for invalid ones.
* Test introduced to cover this issue https://github.com/Codeinwp/feedzy-rss-feeds/issues/917.
* @since 4.4.6
*/
public function test_image_import_url() {
$feedzy = new Feedzy_Rss_Feeds_Import( 'feedzy-rss-feeds', '1.2.0' );

$reflector = new ReflectionClass( $feedzy );
$try_save_featured_image = $reflector->getMethod( 'try_save_featured_image' );
$try_save_featured_image->setAccessible( true );

// Check that NON-IMAGE URL returns invalid
$import_errors = array();
$import_info = array();
$arguments = array( 'a random string', 0, 'Post Title', &$import_errors, &$import_info, array() );
$response = $try_save_featured_image->invokeArgs( $feedzy, $arguments );

$this->assertFalse( $response );

$this->assertTrue( count( $import_errors ) > 0 );
$this->assertEquals( 'Invalid Featured Image URL: a random string', $import_errors[0] );


// For the next test, we will use a valid URL, but the image does not exist. We will check that the error is logged and is the expected one.
add_filter( 'themeisle_log_event', function( $product, $message, $type, $file, $line ) {
if ( $type === 'error' ) {
$this->assertTrue( strpos( $message, 'Unable to download file' ) !== false );
}
}, 10, 5 );

$import_errors = array();
$import_info = array();
$arguments = array( 'https://example.com/path_to_image/image.jpeg', 0, 'Post Title', &$import_errors, &$import_info, array() );
$response = $try_save_featured_image->invokeArgs( $feedzy, $arguments );

// expected response is false because the image does not exist, but the URL is valid so no $import_errors should be set.
$this->assertFalse( $response );
$this->assertTrue( empty( $import_errors ) );

$import_errors = array();
$import_info = array();
$arguments = array( 'https://example.com/path_to_image/image w space in name.jpeg', 0, 'Post Title', &$import_errors, &$import_info, array() );
$response = $try_save_featured_image->invokeArgs( $feedzy, $arguments );

// expected response is false because the image does not exist, but the URL is valid so no $import_errors should be set.
$this->assertFalse( $response );
$this->assertTrue( empty( $import_errors ) );
}
}

0 comments on commit c957b08

Please sign in to comment.