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": {
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);
}
}
}
diff --git a/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php b/src/app/code/community/Hackathon/PromoCodeMessages/Model/Validator.php
index d75bc7a..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)
@@ -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 */
@@ -129,40 +129,42 @@ 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)
{
- $msg = '';
if (!$rule->getIsActive()) {
- Mage::throwException($this->_formatMessage('Your coupon is inactive.'));
+ Mage::throwException($this->_formatMessageWithContainer('Your coupon is inactive.'));
}
// check websites
+ /** @var int[] $websiteIds */
$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
$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');
- $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 +174,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 +186,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 +221,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 +240,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;
}
/**
@@ -266,6 +271,7 @@ protected function _validateConditions($rule)
return $this->_formatMessage($errorMsgs);
}
+ return '';
}
/**
@@ -297,6 +303,7 @@ protected function _validateActions($rule)
return $this->_formatMessage($errorMsgs);
}
+ return '';
}
/**
@@ -304,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'];
@@ -343,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
@@ -373,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;
}
@@ -426,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 {
@@ -447,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('',
$this->_helper->__($message, $params));
- if (!is_null($internalMessage)
+ if ($internalMessage !== null
&& Mage::getStoreConfigFlag('checkout/promocodemessages/add_additional_info_on_frontend')
) {
$message .= sprintf('',
@@ -462,6 +473,27 @@ 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): string
+ {
+ if (!$message) {
+ return '';
+ }
+
+ $message = '- ' . $this->_helper->__($message, $params) . '
';
+ $internalMessage = '- ' . $this->_helper->__($internalMessage, $params) . '
';
+
+ return $this->_formatMessage($message, $params, $internalMessage);
+ }
+
/**
* Implode a multidimensional array.
*
@@ -469,7 +501,7 @@ protected function _formatMessage($message, $params = '', $internalMessage = nul
* @param $array
* @return string
*/
- protected function _multiImplode($glue, $array)
+ protected function _multiImplode($glue, $array): string
{
$ret = '';
@@ -487,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;
}
@@ -498,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'];
}
}
@@ -519,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'];
}
}
@@ -538,7 +566,7 @@ protected function _getActions($rule)
*
* @return array
*/
- protected function _getFoundOperators()
+ protected function _getFoundOperators(): array
{
if (null === $this->_foundOperators) {
$this->_foundOperators = [
@@ -613,6 +641,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;
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/Model/ValidatorTest.php b/src/app/code/community/Hackathon/PromoCodeMessages/Test/Model/ValidatorTest.php
index c55b715..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()
@@ -85,7 +85,7 @@ public function testValidateWithInvalidCode()
{
$couponCode = 'sdfsdf';
$this->expectException(Mage_Core_Exception::class);
- $this->expectExceptionMessage('');
+ $this->expectExceptionMessage('');
$this->validator->validate($couponCode, $this->quoteMock);
}
@@ -96,12 +96,27 @@ public function testValidateInvalidWebsiteIds()
$this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400);
$this->expectException(Mage_Core_Exception::class);
- $exceptionMsg = '';
+ $exceptionMsg
+ = '';
$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 = '';
+ $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,8 +125,9 @@ public function testValidateInvalidWebsiteIdsWithAdditionalInfo()
$this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(400);
$this->expectException(Mage_Core_Exception::class);
- $exceptionMsg = ''
- . '';
+ $exceptionMsg
+ = ''
+ . '';
$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 = '';
+ $exceptionMsg
+ = '';
$this->expectExceptionMessage($exceptionMsg);
$this->validator->validate($this->rule->getCouponCode(), $this->quoteMock);
}
@@ -136,8 +153,9 @@ 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 = '';
+ $exceptionMsg
+ = '';
$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 = '';
+ $exceptionMsg
+ = '';
$this->expectExceptionMessage($exceptionMsg);
$this->validator->validate($this->rule->getCouponCode(), $this->quoteMock);
}
@@ -158,8 +177,9 @@ 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 = '';
+ $exceptionMsg
+ = '';
$this->expectExceptionMessage($exceptionMsg);
$this->validator->validate($this->rule->getCouponCode(), $this->quoteMock);
}
@@ -171,8 +191,9 @@ 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 = '';
+ $exceptionMsg
+ = '';
$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 = '';
+ $exceptionMsg
+ = '';
$this->expectExceptionMessage($exceptionMsg);
$this->validator->validate($this->rule->getCouponCode(), $this->quoteMock);
}
@@ -197,8 +219,9 @@ 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 = ''
- . '';
+ $exceptionMsg
+ = ''
+ . '';
$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 = '';
+ $exceptionMsg
+ = '';
$this->expectExceptionMessage($exceptionMsg);
$this->validator->validate($this->rule->getCouponCode(), $this->quoteMock);
}
@@ -223,8 +247,9 @@ 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 = ''
- . '';
+ $exceptionMsg
+ = ''
+ . '';
$this->expectExceptionMessage($exceptionMsg);
$this->validator->validate($this->rule->getCouponCode(), $this->quoteMock);
}
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);
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 967bf53..a64cab5 100644
--- a/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv
+++ b/src/app/locale/en_US/Hackathon_PromoCodeMessages.csv
@@ -28,5 +28,6 @@
"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"
+"Your coupon ""%s"" could not be redeemed.","Your coupon ""%s"" could not be redeemed."