Issues (334)

unit-tests/Tests/Version4/Data.php (5 issues)

Labels
1
<?php
2
/**
3
 * Data REST API Test
4
 *
5
 * @package WooCommerce Admin\Tests\API
6
 */
7
8
namespace Automattic\WooCommerce\RestApi\UnitTests\Tests\Version4;
9
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * WC Tests API Data
14
 */
15
class Data 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...
16
17
	/**
18
	 * Endpoints.
19
	 *
20
	 * @var string
21
	 */
22
	protected $endpoint = '/wc/v4/data';
23
24
	/**
25
	 * User variable.
26
	 *
27
	 * @var WP_User
0 ignored issues
show
The type Automattic\WooCommerce\R...\Tests\Version4\WP_User was not found. Did you mean WP_User? If so, make sure to prefix the type with \.
Loading history...
28
	 */
29
	protected static $user;
30
31
	/**
32
	 * Setup once before running tests.
33
	 *
34
	 * @param object $factory Factory object.
35
	 */
36
	public static function wpSetUpBeforeClass( $factory ) {
37
		self::$user = $factory->user->create(
38
			array(
39
				'role' => 'administrator',
40
			)
41
		);
42
	}
43
44
	/**
45
	 * Setup test class.
46
	 */
47
	public function setUp() {
48
		parent::setUp();
49
		wp_set_current_user( self::$user );
0 ignored issues
show
self::user of type Automattic\WooCommerce\R...\Tests\Version4\WP_User is incompatible with the type integer expected by parameter $id of wp_set_current_user(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
		wp_set_current_user( /** @scrutinizer ignore-type */ self::$user );
Loading history...
50
	}
51
52
	/**
53
	 * Test that the list of data endpoints includes download-ips.
54
	 */
55
	public function test_get_items_contains_download_ips() {
56
		$request  = new \WP_REST_Request( 'GET', $this->endpoint );
57
		$response = $this->server->dispatch( $request );
58
		$data     = $response->get_data();
59
60
		$this->assertEquals( 200, $response->get_status() );
61
		$this->assertEquals( 4, count( $data ) );
62
		$this->assertEquals( 'download-ips', $data[3]['slug'] );
63
	}
64
65
	/**
66
	 * Test download-ips match searching.
67
	 */
68
	public function test_download_ips() {
69
		$prod_download = new \WC_Product_Download();
70
		$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
71
		$prod_download->set_id( 1 );
72
73
		$product = new \WC_Product_Simple();
74
		$product->set_name( 'Test Product' );
75
		$product->set_downloadable( 'yes' );
76
		$product->set_downloads( array( $prod_download ) );
77
		$product->set_regular_price( 25 );
78
		$product->save();
79
80
		$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( 1, $product );
81
		$order->set_status( 'completed' );
82
		$order->set_total( 100 );
83
		$order->save();
84
85
		$download = new \WC_Customer_Download();
86
		$download->set_user_id( $this->user );
0 ignored issues
show
$this->user of type Automattic\WooCommerce\R...\Tests\Version4\WP_User is incompatible with the type integer expected by parameter $value of WC_Customer_Download::set_user_id(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
		$download->set_user_id( /** @scrutinizer ignore-type */ $this->user );
Loading history...
87
		$download->set_order_id( $order->get_id() );
88
		$download->set_product_id( $product->get_id() );
89
		$download->set_download_id( $prod_download->get_id() );
90
		$download->save();
91
92
		$object = new \WC_Customer_Download_Log();
93
		$object->set_permission_id( $download->get_id() );
94
		$object->set_user_id( $this->user );
0 ignored issues
show
$this->user of type Automattic\WooCommerce\R...\Tests\Version4\WP_User is incompatible with the type integer expected by parameter $value of WC_Customer_Download_Log::set_user_id(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
		$object->set_user_id( /** @scrutinizer ignore-type */ $this->user );
Loading history...
95
		$object->set_user_ip_address( '1.2.3.4' );
96
		$id = $object->save();
97
98
		$object = new \WC_Customer_Download_Log();
99
		$object->set_permission_id( $download->get_id() );
100
		$object->set_user_id( $this->user );
101
		$object->set_user_ip_address( '54.2.1.3' );
102
		$id = $object->save();
103
104
		// Save a second log for the same IP -- only one result for this IP should be returned.
105
		$object = new \WC_Customer_Download_Log();
106
		$object->set_permission_id( $download->get_id() );
107
		$object->set_user_id( $this->user );
108
		$object->set_user_ip_address( '54.2.1.3' );
109
		$id = $object->save();
110
111
		$object = new \WC_Customer_Download_Log();
112
		$object->set_permission_id( $download->get_id() );
113
		$object->set_user_id( $this->user );
114
		$object->set_user_ip_address( '54.5.1.7' );
115
		$id = $object->save();
116
117
		$request = new \WP_REST_Request( 'GET', $this->endpoint . '/download-ips' );
118
		$request->set_query_params(
119
			array(
120
				'match' => '54',
121
			)
122
		);
123
124
		$response  = $this->server->dispatch( $request );
125
		$addresses = $response->get_data();
126
127
		$this->assertEquals( 200, $response->get_status() );
128
		$this->assertEquals( 2, count( $addresses ) );
129
130
		$this->assertEquals( '54.2.1.3', $addresses[0]['user_ip_address'] );
131
		$this->assertEquals( '54.5.1.7', $addresses[1]['user_ip_address'] );
132
	}
133
}
134