Merge pull request #26519 from woocommerce/fix/csv-import-special-columns

Fixed the case conversion for meta key column mapping in product CSV imports
This commit is contained in:
Claudio Sanches 2020-05-18 11:33:50 -03:00 committed by GitHub
commit eea062f9fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 10 deletions

View File

@ -45,7 +45,7 @@
},
"autoload-dev": {
"psr-4": {
"Automattic\\WooCommerce\\Tests\\": "tests/php/"
"Automattic\\WooCommerce\\Tests\\": "tests/php/src"
}
},
"scripts": {

View File

@ -319,6 +319,7 @@ class WC_Product_CSV_Importer_Controller {
return new WP_Error( 'woocommerce_product_csv_importer_upload_file_empty', __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.', 'woocommerce' ) );
}
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
if ( ! self::is_file_valid_csv( wc_clean( wp_unslash( $_FILES['import']['name'] ) ), false ) ) {
return new WP_Error( 'woocommerce_product_csv_importer_upload_file_invalid', __( 'Invalid file type. The importer supports CSV and TXT file formats.', 'woocommerce' ) );
}
@ -327,7 +328,7 @@ class WC_Product_CSV_Importer_Controller {
'test_form' => false,
'mimes' => self::get_valid_csv_filetypes(),
);
$import = $_FILES['import']; // WPCS: sanitization ok, input var ok.
$import = $_FILES['import']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$upload = wp_handle_upload( $import, $overrides );
if ( isset( $upload['error'] ) ) {
@ -577,14 +578,15 @@ class WC_Product_CSV_Importer_Controller {
$headers = array();
foreach ( $raw_headers as $key => $field ) {
$field = strtolower( $field );
$normalized_field = strtolower( $field );
$index = $num_indexes ? $key : $field;
$headers[ $index ] = $field;
$headers[ $index ] = $normalized_field;
if ( isset( $default_columns[ $field ] ) ) {
$headers[ $index ] = $default_columns[ $field ];
if ( isset( $default_columns[ $normalized_field ] ) ) {
$headers[ $index ] = $default_columns[ $normalized_field ];
} else {
foreach ( $special_columns as $regex => $special_key ) {
// Don't use the normalized field in the regex since meta might be case-sensitive.
if ( preg_match( $regex, $field, $matches ) ) {
$headers[ $index ] = $special_key . $matches[1];
break;
@ -619,7 +621,7 @@ class WC_Product_CSV_Importer_Controller {
* @return string
*/
protected function sanitize_special_column_name_regex( $value ) {
return '/' . str_replace( array( '%d', '%s' ), '(.*)', trim( quotemeta( $value ) ) ) . '/';
return '/' . str_replace( array( '%d', '%s' ), '(.*)', trim( quotemeta( $value ) ) ) . '/i';
}
/**

View File

@ -5,12 +5,10 @@
* @package WooCommerce|Tests|WC_Helper.
*/
namespace Automattic\WooCommerce;
/**
* Class WC_Tests_WC_Helper.
*/
class WCHelperTest extends \WC_Unit_Test_Case {
class WC_Helper_Test extends \WC_Unit_Test_Case {
/**
* Test that woo plugins are loaded correctly even if incorrect cache is intially set.

View File

@ -0,0 +1,64 @@
<?php
/**
* Class WC_Product_CSV_Importer_Controller_Test
* @package WooCommerce\Tests\Admin
*
* Tests to ensure that the CSV product importer works as expected.
*/
/**
* Class WC_Product_CSV_Importer_Controller_Test
*/
class WC_Product_CSV_Importer_Controller_Test extends WC_Unit_Test_Case {
/**
* Load up the importer classes since they aren't loaded by default.
*/
public function setUp() {
parent::setUp();
$bootstrap = WC_Unit_Tests_Bootstrap::instance();
require_once $bootstrap->plugin_dir . '/includes/import/class-wc-product-csv-importer.php';
require_once $bootstrap->plugin_dir . '/includes/admin/importers/class-wc-product-csv-importer-controller.php';
}
/**
* Tests that the automatic mapping is case insensitive so that columns can be matched more easily.
*/
public function test_that_auto_mapping_is_case_insensitive() {
// Allow us to call the protected method.
$class = new ReflectionClass( WC_Product_CSV_Importer_Controller::class );
$method = $class->getMethod( 'auto_map_columns' );
$method->setAccessible( true );
$controller = new WC_Product_CSV_Importer_Controller();
// Test a few different casing formats first.
$columns = $method->invoke( $controller, array( 'Name', 'Type' ) );
$this->assertEquals(
array(
0 => 'name',
1 => 'type',
),
$columns
);
$columns = $method->invoke( $controller, array( 'NAME', 'tYpE' ) );
$this->assertEquals(
array(
0 => 'name',
1 => 'type',
),
$columns
);
// Make sure that the case sensitivity doesn't squash the meta keys.
$columns = $method->invoke( $controller, array( 'Meta: _TESTING', 'Meta: _testing' ) );
$this->assertEquals(
array(
0 => 'meta:_TESTING',
1 => 'meta:_testing',
),
$columns
);
}
}