|
23 | 23 | namespace PsApiResourcesTest\Integration\ApiPlatform; |
24 | 24 |
|
25 | 25 | use PrestaShop\PrestaShop\Core\Domain\Discount\Command\AddDiscountCommand; |
| 26 | +use Symfony\Component\HttpFoundation\Response; |
26 | 27 | use Tests\Resources\DatabaseDump; |
27 | 28 | use Tests\Resources\Resetter\LanguageResetter; |
28 | 29 |
|
@@ -248,6 +249,16 @@ public static function getProtectedEndpoints(): iterable |
248 | 249 | 'PATCH', |
249 | 250 | '/discounts/1/conditions', |
250 | 251 | ]; |
| 252 | + |
| 253 | + yield 'bulk toggle status endpoint' => [ |
| 254 | + 'PATCH', |
| 255 | + '/discounts/bulk-update-status', |
| 256 | + ]; |
| 257 | + |
| 258 | + yield 'bulk delete endpoint' => [ |
| 259 | + 'DELETE', |
| 260 | + '/discounts/bulk-delete', |
| 261 | + ]; |
251 | 262 | } |
252 | 263 |
|
253 | 264 | /** |
@@ -434,4 +445,134 @@ public function testGetUpdatedDiscountConditions(int $discountId): void |
434 | 445 |
|
435 | 446 | $this->addToAssertionCount(1); |
436 | 447 | } |
| 448 | + |
| 449 | + /** |
| 450 | + * Test bulk enable discounts |
| 451 | + */ |
| 452 | + public function testBulkEnableDiscounts(): array |
| 453 | + { |
| 454 | + // Create multiple discounts for bulk testing |
| 455 | + $discountIds = []; |
| 456 | + for ($i = 0; $i < 3; ++$i) { |
| 457 | + $discount = $this->createItem('/discounts', [ |
| 458 | + 'type' => self::CART_LEVEL, |
| 459 | + 'names' => [ |
| 460 | + 'en-US' => 'Bulk test discount ' . $i, |
| 461 | + 'fr-FR' => 'Discount test bulk ' . $i, |
| 462 | + ], |
| 463 | + ], ['discount_write']); |
| 464 | + $discountIds[] = $discount['discountId']; |
| 465 | + } |
| 466 | + |
| 467 | + // Bulk enable all discounts |
| 468 | + $this->partialUpdateItem('/discounts/bulk-update-status', [ |
| 469 | + 'discountIds' => $discountIds, |
| 470 | + 'enabled' => true, |
| 471 | + ], ['discount_write'], Response::HTTP_NO_CONTENT); |
| 472 | + |
| 473 | + // Verify all discounts are enabled |
| 474 | + foreach ($discountIds as $discountId) { |
| 475 | + $discount = $this->getItem('/discounts/' . $discountId, ['discount_read']); |
| 476 | + $this->assertTrue($discount['enabled'], "Discount {$discountId} should be enabled"); |
| 477 | + } |
| 478 | + |
| 479 | + return $discountIds; |
| 480 | + } |
| 481 | + |
| 482 | + /** |
| 483 | + * @depends testBulkEnableDiscounts |
| 484 | + * |
| 485 | + * Test bulk disable discounts |
| 486 | + */ |
| 487 | + public function testBulkDisableDiscounts(array $discountIds): array |
| 488 | + { |
| 489 | + // Bulk disable all discounts |
| 490 | + $this->partialUpdateItem('/discounts/bulk-update-status', [ |
| 491 | + 'discountIds' => $discountIds, |
| 492 | + 'enabled' => false, |
| 493 | + ], ['discount_write'], Response::HTTP_NO_CONTENT); |
| 494 | + |
| 495 | + // Verify all discounts are disabled |
| 496 | + foreach ($discountIds as $discountId) { |
| 497 | + $discount = $this->getItem('/discounts/' . $discountId, ['discount_read']); |
| 498 | + $this->assertFalse($discount['enabled'], "Discount {$discountId} should be disabled"); |
| 499 | + } |
| 500 | + |
| 501 | + return $discountIds; |
| 502 | + } |
| 503 | + |
| 504 | + /** |
| 505 | + * @depends testBulkDisableDiscounts |
| 506 | + * |
| 507 | + * Test bulk delete discounts |
| 508 | + */ |
| 509 | + public function testBulkDeleteDiscounts(array $discountIds): void |
| 510 | + { |
| 511 | + // Bulk delete all discounts |
| 512 | + $this->bulkDeleteItems('/discounts/bulk-delete', [ |
| 513 | + 'discountIds' => $discountIds, |
| 514 | + ], ['discount_write'], Response::HTTP_NO_CONTENT); |
| 515 | + |
| 516 | + // Verify all discounts are deleted |
| 517 | + foreach ($discountIds as $discountId) { |
| 518 | + $this->getItem('/discounts/' . $discountId, ['discount_read'], 404); |
| 519 | + } |
| 520 | + } |
| 521 | + |
| 522 | + /** |
| 523 | + * Test bulk enable with mixed valid and invalid IDs |
| 524 | + */ |
| 525 | + public function testBulkEnableWithInvalidIds(): void |
| 526 | + { |
| 527 | + // Create one valid discount |
| 528 | + $discount = $this->createItem('/discounts', [ |
| 529 | + 'type' => self::CART_LEVEL, |
| 530 | + 'names' => [ |
| 531 | + 'en-US' => 'Valid discount', |
| 532 | + 'fr-FR' => 'Discount valide', |
| 533 | + ], |
| 534 | + ], ['discount_write']); |
| 535 | + $validId = $discount['discountId']; |
| 536 | + |
| 537 | + // Try to bulk enable with mixed valid and invalid IDs |
| 538 | + $this->partialUpdateItem('/discounts/bulk-update-status', [ |
| 539 | + 'discountIds' => [$validId, 999999], |
| 540 | + 'enabled' => true, |
| 541 | + ], ['discount_write'], 422); |
| 542 | + } |
| 543 | + |
| 544 | + /** |
| 545 | + * Test bulk delete with empty array |
| 546 | + */ |
| 547 | + public function testBulkDeleteWithEmptyArray(): void |
| 548 | + { |
| 549 | + $this->bulkDeleteItems('/discounts/bulk-delete', [ |
| 550 | + 'discountIds' => [], |
| 551 | + ], ['discount_write'], 422); |
| 552 | + } |
| 553 | + |
| 554 | + /** |
| 555 | + * Test bulk toggle status with empty array |
| 556 | + */ |
| 557 | + public function testBulkToggleStatusWithEmptyArray(): void |
| 558 | + { |
| 559 | + $this->partialUpdateItem('/discounts/bulk-update-status', [ |
| 560 | + 'discountIds' => [], |
| 561 | + 'enabled' => true, |
| 562 | + ], ['discount_write'], 422); |
| 563 | + } |
| 564 | + |
| 565 | + /** |
| 566 | + * Test bulk delete without providing discountIds parameter |
| 567 | + */ |
| 568 | + public function testBulkDeleteWithMissingParameter(): void |
| 569 | + { |
| 570 | + $bearerToken = $this->getBearerToken(['discount_write']); |
| 571 | + static::createClient()->request('DELETE', '/discounts/bulk-delete', [ |
| 572 | + 'auth_bearer' => $bearerToken, |
| 573 | + 'json' => [], |
| 574 | + ]); |
| 575 | + |
| 576 | + self::assertResponseStatusCodeSame(422); |
| 577 | + } |
437 | 578 | } |
0 commit comments