From a02b1b32fefd062786bd3c9ca5cebbaa4dac69d1 Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Sun, 11 Jun 2023 21:30:45 -0700 Subject: [PATCH] Re-check promo code validity during checkout (#1316) * Re-check promo code validity during checkout * Add tools mock to the failing test * Properly resolve issue with the tests --- src/modules/Cart/Service.php | 8 ++++++++ tests/modules/Cart/ServiceTest.php | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/modules/Cart/Service.php b/src/modules/Cart/Service.php index c7b7794f7..a1f31760a 100644 --- a/src/modules/Cart/Service.php +++ b/src/modules/Cart/Service.php @@ -424,6 +424,14 @@ class Service implements InjectionAwareInterface if (!$this->isClientAbleToUsePromo($client, $promo)) { throw new \Box_Exception('You have already used this promo code. Please remove promo code and checkout again.', null, 9874); } + + if (!$promo instanceof \Model_Promo) { + throw new \Box_Exception('Promo code is expired or does not exist'); + } + + if (!$this->isPromoAvailableForClientGroup($promo)) { + throw new \Box_Exception('Promo can not be applied to your account'); + } } $this->di['events_manager']->fire( diff --git a/tests/modules/Cart/ServiceTest.php b/tests/modules/Cart/ServiceTest.php index 17b93b7f3..7db4d7ed3 100644 --- a/tests/modules/Cart/ServiceTest.php +++ b/tests/modules/Cart/ServiceTest.php @@ -654,13 +654,15 @@ class ServiceTest extends \BBTestCase $order->loadBean(new \DummyBean()); $serviceMock = $this->getMockBuilder('\Box\Mod\Cart\Service') - ->setMethods(array('createFromCart', 'isClientAbleToUsePromo', 'rm'))->getMock(); + ->setMethods(array('createFromCart', 'isClientAbleToUsePromo', 'rm', 'isPromoAvailableForClientGroup'))->getMock(); $serviceMock->expects($this->atLeastOnce())->method('createFromCart') ->will($this->returnValue(array($order, rand(1, 100), array(rand(1, 100))))); $serviceMock->expects($this->atLeastOnce())->method('isClientAbleToUsePromo') ->will($this->returnValue(true)); $serviceMock->expects($this->atLeastOnce())->method('rm') ->will($this->returnValue(true)); + $serviceMock->expects($this->atLeastOnce())->method('isPromoAvailableForClientGroup') + ->will($this->returnValue(true)); $eventMock = $this->getMockBuilder('\Box_EventManager')->getMock(); $eventMock->expects($this->atLeastOnce())->method('fire'); @@ -671,7 +673,6 @@ class ServiceTest extends \BBTestCase ->method('getClientAddress') ->will($this->returnValue('1.1.1.1')); - $invoice = new \Model_Invoice(); $invoice->loadBean(new \DummyBean()); $invoice->hash = sha1('str');