From 156b0616020ef33d30f4146fbb59e7118ac7f1aa Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 15 Jul 2020 21:49:26 -0300 Subject: [PATCH] Added unit tests for importer and exporter --- .../class-wc-product-csv-exporter-test.php | 66 +++++++++++++++++++ .../class-wc-product-csv-importer-test.php | 64 ++++++++++++++++++ tests/php/includes/importer/sample.csv | 8 +++ 3 files changed, 138 insertions(+) create mode 100644 tests/php/includes/exporter/class-wc-product-csv-exporter-test.php create mode 100644 tests/php/includes/importer/class-wc-product-csv-importer-test.php create mode 100644 tests/php/includes/importer/sample.csv diff --git a/tests/php/includes/exporter/class-wc-product-csv-exporter-test.php b/tests/php/includes/exporter/class-wc-product-csv-exporter-test.php new file mode 100644 index 00000000000..c2ac4469260 --- /dev/null +++ b/tests/php/includes/exporter/class-wc-product-csv-exporter-test.php @@ -0,0 +1,66 @@ +plugin_dir . '/includes/export/class-wc-product-csv-exporter.php'; + } + + /** + * Helper to set product export query args. + * + * @param array $args Query args. + * @return array + */ + public function set_export_product_query_args( $args ) { + $args['include'] = $this->product_ids; + return $args; + } + + /** + * @testdox variations should use draft status from parent product + */ + public function test_get_column_value_published() { + $product = WC_Helper_Product::create_variation_product(); + $product->set_status( 'draft' ); + $product->save(); + + $reflected_exporter = new ReflectionClass( WC_Product_CSV_Exporter::class ); + $get_data_to_export = $reflected_exporter->getMethod( 'get_data_to_export' ); + $get_data_to_export->setAccessible( true ); + + $this->product_ids = array_merge( array( $product->get_id() ), $product->get_children( 'edit' ) ); + + add_filter( 'woocommerce_product_export_product_query_args', array( $this, 'set_export_product_query_args' ) ); + $exporter = new WC_Product_CSV_Exporter(); + $exporter->prepare_data_to_export(); + $data = $get_data_to_export->invoke( $exporter ); + + foreach ( $data as $row ) { + $this->assertEquals( -1, $row['published'] ); + } + remove_filter( 'woocommerce_product_export_product_query_args', array( $this, 'set_export_product_query_args' ) ); + } + +} diff --git a/tests/php/includes/importer/class-wc-product-csv-importer-test.php b/tests/php/includes/importer/class-wc-product-csv-importer-test.php new file mode 100644 index 00000000000..d2fbc9d07b3 --- /dev/null +++ b/tests/php/includes/importer/class-wc-product-csv-importer-test.php @@ -0,0 +1,64 @@ +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'; + } + + /** + * @testdox variations need to set the status back to published if parent product is a draft + */ + public function test_expand_data_with_draft_variable() { + $csv_file = dirname( __FILE__ ) . '/sample.csv'; + $raw_data = array( + array( + 'type' => 'variable', + 'published' => -1, + ), + array( + 'type' => 'variation', + 'published' => -1, + ), + ); + + $reflected_importer = new ReflectionClass( WC_Product_CSV_Importer::class ); + $expand_data = $reflected_importer->getMethod( 'expand_data' ); + $expand_data->setAccessible( true ); + + $importer = new WC_Product_CSV_Importer( $csv_file ); + $variable = $expand_data->invoke( + $importer, + array( + 'type' => array( 'variable' ), + 'published' => -1, + ) + ); + $variation = $expand_data->invoke( + $importer, + array( + 'type' => array( 'variation' ), + 'published' => -1, + ) + ); + + $this->assertEquals( 'draft', $variable['status'] ); + $this->assertEquals( 'publish', $variation['status'] ); + } + +} diff --git a/tests/php/includes/importer/sample.csv b/tests/php/includes/importer/sample.csv new file mode 100644 index 00000000000..00b323feaf3 --- /dev/null +++ b/tests/php/includes/importer/sample.csv @@ -0,0 +1,8 @@ +Type,SKU,Name,Published,Is featured?,Visibility in catalog,Short description,Description,Date sale price starts,Date sale price ends,Tax status,Tax class,In stock?,Stock,Backorders allowed?,Sold individually?,Weight (kg),Length (cm),Width (cm),Height (cm),Allow customer reviews?,Purchase note,Sale price,Regular price,Categories,Tags,Shipping class,Images,Download limit,Download expiry days,Parent,Upsells,Cross-sells,Grouped products,External URL,BUTTON TEXT,Position,Attribute 1 Name,Attribute 1 Value(s),Attribute 2 name,Attribute 2 value(s),Attribute 1 default,Attribute 2 default,Download 1 name,Download 1 URL +simple,WOOLOGO,Woo Logo,1,,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",2017-01-01,2030-01-01 0:00:00,taxable,standard,1,5,notify,1,1,1,20,40,1,Lorem ipsum dolor sit amet.,18,20,"Clothing, Clothing > T-shirts",,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_back.jpg",,,,WOOALBUM,WOOALBUM,,,,0,Color,Red,,,,,, +"simple, downloadable, virtual",WOOALBUM,Woo Album #1,1,1,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,,,,,,,,1,Lorem ipsum dolor sit amet.,,5,"Music > Albums, Music",Woo,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_flat.jpg",10,90,,WOOLOGO,WOOLOGO,,,,1,Label,WooCommerce,Vinyl,180-Gram,,,Album flac,http://woo.dev/albums/album.flac +external,,WooCommerce Product CSV Suite,1,,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,,,,,,,,0,Lorem ipsum dolor sit amet.,,199,Software,WooCommerce,,,,,,,,,https://woocommerce.com/products/product-csv-import-suite/,Buy on WooCommerce.com,2,,,,,,,, +variable,WOOIDEA,Ship Your Idea,1,,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,,,,,,,,,,,1,Lorem ipsum dolor sit amet.,,,"Clothing, Clothing > T-shirts",,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg",,,,,,,,,3,Color,"Black, Green",Size,"M, L",Green,L,, +variation,,,1,,visible,,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,6,0,,1,2,25,55,,,,20,,,,http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg,,,WOOIDEA,,,,,,1,Color,Black,Size,M,,,, +variation,,,1,,visible,,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,taxable,standard,1,10,1,,1,2,25,55,,,17.99,20,,,,http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg,,,WOOIDEA,,,,,,2,Color,Green,Size,L,,,, +grouped,,Best Woo Products,1,1,visible,"Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.","Lorem ipsum dolor sit amet, at exerci civibus appetere sit, iuvaret hendrerit mea no. Eam integre feugait liberavisse an.",,,,,1,,,,,,,,,,,,"Clothing, Clothing > T-shirts, Music > Albums, Music",,,"http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_1_front.jpg, http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_1_angle.jpg",,,,,,"WOOLOGO, WOOALBUM",,,4,,,,,,,,