|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WCPOS\WooCommercePOS\Tests; |
| 4 | + |
| 5 | +use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper; |
| 6 | +use WC_REST_Unit_Test_Case; |
| 7 | +use WCPOS\WooCommercePOS\WC_API; |
| 8 | + |
| 9 | +/** |
| 10 | + * @internal |
| 11 | + * |
| 12 | + * @coversNothing |
| 13 | + */ |
| 14 | +class Test_WC_API extends WC_REST_Unit_Test_Case { |
| 15 | + public function setup(): void { |
| 16 | + parent::setup(); |
| 17 | + } |
| 18 | + |
| 19 | + public function tearDown(): void { |
| 20 | + parent::tearDown(); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * |
| 25 | + */ |
| 26 | + public function test_pos_only_products_via_store_api() { |
| 27 | + add_filter( |
| 28 | + 'woocommerce_pos_general_settings', |
| 29 | + function () { |
| 30 | + return array( |
| 31 | + 'pos_only_products' => true, |
| 32 | + ); |
| 33 | + } |
| 34 | + ); |
| 35 | + new WC_API(); // reinstantiate the class to apply the filter |
| 36 | + |
| 37 | + // Create a visible product |
| 38 | + $visible_product = ProductHelper::create_simple_product(); |
| 39 | + |
| 40 | + // Create a product with _pos_visibility set to 'pos_only' |
| 41 | + $hidden_product = ProductHelper::create_simple_product(); |
| 42 | + update_post_meta( $hidden_product->get_id(), '_pos_visibility', 'pos_only' ); |
| 43 | + |
| 44 | + // Verify that the meta value is set correctly |
| 45 | + $pos_visibility = get_post_meta( $hidden_product->get_id(), '_pos_visibility', true ); |
| 46 | + $this->assertEquals( 'pos_only', $pos_visibility, 'Meta value for _pos_visibility not set correctly' ); |
| 47 | + |
| 48 | + // Make WC REST request |
| 49 | + add_filter( 'woocommerce_rest_check_permissions', '__return_true' ); |
| 50 | + $request = new \WP_REST_Request( 'GET', '/wc/v3/products' ); |
| 51 | + $response = $this->server->dispatch( $request ); |
| 52 | + |
| 53 | + $data = $response->get_data(); |
| 54 | + $this->assertEquals( 200, $response->get_status() ); |
| 55 | + $this->assertEquals( 1, \count( $data ) ); |
| 56 | + $this->assertEquals( $visible_product->get_id(), $data[0]['id'] ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * |
| 61 | + */ |
| 62 | + public function test_pos_only_variations_via_store_api() { |
| 63 | + add_filter( |
| 64 | + 'woocommerce_pos_general_settings', |
| 65 | + function () { |
| 66 | + return array( |
| 67 | + 'pos_only_products' => true, |
| 68 | + ); |
| 69 | + } |
| 70 | + ); |
| 71 | + new WC_API(); // reinstantiate the class to apply the filter |
| 72 | + |
| 73 | + // Create a variable product |
| 74 | + $variable = ProductHelper::create_variation_product(); |
| 75 | + $variation_ids = $variable->get_children(); |
| 76 | + update_post_meta( $variation_ids[0], '_pos_visibility', 'pos_only' ); |
| 77 | + |
| 78 | + // Verify that the meta value is set correctly |
| 79 | + $pos_visibility = get_post_meta( $variation_ids[0], '_pos_visibility', true ); |
| 80 | + $this->assertEquals( 'pos_only', $pos_visibility, 'Meta value for _pos_visibility not set correctly' ); |
| 81 | + |
| 82 | + // Make WC REST request |
| 83 | + add_filter( 'woocommerce_rest_check_permissions', '__return_true' ); |
| 84 | + |
| 85 | + $request = new \WP_REST_Request( 'GET', '/wc/v3/products/' . $variable->get_id() . '/variations' ); |
| 86 | + $response = $this->server->dispatch( $request ); |
| 87 | + |
| 88 | + $data = $response->get_data(); |
| 89 | + $this->assertEquals( 200, $response->get_status() ); |
| 90 | + $this->assertEquals( 1, \count( $data ) ); |
| 91 | + $this->assertEquals( $variation_ids[1], $data[0]['id'] ); |
| 92 | + |
| 93 | + /** |
| 94 | + * @TODO should we remove the id from the parent response also? |
| 95 | + * The WooCommerce code uses $object->get_children() to get the variation ids, NOT |
| 96 | + * $object->get_visible_children() so it seems they return all variations ids regardless of visibility. |
| 97 | + */ |
| 98 | + // $request = new \WP_REST_Request( 'GET', '/wc/v3/products/' . $variable->get_id() ); |
| 99 | + // $response = $this->server->dispatch( $request ); |
| 100 | + |
| 101 | + // $data = $response->get_data(); |
| 102 | + // $this->assertEquals( 200, $response->get_status() ); |
| 103 | + // $this->assertEquals( $variable->get_id(), $data['id'] ); |
| 104 | + // $this->assertEquals( 1, \count( $data['variations'] ) ); |
| 105 | + // $this->assertEquals( $variation_ids[1], $data['variations'][0] ); |
| 106 | + } |
| 107 | +} |
0 commit comments