From f408152bb954bc2c35cc0bac1fad0b9b8f544204 Mon Sep 17 00:00:00 2001 From: thrijith Date: Fri, 12 Mar 2021 22:54:46 +0530 Subject: [PATCH 1/2] Update hooks priority for `wp_generate_attachment_metadata` to work with azure storage Move `classifai_generate_image_alt_tags_source_url` filter to helper function Use `get_modified_image_source_url` where rescanning is done local file --- includes/Classifai/Helpers.php | 26 ++++++++++ .../Providers/Azure/ComputerVision.php | 47 ++++++++++++------- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index e29ebdc00..786791eb9 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -483,3 +483,29 @@ function get_largest_size_and_dimensions_image_url( $full_image, $full_url, $met return null; } + +/** + * Allows returning modified image URL for a given attachment. + * + * @param int $post_id Post ID. + * + * @return mixed + */ +function get_modified_image_source_url( $post_id ) { + /** + * Filter to modify image source URL in order to allow scanning images, + * stored on third party storages that cannot be used by + * helper function `get_largest_acceptable_image_url()` to determine `filesize()` locally. + * + * Default is null, return filtered string to allow classifying image on external source. + * + * @since 1.6.0 + * @hook classifai_generate_image_alt_tags_source_url + * + * @param {mixed} $image_url New image path for given attachment ID. + * @param {int} $post_id The ID of the attachment to be used in classification. + * + * @return {mixed} NULL or filtered URl for given attachment id. + */ + return apply_filters( 'classifai_generate_image_alt_tags_source_url', null, $post_id ); +} diff --git a/includes/Classifai/Providers/Azure/ComputerVision.php b/includes/Classifai/Providers/Azure/ComputerVision.php index 2f0e133f7..f9da05c60 100644 --- a/includes/Classifai/Providers/Azure/ComputerVision.php +++ b/includes/Classifai/Providers/Azure/ComputerVision.php @@ -11,6 +11,7 @@ use function Classifai\computer_vision_max_filesize; use function Classifai\get_largest_acceptable_image_url; +use function Classifai\get_modified_image_source_url; class ComputerVision extends Provider { @@ -64,8 +65,8 @@ public function register() { add_action( 'add_meta_boxes_attachment', [ $this, 'setup_attachment_meta_box' ] ); add_action( 'edit_attachment', [ $this, 'maybe_rescan_image' ] ); add_filter( 'posts_clauses', [ $this, 'filter_attachment_query_keywords' ], 10, 1 ); - add_filter( 'wp_generate_attachment_metadata', [ $this, 'smart_crop_image' ], 8, 2 ); - add_filter( 'wp_generate_attachment_metadata', [ $this, 'generate_image_alt_tags' ], 10, 2 ); + add_filter( 'wp_generate_attachment_metadata', [ $this, 'smart_crop_image' ], 7, 2 ); + add_filter( 'wp_generate_attachment_metadata', [ $this, 'generate_image_alt_tags' ], 8, 2 ); add_filter( 'posts_clauses', [ $this, 'filter_attachment_query_keywords' ], 10, 1 ); $settings = $this->get_settings(); @@ -233,12 +234,18 @@ public function attachment_data_meta_box( $post ) { public function maybe_rescan_image( $attachment_id ) { $routes = []; $metadata = wp_get_attachment_metadata( $attachment_id ); - $image_url = get_largest_acceptable_image_url( - get_attached_file( $attachment_id ), - wp_get_attachment_url( $attachment_id ), - $metadata['sizes'], - computer_vision_max_filesize() - ); + + // Allow rescanning image that are not stored in local storage. + $image_url = get_modified_image_source_url( $attachment_id ); + + if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { + $image_url = get_largest_acceptable_image_url( + get_attached_file( $attachment_id ), + wp_get_attachment_url( $attachment_id ), + $metadata['sizes'], + computer_vision_max_filesize() + ); + } if ( filter_input( INPUT_POST, 'rescan-captions' ) ) { $routes[] = 'alt-tags'; @@ -341,18 +348,19 @@ public function generate_image_alt_tags( $metadata, $attachment_id ) { 'no' !== $settings['enable_image_captions'] ) { - $image_url = apply_filters( 'classifai_generate_image_alt_tags_source_url', null, $attachment_id ); + // Allow scanning image that are not stored in local storage. + $image_url = get_modified_image_source_url( $attachment_id ); if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) { $image_url = get_largest_acceptable_image_url( get_attached_file( $attachment_id ), - wp_get_attachment_url( $attachment_id, 'full' ), + wp_get_attachment_url( $attachment_id ), $metadata['sizes'], computer_vision_max_filesize() ); } else { - $image_url = wp_get_attachment_url( $attachment_id, 'full' ); + $image_url = wp_get_attachment_url( $attachment_id ); } } @@ -932,12 +940,17 @@ public function rest_endpoint_callback( $post_id, $route_to_call ) { return $this->ocr_processing( $metadata, $post_id, true ); } - $image_url = get_largest_acceptable_image_url( - get_attached_file( $post_id ), - wp_get_attachment_url( $post_id ), - $metadata['sizes'], - computer_vision_max_filesize() - ); + // Allow rescanning image that are not stored in local storage. + $image_url = get_modified_image_source_url( $post_id ); + + if ( empty( $image_url ) || ! filter_var( $image_url, FILTER_VALIDATE_URL ) ) { + $image_url = get_largest_acceptable_image_url( + get_attached_file( $post_id ), + wp_get_attachment_url( $post_id ), + $metadata['sizes'], + computer_vision_max_filesize() + ); + } if ( empty( $image_url ) ) { return new WP_Error( 'error', esc_html__( 'Valid image size not found. Make sure the image is less than 4MB.' ) ); From 30b0b09361aa81c2298ae56b0d038e680cf39456 Mon Sep 17 00:00:00 2001 From: thrijith Date: Fri, 19 Mar 2021 03:30:07 +0530 Subject: [PATCH 2/2] Fix test case assertion and allow having tests with empty assertions --- tests/Classifai/PostClassifierTest.php | 15 +++++++++++++++ .../Providers/Azure/ComputerVisionTest.php | 5 ++--- tests/Classifai/Watson/APIRequestTest.php | 15 +++++++++++++++ tests/Classifai/Watson/ClassifierTest.php | 4 ++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tests/Classifai/PostClassifierTest.php b/tests/Classifai/PostClassifierTest.php index b75c84d7b..654cd77fa 100644 --- a/tests/Classifai/PostClassifierTest.php +++ b/tests/Classifai/PostClassifierTest.php @@ -125,6 +125,8 @@ function test_it_can_link_post() { } function test_it_can_classify_post() { + $this->test_can_have_empty_assertion(); + if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) { $text = 'The quick brown fox jumps over the lazy dog.'; $post_id = $this->factory->post->create( [ @@ -139,6 +141,8 @@ function test_it_can_classify_post() { } function test_it_can_classify_and_link_post() { + $this->test_can_have_empty_assertion(); + if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) { $text = <<test_can_have_empty_assertion(); + if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) { $text = <<assertEmpty( $actual ); } } + + /** + * Set test to not perform assertion to fix risky tests. + */ + public function test_can_have_empty_assertion() { + if ( ! defined( 'WATSON_USERNAME' ) && ! defined( 'WATSON_PASSWORD' ) ) { + $this->expectNotToPerformAssertions(); + } + } } diff --git a/tests/Classifai/Providers/Azure/ComputerVisionTest.php b/tests/Classifai/Providers/Azure/ComputerVisionTest.php index 8058da429..b9fe75f3f 100644 --- a/tests/Classifai/Providers/Azure/ComputerVisionTest.php +++ b/tests/Classifai/Providers/Azure/ComputerVisionTest.php @@ -94,9 +94,8 @@ public function test_set_image_meta_data() { // Instantiate the hooks $this->get_computer_vision()->register(); - $attachment = $this->factory->attachment->create_and_get(); - wp_generate_attachment_metadata( $attachment->ID, DIR_TESTDATA .'/images/33772.jpg' ); - $meta = wp_get_attachment_metadata( $attachment->ID ); + $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/33772.jpg' ); + $meta = wp_get_attachment_metadata( $attachment ); $this->assertNotFalse( $meta ); } } diff --git a/tests/Classifai/Watson/APIRequestTest.php b/tests/Classifai/Watson/APIRequestTest.php index f3a327458..cbca881d5 100644 --- a/tests/Classifai/Watson/APIRequestTest.php +++ b/tests/Classifai/Watson/APIRequestTest.php @@ -19,6 +19,8 @@ function test_it_uses_local_username_if_present() { } function test_it_uses_constant_username_if_present() { + $this->test_can_have_empty_assertion(); + if ( defined( 'WATSON_USERNAME' ) ) { $actual = $this->request->get_username(); $this->assertEquals( WATSON_USERNAME, $actual ); @@ -38,6 +40,8 @@ function test_it_uses_local_password_if_present() { } function test_it_constant_password_if_present() { + $this->test_can_have_empty_assertion(); + if ( defined( 'WATSON_PASSWORD' ) ) { $actual = $this->request->get_password(); $this->assertEquals( WATSON_PASSWORD, $actual ); @@ -81,6 +85,8 @@ function test_it_can_add_auth_header() { } function test_it_can_make_an_api_request() { + $this->test_can_have_empty_assertion(); + if ( defined( 'WATSON_USERNAME' ) && defined( 'WATSON_PASSWORD' ) ) { $url = 'https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27'; $options = [ @@ -101,4 +107,13 @@ function test_it_can_make_an_api_request() { } } + /** + * Set test to not perform assertion to fix risky tests. + */ + public function test_can_have_empty_assertion() { + if ( ! defined( 'WATSON_USERNAME' ) && ! defined( 'WATSON_PASSWORD' ) ) { + $this->expectNotToPerformAssertions(); + } + } + } diff --git a/tests/Classifai/Watson/ClassifierTest.php b/tests/Classifai/Watson/ClassifierTest.php index bcea74e9c..f62d029d9 100644 --- a/tests/Classifai/Watson/ClassifierTest.php +++ b/tests/Classifai/Watson/ClassifierTest.php @@ -32,6 +32,10 @@ function test_it_has_a_default_request_body() { } function test_it_can_classify_text() { + if ( ! defined( 'WATSON_USERNAME' ) && ! defined( 'WATSON_PASSWORD' ) ) { + $this->expectNotToPerformAssertions(); + } + if ( defined( 'WATSON_USERNAME' ) && ! empty( WATSON_USERNAME ) && defined( 'WATSON_PASSWORD' ) && ! empty( WATSON_PASSWORD ) ) { $text = 'The quick brown fox jumps over the lazy dog.'; $actual = $this->classifier->classify( $text );