Refactor handle_upload() rather than change import_error() visibility.

This commit is contained in:
Jeff Stieler 2022-01-24 13:06:37 -07:00 committed by Claudio Sanches
parent c85f3b7e1d
commit 291790c64d
2 changed files with 32 additions and 15 deletions

View File

@ -50,6 +50,13 @@ class WC_Tax_Rate_Importer extends WP_Importer {
*/ */
public $delimiter; public $delimiter;
/**
* Error message for import.
*
* @var string
*/
public $import_error_message;
/** /**
* Constructor. * Constructor.
*/ */
@ -89,6 +96,8 @@ class WC_Tax_Rate_Importer extends WP_Importer {
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
$this->import( $file ); $this->import( $file );
} else {
$this->import_error( $this->import_error_message );
} }
break; break;
} }
@ -193,6 +202,15 @@ class WC_Tax_Rate_Importer extends WP_Importer {
do_action( 'import_end' ); 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. * Handles the CSV upload and initial parsing of the file to prepare for.
* displaying author import options. * displaying author import options.
@ -206,14 +224,18 @@ class WC_Tax_Rate_Importer extends WP_Importer {
$file = wp_import_handle_upload(); $file = wp_import_handle_upload();
if ( isset( $file['error'] ) ) { 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 ) ) { if ( ! wc_is_file_valid_csv( $file['file'], false ) ) {
// Remove file if not valid. // Remove file if not valid.
wp_delete_attachment( $file['id'], true ); 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'] ); $this->id = absint( $file['id'] );
@ -222,12 +244,14 @@ class WC_Tax_Rate_Importer extends WP_Importer {
file_exists( ABSPATH . $file_url ) file_exists( ABSPATH . $file_url )
) { ) {
if ( ! wc_is_file_valid_csv( 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 ); $this->file_url = esc_attr( $file_url );
} else { } else {
$this->import_error(); return false;
} }
return true; return true;
@ -322,7 +346,7 @@ class WC_Tax_Rate_Importer extends WP_Importer {
* *
* @param string $message Error message. * @param string $message Error message.
*/ */
public function import_error( $message = '' ) { private function import_error( $message = '' ) {
echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />'; echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'woocommerce' ) . '</strong><br />';
if ( $message ) { if ( $message ) {
echo esc_html( $message ); echo esc_html( $message );

View File

@ -44,18 +44,11 @@ class WC_Tests_Tax_CSV_Importer extends WC_Unit_Test_Case {
* Test that directory traversal is prevented. * Test that directory traversal is prevented.
*/ */
public function test_import_path_traversal() { public function test_import_path_traversal() {
$importer = $importer = new WC_Tax_Rate_Importer();
$this->getMockBuilder( WC_Tax_Rate_Importer::class )
->setMethods( array( 'import_error' ) )
->getMock();
$importer
->expects( $this->once() )
->method( 'import_error' )
->willReturn( false );
$_POST['file_url'] = '../sample_tax_rates.csv'; $_POST['file_url'] = '../sample_tax_rates.csv';
$importer->handle_upload(); $this->assertFalse( $importer->handle_upload() );
$this->assertEquals( '', $importer->import_error_message );
} }
} }