Skip to content

Commit

Permalink
PHPCS on test file.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfabian committed Mar 13, 2019
1 parent 0aa9a09 commit 2b8d2cf
Showing 1 changed file with 69 additions and 34 deletions.
103 changes: 69 additions & 34 deletions tests/unit-tests/product/data-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* @package WooCommerce\Tests\Product
* @since 3.0.0
*/

/**
* Class WC_Tests_Product_Data_Store
*/
class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {

/**
Expand Down Expand Up @@ -61,7 +65,7 @@ public function test_product_update() {
$product->set_regular_price( 15 );
$product->save();

// Reread from database
// Reread from database.
$product = new WC_Product( $product->get_id() );

$this->assertEquals( '15', $product->get_regular_price() );
Expand Down Expand Up @@ -132,7 +136,7 @@ public function test_grouped_product_update() {
$product->set_children( $children );
$product->set_name( 'Dummy Grouped Product 2' );
$product->save();
// Reread from database
// Reread from database.
$product = new WC_Product_Grouped( $product->get_id() );
$this->assertEquals( 3, count( $product->get_children() ) );
$this->assertEquals( 'Dummy Grouped Product 2', $product->get_name() );
Expand Down Expand Up @@ -189,7 +193,7 @@ public function test_external_product_update() {
$product->set_regular_price( 15 );
$product->save();

// Reread from database
// Reread from database.
$product = new WC_Product_External( $product->get_id() );

$this->assertEquals( 'Buy my external product', $product->get_button_text() );
Expand All @@ -205,7 +209,7 @@ public function test_variable_read() {
$product = WC_Helper_Product::create_variation_product();
$children = $product->get_children();

// Test sale prices too
// Test sale prices too.
$child = wc_get_product( $children[0] );
$child->set_sale_price( 8 );
$child->save();
Expand Down Expand Up @@ -316,6 +320,9 @@ public function test_variables_and_variations() {
$this->assertEquals( 'Renamed Variable Product', $product->get_name() );
}

/**
* Tests saving variation attribute via set_attribute.
*/
public function test_variation_save_attributes() {
// Create a variable product with a color attribute.
$product = new WC_Product_Variable();
Expand Down Expand Up @@ -350,6 +357,9 @@ public function test_variation_save_attributes() {
$this->assertEquals( 'green', $_attribute['color'] );
}

/**
* Tests for set_default_attributes and get_default_attributes.
*/
public function test_save_default_attributes() {

// Create a variable product with sold individually.
Expand Down Expand Up @@ -414,35 +424,40 @@ public function test_save_default_attributes() {
// string types with a value of '0' making it through filtration.
$test_object = new stdClass();
$test_object->property = '12345';
$product->set_default_attributes( array(
'sample-attribute-0' => 0,
'sample-attribute-1' => false,
'sample-attribute-2' => '',
'sample-attribute-3' => null,
'sample-attribute-4' => '0',
'sample-attribute-5' => 1,
'sample-attribute-6' => 'true',
'sample-attribute-7' => 'false',
'sample-attribute-8' => array( 'exists' => 'false' ),
'sample-attribute-9' => $test_object,
));
$product->set_default_attributes(
array(
'sample-attribute-0' => 0,
'sample-attribute-1' => false,
'sample-attribute-2' => '',
'sample-attribute-3' => null,
'sample-attribute-4' => '0',
'sample-attribute-5' => 1,
'sample-attribute-6' => 'true',
'sample-attribute-7' => 'false',
'sample-attribute-8' => array( 'exists' => 'false' ),
'sample-attribute-9' => $test_object,
)
);
$product->save();
$product_id = $product->get_id();

// Revive the product from the database and analyze results
// Revive the product from the database and analyze results.
$product = wc_get_product( $product_id );
$default_attributes = $product->get_default_attributes();
$this->assertEquals(
array(
'sample-attribute-4' => '0',
'sample-attribute-5' => '1',
'sample-attribute-6' => 'true',
'sample-attribute-7' => 'false',
'sample-attribute-4' => '0',
'sample-attribute-5' => '1',
'sample-attribute-6' => 'true',
'sample-attribute-7' => 'false',
),
$default_attributes
);
}

/**
* Tests Product Variable data store child_has_dimension function when variation has a dimension (width).
*/
public function test_variable_child_has_dimensions() {
$product = new WC_Product_Variable();
$product->save();
Expand All @@ -459,6 +474,9 @@ public function test_variable_child_has_dimensions() {
$this->assertTrue( $store->child_has_dimensions( $product ) );
}

/**
* Tests Product Variable data store child_has_dimension function when variation has no dimensions.
*/
public function test_variable_child_has_dimensions_no_dimensions() {
$product = new WC_Product_Variable();
$product->save();
Expand All @@ -474,6 +492,9 @@ public function test_variable_child_has_dimensions_no_dimensions() {
$this->assertFalse( $store->child_has_dimensions( $product ) );
}

/**
* Tests Product data store get_on_sale_products function.
*/
public function test_get_on_sale_products() {
$product_store = WC_Data_Store::load( 'product' );

Expand All @@ -498,7 +519,7 @@ public function test_get_on_sale_products() {
$variable_draft_product = WC_Helper_Product::create_variation_product();
$variable_draft_product->set_status( 'draft' );
$variable_draft_product->save();
$children = $variable_draft_product->get_children();
$children = $variable_draft_product->get_children();
$variable_draft_product_child = wc_get_product( $children[0] );
$variable_draft_product_child->set_sale_price( 8 );
$variable_draft_product_child->save();
Expand All @@ -513,6 +534,9 @@ public function test_get_on_sale_products() {
$this->assertNotContains( $variable_draft_product_child->get_id(), $sale_product_ids );
}

/**
* Tests automatic generation of variation name.
*/
public function test_generate_product_title() {
$product = new WC_Product();
$product->set_name( 'Test Product' );
Expand All @@ -525,19 +549,23 @@ public function test_generate_product_title() {

$two_attribute_variation = new WC_Product_Variation();
$two_attribute_variation->set_parent_id( $product->get_id() );
$two_attribute_variation->set_attributes( array(
'color' => 'Green',
'size' => 'Large',
) );
$two_attribute_variation->set_attributes(
array(
'color' => 'Green',
'size' => 'Large',
)
);
$two_attribute_variation->save();

$multiword_attribute_variation = new WC_Product_Variation();
$multiword_attribute_variation->set_parent_id( $product->get_id() );
$multiword_attribute_variation->set_attributes( array(
'color' => 'Green',
'mounting-plate' => 'galaxy-s6',
'support' => 'one-year',
) );
$multiword_attribute_variation->set_attributes(
array(
'color' => 'Green',
'mounting-plate' => 'galaxy-s6',
'support' => 'one-year',
)
);
$multiword_attribute_variation->save();

// Check the one attribute variation title.
Expand All @@ -550,6 +578,9 @@ public function test_generate_product_title() {
$this->assertEquals( 'Test Product', $multiword_attribute_variation->get_name() );
}

/**
* Tests disabling of automatic generation of variation name.
*/
public function test_generate_product_title_disable() {
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );

Expand All @@ -566,6 +597,9 @@ public function test_generate_product_title_disable() {
$this->assertEquals( 'Test Product', $loaded_variation->get_name() );
}

/**
* Tests automatic generation of variation name for variation with no attributes.
*/
public function test_generate_product_title_no_attributes() {
$product = new WC_Product();
$product->set_name( 'Test Product' );
Expand All @@ -581,7 +615,7 @@ public function test_generate_product_title_no_attributes() {
}

/**
* Test Product variation attribute_summary prop and its update on data store read.
* Tests Product variation attribute_summary prop and its update on data store read.
*
* @since 3.6.0
* @return void
Expand Down Expand Up @@ -670,6 +704,7 @@ public function test_generate_variation_attribute_summary() {
/**
* Test to make sure meta can still be set while hooked using save_post.
* https://github.com/woocommerce/woocommerce/issues/13960
*
* @since 3.0.1
*/
public function test_product_meta_save_post() {
Expand All @@ -689,7 +724,7 @@ public function test_product_meta_save_post() {
$test2 = get_post_meta( $product->get_id(), '_test2', true );

$this->assertEquals( 'hello', $test );
$this->assertEquals( 'world', $test2 ); // this would be 'default' without the force meta refresh in WC_Product_Data_Store::update();
$this->assertEquals( 'world', $test2 ); // this would be 'default' without the force meta refresh in WC_Product_Data_Store::update().
$this->assertEquals( 'world', $product->get_meta( '_test2' ) );
$this->assertEquals( 'Test Product_', $product->get_name() );

Expand Down Expand Up @@ -733,7 +768,7 @@ public function test_search_products() {

$data_store = WC_Data_Store::load( 'product' );

// Search some things :)
// Search some things :).
$results = $data_store->search_products( 'green', '', true, true );
$this->assertNotContains( $product->get_id(), $results );
$this->assertNotContains( $product2->get_id(), $results );
Expand Down

0 comments on commit 2b8d2cf

Please sign in to comment.