PageRenderTime 71ms CodeModel.GetById 44ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/api/reports-performance-indicators.php

https://github.com/woothemes/woocommerce
PHP | 335 lines | 235 code | 46 blank | 54 comment | 2 complexity | 070eac9f5edf63664a88817877d36c31 MD5 | raw file
  1. <?php
  2. /**
  3. * Reports Performance indicators REST API Tests
  4. *
  5. * @package WooCommerce\Admin\Tests\API.
  6. */
  7. /**
  8. * WC_Admin_Tests_API_Reports_Performance_Indicators
  9. */
  10. class WC_Admin_Tests_API_Reports_Performance_Indicators extends WC_REST_Unit_Test_Case {
  11. /**
  12. * Endpoints.
  13. *
  14. * @var string
  15. */
  16. protected $endpoint = '/wc-analytics/reports/performance-indicators';
  17. /**
  18. * Setup tests.
  19. */
  20. public function setUp(): void {
  21. parent::setUp();
  22. $this->user = $this->factory->user->create(
  23. array(
  24. 'role' => 'administrator',
  25. )
  26. );
  27. // Mock the Jetpack endpoints and permissions.
  28. $wp_user = get_userdata( $this->user );
  29. $wp_user->add_cap( 'view_stats' );
  30. $this->mock_jetpack_modules();
  31. add_filter( 'rest_post_dispatch', array( $this, 'mock_rest_responses' ), 10, 3 );
  32. }
  33. /**
  34. * Test route registration.
  35. */
  36. public function test_register_routes() {
  37. $routes = $this->server->get_routes();
  38. $this->assertArrayHasKey( $this->endpoint, $routes );
  39. $this->assertArrayHasKey( $this->endpoint . '/allowed', $routes );
  40. }
  41. /**
  42. * Test getting indicators.
  43. */
  44. public function test_get_indicators() {
  45. wp_set_current_user( $this->user );
  46. WC_Helper_Reports::reset_stats_dbs();
  47. // Populate all of the data. We'll create an order and a download.
  48. $prod_download = new WC_Product_Download();
  49. $prod_download->set_file( plugin_dir_url( $this->wc_core_dir ) . 'woocommerce/assets/images/help.png' );
  50. $prod_download->set_id( '1' );
  51. $product = new WC_Product_Simple();
  52. $product->set_name( 'Test Product' );
  53. $product->set_downloadable( 'yes' );
  54. $product->set_downloads( array( $prod_download ) );
  55. $product->set_regular_price( 25 );
  56. $product->save();
  57. $order = WC_Helper_Order::create_order( 1, $product );
  58. $order->set_status( 'completed' );
  59. $order->set_total( 25 );
  60. $order->save();
  61. $download = new WC_Customer_Download();
  62. $download->set_user_id( $this->user );
  63. $download->set_order_id( $order->get_id() );
  64. $download->set_product_id( $product->get_id() );
  65. $download->set_download_id( $prod_download->get_id() );
  66. $download->save();
  67. $object = new WC_Customer_Download_Log();
  68. $object->set_permission_id( $download->get_id() );
  69. $object->set_user_id( $this->user );
  70. $object->set_user_ip_address( '1.2.3.4' );
  71. $object->save();
  72. $object = new WC_Customer_Download_Log();
  73. $object->set_permission_id( $download->get_id() );
  74. $object->set_user_id( $this->user );
  75. $object->set_user_ip_address( '1.2.3.4' );
  76. $object->save();
  77. WC_Helper_Queue::run_all_pending();
  78. $time = time();
  79. $request = new WP_REST_Request( 'GET', $this->endpoint );
  80. $request->set_query_params(
  81. array(
  82. 'before' => gmdate( 'Y-m-d 23:59:59', $time ),
  83. 'after' => gmdate( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
  84. 'stats' => 'orders/orders_count,downloads/download_count,test/bogus_stat,jetpack/stats/views',
  85. )
  86. );
  87. $response = $this->server->dispatch( $request );
  88. $reports = $response->get_data();
  89. $this->assertEquals( 200, $response->get_status() );
  90. $this->assertEquals( 3, count( $reports ) );
  91. $this->assertEquals( 'orders/orders_count', $reports[0]['stat'] );
  92. $this->assertEquals( 'Orders', $reports[0]['label'] );
  93. $this->assertEquals( 1, $reports[0]['value'] );
  94. $this->assertEquals( 'orders_count', $reports[0]['chart'] );
  95. $this->assertEquals( '/analytics/orders', $response->data[0]['_links']['report'][0]['href'] );
  96. $this->assertEquals( 'downloads/download_count', $reports[1]['stat'] );
  97. $this->assertEquals( 'Downloads', $reports[1]['label'] );
  98. $this->assertEquals( 2, $reports[1]['value'] );
  99. $this->assertEquals( 'download_count', $reports[1]['chart'] );
  100. $this->assertEquals( '/analytics/downloads', $response->data[1]['_links']['report'][0]['href'] );
  101. $this->assertEquals( 'jetpack/stats/views', $reports[2]['stat'] );
  102. $this->assertEquals( 'Views', $reports[2]['label'] );
  103. $this->assertEquals( 10, $reports[2]['value'] );
  104. $this->assertEquals( 'views', $reports[2]['chart'] );
  105. $this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[2]['_links']['api'][0]['href'] );
  106. }
  107. /**
  108. * Test getting indicators with an empty request.
  109. */
  110. public function test_get_indicators_empty_request() {
  111. wp_set_current_user( $this->user );
  112. WC_Helper_Reports::reset_stats_dbs();
  113. $time = time();
  114. $request = new WP_REST_Request( 'GET', $this->endpoint );
  115. $request->set_query_params(
  116. array(
  117. 'before' => gmdate( 'Y-m-d 23:59:59', $time ),
  118. 'after' => gmdate( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
  119. )
  120. );
  121. $response = $this->server->dispatch( $request );
  122. $reports = $response->get_data();
  123. $this->assertEquals( 500, $response->get_status() );
  124. }
  125. /**
  126. * Test getting without valid permissions.
  127. */
  128. public function test_get_indicators_without_permission() {
  129. wp_set_current_user( 0 );
  130. $response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
  131. $this->assertEquals( 401, $response->get_status() );
  132. }
  133. /**
  134. * Test schema.
  135. */
  136. public function test_indicators_schema() {
  137. wp_set_current_user( $this->user );
  138. $request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
  139. $response = $this->server->dispatch( $request );
  140. $data = $response->get_data();
  141. $properties = $data['schema']['properties'];
  142. $this->assertEquals( 5, count( $properties ) );
  143. $this->assertArrayHasKey( 'stat', $properties );
  144. $this->assertArrayHasKey( 'chart', $properties );
  145. $this->assertArrayHasKey( 'label', $properties );
  146. $this->assertArrayHasKey( 'format', $properties );
  147. $this->assertArrayHasKey( 'value', $properties );
  148. }
  149. /**
  150. * Test schema for /allowed indicators endpoint.
  151. */
  152. public function test_indicators_schema_allowed() {
  153. wp_set_current_user( $this->user );
  154. $request = new WP_REST_Request( 'OPTIONS', $this->endpoint . '/allowed' );
  155. $response = $this->server->dispatch( $request );
  156. $data = $response->get_data();
  157. $properties = $data['schema']['properties'];
  158. $this->assertEquals( 3, count( $properties ) );
  159. $this->assertArrayHasKey( 'stat', $properties );
  160. $this->assertArrayHasKey( 'chart', $properties );
  161. $this->assertArrayHasKey( 'label', $properties );
  162. }
  163. /**
  164. * Test the ability to aggregate Jetpack stats based on before and after dates.
  165. */
  166. public function test_jetpack_stats_query_args() {
  167. wp_set_current_user( $this->user );
  168. $request = new WP_REST_Request( 'GET', $this->endpoint );
  169. $request->set_query_params(
  170. array(
  171. 'before' => '2020-01-05 23:59:59',
  172. 'after' => '2020-01-01 00:00:00',
  173. 'stats' => 'jetpack/stats/views',
  174. )
  175. );
  176. $response = $this->server->dispatch( $request );
  177. $reports = $response->get_data();
  178. $this->assertEquals( 200, $response->get_status() );
  179. $this->assertEquals( 1, count( $reports ) );
  180. $this->assertEquals( 'jetpack/stats/views', $reports[0]['stat'] );
  181. $this->assertEquals( 'Views', $reports[0]['label'] );
  182. $this->assertEquals( 18, $reports[0]['value'] );
  183. $this->assertEquals( 'views', $reports[0]['chart'] );
  184. $this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[0]['_links']['api'][0]['href'] );
  185. $request = new WP_REST_Request( 'GET', $this->endpoint );
  186. $request->set_query_params(
  187. array(
  188. 'before' => '2020-01-02 23:59:59',
  189. 'after' => '2020-01-01 00:00:00',
  190. 'stats' => 'jetpack/stats/views',
  191. )
  192. );
  193. $response = $this->server->dispatch( $request );
  194. $reports = $response->get_data();
  195. $this->assertEquals( 200, $response->get_status() );
  196. $this->assertEquals( 1, count( $reports ) );
  197. $this->assertEquals( 'jetpack/stats/views', $reports[0]['stat'] );
  198. $this->assertEquals( 'Views', $reports[0]['label'] );
  199. $this->assertEquals( 4, $reports[0]['value'] );
  200. $this->assertEquals( 'views', $reports[0]['chart'] );
  201. $this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[0]['_links']['api'][0]['href'] );
  202. }
  203. /**
  204. * Test the ability to aggregate Jetpack stats based on default arguments.
  205. */
  206. public function test_jetpack_stats_default_query_args() {
  207. wp_set_current_user( $this->user );
  208. $request = new WP_REST_Request( 'GET', $this->endpoint );
  209. $request->set_query_params(
  210. array(
  211. 'stats' => 'jetpack/stats/views',
  212. )
  213. );
  214. $response = $this->server->dispatch( $request );
  215. $reports = $response->get_data();
  216. $this->assertEquals( 200, $response->get_status() );
  217. $this->assertEquals( 1, count( $reports ) );
  218. $this->assertEquals( 'jetpack/stats/views', $reports[0]['stat'] );
  219. $this->assertEquals( 'Views', $reports[0]['label'] );
  220. $this->assertEquals( 10, $reports[0]['value'] );
  221. $this->assertEquals( 'views', $reports[0]['chart'] );
  222. $this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[0]['_links']['api'][0]['href'] );
  223. }
  224. /**
  225. * Mock the Jetpack REST API responses since we're not really connected.
  226. *
  227. * @param WP_Rest_Response $response Response from the server.
  228. * @param WP_Rest_Server $rest_server WP Rest Server.
  229. * @param WP_REST_Request $request Request made to the server.
  230. *
  231. * @return WP_Rest_Response
  232. */
  233. public function mock_rest_responses( $response, $rest_server, $request ) {
  234. if ( 'GET' === $request->get_method() && '/jetpack/v4/module/stats/data' === $request->get_route() ) {
  235. $general = new \stdClass();
  236. $general->visits = new \stdClass();
  237. $general->visits->fields = array(
  238. 'date',
  239. 'views',
  240. 'visits',
  241. );
  242. $general->visits->data = array(
  243. array(
  244. '2020-01-01',
  245. 1,
  246. 0,
  247. ),
  248. array(
  249. '2020-01-02',
  250. 3,
  251. 0,
  252. ),
  253. array(
  254. '2020-01-03',
  255. 1,
  256. 0,
  257. ),
  258. array(
  259. '2020-01-04',
  260. 8,
  261. 0,
  262. ),
  263. array(
  264. '2020-01-05',
  265. 5,
  266. 0,
  267. ),
  268. array(
  269. gmdate( 'Y-m-d' ),
  270. 10,
  271. 0,
  272. ),
  273. );
  274. $response->set_status( 200 );
  275. $response->set_data(
  276. array( 'general' => $general )
  277. );
  278. }
  279. return $response;
  280. }
  281. /**
  282. * Mock the Jetpack stats module as active.
  283. */
  284. public function mock_jetpack_modules() {
  285. $api_init = \Automattic\WooCommerce\Admin\API\Init::instance();
  286. $controller_name = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
  287. $api_init->$controller_name->set_active_jetpack_modules( array( 'stats' ) );
  288. }
  289. }