Issues (334)

Tests/Version3/reports-reviews-totals.php (2 issues)

1
<?php
2
/**
3
 * Tests for the reports reviews totals REST API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.5.0
7
 */
8
9
class WC_Tests_API_Reports_Reviews_Totals extends WC_REST_Unit_Test_Case {
0 ignored issues
show
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/reviews/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/reviews/totals' ) );
43
		$report   = $response->get_data();
44
		$data     = array();
45
46
		$query_data = array(
47
			'count'      => true,
48
			'post_type'  => 'product',
49
			'meta_key'   => 'rating', // WPCS: slow query ok.
50
			'meta_value' => '', // WPCS: slow query ok.
51
		);
52
53
		for ( $i = 1; $i <= 5; $i++ ) {
54
			$query_data['meta_value'] = $i;
55
56
			$data[] = array(
57
				'slug'  => 'rated_' . $i . '_out_of_5',
58
				/* translators: %s: average rating */
59
				'name'  => sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $i ),
60
				'total' => (int) get_comments( $query_data ),
61
			);
62
		}
63
64
		$this->assertEquals( 200, $response->get_status() );
65
		$this->assertEquals( count( $data ), count( $report ) );
66
		$this->assertEquals( $data, $report );
67
	}
68
69
	/**
70
	 * Tests to make sure product reviews cannot be viewed without valid permissions.
71
	 *
72
	 * @since 3.5.0
73
	 */
74
	public function test_get_reports_without_permission() {
75
		wp_set_current_user( 0 );
76
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/reviews/totals' ) );
77
		$this->assertEquals( 401, $response->get_status() );
78
	}
79
80
	/**
81
	 * Test the product review schema.
82
	 *
83
	 * @since 3.5.0
84
	 */
85
	public function test_product_review_schema() {
86
		wp_set_current_user( $this->user );
87
		$product    = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
88
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/reviews/totals' );
89
		$response   = $this->server->dispatch( $request );
90
		$data       = $response->get_data();
91
		$properties = $data['schema']['properties'];
92
93
		$this->assertEquals( 3, count( $properties ) );
94
		$this->assertArrayHasKey( 'slug', $properties );
95
		$this->assertArrayHasKey( 'name', $properties );
96
		$this->assertArrayHasKey( 'total', $properties );
97
	}
98
}
99