diff --git a/tests/unit-tests/product/data.php b/tests/unit-tests/product/data.php index 5b96b20502c..2bcb7f24c97 100644 --- a/tests/unit-tests/product/data.php +++ b/tests/unit-tests/product/data.php @@ -267,4 +267,81 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case { $this->assertEquals( $product3_id, $product->get_id() ); $this->assertEquals( '£50.00', $product->get_price_html() ); } + + public function test_get_image_should_return_product_image() { + $product = new WC_Product(); + $image_url = $this->set_product_image( $product ); + + $this->assertEquals( + '', + $product->get_image() + ); + + $this->assertEquals( + '', + $product->get_image( 'single' ) + ); + + $this->assertEquals( + '', + $product->get_image( 'single', array( 'class' => 'custom-class' ) ) + ); + } + + public function test_get_image_should_return_parent_product_image() { + $variable_product = WC_Helper_Product::create_variation_product(); + $variations = $variable_product->get_children(); + $variation_1 = wc_get_product( $variations[0] ); + $image_url = $this->set_product_image( $variable_product ); + + $this->assertEquals( + '', + $variation_1->get_image() + ); + + $this->assertEquals( + '', + $variation_1->get_image( 'single' ) + ); + + $this->assertEquals( + '', + $variation_1->get_image( 'single', array( 'class' => 'custom-class' ) ) + ); + } + + public function test_get_image_should_return_place_holder_image() { + $product = new WC_Product(); + $image_url = wc_placeholder_img_src(); + $expected_result = 'Placeholder'; + + $this->assertEquals( $expected_result, $product->get_image() ); + $this->assertEquals( $expected_result, $product->get_image( 'woocommerce_thumbnail', array(), true ) ); + } + + public function test_get_image_should_return_empty_string() { + $product = new WC_Product(); + $this->assertEquals( '', $product->get_image( 'woocommerce_thumbnail', array(), false ) ); + } + + /** + * Helper method to define a image for a product and return its URL. + * + * @param WC_Product $product Product object. + * @return string image URL. + */ + protected function set_product_image( $product ) { + global $wpdb; + + // TODO: find a way to set the product image without performing a HTTP request to make the tests faster. + $image_url = media_sideload_image( 'http://cldup.com/Dr1Bczxq4q.png', $product->get_id(), '', 'src' ); + + $this->assertNotWPError( $image_url ); + + $image_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid = %s", $image_url ) ); + $product->set_image_id( $image_id ); + $product->save(); + + return $image_url; + } }