Passed
Push — master ( a70374...09ef1e )
by Mike
05:06
created

WC_Tests_API_Reports_Coupons_Totals   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_get_reports() 0 34 3
A setUp() 0 5 1
A test_product_review_schema() 0 12 1
A test_register_routes() 0 3 1
A test_get_reports_without_permission() 0 4 1
1
<?php
2
/**
3
 * Tests for the reports coupons totals REST API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.5.0
7
 */
8
9
class WC_Tests_API_Reports_Coupons_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/coupons/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
		global $wpdb;
40
		wp_set_current_user( $this->user );
41
42
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' ) );
43
		$report   = $response->get_data();
44
		$types    = wc_get_coupon_types();
45
		$data     = array();
46
47
		foreach ( $types as $slug => $name ) {
48
			$results = $wpdb->get_results(
49
				$wpdb->prepare(
50
					"
51
					SELECT count(meta_id) AS total
52
					FROM $wpdb->postmeta
53
					WHERE meta_key = 'discount_type'
54
					AND meta_value = %s
55
				",
56
					$slug
57
				)
58
			);
59
60
			$total = isset( $results[0] ) ? (int) $results[0]->total : 0;
61
62
			$data[] = array(
63
				'slug'  => $slug,
64
				'name'  => $name,
65
				'total' => $total,
66
			);
67
		}
68
69
		$this->assertEquals( 200, $response->get_status() );
70
		$this->assertEquals( count( $types ), count( $report ) );
71
		$this->assertEquals( $data, $report );
72
	}
73
74
	/**
75
	 * Tests to make sure product reviews cannot be viewed without valid permissions.
76
	 *
77
	 * @since 3.5.0
78
	 */
79
	public function test_get_reports_without_permission() {
80
		wp_set_current_user( 0 );
81
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' ) );
82
		$this->assertEquals( 401, $response->get_status() );
83
	}
84
85
	/**
86
	 * Test the product review schema.
87
	 *
88
	 * @since 3.5.0
89
	 */
90
	public function test_product_review_schema() {
91
		wp_set_current_user( $this->user );
92
		$product    = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
93
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/coupons/totals' );
94
		$response   = $this->server->dispatch( $request );
95
		$data       = $response->get_data();
96
		$properties = $data['schema']['properties'];
97
98
		$this->assertEquals( 3, count( $properties ) );
99
		$this->assertArrayHasKey( 'slug', $properties );
100
		$this->assertArrayHasKey( 'name', $properties );
101
		$this->assertArrayHasKey( 'total', $properties );
102
	}
103
}
104