|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2026 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\SalesRule\Test\Mftf\Helper; |
| 9 | + |
| 10 | +use Magento\FunctionalTestingFramework\DataGenerator\Persist\CurlHandler; |
| 11 | +use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject; |
| 12 | +use Magento\FunctionalTestingFramework\Helper\Helper; |
| 13 | +use Magento\FunctionalTestingFramework\ObjectManagerFactory; |
| 14 | +use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil; |
| 15 | + |
| 16 | +/** |
| 17 | + * This helper use to delete sales rules via API: |
| 18 | + * |
| 19 | + * - CurlHandler::executeRequest() for all API calls |
| 20 | + * - Operation definitions from SalesRuleMeta.xml |
| 21 | + * - EntityDataObject for request structure |
| 22 | + * |
| 23 | + * NO custom HTTP clients, NO external dependencies, NO custom helpers |
| 24 | + */ |
| 25 | +class SalesRuleApiHelper extends Helper |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Delete all cart price rules using API (faster and more reliable than UI-based deletion) |
| 29 | + * |
| 30 | + * @param string $enableLogging Enable progress logging (true/false) |
| 31 | + * @param string $pageSize Rules per page |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function deleteAllSalesRulesApi(string $enableLogging = 'true', string $pageSize = '100'): void |
| 35 | + { |
| 36 | + $enableLog = ($enableLogging === 'true'); |
| 37 | + $pageNum = (int)$pageSize; |
| 38 | + $stats = [ |
| 39 | + 'total_deleted' => 0, |
| 40 | + 'total_failed' => 0, |
| 41 | + ]; |
| 42 | + |
| 43 | + $this->logMessage($enableLog, "Starting cart price rule deletion via API..."); |
| 44 | + |
| 45 | + try { |
| 46 | + $allRules = $this->getAllSalesRules($pageNum); |
| 47 | + |
| 48 | + if ($this->handleEmptyRuleList($allRules, $enableLog)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + $this->processRuleDeletion($allRules, $stats, $enableLog); |
| 53 | + $message = "Cart price rule deletion completed: {$stats['total_deleted']} successful, " . |
| 54 | + "{$stats['total_failed']} failed."; |
| 55 | + $this->logMessage($enableLog, $message); |
| 56 | + |
| 57 | + } catch (\Exception $e) { |
| 58 | + $this->logMessage($enableLog, "ERROR: Cart price rule deletion failed: " . $e->getMessage()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Delete single rule by ID using MFTF CurlHandler |
| 64 | + * |
| 65 | + * @param int $ruleId |
| 66 | + * @param array $stats |
| 67 | + * @return void |
| 68 | + * @throws \Exception |
| 69 | + */ |
| 70 | + private function deleteByRuleId(int $ruleId, array &$stats): void |
| 71 | + { |
| 72 | + if (empty($ruleId)) { |
| 73 | + throw new \Exception("Rule ID cannot be empty"); |
| 74 | + } |
| 75 | + |
| 76 | + $ruleEntity = new EntityDataObject( |
| 77 | + name: 'salesrule_to_delete_' . $ruleId, |
| 78 | + type: 'SalesRule', |
| 79 | + data: ['rule_id' => $ruleId], |
| 80 | + linkedEntities: [], |
| 81 | + uniquenessData: [], |
| 82 | + vars: [], |
| 83 | + parentEntity: null, |
| 84 | + filename: null, |
| 85 | + deprecated: null |
| 86 | + ); |
| 87 | + |
| 88 | + $curlHandler = ObjectManagerFactory::getObjectManager()->create( |
| 89 | + CurlHandler::class, |
| 90 | + [ |
| 91 | + 'operation' => 'delete', |
| 92 | + 'entityObject' => $ruleEntity, |
| 93 | + 'storeCode' => null |
| 94 | + ] |
| 95 | + ); |
| 96 | + $response = $curlHandler->executeRequest([]); |
| 97 | + |
| 98 | + if (!$this->isResponseSuccessful($response)) { |
| 99 | + $errorMessage = "Rule deletion failed for ID '{$ruleId}' - unexpected response: " . json_encode($response); |
| 100 | + throw new \Exception($errorMessage); |
| 101 | + } |
| 102 | + |
| 103 | + $stats['total_deleted']++; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Log message if logging is enabled |
| 108 | + * |
| 109 | + * @param bool $enableLog |
| 110 | + * @param string $message |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + private function logMessage(bool $enableLog, string $message): void |
| 114 | + { |
| 115 | + if ($enableLog) { |
| 116 | + LoggingUtil::getInstance()->getLogger(self::class)->info($message); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Handle empty rule list scenario |
| 122 | + * |
| 123 | + * @param array $allRules |
| 124 | + * @param bool $enableLog |
| 125 | + * @return bool True if should return early, false to continue |
| 126 | + */ |
| 127 | + private function handleEmptyRuleList(array $allRules, bool $enableLog): bool |
| 128 | + { |
| 129 | + if (empty($allRules)) { |
| 130 | + $this->logMessage($enableLog, "No cart price rules found."); |
| 131 | + return true; |
| 132 | + } |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Process deletion of all rules |
| 138 | + * |
| 139 | + * @param array $allRules |
| 140 | + * @param array $stats |
| 141 | + * @param bool $enableLog |
| 142 | + * @return void |
| 143 | + */ |
| 144 | + private function processRuleDeletion(array $allRules, array &$stats, bool $enableLog): void |
| 145 | + { |
| 146 | + foreach ($allRules as $rule) { |
| 147 | + $ruleId = $rule['rule_id'] ?? ''; |
| 148 | + |
| 149 | + if (empty($ruleId)) { |
| 150 | + $stats['total_failed']++; |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + try { |
| 155 | + $this->deleteByRuleId((int)$ruleId, $stats); |
| 156 | + } catch (\Exception $e) { |
| 157 | + $stats['total_failed']++; |
| 158 | + $ruleName = $rule['name'] ?? 'Unknown'; |
| 159 | + $errorMsg = "Failed to delete rule '{$ruleName}' (ID: {$ruleId}): " . $e->getMessage(); |
| 160 | + $this->logMessage($enableLog, $errorMsg); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Check if API response indicates success |
| 167 | + * |
| 168 | + * @param mixed $response |
| 169 | + * @return bool |
| 170 | + */ |
| 171 | + private function isResponseSuccessful($response): bool |
| 172 | + { |
| 173 | + // Direct success values |
| 174 | + if (in_array($response, [true, 'true', '1', ''], true)) { |
| 175 | + return true; |
| 176 | + } |
| 177 | + |
| 178 | + // String response handling |
| 179 | + if (is_string($response)) { |
| 180 | + return $this->validateStringResponse($response); |
| 181 | + } |
| 182 | + |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Validate string response for success indicators |
| 188 | + * |
| 189 | + * @param string $response |
| 190 | + * @return bool |
| 191 | + */ |
| 192 | + private function validateStringResponse(string $response): bool |
| 193 | + { |
| 194 | + // Try JSON parsing first |
| 195 | + $jsonData = json_decode($response, true); |
| 196 | + if (json_last_error() === JSON_ERROR_NONE) { |
| 197 | + return ($jsonData === true || |
| 198 | + ($jsonData['success'] ?? false) || |
| 199 | + ($jsonData['status'] ?? '') === 'success'); |
| 200 | + } |
| 201 | + |
| 202 | + // Plain text fallback |
| 203 | + return in_array(trim($response), ['true', '1', 'success', ''], true); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Get all cart price rules using MFTF CurlHandler with GetSalesRuleList operation |
| 208 | + * |
| 209 | + * @param int $pageSize Rules per page |
| 210 | + * @return array Rule data |
| 211 | + * @throws \Exception |
| 212 | + */ |
| 213 | + private function getAllSalesRules(int $pageSize): array |
| 214 | + { |
| 215 | + try { |
| 216 | + // Create EntityDataObject for GetSalesRuleList operation |
| 217 | + $ruleListEntity = new EntityDataObject( |
| 218 | + name: 'salesrule_list_all', |
| 219 | + type: 'salesrule_list', |
| 220 | + data: [ |
| 221 | + 'pageSize' => $pageSize, |
| 222 | + 'currentPage' => 1, |
| 223 | + 'fields' => 'items[rule_id,name,is_active]' |
| 224 | + ], |
| 225 | + linkedEntities: [], |
| 226 | + uniquenessData: [], |
| 227 | + vars: [], |
| 228 | + parentEntity: null, |
| 229 | + filename: null, |
| 230 | + deprecated: null |
| 231 | + ); |
| 232 | + |
| 233 | + // Create CurlHandler for GetSalesRuleList operation |
| 234 | + $curlHandler = ObjectManagerFactory::getObjectManager()->create( |
| 235 | + CurlHandler::class, |
| 236 | + [ |
| 237 | + 'operation' => 'get', |
| 238 | + 'entityObject' => $ruleListEntity, |
| 239 | + 'storeCode' => null |
| 240 | + ] |
| 241 | + ); |
| 242 | + |
| 243 | + // Execute using MFTF CurlHandler |
| 244 | + $response = $curlHandler->executeRequest([]); |
| 245 | + |
| 246 | + // Parse response |
| 247 | + if (is_string($response)) { |
| 248 | + $responseData = json_decode($response, true, 512, JSON_THROW_ON_ERROR); |
| 249 | + } else { |
| 250 | + $responseData = $response; |
| 251 | + } |
| 252 | + |
| 253 | + return $responseData['items'] ?? []; |
| 254 | + |
| 255 | + } catch (\Exception $e) { |
| 256 | + $errorMessage = "Failed to retrieve cart price rules: " . $e->getMessage(); |
| 257 | + throw new \Exception($errorMessage, 0, $e); |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments