PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/product-attributes.php

https://github.com/woothemes/woocommerce
PHP | 268 lines | 167 code | 45 blank | 56 comment | 1 complexity | 58272fd3e092a8458603a8826a7eba85 MD5 | raw file
  1. <?php
  2. /**
  3. * Product Attributes REST API Test
  4. *
  5. * @package WooCommerce\Admin\Tests\API
  6. */
  7. /**
  8. * Class WC_Admin_Tests_API_Product_Attributes
  9. */
  10. class WC_Admin_Tests_API_Product_Attributes extends WC_REST_Unit_Test_Case {
  11. /**
  12. * Endpoints.
  13. *
  14. * @var string
  15. */
  16. protected $endpoint = '/wc-analytics/products/attributes';
  17. /**
  18. * Setup test user.
  19. */
  20. public function setUp(): void {
  21. parent::setUp();
  22. $this->user = $this->factory->user->create(
  23. array(
  24. 'role' => 'administrator',
  25. )
  26. );
  27. }
  28. public static function tearDownAfterClass(): void {
  29. parent::tearDownAfterClass();
  30. global $wpdb;
  31. $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies" );
  32. $wpdb->query('commit');
  33. }
  34. /**
  35. * Setup test product attributes data.
  36. */
  37. public static function setUpBeforeClass(): void {
  38. parent::setUpBeforeClass();
  39. // Use the test helper to populate some global attributes.
  40. $product = \WC_Helper_Product::create_variation_product();
  41. $attributes = $product->get_attributes();
  42. // Add a custom attribute.
  43. $custom_attr = new WC_Product_Attribute();
  44. $custom_attr->set_name( 'Numeric Size' );
  45. $custom_attr->set_options( array( '1', '2', '3', '4', '5' ) );
  46. $custom_attr->set_visible( true );
  47. $custom_attr->set_variation( true );
  48. $attributes[] = $custom_attr;
  49. $product->set_attributes( $attributes );
  50. $product->save();
  51. // Assign one variation to the '1' size.
  52. $variation = $product->get_available_variations( 'objects' )[0];
  53. $attributes = $variation->get_attributes();
  54. $attributes[ sanitize_title( $custom_attr->get_name() ) ] = '1';
  55. $variation->set_attributes( $attributes );
  56. $variation->save();
  57. // Add more custom Numeric Size values to another product.
  58. $product_2 = new WC_Product_Variable();
  59. $product_2->set_props(
  60. array(
  61. 'name' => 'Dummy Variable Product 2',
  62. 'sku' => 'DUMMY VARIABLE SKU 2',
  63. )
  64. );
  65. $custom_attr_2 = new WC_Product_Attribute();
  66. $custom_attr_2->set_name( 'Numeric Size' );
  67. $custom_attr_2->set_options( array( '6', '7', '8', '9', '10' ) );
  68. $custom_attr_2->set_visible( true );
  69. $custom_attr_2->set_variation( true );
  70. $product_2->set_attributes(
  71. array(
  72. $custom_attr_2,
  73. )
  74. );
  75. $product_2->save();
  76. }
  77. /**
  78. * Test route registration.
  79. */
  80. public function test_register_routes() {
  81. $routes = $this->server->get_routes();
  82. $this->assertArrayHasKey( $this->endpoint, $routes );
  83. }
  84. /**
  85. * Test request without valid permissions.
  86. */
  87. public function test_get_reports_without_permission() {
  88. wp_set_current_user( 0 );
  89. $response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
  90. $this->assertEquals( 401, $response->get_status() );
  91. }
  92. /**
  93. * Test schema.
  94. */
  95. public function test_schema() {
  96. wp_set_current_user( $this->user );
  97. $request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
  98. $response = $this->server->dispatch( $request );
  99. $data = $response->get_data();
  100. $properties = $data['schema']['properties'];
  101. $this->assertEquals( 6, count( $properties ) );
  102. $this->assertArrayHasKey( 'id', $properties );
  103. $this->assertEquals( array( 'integer', 'string' ), $properties['id']['type'] );
  104. $this->assertArrayHasKey( 'name', $properties );
  105. $this->assertArrayHasKey( 'slug', $properties );
  106. $this->assertArrayHasKey( 'type', $properties );
  107. $this->assertArrayHasKey( 'order_by', $properties );
  108. $this->assertArrayHasKey( 'has_archives', $properties );
  109. }
  110. /**
  111. * Test our passthrough case to the wc/v3 endpoint.
  112. */
  113. public function test_without_search() {
  114. wp_set_current_user( $this->user );
  115. $request = new WP_REST_Request( 'GET', $this->endpoint );
  116. $response = $this->server->dispatch( $request );
  117. $attributes = $response->get_data();
  118. $this->assertEquals( 200, $response->get_status() );
  119. $this->assertEquals( 3, count( $attributes ) );
  120. // Ensure our custom attribute is not in the results (proof of core endpoint).
  121. $names = wp_list_pluck( $attributes, 'name' );
  122. $this->assertNotContains( 'Numeric Size', $names );
  123. }
  124. /**
  125. * Test our search functionality.
  126. */
  127. public function test_with_search() {
  128. wp_set_current_user( $this->user );
  129. $request = new WP_REST_Request( 'GET', $this->endpoint );
  130. $request->set_query_params(
  131. array(
  132. 'search' => 'num',
  133. )
  134. );
  135. $response = $this->server->dispatch( $request );
  136. $attributes = $response->get_data();
  137. $this->assertEquals( 200, $response->get_status() );
  138. $this->assertEquals( 2, count( $attributes ) );
  139. // Results should include "number" and "Numeric Size".
  140. $names = wp_list_pluck( $attributes, 'name' );
  141. $this->assertContains( 'number', $names );
  142. $this->assertContains( 'Numeric Size', $names );
  143. }
  144. /**
  145. * Test getting a single attribute by slug.
  146. */
  147. public function test_by_slug() {
  148. wp_set_current_user( $this->user );
  149. $request = new WP_REST_Request( 'GET', $this->endpoint . '/numeric-size' );
  150. $response = $this->server->dispatch( $request );
  151. $attribute = $response->get_data();
  152. $this->assertEquals( 200, $response->get_status() );
  153. $this->assertEquals( 'numeric-size', $attribute['id'] );
  154. $this->assertEquals( 'numeric-size', $attribute['slug'] );
  155. $this->assertEquals( 'Numeric Size', $attribute['name'] );
  156. }
  157. /**
  158. * Test not finding a single attribute by slug.
  159. */
  160. public function test_by_slug_404() {
  161. wp_set_current_user( $this->user );
  162. $request = new WP_REST_Request( 'GET', $this->endpoint . '/not-a-real-slug' );
  163. $response = $this->server->dispatch( $request );
  164. $attribute = $response->get_data();
  165. $this->assertEquals( 404, $response->get_status() );
  166. }
  167. /**
  168. * Test terms schema.
  169. */
  170. public function test_terms_schema() {
  171. wp_set_current_user( $this->user );
  172. $request = new WP_REST_Request( 'OPTIONS', $this->endpoint . '/numeric-size/terms' );
  173. $response = $this->server->dispatch( $request );
  174. $data = $response->get_data();
  175. $properties = $data['schema']['properties'];
  176. $this->assertEquals( 6, count( $properties ) );
  177. $this->assertArrayHasKey( 'id', $properties );
  178. $this->assertEquals( array( 'integer', 'string' ), $properties['id']['type'] );
  179. $this->assertArrayHasKey( 'name', $properties );
  180. $this->assertArrayHasKey( 'slug', $properties );
  181. $this->assertArrayHasKey( 'description', $properties );
  182. $this->assertArrayHasKey( 'menu_order', $properties );
  183. $this->assertArrayHasKey( 'count', $properties );
  184. }
  185. /**
  186. * Test our passthrough case to the wc/v3 terms endpoint.
  187. */
  188. public function test_taxonomy_backed_terms() {
  189. wp_set_current_user( $this->user );
  190. $global_attrs = wc_get_attribute_taxonomies();
  191. // Grab the size attribute.
  192. $size_attr_id = false;
  193. foreach ( $global_attrs as $global_attr ) {
  194. if ( 'size' === $global_attr->attribute_name ) {
  195. $size_attr_id = $global_attr->attribute_id;
  196. break;
  197. }
  198. }
  199. $request = new WP_REST_Request( 'GET', $this->endpoint . '/' . $size_attr_id . '/terms' );
  200. $response = $this->server->dispatch( $request );
  201. $terms = $response->get_data();
  202. $this->assertEquals( 200, $response->get_status() );
  203. $this->assertEquals( 3, count( $terms ) );
  204. $slugs = wp_list_pluck( $terms, 'slug' );
  205. $this->assertContains( 'small', $slugs );
  206. $this->assertContains( 'large', $slugs );
  207. $this->assertContains( 'huge', $slugs );
  208. }
  209. /**
  210. * Test that our endpoint supports custom attributes.
  211. */
  212. public function test_custom_attribute_terms() {
  213. wp_set_current_user( $this->user );
  214. $request = new WP_REST_Request( 'GET', $this->endpoint . '/numeric-size/terms' );
  215. $response = $this->server->dispatch( $request );
  216. $terms = $response->get_data();
  217. $this->assertEquals( 200, $response->get_status() );
  218. $this->assertEquals( 10, count( $terms ) );
  219. $this->assertEquals( '1', $terms[0]['slug'] );
  220. $this->assertEquals( 1, $terms[0]['count'] );
  221. $this->assertEquals( 0, $terms[1]['count'] );
  222. }
  223. }