|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @package WooCommerce\Tests\API |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Product Controller "products by attributes" REST API Test |
| 8 | + * |
| 9 | + * @since 1.2.0 |
| 10 | + */ |
| 11 | +class WC_Tests_API_Products_By_Attributes_Controller extends WC_REST_Unit_Test_Case { |
| 12 | + |
| 13 | + /** |
| 14 | + * Endpoints. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $endpoint = '/wc-pb/v3'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Setup test products data. Called before every test. |
| 22 | + * |
| 23 | + * @since 1.2.0 |
| 24 | + */ |
| 25 | + public function setUp() { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->user = $this->factory->user->create( |
| 29 | + array( |
| 30 | + 'role' => 'administrator', |
| 31 | + ) |
| 32 | + ); |
| 33 | + |
| 34 | + // Create 2 product attributes with terms. |
| 35 | + $attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) ); |
| 36 | + $attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large' ) ); |
| 37 | + |
| 38 | + $red_term = get_term_by( 'slug', 'red', $attr_color['attribute_taxonomy'] ); |
| 39 | + $blue_term = get_term_by( 'slug', 'blue', $attr_color['attribute_taxonomy'] ); |
| 40 | + $yellow_term = get_term_by( 'slug', 'yellow', $attr_color['attribute_taxonomy'] ); |
| 41 | + $small_term = get_term_by( 'slug', 'small', $attr_size['attribute_taxonomy'] ); |
| 42 | + $medium_term = get_term_by( 'slug', 'medium', $attr_size['attribute_taxonomy'] ); |
| 43 | + $large_term = get_term_by( 'slug', 'large', $attr_size['attribute_taxonomy'] ); |
| 44 | + |
| 45 | + $this->attr_term_ids = array( |
| 46 | + 'red' => $red_term->term_id, |
| 47 | + 'blue' => $blue_term->term_id, |
| 48 | + 'yellow' => $yellow_term->term_id, |
| 49 | + 'small' => $small_term->term_id, |
| 50 | + 'medium' => $medium_term->term_id, |
| 51 | + 'large' => $large_term->term_id, |
| 52 | + ); |
| 53 | + |
| 54 | + $color = new WC_Product_Attribute(); |
| 55 | + $color->set_id( $attr_color['attribute_id'] ); |
| 56 | + $color->set_name( $attr_color['attribute_taxonomy'] ); |
| 57 | + $color->set_visible( true ); |
| 58 | + |
| 59 | + $size = new WC_Product_Attribute(); |
| 60 | + $size->set_id( $attr_size['attribute_id'] ); |
| 61 | + $size->set_name( $attr_size['attribute_taxonomy'] ); |
| 62 | + $size->set_visible( true ); |
| 63 | + |
| 64 | + // Create some products with a mix of attributes: |
| 65 | + // - Product 1 – color: red, blue; size: medium. |
| 66 | + // - Product 2 – color: blue; size: large, medium. |
| 67 | + // - Product 3 – color: yellow. |
| 68 | + $this->products = array(); |
| 69 | + $color->set_options( [ $this->attr_term_ids['red'], $this->attr_term_ids['blue'] ] ); |
| 70 | + $size->set_options( [ $this->attr_term_ids['medium'] ] ); |
| 71 | + $this->products[0] = WC_Helper_Product::create_simple_product( false ); |
| 72 | + $this->products[0]->set_attributes( [ $color, $size ] ); |
| 73 | + $this->products[0]->save(); |
| 74 | + |
| 75 | + $color->set_options( [ $this->attr_term_ids['blue'] ] ); |
| 76 | + $size->set_options( [ $this->attr_term_ids['medium'], $this->attr_term_ids['large'] ] ); |
| 77 | + $this->products[1] = WC_Helper_Product::create_simple_product( false ); |
| 78 | + $this->products[1]->set_attributes( [ $color, $size ] ); |
| 79 | + $this->products[1]->save(); |
| 80 | + |
| 81 | + $color->set_options( [ $this->attr_term_ids['yellow'] ] ); |
| 82 | + $this->products[2] = WC_Helper_Product::create_simple_product( false ); |
| 83 | + $this->products[2]->set_attributes( [ $color ] ); |
| 84 | + $this->products[2]->save(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Test getting products by a single attribute term. |
| 89 | + * |
| 90 | + * @since 1.2.0 |
| 91 | + */ |
| 92 | + public function test_get_products() { |
| 93 | + wp_set_current_user( $this->user ); |
| 94 | + $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); |
| 95 | + $request->set_param( 'attribute', 'pa_color' ); |
| 96 | + $request->set_param( 'attribute_term', (string) $this->attr_term_ids['red'] ); |
| 97 | + |
| 98 | + $response = $this->server->dispatch( $request ); |
| 99 | + $response_products = $response->get_data(); |
| 100 | + $this->assertEquals( 200, $response->get_status() ); |
| 101 | + $this->assertEquals( 1, count( $response_products ) ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Test getting products by multiple terms in one attribute. |
| 106 | + * |
| 107 | + * @since 1.2.0 |
| 108 | + */ |
| 109 | + public function test_get_products_by_multiple_terms() { |
| 110 | + wp_set_current_user( $this->user ); |
| 111 | + $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); |
| 112 | + $request->set_param( 'attribute', 'pa_color' ); |
| 113 | + $request->set_param( |
| 114 | + 'attribute_term', |
| 115 | + // Terms list needs to be a string. |
| 116 | + $this->attr_term_ids['red'] . ',' . $this->attr_term_ids['yellow'] |
| 117 | + ); |
| 118 | + |
| 119 | + $response = $this->server->dispatch( $request ); |
| 120 | + $response_products = $response->get_data(); |
| 121 | + $this->assertEquals( 200, $response->get_status() ); |
| 122 | + $this->assertEquals( 2, count( $response_products ) ); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test getting products by multiple terms in one attribute, matching all. |
| 127 | + * |
| 128 | + * @since 1.2.0 |
| 129 | + */ |
| 130 | + public function test_get_products_by_multiple_terms_all() { |
| 131 | + wp_set_current_user( $this->user ); |
| 132 | + $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); |
| 133 | + $request->set_param( |
| 134 | + 'attributes', |
| 135 | + array( |
| 136 | + 'pa_color' => array( |
| 137 | + $this->attr_term_ids['red'], |
| 138 | + $this->attr_term_ids['blue'], |
| 139 | + ), |
| 140 | + ) |
| 141 | + ); |
| 142 | + $request->set_param( 'attr_operator', 'AND' ); |
| 143 | + |
| 144 | + $response = $this->server->dispatch( $request ); |
| 145 | + $response_products = $response->get_data(); |
| 146 | + $this->assertEquals( 200, $response->get_status() ); |
| 147 | + $this->assertEquals( 1, count( $response_products ) ); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test getting products by multiple terms in multiple attributes, matching any. |
| 152 | + * |
| 153 | + * @since 1.2.0 |
| 154 | + */ |
| 155 | + public function test_get_products_by_multiple_terms_multiple_attrs_any() { |
| 156 | + wp_set_current_user( $this->user ); |
| 157 | + $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); |
| 158 | + $request->set_param( |
| 159 | + 'attributes', |
| 160 | + array( |
| 161 | + 'pa_color' => array( $this->attr_term_ids['red'] ), |
| 162 | + 'pa_size' => array( $this->attr_term_ids['large'] ), |
| 163 | + ) |
| 164 | + ); |
| 165 | + $request->set_param( 'attr_operator', 'IN' ); |
| 166 | + |
| 167 | + $response = $this->server->dispatch( $request ); |
| 168 | + $response_products = $response->get_data(); |
| 169 | + $this->assertEquals( 200, $response->get_status() ); |
| 170 | + $this->assertEquals( 2, count( $response_products ) ); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Test getting products by multiple terms in multiple attributes, matching all. |
| 175 | + * |
| 176 | + * @since 1.2.0 |
| 177 | + */ |
| 178 | + public function test_get_products_by_multiple_terms_multiple_attrs_all() { |
| 179 | + wp_set_current_user( $this->user ); |
| 180 | + $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); |
| 181 | + $request->set_param( |
| 182 | + 'attributes', |
| 183 | + array( |
| 184 | + 'pa_color' => array( $this->attr_term_ids['blue'] ), |
| 185 | + 'pa_size' => array( $this->attr_term_ids['medium'] ), |
| 186 | + ) |
| 187 | + ); |
| 188 | + $request->set_param( 'attr_operator', 'AND' ); |
| 189 | + |
| 190 | + $response = $this->server->dispatch( $request ); |
| 191 | + $response_products = $response->get_data(); |
| 192 | + $this->assertEquals( 200, $response->get_status() ); |
| 193 | + $this->assertEquals( 2, count( $response_products ) ); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Test getting products by attributes that don't exist. |
| 198 | + * |
| 199 | + * Note: This test is currently skipped because the API isn't registering the attribute |
| 200 | + * properties correctly, and therefor not validating attribute names against "real" attributes. |
| 201 | + * |
| 202 | + * @since 1.2.0 |
| 203 | + */ |
| 204 | + public function xtest_get_products_by_fake_attrs() { |
| 205 | + wp_set_current_user( $this->user ); |
| 206 | + $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); |
| 207 | + $request->set_param( 'attributes', array( 'pa_fake' => array( 21 ) ) ); |
| 208 | + $response = $this->server->dispatch( $request ); |
| 209 | + $this->assertEquals( 400, $response->get_status() ); |
| 210 | + } |
| 211 | +} |
0 commit comments