WC_Tests_API_Reports_Customers_Totals::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tests for the reports customers totals REST API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.5.0
7
 */
8
9
class WC_Tests_API_Reports_Customers_Totals extends WC_REST_Unit_Test_Case {
0 ignored issues
show
Bug introduced by
The type WC_REST_Unit_Test_Case was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
	/**
12
	 * Setup our test server, endpoints, and user info.
13
	 */
14
	public function setUp() {
15
		parent::setUp();
16
		$this->user = $this->factory->user->create(
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
			array(
18
				'role' => 'administrator',
19
			)
20
		);
21
	}
22
23
	/**
24
	 * Test route registration.
25
	 *
26
	 * @since 3.5.0
27
	 */
28
	public function test_register_routes() {
29
		$routes = $this->server->get_routes();
30
		$this->assertArrayHasKey( '/wc/v3/reports/customers/totals', $routes );
31
	}
32
33
	/**
34
	 * Test getting all product reviews.
35
	 *
36
	 * @since 3.5.0
37
	 */
38
	public function test_get_reports() {
39
		wp_set_current_user( $this->user );
40
41
		$response        = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' ) );
42
		$report          = $response->get_data();
43
		$users_count     = count_users();
44
		$total_customers = 0;
45
46
		foreach ( $users_count['avail_roles'] as $role => $total ) {
47
			if ( in_array( $role, array( 'administrator', 'shop_manager' ), true ) ) {
48
				continue;
49
			}
50
51
			$total_customers += (int) $total;
52
		}
53
54
		$customers_query = new WP_User_Query(
55
			array(
56
				'role__not_in' => array( 'administrator', 'shop_manager' ),
57
				'number'       => 0,
58
				'fields'       => 'ID',
59
				'count_total'  => true,
60
				'meta_query'   => array( // WPCS: slow query ok.
61
					array(
62
						'key'     => 'paying_customer',
63
						'value'   => 1,
64
						'compare' => '=',
65
					),
66
				),
67
			)
68
		);
69
70
		$total_paying = (int) $customers_query->get_total();
71
72
		$data = array(
73
			array(
74
				'slug'  => 'paying',
75
				'name'  => __( 'Paying customer', 'woocommerce' ),
76
				'total' => $total_paying,
77
			),
78
			array(
79
				'slug'  => 'non_paying',
80
				'name'  => __( 'Non-paying customer', 'woocommerce' ),
81
				'total' => $total_customers - $total_paying,
82
			),
83
		);
84
85
		$this->assertEquals( 200, $response->get_status() );
86
		$this->assertEquals( 2, count( $report ) );
87
		$this->assertEquals( $data, $report );
88
	}
89
90
	/**
91
	 * Tests to make sure product reviews cannot be viewed without valid permissions.
92
	 *
93
	 * @since 3.5.0
94
	 */
95
	public function test_get_reports_without_permission() {
96
		wp_set_current_user( 0 );
97
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' ) );
98
		$this->assertEquals( 401, $response->get_status() );
99
	}
100
101
	/**
102
	 * Test the product review schema.
103
	 *
104
	 * @since 3.5.0
105
	 */
106
	public function test_product_review_schema() {
107
		wp_set_current_user( $this->user );
108
		$product    = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
109
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/customers/totals' );
110
		$response   = $this->server->dispatch( $request );
111
		$data       = $response->get_data();
112
		$properties = $data['schema']['properties'];
113
114
		$this->assertEquals( 3, count( $properties ) );
115
		$this->assertArrayHasKey( 'slug', $properties );
116
		$this->assertArrayHasKey( 'name', $properties );
117
		$this->assertArrayHasKey( 'total', $properties );
118
	}
119
}
120