From 291790c64dc935996e9b044945b11822327d86d7 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 24 Jan 2022 13:06:37 -0700 Subject: [PATCH] Refactor handle_upload() rather than change import_error() visibility. --- .../importers/class-wc-tax-rate-importer.php | 34 ++++++++++++++++--- .../tests/legacy/unit-tests/importer/tax.php | 13 ++----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php b/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php index 63251eaf03b..54fd048c8ee 100644 --- a/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php +++ b/plugins/woocommerce/includes/admin/importers/class-wc-tax-rate-importer.php @@ -50,6 +50,13 @@ class WC_Tax_Rate_Importer extends WP_Importer { */ public $delimiter; + /** + * Error message for import. + * + * @var string + */ + public $import_error_message; + /** * Constructor. */ @@ -89,6 +96,8 @@ class WC_Tax_Rate_Importer extends WP_Importer { add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); $this->import( $file ); + } else { + $this->import_error( $this->import_error_message ); } break; } @@ -193,6 +202,15 @@ class WC_Tax_Rate_Importer extends WP_Importer { do_action( 'import_end' ); } + /** + * Set the import error message. + * + * @param string $message Error message. + */ + protected function set_import_error_message( $message ) { + $this->import_error_message = $message; + } + /** * Handles the CSV upload and initial parsing of the file to prepare for. * displaying author import options. @@ -206,14 +224,18 @@ class WC_Tax_Rate_Importer extends WP_Importer { $file = wp_import_handle_upload(); if ( isset( $file['error'] ) ) { - $this->import_error( $file['error'] ); + $this->set_import_error_message( $file['error'] ); + + return false; } if ( ! wc_is_file_valid_csv( $file['file'], false ) ) { // Remove file if not valid. wp_delete_attachment( $file['id'], true ); - $this->import_error( __( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) ); + $this->set_import_error_message( __( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) ); + + return false; } $this->id = absint( $file['id'] ); @@ -222,12 +244,14 @@ class WC_Tax_Rate_Importer extends WP_Importer { file_exists( ABSPATH . $file_url ) ) { if ( ! wc_is_file_valid_csv( ABSPATH . $file_url ) ) { - $this->import_error( __( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) ); + $this->set_import_error_message( __( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) ); + + return false; } $this->file_url = esc_attr( $file_url ); } else { - $this->import_error(); + return false; } return true; @@ -322,7 +346,7 @@ class WC_Tax_Rate_Importer extends WP_Importer { * * @param string $message Error message. */ - public function import_error( $message = '' ) { + private function import_error( $message = '' ) { echo '

' . esc_html__( 'Sorry, there has been an error.', 'woocommerce' ) . '
'; if ( $message ) { echo esc_html( $message ); diff --git a/plugins/woocommerce/tests/legacy/unit-tests/importer/tax.php b/plugins/woocommerce/tests/legacy/unit-tests/importer/tax.php index 5089f495664..5ecccbe4f13 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/importer/tax.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/importer/tax.php @@ -44,18 +44,11 @@ class WC_Tests_Tax_CSV_Importer extends WC_Unit_Test_Case { * Test that directory traversal is prevented. */ public function test_import_path_traversal() { - $importer = - $this->getMockBuilder( WC_Tax_Rate_Importer::class ) - ->setMethods( array( 'import_error' ) ) - ->getMock(); - - $importer - ->expects( $this->once() ) - ->method( 'import_error' ) - ->willReturn( false ); + $importer = new WC_Tax_Rate_Importer(); $_POST['file_url'] = '../sample_tax_rates.csv'; - $importer->handle_upload(); + $this->assertFalse( $importer->handle_upload() ); + $this->assertEquals( '', $importer->import_error_message ); } }