woocommerce/tests/unit-tests/importer/product.php

196 lines
4.4 KiB
PHP
Raw Normal View History

2017-05-04 21:20:59 +00:00
<?php
/**
* Meta
* @package WooCommerce\Tests\Importer
*/
2017-05-15 22:49:53 +00:00
class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
/**
* Test CSV file path.
*
* @var string
*/
protected $csv_file = string;
2017-05-04 21:20:59 +00:00
/**
* Load up the importer classes since they aren't loaded by default.
*/
public function setUp() {
2017-05-15 22:49:53 +00:00
$this->csv_file = dirname( __FILE__ ) . '/sample.csv';
2017-05-04 21:20:59 +00:00
$bootstrap = WC_Unit_Tests_Bootstrap::instance();
2017-05-15 22:49:53 +00:00
require_once $bootstrap->plugin_dir . '/includes/import/class-wc-product-csv-importer.php';
2017-05-04 21:20:59 +00:00
}
/**
2017-05-15 22:49:53 +00:00
* Test import.
2017-05-15 23:43:53 +00:00
* @todo enable the importer again after conclude the parser.
2017-05-04 21:20:59 +00:00
* @since 3.1.0
*/
2017-05-15 22:49:53 +00:00
public function test_import() {
2017-05-16 04:42:55 +00:00
$mapped = array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
2017-05-19 22:18:09 +00:00
'Published' => 'published',
2017-05-16 04:42:55 +00:00
'Regular price' => 'regular_price',
);
2017-05-04 21:20:59 +00:00
2017-05-16 04:42:55 +00:00
$args = array(
'mapping' => $mapped,
'parse' => true,
2017-05-15 22:49:53 +00:00
);
2017-05-04 21:20:59 +00:00
2017-05-16 04:42:55 +00:00
$importer = new WC_Product_CSV_Importer( $this->csv_file, $args );
$results = $importer->import();
$this->assertEquals( 3, count( $results['imported'] ) );
$this->assertEquals( 0, count( $results['failed'] ) );
// Exclude imported products.
foreach ( $results['imported'] as $id ) {
wp_delete_post( $id );
}
2017-05-04 21:20:59 +00:00
}
/**
2017-05-15 22:49:53 +00:00
* Test get_raw_keys.
2017-05-04 21:20:59 +00:00
* @since 3.1.0
*/
2017-05-15 22:49:53 +00:00
public function test_get_raw_keys() {
$importer = new WC_Product_CSV_Importer( $this->csv_file );
$raw_keys = array(
'Type',
'SKU',
'Name',
2017-05-19 22:18:09 +00:00
'Published',
2017-05-15 22:49:53 +00:00
'Regular price',
);
$this->assertEquals( $raw_keys, $importer->get_raw_keys() );
2017-05-04 21:20:59 +00:00
}
/**
2017-05-15 22:49:53 +00:00
* Test get_mapped_keys.
2017-05-04 21:20:59 +00:00
* @since 3.1.0
*/
2017-05-15 22:49:53 +00:00
public function test_get_mapped_keys() {
$mapped = array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
2017-05-19 22:18:09 +00:00
'Published' => 'published',
2017-05-15 22:49:53 +00:00
'Regular price' => 'regular_price',
);
$args = array(
'mapping' => $mapped,
);
2017-05-04 21:20:59 +00:00
2017-05-15 22:49:53 +00:00
$importer = new WC_Product_CSV_Importer( $this->csv_file, $args );
2017-05-04 21:20:59 +00:00
2017-05-15 22:49:53 +00:00
$this->assertEquals( array_values( $mapped ), $importer->get_mapped_keys() );
2017-05-04 21:20:59 +00:00
}
/**
2017-05-15 22:49:53 +00:00
* Test get_raw_data.
2017-05-04 21:20:59 +00:00
* @since 3.1.0
*/
2017-05-15 22:49:53 +00:00
public function test_get_raw_data() {
$importer = new WC_Product_CSV_Importer( $this->csv_file, array( 'parse' => false ) );
$items = array(
2017-05-04 21:20:59 +00:00
array(
2017-05-16 04:42:55 +00:00
'simple',
2017-05-15 22:49:53 +00:00
'PRODUCT-01',
'Imported Product 1',
1,
40,
2017-05-04 21:20:59 +00:00
),
array(
2017-05-16 04:42:55 +00:00
'simple',
2017-05-15 22:49:53 +00:00
'PRODUCT-02',
'Imported Product 2',
1,
41,
2017-05-04 21:20:59 +00:00
),
array(
2017-05-16 04:42:55 +00:00
'simple',
2017-05-15 22:49:53 +00:00
'PRODUCT-03',
'Imported Product 3',
1,
42,
2017-05-04 21:20:59 +00:00
),
);
2017-05-15 22:49:53 +00:00
$this->assertEquals( $items, $importer->get_raw_data() );
2017-05-04 21:20:59 +00:00
}
/**
2017-05-15 22:49:53 +00:00
* Test get_parsed_data.
2017-05-04 21:20:59 +00:00
* @since 3.1.0
*/
2017-05-15 22:49:53 +00:00
public function test_get_parsed_data() {
$mapped = array(
'Type' => 'type',
'SKU' => 'sku',
'Name' => 'name',
2017-05-19 22:18:09 +00:00
'Published' => 'published',
2017-05-15 22:49:53 +00:00
'Regular price' => 'regular_price',
2017-05-04 21:20:59 +00:00
);
2017-05-15 22:49:53 +00:00
$args = array(
'mapping' => $mapped,
'parse' => true,
);
$importer = new WC_Product_CSV_Importer( $this->csv_file, $args );
$items = array(
array(
2017-05-19 22:18:09 +00:00
'type' => 'simple',
'sku' => 'PRODUCT-01',
'name' => 'Imported Product 1',
'published' => 1,
'regular_price' => '40',
'meta_data' => array(),
'attributes' => array(),
'default_attributes' => array(),
'downloads' => array(),
'virtual' => false,
'downloadable' => false,
'gallery_image_ids' => array(),
2017-05-15 22:49:53 +00:00
),
2017-05-04 21:20:59 +00:00
array(
2017-05-19 22:18:09 +00:00
'type' => 'simple',
'sku' => 'PRODUCT-02',
'name' => 'Imported Product 2',
'published' => 1,
'regular_price' => '41',
'meta_data' => array(),
'attributes' => array(),
'default_attributes' => array(),
'downloads' => array(),
'virtual' => false,
'downloadable' => false,
'gallery_image_ids' => array(),
2017-05-04 21:20:59 +00:00
),
array(
2017-05-19 22:18:09 +00:00
'type' => 'simple',
'sku' => 'PRODUCT-03',
'name' => 'Imported Product 3',
'published' => 1,
'regular_price' => '42',
'meta_data' => array(),
'attributes' => array(),
'default_attributes' => array(),
'downloads' => array(),
'virtual' => false,
'downloadable' => false,
'gallery_image_ids' => array(),
2017-05-04 21:20:59 +00:00
),
);
2017-05-15 22:49:53 +00:00
$this->assertEquals( $items, $importer->get_parsed_data() );
2017-05-04 21:20:59 +00:00
}
}