Skip to content

Commit

Permalink
Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Sep 23, 2024
1 parent e8cabaa commit 6a9a4dc
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions includes/api/class-wc-gzd-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function rest_api_includes() {
include_once dirname( __FILE__ ) . '/class-wc-gzd-rest-nutrients-controller.php';
include_once dirname( __FILE__ ) . '/class-wc-gzd-rest-allergenic-controller.php';
include_once dirname( __FILE__ ) . '/class-wc-gzd-rest-product-deposit-types-controller.php';
include_once dirname( __FILE__ ) . '/class-wc-gzd-rest-product-manufacturers-controller.php';
}

public function register_rest_routes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @since 3.9.0
* @author vendidero
*/
class WC_GZD_REST_Product_Manufacturer_Controller extends WC_REST_Terms_Controller {
class WC_GZD_REST_Product_Manufacturers_Controller extends WC_REST_Terms_Controller {

/**
* Endpoint namespace.
Expand All @@ -20,7 +20,7 @@ class WC_GZD_REST_Product_Manufacturer_Controller extends WC_REST_Terms_Controll
*
* @var string
*/
protected $rest_base = 'products/manufacturer';
protected $rest_base = 'products/manufacturers';

/**
* Taxonomy.
Expand Down
2 changes: 1 addition & 1 deletion src/Blocks/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function( $additional_hook_data ) {
/**
* Exclude price labels which are attached to the product safety tab
*/
if ( 'woocommerce_gzd_single_product_safety_information' === $price_label->get_filter() ) {
if ( in_array( $price_label->get_filter(), array( 'woocommerce_gzd_single_product_safety_information', 'woocommerce_product_additional_information' ), true ) ) {
continue;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Shopmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,11 @@ public static function get_filters( $location = 'single_product' ) {
'is_action' => true,
'number_of_params' => 1,
),
'woocommerce_product_additional_information' => array(
'title' => __( 'Additional information tab', 'woocommerce-germanized' ),
'is_action' => true,
'number_of_params' => 1,
),
),
'single_product_grouped' => array(
'woocommerce_grouped_product_list_column_price' => array(
Expand Down
59 changes: 59 additions & 0 deletions tests/framework/helpers/class-wc-gzd-helper-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public static function create_deposit_type() {
return $term;
}

public static function create_manufacturer() {
$term = get_term_by( 'slug', 'vendidero', 'product_manufacturer' );

if ( is_wp_error( $term ) || ! $term ) {
$term = wp_insert_term( 'vendidero', 'product_manufacturer' );

if ( ! is_wp_error( $term ) ) {
update_term_meta( $term['term_id'], 'formatted_address', "vendidero Gmbh\nMusterstr. 36\n12207 Berlin" );
update_term_meta( $term['term_id'], 'formatted_eu_address', "vendidero Gmbh\nMusterstr. 36\n12207 Berlin" );

$term = get_term_by( 'id', $term['term_id'], 'product_manufacturer' );
}
}

return $term;
}

public static function create_nutrient() {
$term = get_term_by( 'slug', 'energy', 'product_nutrient' );

Expand Down Expand Up @@ -52,10 +69,43 @@ public static function create_allergen() {
return $term;
}

public static function create_attachment() {
$attachment = get_page_by_path( 'woocommerce-placeholder', 'OBJECT', 'attachment' );

if ( ! is_a( $attachment, 'WP_Post' ) ) {
$upload_dir = wp_upload_dir();
$source = WC()->plugin_path() . '/assets/images/placeholder-attachment.png';
$filename = $upload_dir['basedir'] . '/woocommerce-placeholder.png';

if ( ! file_exists( $filename ) ) {
copy( $source, $filename ); // @codingStandardsIgnoreLine.
}

$filetype = wp_check_filetype( basename( $filename ), null );
$attachment = array(
'guid' => $upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
);

$attachment_id = wp_insert_attachment( $attachment, $filename );

if ( ! is_wp_error( $attachment_id ) ) {
$attachment = get_post( $attachment_id );
}
}

return is_a( $attachment, 'WP_Post' ) ? $attachment->ID : 0;
}

public static function create_simple_product() {
$deposit_type = self::create_deposit_type();
$allergen = self::create_allergen();
$nutrient = self::create_nutrient();
$manufacturer = self::create_manufacturer();
$attachment = self::create_attachment();

$product = WC_Helper_Product::create_simple_product();

Expand Down Expand Up @@ -88,6 +138,7 @@ public static function create_simple_product() {
'AT' => '4-5-days'
),
'_is_food' => 'yes',
'_manufacturer_slug' => $manufacturer->slug,
'_deposit_type' => $deposit_type->slug,
'_deposit_quantity' => 5,
'_ingredients' => '<strong>Hazelnut</strong>, Fish',
Expand All @@ -105,6 +156,10 @@ public static function create_simple_product() {
'ref_value' => 22.1,
),
),
'_safety_attachment_ids' => array(
$attachment
),
'_warranty_attachment_id' => $attachment,
'_allergen_ids' => array(
$allergen->term_id,
),
Expand All @@ -118,6 +173,7 @@ public static function create_simple_product() {

wp_set_object_terms( $product->get_id(), array( '2-3 Days', '3-4 Days', '4-5 Days' ), 'product_delivery_time' );
wp_set_object_terms( $product->get_id(), array( $deposit_type->slug ), 'product_deposit_type' );
wp_set_object_terms( $product->get_id(), array( $manufacturer->slug ), 'product_manufacturer' );

return wc_gzd_get_gzd_product( $product );
}
Expand All @@ -130,6 +186,8 @@ public static function create_variation_product() {
$deposit_type = self::create_deposit_type();
$allergen = self::create_allergen();
$nutrient = self::create_nutrient();
$manufacturer = self::create_manufacturer();
$attachment = self::create_attachment();

$data = array(
'_unit' => 'g',
Expand All @@ -152,6 +210,7 @@ public static function create_variation_product() {
'_ts_mpn' => 'child_mpn',
'_is_food' => 'yes',
'_deposit_type' => $deposit_type->slug,
'_manufacturer_slug' => $manufacturer->slug,
'_deposit_quantity' => 5,
'_ingredients' => '<strong>Hazelnut</strong>, Fish',
'_food_description' => 'A sample food description',
Expand Down
56 changes: 56 additions & 0 deletions tests/unit-tests/api/manufacturers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Tests for Manufacturers API.
*
* @package Germanized\Tests\API
* @since 3.9.0
*/

class WC_GZD_Manufacturers_API extends WC_GZD_REST_Unit_Test_Case {

/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() : void {
parent::setUp();
$this->endpoint = new WC_GZD_REST_Product_Manufacturers_Controller();
$this->user = $this->factory->user->create( array(
'role' => 'administrator',
) );
}

/**
* Test creating a deposit type.
*
* @since 3.9.0
*/
public function test_get_deposit_type() {
wp_set_current_user( $this->user );

$term = wp_insert_term( 'vendidero', 'product_manufacturer' );

if ( ! is_wp_error( $term ) ) {
update_term_meta( $term['term_id'], 'formatted_address', "vendidero Gmbh\nMusterstr. 36\n12207 Berlin" );
update_term_meta( $term['term_id'], 'formatted_eu_address', "vendidero Gmbh\nMusterstr. 36\n12207 Berlin" );
}

$request = new WP_REST_Request( 'GET', '/wc/v3/products/manufacturers/' . $term['term_id'] );
$response = $this->server->dispatch( $request );

$manufacturer = $response->get_data();

$this->assertEquals( 200, $response->get_status() );

$this->assertEquals( $manufacturer, array(
'id' => $term['term_id'],
'name' => 'vendidero',
'slug' => 'vendidero',
'description' => '',
'count' => 0,
'formatted_address' => "vendidero Gmbh\nMusterstr. 36\n12207 Berlin",
'formatted_eu_address' => "vendidero Gmbh\nMusterstr. 36\n12207 Berlin",
) );

wp_delete_term( $term['term_id'], 'product_manufacturer' );
}
}
13 changes: 13 additions & 0 deletions tests/unit-tests/api/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function test_get_product() {
$this->assertEquals( '3-4-days', $country_specific[1]['slug'] );
$this->assertEquals( 'BG', $country_specific[1]['country'] );


$this->assertEquals( 'vendidero', $product['manufacturer']['slug'] );
$this->assertEquals( '1', $product['unit_price']['product'] );
$this->assertEquals( '100.0', $product['unit_price']['price_regular'] );
$this->assertEquals( '90.0', $product['unit_price']['price_sale'] );
Expand All @@ -60,6 +62,9 @@ public function test_get_product() {
$this->assertEquals( true, $product['free_shipping'] );
$this->assertEquals( true, $product['is_food'] );

$this->assertEquals( true, ! empty( $product['safety_attachment_ids'] ) );
$this->assertEquals( true, ! empty( $product['warranty_attachment_id'] ) );

$nutrient = get_term_by( 'slug', 'energy', 'product_nutrient' );
$allergen = get_term_by( 'slug', 'hazelnut', 'product_allergen' );

Expand Down Expand Up @@ -92,6 +97,7 @@ public function test_create_product() {
WC_GZD_Helper_Product::create_deposit_type();

$term = wp_insert_term( '3-4 days', 'product_delivery_time', array( 'slug' => '3-4-days' ) );
$manufacturer = wp_insert_term( 'woocommerce', 'product_manufacturer', array( 'slug' => 'woocommerce' ) );
$sale_term = wp_insert_term( 'Test Sale', 'product_price_label', array( 'slug' => 'test-sale' ) );
$nutrient = wp_insert_term( 'Salt', 'product_nutrient', array( 'slug' => 'salt' ) );
$nutrient_2 = wp_insert_term( 'Natrium', 'product_nutrient', array( 'slug' => 'natrium' ) );
Expand All @@ -110,6 +116,9 @@ public function test_create_product() {
'gtin' => 'test_gtin',
'mpn' => 'test_mpn',
'delivery_time' => array( 'id' => $term['term_id'] ),
'manufacturer' => array( 'id' => $manufacturer['term_id'] ),
'safety_attachment_ids' => array( 123, 1234 ),
'warranty_attachment_id' => 123,
'country_specific_delivery_times' => array(
array(
'slug' => '4-5-days',
Expand Down Expand Up @@ -161,6 +170,10 @@ public function test_create_product() {
$this->assertEquals( '80.0', $data['unit_price']['price_regular'] );
$this->assertEquals( '70.0', $data['unit_price']['price_sale'] );

$this->assertEquals( 'woocommerce', $data['manufacturer']['slug'] );
$this->assertEquals( array( 123, 1234 ), $data['safety_attachment_ids'] );
$this->assertEquals( 123, $data['warranty_attachment_id'] );

$this->assertEquals( 'test_gtin', $data['gtin'] );
$this->assertEquals( 'test_mpn', $data['mpn'] );

Expand Down

0 comments on commit 6a9a4dc

Please sign in to comment.