1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\SalesRule\Api;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;
class CouponManagementTest extends WebapiAbstract
{
const SERVICE_NAME = 'salesRuleCouponManagementV1';
const RESOURCE_PATH = '/V1/coupons';
const SERVICE_VERSION = "V1";
const SERVICE_NAME_COUPON = 'salesRuleCouponRepositoryV1';
const RESOURCE_PATH_COUPON = '/V1/coupons';
const SERVICE_VERSION_COUPON = "V1";
/**
* @param int $count
* @param int $length
* @param string $format
* @param string $regex
* @dataProvider dataProviderForTestGenerate
* @magentoApiDataFixture Magento/SalesRule/_files/rules_autogeneration.php
*/
public function testManagement($count, $length, $format, $regex)
{
/** @var $registry \Magento\Framework\Registry */
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
/** @var $salesRule \Magento\SalesRule\Model\Rule */
$salesRule = $registry->registry('_fixture/Magento_SalesRule_Api_RuleRepository');
$ruleId = $salesRule->getRuleId();
$result = $this->generate($ruleId, $count, $length, $format);
$this->assertTrue(is_array($result));
$this->assertTrue(count($result) == $count);
foreach ($result as $code) {
$this->assertRegExp($regex, $code);
}
$couponList = $this->getList($ruleId);
$couponIds = [];
$couponCodes = [];
$cnt = 0;
if (is_array($couponList)) {
foreach ($couponList as $coupon) {
if ($cnt < $count / 2) {
$couponIds[] = $coupon['coupon_id'];
}
$cnt++;
}
$cnt=0;
foreach ($couponList as $coupon) {
if ($cnt >= $count / 2) {
$couponCodes[] = $coupon['code'];
}
$cnt++;
}
}
$couponMassDeleteResult = $this->deleteCouponsByCodes($couponCodes);
$this->assertEmpty($couponMassDeleteResult['failed_items']);
$couponList = $this->getList($ruleId);
$this->assertTrue(count($couponList) == $cnt / 2);
$couponMassDeleteResult = $this->deleteCouponsById($couponIds);
$this->assertEmpty($couponMassDeleteResult['failed_items']);
$couponList = $this->getList($ruleId);
$this->assertTrue(count($couponList) == 0);
}
/**
* @return array
*/
public function dataProviderForTestGenerate()
{
return [
[
10,
12,
'alphanum',
'/[a-zA-Z0-9]{12}/',
],
[
10,
10,
'num',
'/[0-9]{10}/',
],
[
10,
8,
'alpha',
'/[a-zA-Z]{8}/',
],
];
}
/**
* @param int $ruleId
* @param int $count
* @param int $length
* @param string $format
* @return array
*/
public function generate($ruleId, $count, $length, $format)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . "/generate",
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'generate',
],
];
$requestData = [ "couponSpec"=>
[
"rule_id" => $ruleId,
"quantity" => $count,
"length" => $length,
"format" => $format
]
];
$result = $this->_webApiCall($serviceInfo, $requestData);
return $result;
}
/**
* @param int $ruleId
* @return array
*/
protected function getList($ruleId)
{
$searchCriteria = [
'searchCriteria' => [
'filter_groups' => [
[
'filters' => [
[
'field' => 'rule_id',
'value' => $ruleId,
'condition_type' => 'eq',
],
],
],
],
'current_page' => 1,
'page_size' => 9999,
],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH_COUPON . '/search' . '?' . http_build_query($searchCriteria),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME_COUPON,
'serviceVersion' => self::SERVICE_VERSION_COUPON,
'operation' => self::SERVICE_NAME_COUPON . 'GetList',
],
];
$response = $this->_webApiCall($serviceInfo, $searchCriteria);
return $response['items'];
}
/**
* @param array $couponArray
* @return array
*/
protected function deleteCouponsById($couponArray)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/deleteByIds' ,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'deleteByIds',
],
];
return $this->_webApiCall($serviceInfo, ['ids' => $couponArray]);
}
/**
* @param array $couponArray
* @return array
*/
protected function deleteCouponsByCodes($couponArray)
{
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/deleteByCodes' ,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
],
'soap' => [
'service' => self::SERVICE_NAME,
'serviceVersion' => self::SERVICE_VERSION,
'operation' => self::SERVICE_NAME . 'deleteByCodes',
],
];
return $this->_webApiCall($serviceInfo, ['codes' => $couponArray]);
}
}