Coupons   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 174
dl 0
loc 298
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A test_update() 0 24 2
A test_guest_delete() 0 5 1
A test_delete() 0 18 1
A test_create() 0 29 1
A test_guest_update() 0 12 1
A test_read() 0 31 2
A test_enum_discount_type() 0 13 1
A test_guest_read() 0 4 1
A test_guest_create() 0 30 1
A test_batch() 0 37 1
1
<?php
2
/**
3
 * Coupon REST API tests.
4
 *
5
 * @package Automattic/WooCommerce/RestApi/Tests
6
 */
7
8
namespace Automattic\WooCommerce\RestApi\UnitTests\Tests\Version4;
9
10
defined( 'ABSPATH' ) || exit;
11
12
use Automattic\WooCommerce\RestApi\UnitTests\AbstractRestApiTest;
13
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
14
15
/**
16
 * Abstract Rest API Test Class
17
 *
18
 * @extends AbstractRestApiTest
19
 */
20
class Coupons extends AbstractRestApiTest {
21
	/**
22
	 * Routes that this endpoint creates.
23
	 *
24
	 * @var array
25
	 */
26
	protected $routes = [
27
		'/wc/v4/coupons',
28
		'/wc/v4/coupons/(?P<id>[\d]+)',
29
		'/wc/v4/coupons/batch',
30
	];
31
32
	/**
33
	 * The endpoint schema.
34
	 *
35
	 * @var array Keys are property names, values are supported context.
36
	 */
37
	protected $properties = [
38
		'id'                          => array( 'view', 'edit' ),
39
		'code'                        => array( 'view', 'edit' ),
40
		'amount'                      => array( 'view', 'edit' ),
41
		'date_created'                => array( 'view', 'edit' ),
42
		'date_created_gmt'            => array( 'view', 'edit' ),
43
		'date_modified'               => array( 'view', 'edit' ),
44
		'date_modified_gmt'           => array( 'view', 'edit' ),
45
		'discount_type'               => array( 'view', 'edit' ),
46
		'description'                 => array( 'view', 'edit' ),
47
		'date_expires'                => array( 'view', 'edit' ),
48
		'date_expires_gmt'            => array( 'view', 'edit' ),
49
		'usage_count'                 => array( 'view', 'edit' ),
50
		'individual_use'              => array( 'view', 'edit' ),
51
		'product_ids'                 => array( 'view', 'edit' ),
52
		'excluded_product_ids'        => array( 'view', 'edit' ),
53
		'usage_limit'                 => array( 'view', 'edit' ),
54
		'usage_limit_per_user'        => array( 'view', 'edit' ),
55
		'limit_usage_to_x_items'      => array( 'view', 'edit' ),
56
		'free_shipping'               => array( 'view', 'edit' ),
57
		'product_categories'          => array( 'view', 'edit' ),
58
		'excluded_product_categories' => array( 'view', 'edit' ),
59
		'exclude_sale_items'          => array( 'view', 'edit' ),
60
		'minimum_amount'              => array( 'view', 'edit' ),
61
		'maximum_amount'              => array( 'view', 'edit' ),
62
		'email_restrictions'          => array( 'view', 'edit' ),
63
		'used_by'                     => array( 'view', 'edit' ),
64
		'meta_data'                   => array( 'view', 'edit' ),
65
	];
66
67
	/**
68
	 * Test create.
69
	 */
70
	public function test_create() {
71
		$valid_data = [
72
			'code'                        => 'test-coupon',
73
			'amount'                      => '5.00',
74
			'discount_type'               => 'fixed_product',
75
			'description'                 => 'Test description.',
76
			'date_expires'                => date( 'Y-m-d\T00:00:00', strtotime( '+1 day' ) ),
77
			'individual_use'              => true,
78
			'product_ids'                 => [ 1, 2, 3 ],
79
			'excluded_product_ids'        => [ 3, 4, 5 ],
80
			'usage_limit'                 => 10,
81
			'usage_limit_per_user'        => 10,
82
			'limit_usage_to_x_items'      => 10,
83
			'free_shipping'               => false,
84
			'product_categories'          => [ 1, 2, 3 ],
85
			'excluded_product_categories' => [ 3, 4, 5 ],
86
			'exclude_sale_items'          => true,
87
			'minimum_amount'              => '100',
88
			'maximum_amount'              => '200',
89
			'email_restrictions'          => [ '[email protected]' ],
90
			'meta_data'                   => [
91
				[
92
					'key'   => 'test_key',
93
					'value' => 'test_value',
94
				]
95
			]
96
		];
97
		$response = $this->do_request( '/wc/v4/coupons', 'POST', $valid_data );
98
		$this->assertExpectedResponse( $response, 201, $valid_data );
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type array expected by parameter $response of Automattic\WooCommerce\R...ssertExpectedResponse(). ( Ignorable by Annotation )

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

98
		$this->assertExpectedResponse( /** @scrutinizer ignore-type */ $response, 201, $valid_data );
Loading history...
99
	}
100
101
	/**
102
	 * Test read.
103
	 */
104
	public function test_read() {
105
		$coupon1 = CouponHelper::create_coupon( 'testcoupon-1' );
106
		$coupon2 = CouponHelper::create_coupon( 'testcoupon-2' );
107
		$coupon3 = CouponHelper::create_coupon( 'anothertestcoupon-3' );
108
		$coupon4 = CouponHelper::create_coupon( 'anothertestcoupon-4' );
109
110
		// Collection.
111
		$response = $this->do_request( '/wc/v4/coupons', 'GET' );
112
		$this->assertExpectedResponse( $response, 200 );
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type array expected by parameter $response of Automattic\WooCommerce\R...ssertExpectedResponse(). ( Ignorable by Annotation )

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

112
		$this->assertExpectedResponse( /** @scrutinizer ignore-type */ $response, 200 );
Loading history...
113
		$this->assertEquals( 4, count( $response->data ) );
114
115
		// Collection args.
116
		$response = $this->do_request( '/wc/v4/coupons', 'GET', [ 'code' => 'testcoupon-1' ] );
117
		$this->assertExpectedResponse( $response, 200 );
118
		$this->assertEquals( 1, count( $response->data ) );
119
120
		$response = $this->do_request( '/wc/v4/coupons', 'GET', [ 'search' => 'anothertestcoupon' ] );
121
		$this->assertExpectedResponse( $response, 200 );
122
		$this->assertEquals( 2, count( $response->data ) );
123
124
		// Single.
125
		$response = $this->do_request( '/wc/v4/coupons/' . $coupon1->get_id(), 'GET' );
126
		$this->assertExpectedResponse( $response, 200 );
127
128
		foreach ( $this->get_properties( 'view' ) as $property ) {
129
			$this->assertArrayHasKey( $property, $response->data );
130
		}
131
132
		// Invalid.
133
		$response = $this->do_request( '/wc/v4/coupons/0', 'GET' );
134
		$this->assertExpectedResponse( $response, 404 );
135
	}
136
137
	/**
138
	 * Test update.
139
	 */
140
	public function test_update() {
141
		// Invalid.
142
		$response = $this->do_request( '/wc/v4/coupons/0', 'POST', [ 'code' => 'test' ] );
143
		$this->assertExpectedResponse( $response, 404 );
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type array expected by parameter $response of Automattic\WooCommerce\R...ssertExpectedResponse(). ( Ignorable by Annotation )

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

143
		$this->assertExpectedResponse( /** @scrutinizer ignore-type */ $response, 404 );
Loading history...
144
145
		// Update existing.
146
		$coupon   = CouponHelper::create_coupon( 'testcoupon-1' );
147
		$response = $this->do_request(
148
			'/wc/v4/coupons/' . $coupon->get_id(),
149
			'POST',
150
			[
151
				'code'        => 'new-code',
152
				'description' => 'new description',
153
			]
154
		);
155
		$this->assertExpectedResponse( $response, 200 );
156
157
		foreach ( $this->get_properties( 'view' ) as $property ) {
158
			$this->assertArrayHasKey( $property, $response->data );
159
		}
160
161
		$this->assertEquals( $coupon->get_id(), $response->data['id'] );
162
		$this->assertEquals( 'new-code', $response->data['code'] );
163
		$this->assertEquals( 'new description', $response->data['description'] );
164
	}
165
166
	/**
167
	 * Test delete.
168
	 */
169
	public function test_delete() {
170
		// Invalid.
171
		$result = $this->do_request( '/wc/v4/coupons/0', 'DELETE', [ 'force' => false ] );
172
		$this->assertEquals( 404, $result->status );
173
174
		// Trash.
175
		$coupon = CouponHelper::create_coupon( 'testcoupon-1' );
176
177
		$result = $this->do_request( '/wc/v4/coupons/' . $coupon->get_id(), 'DELETE', [ 'force' => false ] );
178
		$this->assertEquals( 200, $result->status );
179
		$this->assertEquals( 'trash', get_post_status( $coupon->get_id() ) );
180
181
		// Force.
182
		$coupon = CouponHelper::create_coupon( 'testcoupon-2' );
183
184
		$result = $this->do_request( '/wc/v4/coupons/' . $coupon->get_id(), 'DELETE', [ 'force' => true ] );
185
		$this->assertEquals( 200, $result->status );
186
		$this->assertEquals( false, get_post( $coupon->get_id() ) );
187
	}
188
189
	/**
190
	 * Test read.
191
	 */
192
	public function test_guest_create() {
193
		wp_set_current_user( 0 );
194
		$valid_data = [
195
			'code'                        => 'test-coupon',
196
			'amount'                      => '5.00',
197
			'discount_type'               => 'fixed_product',
198
			'description'                 => 'Test description.',
199
			'date_expires'                => date( 'Y-m-d\T00:00:00', strtotime( '+1 day' ) ),
200
			'individual_use'              => true,
201
			'product_ids'                 => [ 1, 2, 3 ],
202
			'excluded_product_ids'        => [ 3, 4, 5 ],
203
			'usage_limit'                 => 10,
204
			'usage_limit_per_user'        => 10,
205
			'limit_usage_to_x_items'      => 10,
206
			'free_shipping'               => false,
207
			'product_categories'          => [ 1, 2, 3 ],
208
			'excluded_product_categories' => [ 3, 4, 5 ],
209
			'exclude_sale_items'          => true,
210
			'minimum_amount'              => '100',
211
			'maximum_amount'              => '200',
212
			'email_restrictions'          => [ '[email protected]' ],
213
			'meta_data'                   => [
214
				[
215
					'key'   => 'test_key',
216
					'value' => 'test_value',
217
				]
218
			]
219
		];
220
		$response = $this->do_request( '/wc/v4/coupons', 'POST', $valid_data );
221
		$this->assertExpectedResponse( $response, 401 );
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type array expected by parameter $response of Automattic\WooCommerce\R...ssertExpectedResponse(). ( Ignorable by Annotation )

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

221
		$this->assertExpectedResponse( /** @scrutinizer ignore-type */ $response, 401 );
Loading history...
222
	}
223
224
	/**
225
	 * Test read.
226
	 */
227
	public function test_guest_read() {
228
		wp_set_current_user( 0 );
229
		$response = $this->do_request( '/wc/v4/coupons', 'GET' );
230
		$this->assertExpectedResponse( $response, 401 );
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type array expected by parameter $response of Automattic\WooCommerce\R...ssertExpectedResponse(). ( Ignorable by Annotation )

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

230
		$this->assertExpectedResponse( /** @scrutinizer ignore-type */ $response, 401 );
Loading history...
231
	}
232
233
	/**
234
	 * Test update.
235
	 */
236
	public function test_guest_update() {
237
		wp_set_current_user( 0 );
238
		$coupon   = CouponHelper::create_coupon( 'testcoupon-1' );
239
		$response = $this->do_request(
240
			'/wc/v4/coupons/' . $coupon->get_id(),
241
			'POST',
242
			[
243
				'code'        => 'new-code',
244
				'description' => 'new description',
245
			]
246
		);
247
		$this->assertExpectedResponse( $response, 401 );
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type array expected by parameter $response of Automattic\WooCommerce\R...ssertExpectedResponse(). ( Ignorable by Annotation )

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

247
		$this->assertExpectedResponse( /** @scrutinizer ignore-type */ $response, 401 );
Loading history...
248
	}
249
250
	/**
251
	 * Test delete.
252
	 */
253
	public function test_guest_delete() {
254
		wp_set_current_user( 0 );
255
		$coupon = CouponHelper::create_coupon( 'testcoupon-1' );
256
		$result = $this->do_request( '/wc/v4/coupons/' . $coupon->get_id(), 'DELETE', [ 'force' => false ] );
257
		$this->assertEquals( 401, $result->status );
258
	}
259
260
	/**
261
	 * Test validation.
262
	 */
263
	public function test_enum_discount_type() {
264
		$result = $this->do_request(
265
			'/wc/v4/coupons',
266
			'POST',
267
			[
268
				'code'          => 'test',
269
				'amount'        => '5.00',
270
				'discount_type' => 'fake',
271
			]
272
		);
273
274
		$this->assertEquals( 400, $result->status );
275
		$this->assertEquals( 'Invalid parameter(s): discount_type', $result->data['message'] );
276
	}
277
278
	/**
279
	 * Test a batch update.
280
	 */
281
	public function test_batch() {
282
		$coupon_1 = CouponHelper::create_coupon( 'batchcoupon-1' );
283
		$coupon_2 = CouponHelper::create_coupon( 'batchcoupon-2' );
284
		$coupon_3 = CouponHelper::create_coupon( 'batchcoupon-3' );
285
		$coupon_4 = CouponHelper::create_coupon( 'batchcoupon-4' );
286
287
		$result = $this->do_request(
288
			'/wc/v4/coupons/batch',
289
			'POST',
290
			array(
291
				'update' => array(
292
					array(
293
						'id'     => $coupon_1->get_id(),
294
						'amount' => '5.15',
295
					),
296
				),
297
				'delete' => array(
298
					$coupon_2->get_id(),
299
					$coupon_3->get_id(),
300
				),
301
				'create' => array(
302
					array(
303
						'code'   => 'new-coupon',
304
						'amount' => '11.00',
305
					),
306
				),
307
			)
308
		);
309
310
		$this->assertEquals( '5.15', $result->data['update'][0]['amount'] );
311
		$this->assertEquals( '11.00', $result->data['create'][0]['amount'] );
312
		$this->assertEquals( 'new-coupon', $result->data['create'][0]['code'] );
313
		$this->assertEquals( $coupon_2->get_id(), $result->data['delete'][0]['previous']['id'] );
314
		$this->assertEquals( $coupon_3->get_id(), $result->data['delete'][1]['previous']['id'] );
315
316
		$result = $this->do_request( '/wc/v4/coupons' );
317
		$this->assertEquals( 3, count( $result->data ) );
318
	}
319
}
320