From aa6ce06dd79637b2758446623086b3ab10848121 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Fri, 29 Mar 2024 17:50:22 +0200 Subject: [PATCH 01/14] fix: guard against missing function Closes: #909 --- includes/admin/feedzy-rss-feeds-import.php | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index cc4a9f78..75e225e8 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -1977,6 +1977,25 @@ public function tryReuseExistingFeaturedImage( &$result, $title_feed, $post_id = return true; } + /** + * Will retireve the file type of a file by its URL. + * + * @param string $url The URL of the file. + * + * @return string + */ + private function get_file_type_by_url( $url ) { + $response = wp_remote_get( $url ); + + // wp_remote_retrieve_header can return an array if there are multiple headers with the same name + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); + if ( is_array( $content_type ) ) { + $content_type = $content_type[0]; + } + + return $content_type; + } + /** * Downloads and sets a post featured image if possible. * @@ -2019,7 +2038,10 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title return false; } - $type = mime_content_type( $local_file ); + $type = $this->get_file_type_by_url( $img_source_url ); + if ( empty( $type ) && function_exists( 'mime_content_type' ) ) { + $type = mime_content_type( $local_file ); + } // the file is downloaded with a .tmp extension // if the URL mentions the extension of the file, the upload succeeds // but if the URL is like https://source.unsplash.com/random, then the upload fails From 65e4a5b7e8902eaf60a12830e7d090e8d78b1e5f Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Fri, 29 Mar 2024 17:52:22 +0200 Subject: [PATCH 02/14] chore: code cleanup --- includes/admin/feedzy-rss-feeds-import.php | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index 75e225e8..77e3fc25 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -1978,23 +1978,23 @@ public function tryReuseExistingFeaturedImage( &$result, $title_feed, $post_id = } /** - * Will retireve the file type of a file by its URL. - * + * Will retireve the file type of a file by its URL. + * * @param string $url The URL of the file. * * @return string */ - private function get_file_type_by_url( $url ) { - $response = wp_remote_get( $url ); + private function get_file_type_by_url( $url ) { + $response = wp_remote_get( $url ); - // wp_remote_retrieve_header can return an array if there are multiple headers with the same name - $content_type = wp_remote_retrieve_header( $response, 'content-type' ); - if ( is_array( $content_type ) ) { - $content_type = $content_type[0]; - } + // wp_remote_retrieve_header can return an array if there are multiple headers with the same name + $content_type = wp_remote_retrieve_header( $response, 'content-type' ); + if ( is_array( $content_type ) ) { + $content_type = $content_type[0]; + } - return $content_type; - } + return $content_type; + } /** * Downloads and sets a post featured image if possible. @@ -2038,10 +2038,10 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title return false; } - $type = $this->get_file_type_by_url( $img_source_url ); - if ( empty( $type ) && function_exists( 'mime_content_type' ) ) { - $type = mime_content_type( $local_file ); - } + $type = $this->get_file_type_by_url( $img_source_url ); + if ( empty( $type ) && function_exists( 'mime_content_type' ) ) { + $type = mime_content_type( $local_file ); + } // the file is downloaded with a .tmp extension // if the URL mentions the extension of the file, the upload succeeds // but if the URL is like https://source.unsplash.com/random, then the upload fails From 4a8f20a014e76be5edab406e7ffad05fc7087482 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 3 Apr 2024 10:27:30 +0300 Subject: [PATCH 03/14] chore: added unit test --- tests/test-import.php | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/test-import.php b/tests/test-import.php index 7fd6a6d4..38a9f204 100644 --- a/tests/test-import.php +++ b/tests/test-import.php @@ -10,6 +10,8 @@ */ class Test_Feedzy_Import extends WP_UnitTestCase { + private $import_limit = 1; + /** * Sets up the test methods. */ @@ -28,10 +30,10 @@ public function setUp(): void { * @access public * @dataProvider importDataProvider */ - public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic_tags = '[#item_content]', $use_filter = false ) { + public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic_tags = '[#item_content]', $use_filter = false, $type = 'post' ) { do_action( 'init' ); - $num_items = 1; + $num_items = $this->import_limit; $user_id = $this->factory->user->create( array( 'role' => 'administrator', @@ -76,7 +78,7 @@ public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic $_POST['feedzy_post_nonce'] = wp_create_nonce( 'feedzy_post_nonce' ); $_POST['post_type'] = 'feedzy_imports'; $_POST['feedzy_meta_data']['source'] = $slug; - $_POST['feedzy_meta_data']['import_post_type'] = 'post'; + $_POST['feedzy_meta_data']['import_post_type'] = $type; $_POST['feedzy_meta_data']['import_post_term'] = 'category_' . $category_id; $_POST['feedzy_meta_data']['import_post_status'] = 'publish'; $_POST['feedzy_meta_data']['inc_key'] = ''; @@ -106,7 +108,7 @@ public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic $import_custom_fields = get_post_meta( $p->ID, 'imports_custom_fields', true ); $import_feed_limit = get_post_meta( $p->ID, 'import_feed_limit', true ); - $this->assertEquals( 'post', $import_post_type ); + $this->assertEquals( $type, $import_post_type ); $this->assertEquals( 'category_' . $category_id, $import_post_term ); $this->assertEquals( 'publish', $import_post_status ); $this->assertEquals( $slug, $source ); @@ -174,6 +176,13 @@ public function test_feedzy_imports( $random_name1, $random_name2, $urls, $magic do_action( 'feedzy_cron', '1' ); + /** + * We bail for non post types as the subsequent tests might not apply. + */ + if ( $type !== 'post' ) { + return; + } + $created = get_posts( array( 'numberposts' => $num_items, @@ -276,6 +285,24 @@ public function test_canonical_url( $post ) { } + /** + * Test the attachment import works and the mime type is correct. + */ + public function test_attachement_import() { + $this->test_feedzy_imports( $this->get_rand_name(), $this->get_rand_name(), $this->get_two_rand_feeds(), '[#item_content]', false, 'attachment' ); + $args = array( + 'post_type' =>'attachment', + 'numberposts' => -1, + 'orderby' => 'date', + 'order' => 'DESC', + ); + $attachments = get_posts($args); + $this->assertEquals( $this->import_limit, count( $attachments ) ); + + $this->assertTrue( isset( $attachments[0]->post_mime_type ) ); + $this->assertTrue( $attachments[0]->post_mime_type === 'image/jpeg' ); + } + /** * Utility method to generate a random 5 char string. From b02f7a6753996fd15068f3957ee3c3a106055c82 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 3 Apr 2024 18:36:48 +0300 Subject: [PATCH 04/14] chore: code optimisation --- includes/admin/feedzy-rss-feeds-import.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index 77e3fc25..e0982440 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -2038,10 +2038,16 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title return false; } - $type = $this->get_file_type_by_url( $img_source_url ); - if ( empty( $type ) && function_exists( 'mime_content_type' ) ) { + // try first to get the file type using the built-in function if available. + if ( function_exists( 'mime_content_type' ) ) { $type = mime_content_type( $local_file ); } + + // if the file type is not found, try to get it from the URL. + if ( empty( $type ) ) { + $type = $this->get_file_type_by_url( $img_source_url ); + } + // the file is downloaded with a .tmp extension // if the URL mentions the extension of the file, the upload succeeds // but if the URL is like https://source.unsplash.com/random, then the upload fails From 08df282d58096676bfbe90354ab0412fa1d6db1e Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 3 Apr 2024 20:03:20 +0300 Subject: [PATCH 05/14] chore: init type --- includes/admin/feedzy-rss-feeds-import.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index e0982440..de8a0742 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -2038,6 +2038,7 @@ private function try_save_featured_image( $img_source_url, $post_id, $post_title return false; } + $type = ''; // try first to get the file type using the built-in function if available. if ( function_exists( 'mime_content_type' ) ) { $type = mime_content_type( $local_file ); From f9c45aa63ceaec3172a5b404965e33ae6a21d9f9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 8 Apr 2024 03:05:49 +0000 Subject: [PATCH 06/14] chore(deps): bump codeinwp/themeisle-sdk from 3.3.17 to 3.3.18 Bumps [codeinwp/themeisle-sdk](https://github.com/Codeinwp/themeisle-sdk) from 3.3.17 to 3.3.18. - [Release notes](https://github.com/Codeinwp/themeisle-sdk/releases) - [Changelog](https://github.com/Codeinwp/themeisle-sdk/blob/master/CHANGELOG.md) - [Commits](https://github.com/Codeinwp/themeisle-sdk/compare/v3.3.17...v3.3.18) --- updated-dependencies: - dependency-name: codeinwp/themeisle-sdk dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 44c77ba7..00a942a8 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "codeinwp/themeisle-sdk", - "version": "3.3.17", + "version": "3.3.18", "source": { "type": "git", "url": "https://github.com/Codeinwp/themeisle-sdk.git", - "reference": "b5698f21640d0156bd6777aa5c71e0704748755c" + "reference": "5463d7170ed7b9735223d6715442b8670a477688" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/b5698f21640d0156bd6777aa5c71e0704748755c", - "reference": "b5698f21640d0156bd6777aa5c71e0704748755c", + "url": "https://api.github.com/repos/Codeinwp/themeisle-sdk/zipball/5463d7170ed7b9735223d6715442b8670a477688", + "reference": "5463d7170ed7b9735223d6715442b8670a477688", "shasum": "" }, "require-dev": { @@ -42,9 +42,9 @@ ], "support": { "issues": "https://github.com/Codeinwp/themeisle-sdk/issues", - "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.17" + "source": "https://github.com/Codeinwp/themeisle-sdk/tree/v3.3.18" }, - "time": "2024-04-01T07:35:31+00:00" + "time": "2024-04-05T09:47:58+00:00" } ], "packages-dev": [ From 55879c31f1e94a42793927124004ad8a05bfa09f Mon Sep 17 00:00:00 2001 From: GrigoreMihai Date: Tue, 9 Apr 2024 22:04:26 +0300 Subject: [PATCH 07/14] fix: displaying detailed errors for non-admin users --- .../abstract/feedzy-rss-feeds-admin-abstract.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/abstract/feedzy-rss-feeds-admin-abstract.php b/includes/abstract/feedzy-rss-feeds-admin-abstract.php index 1e7c78e9..15134a69 100644 --- a/includes/abstract/feedzy-rss-feeds-admin-abstract.php +++ b/includes/abstract/feedzy-rss-feeds-admin-abstract.php @@ -194,7 +194,7 @@ public function feedzy_default_error_notice( $errors, $feed, $feed_url ) { // Show the error message only if the user who has created this post (which contains the feed) is logged in. // Or if this is in the dry run window. // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison - $show_error = is_admin() || ( is_user_logged_in() && $post && get_current_user_id() == $post->post_author ); + $show_error = is_admin() || ( is_user_logged_in() && $post && current_user_can( 'manage_options' ) && get_current_user_id() == $post->post_author ); $error_msg = ''; if ( is_array( $errors ) ) { @@ -735,6 +735,7 @@ private function smart_convert( $url ) { * @return SimplePie|string|void|WP_Error The feed resource. */ public function fetch_feed( $feed_url, $cache = '12_hours', $sc = '' ) { + error_log('fetch_feed'); // Load SimplePie if not already. do_action( 'feedzy_pre_http_setup', $feed_url ); if ( function_exists( 'feedzy_amazon_get_locale_hosts' ) ) { @@ -754,6 +755,7 @@ function( $url ) { $is_amazon_source = ! empty( $amazon_hosts ) && in_array( $url_host, $amazon_hosts, true ); } if ( $is_amazon_source ) { + error_log('is_amazon_source'); $feed = $this->init_amazon_api( $feed_url, isset( $sc['refresh'] ) ? $sc['refresh'] : '12_hours', @@ -766,6 +768,10 @@ function( $url ) { } } // Load SimplePie Instance. + error_log('init_feed'); + error_log(print_r($feed_url,true)); + error_log($cache); + error_log(print_r($sc,true)); $feed = $this->init_feed( $feed_url, $cache, $sc ); // Not used as log as #41304 is Opened. // Report error when is an error loading the feed. @@ -903,8 +909,10 @@ function() { ); } + error_log('starting to init'); $feed->init(); + error_log('init done'); if ( ! $feed->get_type() ) { return $feed; } @@ -924,7 +932,9 @@ function() { $feed = $this->init_feed( $feed_url, $cache, $sc, false ); } elseif ( is_string( $feed_url ) || ( is_array( $feed_url ) && 1 === count( $feed_url ) ) ) { do_action( 'themeisle_log_event', FEEDZY_NAME, 'Trying to use raw data', 'debug', __FILE__, __LINE__ ); + error_log('trying to use raw data'); $data = wp_remote_retrieve_body( wp_safe_remote_get( $feed_url, array( 'user-agent' => $default_agent ) ) ); + error_log('got data'); $cloned_feed->set_raw_data( $data ); $cloned_feed->init(); $error_raw = $cloned_feed->error(); From c24e898d9427de258be6362df0c3dbb020767061 Mon Sep 17 00:00:00 2001 From: GrigoreMihai Date: Tue, 9 Apr 2024 22:10:01 +0300 Subject: [PATCH 08/14] chore: removed leftover error_log --- includes/abstract/feedzy-rss-feeds-admin-abstract.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/includes/abstract/feedzy-rss-feeds-admin-abstract.php b/includes/abstract/feedzy-rss-feeds-admin-abstract.php index 15134a69..8147aa27 100644 --- a/includes/abstract/feedzy-rss-feeds-admin-abstract.php +++ b/includes/abstract/feedzy-rss-feeds-admin-abstract.php @@ -735,7 +735,6 @@ private function smart_convert( $url ) { * @return SimplePie|string|void|WP_Error The feed resource. */ public function fetch_feed( $feed_url, $cache = '12_hours', $sc = '' ) { - error_log('fetch_feed'); // Load SimplePie if not already. do_action( 'feedzy_pre_http_setup', $feed_url ); if ( function_exists( 'feedzy_amazon_get_locale_hosts' ) ) { @@ -755,7 +754,6 @@ function( $url ) { $is_amazon_source = ! empty( $amazon_hosts ) && in_array( $url_host, $amazon_hosts, true ); } if ( $is_amazon_source ) { - error_log('is_amazon_source'); $feed = $this->init_amazon_api( $feed_url, isset( $sc['refresh'] ) ? $sc['refresh'] : '12_hours', @@ -768,10 +766,6 @@ function( $url ) { } } // Load SimplePie Instance. - error_log('init_feed'); - error_log(print_r($feed_url,true)); - error_log($cache); - error_log(print_r($sc,true)); $feed = $this->init_feed( $feed_url, $cache, $sc ); // Not used as log as #41304 is Opened. // Report error when is an error loading the feed. @@ -909,10 +903,8 @@ function() { ); } - error_log('starting to init'); $feed->init(); - error_log('init done'); if ( ! $feed->get_type() ) { return $feed; } @@ -932,9 +924,7 @@ function() { $feed = $this->init_feed( $feed_url, $cache, $sc, false ); } elseif ( is_string( $feed_url ) || ( is_array( $feed_url ) && 1 === count( $feed_url ) ) ) { do_action( 'themeisle_log_event', FEEDZY_NAME, 'Trying to use raw data', 'debug', __FILE__, __LINE__ ); - error_log('trying to use raw data'); $data = wp_remote_retrieve_body( wp_safe_remote_get( $feed_url, array( 'user-agent' => $default_agent ) ) ); - error_log('got data'); $cloned_feed->set_raw_data( $data ); $cloned_feed->init(); $error_raw = $cloned_feed->error(); From a4b35492de5496dd1daf8e06b32d38eb774e5ba6 Mon Sep 17 00:00:00 2001 From: GrigoreMihai Date: Wed, 10 Apr 2024 15:16:15 +0300 Subject: [PATCH 09/14] chore: updated cypress tests --- cypress/e2e/gutenberg/gutenberg_free.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cypress/e2e/gutenberg/gutenberg_free.js b/cypress/e2e/gutenberg/gutenberg_free.js index aa1e7d78..5cd86ba0 100644 --- a/cypress/e2e/gutenberg/gutenberg_free.js +++ b/cypress/e2e/gutenberg/gutenberg_free.js @@ -21,9 +21,9 @@ describe('Test Free - gutenberg', function() { cy.get('.edit-post-visual-editor__post-title-wrapper .editor-post-title__input').type(PREFIX); // insert a feedzy block - cy.get('div.edit-post-header__toolbar button.edit-post-header-toolbar__inserter-toggle').click({force:true}); - cy.get('.edit-post-editor__inserter-panel-content').then(function ($popup) { - cy.wrap($popup).find('.components-search-control__input').type('feedzy'); + cy.get('div.edit-post-header__toolbar button.editor-document-tools__inserter-toggle').click({force:true}); + cy.get('.editor-inserter-sidebar__content').then(function ($popup) { + cy.wrap($popup).find('.components-search-control input.components-input-control__input').type('feedzy'); cy.wrap($popup).find('.block-editor-block-types-list .editor-block-list-item-feedzy-rss-feeds-feedzy-block').should('have.length', 1); cy.wrap($popup).find('.block-editor-block-types-list .editor-block-list-item-feedzy-rss-feeds-feedzy-block').click({force:true}); }); @@ -62,7 +62,7 @@ describe('Test Free - gutenberg', function() { // item options cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-meta input.components-text-control__input').type(gutenberg.meta, {force: true}); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-multiple-meta input.components-text-control__input').type(gutenberg.multiple_meta, {force: true}); - /* for pro + /* for pro cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-include input.components-text-control__input').type(gutenberg.include); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-ban input.components-text-control__input').type(gutenberg.ban); */ @@ -116,7 +116,7 @@ describe('Test Free - gutenberg', function() { // item options cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-meta input.components-text-control__input').should('have.value', gutenberg.meta); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-multiple-meta input.components-text-control__input').should('have.value', gutenberg.multiple_meta); - /* for pro + /* for pro cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-include input.components-text-control__input').should('have.value', gutenberg.include); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-ban input.components-text-control__input').should('have.value', gutenberg.ban); */ From a0bd6b0ab8a1060fb4c701afd173cda7c9002e3d Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 10 Apr 2024 16:13:07 +0300 Subject: [PATCH 10/14] fix: image import for images with spaces in url --- includes/admin/feedzy-rss-feeds-import.php | 3 +- tests/test-image-import.php | 62 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/test-image-import.php diff --git a/includes/admin/feedzy-rss-feeds-import.php b/includes/admin/feedzy-rss-feeds-import.php index cc4a9f78..655c8739 100644 --- a/includes/admin/feedzy-rss-feeds-import.php +++ b/includes/admin/feedzy-rss-feeds-import.php @@ -1999,7 +1999,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; } diff --git a/tests/test-image-import.php b/tests/test-image-import.php new file mode 100644 index 00000000..e1d9d619 --- /dev/null +++ b/tests/test-image-import.php @@ -0,0 +1,62 @@ +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 ) ); + } +} From e57c55b9b05ba4c827c79bf0e497d2ca4f9bd9c4 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 10 Apr 2024 16:20:13 +0300 Subject: [PATCH 11/14] chore: change unit method name --- tests/test-image-import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-image-import.php b/tests/test-image-import.php index e1d9d619..37616005 100644 --- a/tests/test-image-import.php +++ b/tests/test-image-import.php @@ -15,7 +15,7 @@ class Test_Image_Import extends WP_UnitTestCase { * Test introduced to cover this issue https://github.com/Codeinwp/feedzy-rss-feeds/issues/917. * @since 4.4.6 */ - public function test_image_import() { + public function test_image_import_url() { $feedzy = new Feedzy_Rss_Feeds_Import( 'feedzy-rss-feeds', '1.2.0' ); $reflector = new ReflectionClass( $feedzy ); From be1b51278f648cdbfe660301d3f1a035d824f76e Mon Sep 17 00:00:00 2001 From: GrigoreMihai Date: Wed, 10 Apr 2024 15:16:15 +0300 Subject: [PATCH 12/14] chore: updated cypress tests --- cypress/e2e/gutenberg/gutenberg_free.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cypress/e2e/gutenberg/gutenberg_free.js b/cypress/e2e/gutenberg/gutenberg_free.js index aa1e7d78..5cd86ba0 100644 --- a/cypress/e2e/gutenberg/gutenberg_free.js +++ b/cypress/e2e/gutenberg/gutenberg_free.js @@ -21,9 +21,9 @@ describe('Test Free - gutenberg', function() { cy.get('.edit-post-visual-editor__post-title-wrapper .editor-post-title__input').type(PREFIX); // insert a feedzy block - cy.get('div.edit-post-header__toolbar button.edit-post-header-toolbar__inserter-toggle').click({force:true}); - cy.get('.edit-post-editor__inserter-panel-content').then(function ($popup) { - cy.wrap($popup).find('.components-search-control__input').type('feedzy'); + cy.get('div.edit-post-header__toolbar button.editor-document-tools__inserter-toggle').click({force:true}); + cy.get('.editor-inserter-sidebar__content').then(function ($popup) { + cy.wrap($popup).find('.components-search-control input.components-input-control__input').type('feedzy'); cy.wrap($popup).find('.block-editor-block-types-list .editor-block-list-item-feedzy-rss-feeds-feedzy-block').should('have.length', 1); cy.wrap($popup).find('.block-editor-block-types-list .editor-block-list-item-feedzy-rss-feeds-feedzy-block').click({force:true}); }); @@ -62,7 +62,7 @@ describe('Test Free - gutenberg', function() { // item options cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-meta input.components-text-control__input').type(gutenberg.meta, {force: true}); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-multiple-meta input.components-text-control__input').type(gutenberg.multiple_meta, {force: true}); - /* for pro + /* for pro cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-include input.components-text-control__input').type(gutenberg.include); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-ban input.components-text-control__input').type(gutenberg.ban); */ @@ -116,7 +116,7 @@ describe('Test Free - gutenberg', function() { // item options cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-meta input.components-text-control__input').should('have.value', gutenberg.meta); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-multiple-meta input.components-text-control__input').should('have.value', gutenberg.multiple_meta); - /* for pro + /* for pro cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-include input.components-text-control__input').should('have.value', gutenberg.include); cy.get('div.edit-post-sidebar div.components-panel__body.feedzy-advanced-options div.components-base-control.feedzy-ban input.components-text-control__input').should('have.value', gutenberg.ban); */ From 1342715fb329c46428e2221f39c0eba0c352b558 Mon Sep 17 00:00:00 2001 From: GrigoreMihai Date: Wed, 10 Apr 2024 16:41:42 +0300 Subject: [PATCH 13/14] chore: added unit tests --- tests/test-post-access.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test-post-access.php b/tests/test-post-access.php index 15fc1532..59978a24 100644 --- a/tests/test-post-access.php +++ b/tests/test-post-access.php @@ -62,4 +62,23 @@ public function test_custom_post_access() { } + public function test_contributor_user_with_errors() { + $feedzy = new Feedzy_Rss_Feeds_Admin('feedzy', 'latest'); + $contributor_id = $this->factory->user->create( + array( + 'role' => 'contributor', + ) + ); + wp_set_current_user( $contributor_id ); + $post_id = $this->factory->post->create( array( 'post_author' => get_current_user_id() ) ); + $GLOBALS['post'] = get_post( $post_id ); + // Mock feed object and errors. + $feed = (object) array( 'multifeed_url' => array( 'http://example.com/feed' ) ); + $errors = array( 'Error 1', 'Error 2' ); + + + $actual_output = $feedzy->feedzy_default_error_notice( $errors, $feed, 'http://example.com/feed' ); + + $this->assertEquals( '', $actual_output ); + } } From 8e56406b5d85323dbee01cda1ab10632f55072b7 Mon Sep 17 00:00:00 2001 From: GrigoreMihai Date: Wed, 10 Apr 2024 16:50:35 +0300 Subject: [PATCH 14/14] chore: updated tests --- includes/abstract/feedzy-rss-feeds-admin-abstract.php | 2 +- tests/test-post-access.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/abstract/feedzy-rss-feeds-admin-abstract.php b/includes/abstract/feedzy-rss-feeds-admin-abstract.php index 8147aa27..b80d2996 100644 --- a/includes/abstract/feedzy-rss-feeds-admin-abstract.php +++ b/includes/abstract/feedzy-rss-feeds-admin-abstract.php @@ -191,7 +191,7 @@ public function get_usage_data( $data ) { */ public function feedzy_default_error_notice( $errors, $feed, $feed_url ) { global $post; - // Show the error message only if the user who has created this post (which contains the feed) is logged in. + // Show the error message only if the user who has created this post (which contains the feed) is logged in and the user has admin privileges. // Or if this is in the dry run window. // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison $show_error = is_admin() || ( is_user_logged_in() && $post && current_user_can( 'manage_options' ) && get_current_user_id() == $post->post_author ); diff --git a/tests/test-post-access.php b/tests/test-post-access.php index 59978a24..4c019f5c 100644 --- a/tests/test-post-access.php +++ b/tests/test-post-access.php @@ -74,7 +74,7 @@ public function test_contributor_user_with_errors() { $GLOBALS['post'] = get_post( $post_id ); // Mock feed object and errors. $feed = (object) array( 'multifeed_url' => array( 'http://example.com/feed' ) ); - $errors = array( 'Error 1', 'Error 2' ); + $errors = array( 'Error 1' ); $actual_output = $feedzy->feedzy_default_error_notice( $errors, $feed, 'http://example.com/feed' );