From d796e02172cf35d6c34f3651ead091be84147aa9 Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Thu, 14 Nov 2019 12:02:59 +0100 Subject: [PATCH 01/10] Cleanup Add early exit for "fatal" errors like wrong usergroup, not valid yet, not valid anymore, etc. fix broken HTML fix date format for magemail (hopefully) --- .../PromoCodeMessages/Model/Validator.php | 77 +++++++++++++------ 1 file changed, 52 insertions(+), 25 deletions(-) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php index d75bc7a..83b7ceb 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php @@ -105,7 +105,7 @@ public function validate($couponCode, $quote) // no coupon if (!$coupon->getId()) { - Mage::throwException($this->_formatMessage('Coupon code does not exist.')); + Mage::throwException($this->_formatMessageWithContainer('Coupon code does not exist.')); } /** @var $rule Mage_SalesRule_Model_Rule */ @@ -134,22 +134,22 @@ public function validate($couponCode, $quote) */ protected function _validateGeneral($rule, $coupon) { - $msg = ''; if (!$rule->getIsActive()) { - Mage::throwException($this->_formatMessage('Your coupon is inactive.')); + Mage::throwException($this->_formatMessageWithContainer('Your coupon is inactive.')); } // check websites $websiteIds = $rule->getWebsiteIds(); - if (!in_array($this->_getQuote()->getStore()->getWebsiteId(), $websiteIds)) { + if (!in_array($this->_getQuote()->getStore()->getWebsiteId(), $websiteIds, false)) { $websiteNames = Mage::getResourceModel('core/website_collection') ->addFieldToFilter('website_id', ['in' => $websiteIds]) ->getColumnValues('name'); - $msg .= $this->_formatMessage( + $msg .= $this->_formatMessageWithContainer( 'Your coupon is not valid for this store.', implode(', ', $websiteNames), 'Allowed Websites: %s.' ); + Mage::throwException($msg); } // check customer groups @@ -158,11 +158,12 @@ protected function _validateGeneral($rule, $coupon) $customerGroupNames = Mage::getResourceModel('customer/group_collection') ->addFieldToFilter('customer_group_id', ['in' => $groupIds]) ->getColumnValues('customer_group_code'); - $msg .= $this->_formatMessage( + $msg .= $this->_formatMessageWithContainer( 'Your coupon is not valid for your Customer Group.', implode(', ', $customerGroupNames), 'Allowed Customer Groups: %s.' ); + Mage::throwException($msg); } // check dates @@ -172,10 +173,11 @@ protected function _validateGeneral($rule, $coupon) if ($rule->getFromDate()) { $fromDate = new Zend_Date($rule->getFromDate(), Varien_Date::DATE_INTERNAL_FORMAT); if ($now->isEarlier($fromDate, Zend_Date::DATE_MEDIUM)) { - $msg .= $this->_formatMessage( + $msg .= $this->_formatMessageWithContainer( 'Your coupon is not valid yet. It will be active on %s.', Mage::helper('core')->formatDate($fromDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); + Mage::throwException($msg); } } @@ -183,30 +185,33 @@ protected function _validateGeneral($rule, $coupon) if ($rule->getToDate()) { $toDate = new Zend_Date($rule->getToDate(), Varien_Date::DATE_INTERNAL_FORMAT); if ($now->isLater($toDate, Zend_Date::DATE_MEDIUM)) { - $msg .= $this->_formatMessage( + $msg .= $this->_formatMessageWithContainer( 'Your coupon is no longer valid. It expired on %s.', Mage::helper('core')->formatDate($toDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); + Mage::throwException($msg); } } // magemail coupon-level auto-expiration date $isCouponAlreadyUsed = $coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit(); - if ($coupon->getdata('magemail_expired_at') && $isCouponAlreadyUsed) { - $expirationDate = Mage::getSingleton('core/date')->date('M d, Y', $coupon->getdata('magemail_expired_at')); - $msg .= $this->_formatMessage( + if ($isCouponAlreadyUsed && $coupon->getData('magemail_expired_at')) { + $mageMailToDate = new Zend_Date($coupon->getData('magemail_expired_at'), Varien_Date::DATE_INTERNAL_FORMAT); + $msg .= $this->_formatMessageWithContainer( 'Your coupon is no longer valid. It expired on %s.', - $expirationDate + Mage::helper('core')->formatDate($mageMailToDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); + Mage::throwException($msg); } // check global usage limit if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) { - $msg .= $this->_formatMessage( + $msg .= $this->_formatMessageWithContainer( 'Your coupon was already used.', $coupon->getUsageLimit(), 'It may only be used %s time(s).' ); + Mage::throwException($msg); } // check per customer usage limit $customerId = $this->_getQuote()->getCustomerId(); @@ -215,15 +220,17 @@ protected function _validateGeneral($rule, $coupon) Mage::getResourceModel('salesrule/coupon_usage')->loadByCustomerCoupon( $couponUsage, $customerId, - $coupon->getId()); + $coupon->getId() + ); if ($couponUsage->getCouponId() && $couponUsage->getTimesUsed() >= $coupon->getUsagePerCustomer() ) { - $msg .= $this->_formatMessage( + $msg .= $this->_formatMessageWithContainer( 'You have already used your coupon.', $coupon->getUsagePerCustomer(), 'It may only be used %s time(s).' ); + Mage::throwException($msg); } } @@ -232,18 +239,15 @@ protected function _validateGeneral($rule, $coupon) if ($ruleId && $rule->getUsesPerCustomer()) { $ruleCustomer = Mage::getModel('salesrule/rule_customer'); $ruleCustomer->loadByCustomerRule($customerId, $ruleId); - if ($ruleCustomer->getId()) { - if ($ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) { - $msg .= $this->_formatMessage( - 'You have already used your coupon.', - $rule->getUsesPerCustomer(), - 'It may only be used %s time(s).' - ); - } + if ($ruleCustomer->getId() && $ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) { + $msg .= $this->_formatMessageWithContainer( + 'You have already used your coupon.', + $rule->getUsesPerCustomer(), + 'It may only be used %s time(s).' + ); + Mage::throwException($msg); } } - - return $msg; } /** @@ -462,6 +466,25 @@ protected function _formatMessage($message, $params = '', $internalMessage = nul return $message; } + /** + * Wrap the message in a li + * + * @param string $message + * @param string $params + * @param string $internalMessage + * + * @return string containing entire error message + */ + protected function _formatMessageWithContainer($message, $params = '', $internalMessage = null) + { + if (!$message) { + return ''; + } + $message = '
  • ' . $this->_helper->__($message, $params) . '
  • '; + + return $this->_formatMessage($message, $params, $internalMessage); + } + /** * Implode a multidimensional array. * @@ -613,6 +636,10 @@ protected function getAttributeDisplayValue($attribute, $value, $isCurrency) $country = Mage::getModel('directory/country')->loadByCode($value); $value = $country->getName(); break; + case 'attribute_set_id': + $attributeSet = Mage::getModel('eav/entity_attribute_set')->load($value); + $value = $attributeSet->getAttributeSetName(); + break; default: $value = $isCurrency ? Mage::helper('core')->currency($value, true, false) : $value; From 2ca42109a9bda8b3214d044e88a8554803683f8c Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Thu, 14 Nov 2019 17:29:56 +0100 Subject: [PATCH 02/10] Fix undefined variable --- .../PromoCodeMessages/Model/Validator.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php index 83b7ceb..0cd55d1 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php @@ -144,7 +144,7 @@ protected function _validateGeneral($rule, $coupon) $websiteNames = Mage::getResourceModel('core/website_collection') ->addFieldToFilter('website_id', ['in' => $websiteIds]) ->getColumnValues('name'); - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'Your coupon is not valid for this store.', implode(', ', $websiteNames), 'Allowed Websites: %s.' @@ -158,7 +158,7 @@ protected function _validateGeneral($rule, $coupon) $customerGroupNames = Mage::getResourceModel('customer/group_collection') ->addFieldToFilter('customer_group_id', ['in' => $groupIds]) ->getColumnValues('customer_group_code'); - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'Your coupon is not valid for your Customer Group.', implode(', ', $customerGroupNames), 'Allowed Customer Groups: %s.' @@ -173,7 +173,7 @@ protected function _validateGeneral($rule, $coupon) if ($rule->getFromDate()) { $fromDate = new Zend_Date($rule->getFromDate(), Varien_Date::DATE_INTERNAL_FORMAT); if ($now->isEarlier($fromDate, Zend_Date::DATE_MEDIUM)) { - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'Your coupon is not valid yet. It will be active on %s.', Mage::helper('core')->formatDate($fromDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); @@ -185,7 +185,7 @@ protected function _validateGeneral($rule, $coupon) if ($rule->getToDate()) { $toDate = new Zend_Date($rule->getToDate(), Varien_Date::DATE_INTERNAL_FORMAT); if ($now->isLater($toDate, Zend_Date::DATE_MEDIUM)) { - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'Your coupon is no longer valid. It expired on %s.', Mage::helper('core')->formatDate($toDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); @@ -197,7 +197,7 @@ protected function _validateGeneral($rule, $coupon) $isCouponAlreadyUsed = $coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit(); if ($isCouponAlreadyUsed && $coupon->getData('magemail_expired_at')) { $mageMailToDate = new Zend_Date($coupon->getData('magemail_expired_at'), Varien_Date::DATE_INTERNAL_FORMAT); - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'Your coupon is no longer valid. It expired on %s.', Mage::helper('core')->formatDate($mageMailToDate, Mage_Core_Model_Locale::FORMAT_TYPE_LONG) ); @@ -206,7 +206,7 @@ protected function _validateGeneral($rule, $coupon) // check global usage limit if ($coupon->getUsageLimit() && $coupon->getTimesUsed() >= $coupon->getUsageLimit()) { - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'Your coupon was already used.', $coupon->getUsageLimit(), 'It may only be used %s time(s).' @@ -225,7 +225,7 @@ protected function _validateGeneral($rule, $coupon) if ($couponUsage->getCouponId() && $couponUsage->getTimesUsed() >= $coupon->getUsagePerCustomer() ) { - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'You have already used your coupon.', $coupon->getUsagePerCustomer(), 'It may only be used %s time(s).' @@ -240,7 +240,7 @@ protected function _validateGeneral($rule, $coupon) $ruleCustomer = Mage::getModel('salesrule/rule_customer'); $ruleCustomer->loadByCustomerRule($customerId, $ruleId); if ($ruleCustomer->getId() && $ruleCustomer->getTimesUsed() >= $rule->getUsesPerCustomer()) { - $msg .= $this->_formatMessageWithContainer( + $msg = $this->_formatMessageWithContainer( 'You have already used your coupon.', $rule->getUsesPerCustomer(), 'It may only be used %s time(s).' From 53402c9ebb4e545fb2495348d5be174d03ff4bd9 Mon Sep 17 00:00:00 2001 From: lfolco Date: Fri, 15 Nov 2019 19:02:27 -0500 Subject: [PATCH 03/10] updated tests to reflect new
  • elements --- .../Test/Model/ValidatorTest.php | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php index c55b715..bec7a72 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php @@ -85,7 +85,7 @@ public function testValidateWithInvalidCode() { $couponCode = 'sdfsdf'; $this->expectException(Mage_Core_Exception::class); - $this->expectExceptionMessage('
      Coupon code does not exist.
    '); + $this->expectExceptionMessage('
    • Coupon code does not exist.
    '); $this->validator->validate($couponCode, $this->quoteMock); } @@ -96,7 +96,7 @@ public function testValidateInvalidWebsiteIds() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is not valid for this store.
    '; + $exceptionMsg = '
    • Your coupon is not valid for this store.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); @@ -110,8 +110,8 @@ public function testValidateInvalidWebsiteIdsWithAdditionalInfo() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is not valid for this store.
    ' - . '
      Allowed Websites: Main Website.
    '; + $exceptionMsg = '
    • Your coupon is not valid for this store.
    ' + . '
    • Allowed Websites: Main Website.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -123,7 +123,7 @@ public function testInvalidCustomerGroups() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(0); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is not valid for your Customer Group.
    '; + $exceptionMsg = '
    • Your coupon is not valid for your Customer Group.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -136,8 +136,8 @@ public function testInvalidCustomerGroupsWithAdditionalInfo() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(0); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is not valid for your Customer ' - . 'Group.
      Allowed Customer Groups: General.
    '; + $exceptionMsg = '
    • Your coupon is not valid for your Customer ' + . 'Group.
    • Allowed Customer Groups: General.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -146,7 +146,7 @@ public function testInactive() { $this->rule = $this->ruleMother->generateInactiveRule(); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is inactive.
    '; + $exceptionMsg = '
    • Your coupon is inactive.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -158,8 +158,8 @@ public function testExpired() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is no longer valid. It expired on ' - . 'January 1, 2010.
    '; + $exceptionMsg = '
    • Your coupon is no longer valid. It expired on ' + . 'January 1, 2010.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -171,8 +171,8 @@ public function testNotYetActive() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon is not valid yet. It will be active ' - . 'on January 1, 2030.
    '; + $exceptionMsg = '
    • Your coupon is not valid yet. It will be active ' + . 'on January 1, 2030.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -184,7 +184,7 @@ public function testGloballyAlreadyUsed() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon was already used.
    '; + $exceptionMsg = '
    • Your coupon was already used.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -197,8 +197,8 @@ public function testGloballyAlreadyUsedWithAdditional() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon was already used.
    ' - . '
      It may only be used 1 time(s).
    '; + $exceptionMsg = '
    • Your coupon was already used.
    ' + . '
    • It may only be used 1 time(s).
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -210,7 +210,7 @@ public function testCustomerAlreadyUsed() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon was already used.
    '; + $exceptionMsg = '
    • Your coupon was already used.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -223,8 +223,8 @@ public function testCustomerAlreadyUsedWithAdditional() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      Your coupon was already used.
    ' - . '
      It may only be used 1 time(s).
    '; + $exceptionMsg = '
    • Your coupon was already used.
    ' + . '
    • It may only be used 1 time(s).
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } From 37b741b5b1566e87920d35d9d3714cc4f80f61ba Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 15:10:28 +0100 Subject: [PATCH 04/10] Early exit and coupon code inside of message --- .../PromoCodeMessages/Model/Observer.php | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php index fdf132e..0977bc1 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Observer.php @@ -23,7 +23,6 @@ class Hackathon_PromoCodeMessages_Model_Observer { - /** * Called on sales_quote_collect_totals_after. Ensure that the action is couponPost before continuing. * @@ -31,23 +30,32 @@ class Hackathon_PromoCodeMessages_Model_Observer */ public function validateCode(Varien_Event_Observer $observer) { - if (Mage::getStoreConfigFlag('checkout/promocodemessages/enabled')) { - $action = Mage::app()->getRequest()->getActionName(); + if (!Mage::getStoreConfigFlag('checkout/promocodemessages/enabled')) { + return; + } - if ($action == 'couponPost') { + if (Mage::app()->getRequest()->getActionName() !== 'couponPost') { + return; + } - if (Mage::app()->getRequest()->getParam('remove') == 1) { - return; - } + if ((int)Mage::app()->getRequest()->getParam('remove') === 1) { + return; + } - $quote = $observer->getQuote(); - $couponCode = $quote->getCouponCode(); + $quote = $observer->getQuote(); + $couponCode = $quote->getCouponCode(); - if (!$couponCode || $couponCode == '') { - // parent validation has failed - $couponCode = (string)Mage::app()->getRequest()->getParam('coupon_code'); - Mage::getModel('hackathon_promocodemessages/validator')->validate($couponCode, $quote); - } + if (!$couponCode) { + // parent validation has failed + $couponCode = (string)Mage::app()->getRequest()->getParam('coupon_code'); + try { + Mage::getModel('hackathon_promocodemessages/validator')->validate($couponCode, $quote); + } catch (Mage_Core_Exception $e) { + $msg = Mage::helper('hackathon_promocodemessages') + ->__('Your coupon "%s" could not be redeemed.', $couponCode); + $msg = "

    $msg

    "; + $msg .= $e->getMessage(); + throw new Mage_Core_Exception($msg, 0, $e); } } } From 8838bb0d80db6725e13d148f4534b243ed7e688d Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 15:50:47 +0100 Subject: [PATCH 05/10] fix broken html for internal message --- .../community/Hackathon/PromoCodeMessages/Model/Validator.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php index 0cd55d1..bc3beef 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php @@ -480,7 +480,9 @@ protected function _formatMessageWithContainer($message, $params = '', $internal if (!$message) { return ''; } + $message = '
  • ' . $this->_helper->__($message, $params) . '
  • '; + $internalMessage = '
  • ' . $this->_helper->__($internalMessage, $params) . '
  • '; return $this->_formatMessage($message, $params, $internalMessage); } From 11cc13db780e78bb2c07b2151850ad1ebe49059d Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 17:09:49 +0100 Subject: [PATCH 06/10] Add test for show stopper --- .../Test/Model/ValidatorTest.php | 49 ++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php index bec7a72..9b26714 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php @@ -66,7 +66,7 @@ protected function setUp() ->getMock(); Mage::getSingleton('core/resource')->getConnection('core_write')->beginTransaction(); $this->ruleMother = new Hackathon_PromoCodeMessages_Model_SalesRuleMother(); - $this->validator = Mage::getModel('hackathon_promocodemessages/validator'); + $this->validator = Mage::getModel('hackathon_promocodemessages/validator'); } protected function tearDown() @@ -96,12 +96,27 @@ public function testValidateInvalidWebsiteIds() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
    • Your coupon is not valid for this store.
    '; + $exceptionMsg + = '
    • Your coupon is not valid for this store.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } + public function testShowStopperOnlyShowsOneMessage() + { + // this is a failed customer group rule - but it is not checked, because the website is already wrong + + $this->rule = $this->ruleMother->generateCustomerGroupIdRule(); + $this->quoteMock->expects($this->once())->method('getStore')->willReturn($this->storeMock); + $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400); + + $this->expectException(Mage_Core_Exception::class); + $exceptionMsg = '
    • Your coupon is not valid for this store.
    '; + $this->expectExceptionMessage($exceptionMsg); + $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); + } + public function testValidateInvalidWebsiteIdsWithAdditionalInfo() { Mage::app()->getStore()->setConfig('checkout/promocodemessages/add_additional_info_on_frontend', 1); @@ -110,7 +125,8 @@ public function testValidateInvalidWebsiteIdsWithAdditionalInfo() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
    • Your coupon is not valid for this store.
    ' + $exceptionMsg + = '
    • Your coupon is not valid for this store.
    ' . '
    • Allowed Websites: Main Website.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); @@ -123,7 +139,8 @@ public function testInvalidCustomerGroups() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(0); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
    • Your coupon is not valid for your Customer Group.
    '; + $exceptionMsg + = '
    • Your coupon is not valid for your Customer Group.
    '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -136,7 +153,8 @@ public function testInvalidCustomerGroupsWithAdditionalInfo() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(0); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
    • Your coupon is not valid for your Customer ' + $exceptionMsg + = '
      • Your coupon is not valid for your Customer ' . 'Group.
      • Allowed Customer Groups: General.
      '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); @@ -146,7 +164,8 @@ public function testInactive() { $this->rule = $this->ruleMother->generateInactiveRule(); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      • Your coupon is inactive.
      '; + $exceptionMsg + = '
      • Your coupon is inactive.
      '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -158,7 +177,8 @@ public function testExpired() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
      • Your coupon is no longer valid. It expired on ' + $exceptionMsg + = '
        • Your coupon is no longer valid. It expired on ' . 'January 1, 2010.
        '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); @@ -171,7 +191,8 @@ public function testNotYetActive() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
        • Your coupon is not valid yet. It will be active ' + $exceptionMsg + = '
          • Your coupon is not valid yet. It will be active ' . 'on January 1, 2030.
          '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); @@ -184,7 +205,8 @@ public function testGloballyAlreadyUsed() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
          • Your coupon was already used.
          '; + $exceptionMsg + = '
          • Your coupon was already used.
          '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -197,7 +219,8 @@ public function testGloballyAlreadyUsedWithAdditional() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
          • Your coupon was already used.
          ' + $exceptionMsg + = '
          • Your coupon was already used.
          ' . '
          • It may only be used 1 time(s).
          '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); @@ -210,7 +233,8 @@ public function testCustomerAlreadyUsed() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
          • Your coupon was already used.
          '; + $exceptionMsg + = '
          • Your coupon was already used.
          '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); } @@ -223,7 +247,8 @@ public function testCustomerAlreadyUsedWithAdditional() $this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1); $this->quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn(1); $this->expectException(Mage_Core_Exception::class); - $exceptionMsg = '
          • Your coupon was already used.
          ' + $exceptionMsg + = '
          • Your coupon was already used.
          ' . '
          • It may only be used 1 time(s).
          '; $this->expectExceptionMessage($exceptionMsg); $this->validator->validate($this->rule->getCouponCode(), $this->quoteMock); From e52079128a296d418bbd58676f76150747a94f56 Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 17:12:08 +0100 Subject: [PATCH 07/10] Fix wrong CSV escaping --- src/app/locale/en_US/Hackathon_PromoCodeMessages.csv | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv b/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv index 967bf53..9ca67f8 100644 --- a/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv +++ b/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv @@ -28,5 +28,5 @@ "This will give customers further information on what exactly has failed with their coupon code.","This will give customers further information on what exactly has failed with their coupon code." "Validate cart rule conditions","Validate cart rule conditions" "Validate cart rule actions","Validate cart rule actions" -"Provides error messaging for conditions under \"Conditions\" tab","Provides error messaging for conditions under \"Conditions\" tab" -"Provides error messaging for actions under \"Actions\" tab","Provides error messaging for actions under \"Actions\" tab" +"Provides error messaging for conditions under ""Conditions"" tab","Provides error messaging for conditions under ""Conditions"" tab" +"Provides error messaging for actions under ""Actions"" tab","Provides error messaging for actions under ""Actions"" tab" From c0ed9e43672fe7c6c616bfa9fb049c46f5a6ce54 Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 17:15:11 +0100 Subject: [PATCH 08/10] add myself as author and add php7 as dependency --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index 4ce3d80..31bdfbb 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,14 @@ { "name": "Andreas von Studnitz", "email": "avs@integer-net.de" + }, + { + "name": "Fabian Blechschmidt", + "email": "blechschmidt@fabian-blechschmidt.de" } ], "require": { + "php": "^7.0", "magento-hackathon/magento-composer-installer": "*" }, "require-dev": { From 5ce574776eb6fc74b6d25917e98024991f4c5651 Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 17:25:52 +0100 Subject: [PATCH 09/10] Cleanup with https://kalessil.github.io/php-inspections-ultimate.html --- .../PromoCodeMessages/Model/Validator.php | 85 ++++++++++--------- .../Test/Model/SalesRuleMother.php | 76 ++++++++--------- .../PromoCodeMessages/Test/bootstrap.php | 2 +- 3 files changed, 83 insertions(+), 80 deletions(-) diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php index bc3beef..ac1f300 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php @@ -24,7 +24,7 @@ class Hackathon_PromoCodeMessages_Model_Validator extends Mage_Core_Model_Abstra { /** @var Mage_Sales_Model_Quote */ - protected $_quote = null; + protected $_quote; /** * Array of conditions attached to the current rule. @@ -45,13 +45,13 @@ class Hackathon_PromoCodeMessages_Model_Validator extends Mage_Core_Model_Abstra * * @var array */ - protected $_foundOperators = null; + protected $_foundOperators; /** * Default values for 'not found' operator options. * @var array */ - protected $_notFoundOperators = null; + protected $_notFoundOperators; /** * Rule-specific attributes that use price. Note that product attribute type is determined dynamically. @@ -87,7 +87,7 @@ public function __construct() * * @param string $couponCode * @param Mage_Sales_Model_Quote $quote - * @return string + * @return void * @throws Mage_Core_Exception */ public function validate($couponCode, $quote) @@ -129,7 +129,7 @@ public function validate($couponCode, $quote) * * @param Mage_SalesRule_Model_Rule $rule * @param Mage_SalesRule_Model_Coupon $coupon - * @return string + * @return void * @throws Mage_Core_Exception */ protected function _validateGeneral($rule, $coupon) @@ -139,6 +139,7 @@ protected function _validateGeneral($rule, $coupon) } // check websites + /** @var int[] $websiteIds */ $websiteIds = $rule->getWebsiteIds(); if (!in_array($this->_getQuote()->getStore()->getWebsiteId(), $websiteIds, false)) { $websiteNames = Mage::getResourceModel('core/website_collection') @@ -154,7 +155,7 @@ protected function _validateGeneral($rule, $coupon) // check customer groups $groupIds = $rule->getCustomerGroupIds(); - if (!in_array($this->_getQuote()->getCustomerGroupId(), $groupIds)) { + if (!in_array($this->_getQuote()->getCustomerGroupId(), $groupIds, false)) { $customerGroupNames = Mage::getResourceModel('customer/group_collection') ->addFieldToFilter('customer_group_id', ['in' => $groupIds]) ->getColumnValues('customer_group_code'); @@ -270,6 +271,7 @@ protected function _validateConditions($rule) return $this->_formatMessage($errorMsgs); } + return ''; } /** @@ -301,6 +303,7 @@ protected function _validateActions($rule) return $this->_formatMessage($errorMsgs); } + return ''; } /** @@ -308,27 +311,29 @@ protected function _validateActions($rule) * complete. * * @param array $condition + * @param bool $isNotFoundOperator + * * @return array * @throws Mage_Core_Exception */ - protected function _processCondition($condition = [], $isNotFoundOperator = false) + protected function _processCondition($condition = [], $isNotFoundOperator = false): array { $msgs = []; // TODO: we need to get a heading for aggregate here $msg = $this->_processRule($condition, $isNotFoundOperator); - if (!is_null($msg)) { + if ($msg !== null) { $msgs[] = $msg; } // aggregate conditions - if (isset($condition['aggregator']) && isset($condition['conditions'])) { + if (isset($condition['aggregator'], $condition['conditions'])) { $headingMsg = sprintf('
        • %s
            ', $this->_createAggregatedHeading($condition['aggregator'])); $msgs[] = $headingMsg; $subMsgs = []; $isNotFoundOperator = false; - if ($condition['type'] == 'salesrule/rule_condition_product_found' && - $condition['value'] == '0') { + if ($condition['type'] === 'salesrule/rule_condition_product_found' && + $condition['value'] === '0') { $isNotFoundOperator = true; } $subConditions = $condition['conditions']; @@ -347,24 +352,26 @@ protected function _processCondition($condition = [], $isNotFoundOperator = fals * TODO: cleanup a bit * * @param array $condition + * @param bool $isNegativeOperator + * * @return String containing error message - * @throws Mage_Core_Exception + * @throws Mage_Core_Model_Store_Exception */ - protected function _processRule($condition = [], $isNegativeOperator = false) + protected function _processRule($condition = [], $isNegativeOperator = false): string { $attribute = $condition['attribute']; - if (is_null($attribute)) { + if ($attribute === null) { return null; } $operator = $condition['operator']; $value = $condition['value']; $type = $condition['type']; $ruleType = Mage::getModel($type); - $isCurrency = in_array($attribute, $this->_currency_attributes); + $isCurrency = in_array($attribute, $this->_currency_attributes, false); $msg = null; // categories - if ($attribute == 'category_ids') { + if ($attribute === 'category_ids') { $categoryIds = explode(',', $value); // get collection and filter by cat ids @@ -377,13 +384,13 @@ protected function _processRule($condition = [], $isNegativeOperator = false) } // product attributes - if ($type == 'salesrule/rule_condition_product') { + if ($type === 'salesrule/rule_condition_product') { $attributeModel = Mage::getModel('eav/entity_attribute') ->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attribute); $storeId = Mage::app()->getStore()->getStoreId(); // determine if we should format currency - if ($attributeModel->getBackendModel() == 'catalog/product_attribute_backend_price') { + if ($attributeModel->getBackendModel() === 'catalog/product_attribute_backend_price') { $isCurrency = true; } @@ -430,9 +437,9 @@ protected function _processRule($condition = [], $isNegativeOperator = false) * @param String $aggregator "any" or "all" * @return String containing aggregate heading */ - protected function _createAggregatedHeading($aggregator) + protected function _createAggregatedHeading($aggregator): string { - if ($aggregator == 'any') { + if ($aggregator === 'any') { $heading = sprintf('%s', $this->_helper->__('At least one of the following conditions must be met:')); } else { @@ -451,12 +458,12 @@ protected function _createAggregatedHeading($aggregator) * @param string $internalMessage * @return string containing entire error message */ - protected function _formatMessage($message, $params = '', $internalMessage = null) + protected function _formatMessage($message, $params = '', $internalMessage = null): string { $message = sprintf('
              %s
            ', $this->_helper->__($message, $params)); - if (!is_null($internalMessage) + if ($internalMessage !== null && Mage::getStoreConfigFlag('checkout/promocodemessages/add_additional_info_on_frontend') ) { $message .= sprintf('
              %s
            ', @@ -475,7 +482,7 @@ protected function _formatMessage($message, $params = '', $internalMessage = nul * * @return string containing entire error message */ - protected function _formatMessageWithContainer($message, $params = '', $internalMessage = null) + protected function _formatMessageWithContainer($message, $params = '', $internalMessage = null): string { if (!$message) { return ''; @@ -494,7 +501,7 @@ protected function _formatMessageWithContainer($message, $params = '', $internal * @param $array * @return string */ - protected function _multiImplode($glue, $array) + protected function _multiImplode($glue, $array): string { $ret = ''; @@ -512,7 +519,7 @@ protected function _multiImplode($glue, $array) /** * @return Mage_Sales_Model_Quote */ - protected function _getQuote() + protected function _getQuote(): \Mage_Sales_Model_Quote { return $this->_quote; } @@ -523,15 +530,13 @@ protected function _getQuote() * @param Mage_SalesRule_Model_Rule $rule * @return array Array of rule conditions */ - protected function _getConditions($rule) + protected function _getConditions($rule): array { - if (count($this->_conditions) == 0) { - if ($rule->getId()) { - $data = unserialize($rule->getData('conditions_serialized')); - //$this->_conditions = $data; - if (isset($data['conditions'])) { - $this->_conditions = $data['conditions']; - } + if ((count($this->_conditions) === 0) && $rule->getId()) { + $data = unserialize($rule->getData('conditions_serialized'), ''); + //$this->_conditions = $data; + if (isset($data['conditions'])) { + $this->_conditions = $data['conditions']; } } @@ -544,14 +549,12 @@ protected function _getConditions($rule) * @param $rule * @return array */ - protected function _getActions($rule) + protected function _getActions($rule): array { - if (count($this->_actions) == 0) { - if ($rule->getId()) { - $data = unserialize($rule->getData('actions_serialized')); - if (isset($data['conditions'])) { - $this->_actions = $data['conditions']; - } + if ((count($this->_actions) === 0) && $rule->getId()) { + $data = unserialize($rule->getData('actions_serialized'), ''); + if (isset($data['conditions'])) { + $this->_actions = $data['conditions']; } } @@ -563,7 +566,7 @@ protected function _getActions($rule) * * @return array */ - protected function _getFoundOperators() + protected function _getFoundOperators(): array { if (null === $this->_foundOperators) { $this->_foundOperators = [ diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/SalesRuleMother.php b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/SalesRuleMother.php index c3ddbaa..bdc375f 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/SalesRuleMother.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/SalesRuleMother.php @@ -80,10 +80,10 @@ public function generateCustomerAlreadyUsedRule() return $rule; } - public function generateAddressConditionSubtotalRule() + public function generateAddressConditionSubtotalRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -97,10 +97,10 @@ public function generateAddressConditionSubtotalRule() return $rule; } - public function generateAddressConditionTotalQtyRule() + public function generateAddressConditionTotalQtyRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -114,10 +114,10 @@ public function generateAddressConditionTotalQtyRule() return $rule; } - public function generateAddressConditionWeightRule() + public function generateAddressConditionWeightRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -131,11 +131,11 @@ public function generateAddressConditionWeightRule() return $rule; } - public function generateAddressConditionPaymentMethodRule() + public function generateAddressConditionPaymentMethodRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -149,11 +149,11 @@ public function generateAddressConditionPaymentMethodRule() return $rule; } - public function generateAddressConditionShippingMethodRule() + public function generateAddressConditionShippingMethodRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -167,11 +167,11 @@ public function generateAddressConditionShippingMethodRule() return $rule; } - public function generateAddressConditionPostCodeRule() + public function generateAddressConditionPostCodeRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -185,11 +185,11 @@ public function generateAddressConditionPostCodeRule() return $rule; } - public function generateAddressConditionRegionRule() + public function generateAddressConditionRegionRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -203,11 +203,11 @@ public function generateAddressConditionRegionRule() return $rule; } - public function generateAddressConditionRegionIdRule() + public function generateAddressConditionRegionIdRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -221,11 +221,11 @@ public function generateAddressConditionRegionIdRule() return $rule; } - public function generateAddressConditionCountryIdRule() + public function generateAddressConditionCountryIdRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_address', @@ -239,13 +239,13 @@ public function generateAddressConditionCountryIdRule() return $rule; } - public function generateProductConditionCategoriesRule() + public function generateProductConditionCategoriesRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; $categoryIds = Mage::getModel('catalog/category')->getCollection()->getAllIds(2); - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_product_found', @@ -267,11 +267,11 @@ public function generateProductConditionCategoriesRule() return $rule; } - public function generateFoundProductConditionAttributeRule() + public function generateFoundProductConditionAttributeRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_product_found', @@ -293,11 +293,11 @@ public function generateFoundProductConditionAttributeRule() return $rule; } - public function generateNotFoundProductConditionAttributeRule() + public function generateNotFoundProductConditionAttributeRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_product_found', @@ -319,7 +319,7 @@ public function generateNotFoundProductConditionAttributeRule() return $rule; } - public function generateFoundActionAttributeRule() + public function generateFoundActionAttributeRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; $actions = []; @@ -344,11 +344,11 @@ public function generateFoundActionAttributeRule() return $rule; } - public function generateNotFoundActionAttributeRule() + public function generateNotFoundActionAttributeRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; - $actions = $this->generateRuleConditionCombineArray(); + $actions = self::generateRuleConditionCombineArray(); $actions['1--1'] = [ 'type' => 'salesrule/rule_condition_product_found', @@ -370,12 +370,12 @@ public function generateNotFoundActionAttributeRule() return $rule; } - public function generateConditionAndActionRule() + public function generateConditionAndActionRule(): \Mage_SalesRule_Model_Rule { $rule = $this->rule; $categoryIds = Mage::getModel('catalog/category')->getCollection()->getAllIds(2); - $conditions = $this->generateRuleConditionCombineArray(); + $conditions = self::generateRuleConditionCombineArray(); $conditions['1--1'] = [ 'type' => 'salesrule/rule_condition_product_found', @@ -421,7 +421,7 @@ public function generateConditionAndActionRule() * * @return Mage_SalesRule_Model_Rule */ - private function setupBaseRule() + private function setupBaseRule(): \Mage_SalesRule_Model_Rule { // SalesRule Rule model $rule = Mage::getModel('salesrule/rule'); @@ -463,20 +463,20 @@ private static function generateRuleConditionCombineArray(): array $conditions[1] = [ 'type' => 'salesrule/rule_condition_combine', 'aggregator' => 'all', - 'value' => "1", + 'value' => '1', 'new_child' => '' ]; return $conditions; } - private function generateUniqueId($length = null) + private function generateUniqueId($length = null): string { - $rndId = crypt(uniqid(rand(), 1)); + $rndId = crypt(uniqid(mt_rand(), 1)); $rndId = strip_tags(stripslashes($rndId)); - $rndId = str_replace([".", "$"], "", $rndId); - $rndId = strrev(str_replace("/", "", $rndId)); - if (!is_null($rndId)) { + $rndId = str_replace(['.', '$'], '', $rndId); + $rndId = strrev(str_replace('/', '', $rndId)); + if ($rndId !== null) { return strtoupper(substr($rndId, 0, $length)); } @@ -486,7 +486,7 @@ private function generateUniqueId($length = null) /** * @return string */ - private function getAllCustomerGroups() + private function getAllCustomerGroups(): string { $customerGroups = Mage::getModel('customer/group')->getCollection()->getAllIds(); @@ -496,7 +496,7 @@ private function getAllCustomerGroups() /** * @return string */ - private function getAllWebsites() + private function getAllWebsites(): string { $websites = Mage::getModel('core/website')->getCollection()->getAllIds(); diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Test/bootstrap.php b/src/app/code/community/Hackathon/PromoCodeMessages/Test/bootstrap.php index c552bda..d04d7bf 100644 --- a/src/app/code/community/Hackathon/PromoCodeMessages/Test/bootstrap.php +++ b/src/app/code/community/Hackathon/PromoCodeMessages/Test/bootstrap.php @@ -2,7 +2,7 @@ $magentoRoot = getenv('MAGENTO_ROOT'); if (empty($magentoRoot)) { - $magentoRoot = realpath(dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))))); + $magentoRoot = realpath(dirname(__DIR__, 6)); } define('MAGENTO_ROOT', $magentoRoot); From 8815e210b8d1aeb3f0ba5b942ecb93ddfe11e5fd Mon Sep 17 00:00:00 2001 From: Fabian Blechschmidt Date: Mon, 13 Jan 2020 17:29:52 +0100 Subject: [PATCH 10/10] add translation --- src/app/locale/de_DE/Hackathon_PromoCodeMessages.csv | 1 + src/app/locale/en_US/Hackathon_PromoCodeMessages.csv | 1 + 2 files changed, 2 insertions(+) diff --git a/src/app/locale/de_DE/Hackathon_PromoCodeMessages.csv b/src/app/locale/de_DE/Hackathon_PromoCodeMessages.csv index 27c3c13..f680555 100644 --- a/src/app/locale/de_DE/Hackathon_PromoCodeMessages.csv +++ b/src/app/locale/de_DE/Hackathon_PromoCodeMessages.csv @@ -30,3 +30,4 @@ "Your coupon is not valid for your Customer Group.","Der Gutschein ist für Ihre Kundengruppe nicht freigeschaltet." "Your coupon is not valid yet. It will be active on %s.","Der Gutschein ist noch nicht aktiv. Er wird am %s freigeschaltet." "Your coupon was already used.","Ihr Gutschein wurde bereits benutzt." +"Your coupon ""%s"" could not be redeemed.","Ihr Gutschein ""%s"" konnte nicht eingelöst werden." diff --git a/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv b/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv index 9ca67f8..a64cab5 100644 --- a/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv +++ b/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv @@ -30,3 +30,4 @@ "Validate cart rule actions","Validate cart rule actions" "Provides error messaging for conditions under ""Conditions"" tab","Provides error messaging for conditions under ""Conditions"" tab" "Provides error messaging for actions under ""Actions"" tab","Provides error messaging for actions under ""Actions"" tab" +"Your coupon ""%s"" could not be redeemed.","Your coupon ""%s"" could not be redeemed."