Skip to content

Commit 8363187

Browse files
authored
Merge pull request #1360 from magento-okapis/Okapis-PR
[okapis] MAGETWO-62405: Cannot exclude a category for Cart Price Rule on complex products
2 parents 14e7a37 + 711885d commit 8363187

File tree

22 files changed

+340
-29
lines changed

22 files changed

+340
-29
lines changed

app/code/Magento/Config/Model/Config/Backend/Email/Address.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Address extends \Magento\Framework\App\Config\Value
2323
public function beforeSave()
2424
{
2525
$value = $this->getValue();
26-
if (!\Zend_Validate::is($value, 'EmailAddress')) {
26+
if (!\Zend_Validate::is($value, \Magento\Framework\Validator\EmailAddress::class)) {
2727
throw new LocalizedException(__('Please correct the email address: "%1".', $value));
2828
}
2929
return $this;

app/code/Magento/Customer/Controller/Account/ForgotPasswordPost.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function execute()
6060
$resultRedirect = $this->resultRedirectFactory->create();
6161
$email = (string)$this->getRequest()->getPost('email');
6262
if ($email) {
63-
if (!\Zend_Validate::is($email, 'EmailAddress')) {
63+
if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
6464
$this->session->setForgottenEmail($email);
6565
$this->messageManager->addErrorMessage(__('Please correct the email address.'));
6666
return $resultRedirect->setPath('*/*/forgotpassword');

app/code/Magento/Customer/Model/Metadata/Form/AbstractData.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Magento\Customer\Model\Metadata\Form;
1212

1313
use Magento\Framework\Api\ArrayObjectSearch;
14+
use Magento\Framework\Validator\EmailAddress;
1415

1516
/**
1617
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -336,7 +337,7 @@ protected function _validateInputRule($value)
336337
__("'%value%' appears to be a DNS hostname but cannot extract TLD part")
337338
__("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
338339
*/
339-
$validator = new \Zend_Validate_EmailAddress();
340+
$validator = new EmailAddress();
340341
$validator->setMessage(
341342
__('"%1" invalid type entered.', $label),
342343
\Zend_Validate_EmailAddress::INVALID
@@ -377,10 +378,6 @@ protected function _validateInputRule($value)
377378
__("'%value%' looks like an IP address, which is not an acceptable format."),
378379
\Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
379380
);
380-
$validator->setMessage(
381-
__("'%value%' looks like a DNS hostname but we cannot match the TLD against known list."),
382-
\Zend_Validate_Hostname::UNKNOWN_TLD
383-
);
384381
$validator->setMessage(
385382
__("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
386383
\Zend_Validate_Hostname::INVALID_DASH

app/code/Magento/CustomerImportExport/Model/Import/AbstractCustomer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected function _checkUniqueKey(array $rowData, $rowNumber)
235235
$email = strtolower($rowData[static::COLUMN_EMAIL]);
236236
$website = $rowData[static::COLUMN_WEBSITE];
237237

238-
if (!\Zend_Validate::is($email, 'EmailAddress')) {
238+
if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
239239
$this->addRowError(static::ERROR_INVALID_EMAIL, $rowNumber, static::COLUMN_EMAIL);
240240
} elseif (!isset($this->_websiteCodeToId[$website])) {
241241
$this->addRowError(static::ERROR_INVALID_WEBSITE, $rowNumber, static::COLUMN_WEBSITE);

app/code/Magento/Eav/Model/Attribute/Data/AbstractData.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Framework\App\RequestInterface;
1212
use Magento\Framework\Exception\LocalizedException as CoreException;
13+
use Magento\Framework\Validator\EmailAddress;
1314

1415
/**
1516
* EAV Attribute Abstract Data Model
@@ -365,7 +366,7 @@ protected function _validateInputRule($value)
365366
__("'%value%' appears to be a DNS hostname but cannot extract TLD part")
366367
__("'%value%' appears to be a DNS hostname but cannot match TLD against known list")
367368
*/
368-
$validator = new \Zend_Validate_EmailAddress();
369+
$validator = new EmailAddress();
369370
$validator->setMessage(
370371
__('"%1" invalid type entered.', $label),
371372
\Zend_Validate_EmailAddress::INVALID
@@ -406,10 +407,6 @@ protected function _validateInputRule($value)
406407
__("'%value%' looks like an IP address, which is not an acceptable format."),
407408
\Zend_Validate_Hostname::IP_ADDRESS_NOT_ALLOWED
408409
);
409-
$validator->setMessage(
410-
__("'%value%' looks like a DNS hostname but we cannot match the TLD against known list."),
411-
\Zend_Validate_Hostname::UNKNOWN_TLD
412-
);
413410
$validator->setMessage(
414411
__("'%value%' looks like a DNS hostname but contains a dash in an invalid position."),
415412
\Zend_Validate_Hostname::INVALID_DASH

app/code/Magento/Newsletter/Controller/Subscriber/NewAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected function validateGuestSubscription()
103103
*/
104104
protected function validateEmailFormat($email)
105105
{
106-
if (!\Zend_Validate::is($email, 'EmailAddress')) {
106+
if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
107107
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a valid email address.'));
108108
}
109109
}

app/code/Magento/Quote/Model/Quote/Address/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function isValid($value)
3939
{
4040
$messages = [];
4141
$email = $value->getEmail();
42-
if (!empty($email) && !\Zend_Validate::is($email, 'EmailAddress')) {
42+
if (!empty($email) && !\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
4343
$messages['invalid_email_format'] = 'Invalid email format';
4444
}
4545

app/code/Magento/SalesRule/Model/Rule/Condition/Product.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public function validate(\Magento\Framework\Model\AbstractModel $model)
4949
$model->getBaseRowTotal()
5050
);
5151

52+
$attrCode = $this->getAttribute();
53+
54+
if ('category_ids' == $attrCode) {
55+
return $this->validateAttribute($this->_getAvailableInCategories($product->getId()));
56+
}
57+
5258
return parent::validate($product);
5359
}
5460

app/code/Magento/SalesRule/Test/Unit/Model/Rule/Condition/ProductTest.php

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\SalesRule\Test\Unit\Model\Rule\Condition;
77

8+
use \Magento\Framework\DB\Adapter\AdapterInterface;
9+
use \Magento\Framework\DB\Select;
10+
use \Magento\Framework\Model\AbstractModel;
11+
use Magento\Quote\Model\Quote\Item\AbstractItem;
812
use \Magento\Rule\Model\Condition\Context;
913
use \Magento\Backend\Helper\Data;
1014
use \Magento\Eav\Model\Config;
@@ -52,6 +56,12 @@ class ProductTest extends \PHPUnit_Framework_TestCase
5256
/** @var AttributeLoaderInterface|\PHPUnit_Framework_MockObject_MockObject */
5357
protected $attributeLoaderInterfaceMock;
5458

59+
/** @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
60+
protected $adapterInterfaceMock;
61+
62+
/** @var Select|\PHPUnit_Framework_MockObject_MockObject */
63+
protected $selectMock;
64+
5565
/**
5666
* Setup the test
5767
*/
@@ -78,15 +88,45 @@ protected function setUp()
7888
$this->attributeLoaderInterfaceMock
7989
->expects($this->any())
8090
->method('getAttributesByCode')
81-
->will($this->returnValue([]));
91+
->willReturn([]);
92+
$this->selectMock = $this->getMockBuilder(Select::class)
93+
->disableOriginalConstructor()
94+
->setMethods(['distinct', 'from', 'where'])
95+
->getMock();
96+
$this->selectMock
97+
->expects($this->any())
98+
->method('distinct')
99+
->willReturnSelf();
100+
$this->selectMock
101+
->expects($this->any())
102+
->method('from')
103+
->with($this->anything(), $this->anything())
104+
->willReturnSelf();
105+
$this->adapterInterfaceMock = $this->getMockBuilder(AdapterInterface::class)
106+
->disableOriginalConstructor()
107+
->setMethods(['fetchCol', 'select'])
108+
->getMockForAbstractClass();
109+
$this->adapterInterfaceMock
110+
->expects($this->any())
111+
->method('select')
112+
->willReturn($this->selectMock);
82113
$this->productMock = $this->getMockBuilder(Product::class)
83114
->disableOriginalConstructor()
84-
->setMethods(['loadAllAttributes'])
115+
->setMethods(['loadAllAttributes', 'getConnection', 'getTable'])
85116
->getMock();
86117
$this->productMock
87118
->expects($this->any())
88119
->method('loadAllAttributes')
89-
->will($this->returnValue($this->attributeLoaderInterfaceMock));
120+
->willReturn($this->attributeLoaderInterfaceMock);
121+
$this->productMock
122+
->expects($this->any())
123+
->method('getConnection')
124+
->willReturn($this->adapterInterfaceMock);
125+
$this->productMock
126+
->expects($this->any())
127+
->method('getTable')
128+
->with($this->anything())
129+
->willReturn('table_name');
90130
$this->collectionMock = $this->getMockBuilder(Collection::class)
91131
->disableOriginalConstructor()
92132
->getMock();
@@ -158,4 +198,37 @@ public function testGetValueElementChooserUrl($attribute, $url, $jsObject = '')
158198

159199
$this->assertEquals($url, $this->model->getValueElementChooserUrl());
160200
}
201+
202+
public function testValidateCategoriesIgnoresVisibility()
203+
{
204+
/* @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject $product */
205+
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
206+
->disableOriginalConstructor()
207+
->setMethods(['getAttribute', 'getId', 'setQuoteItemQty', 'setQuoteItemPrice'])
208+
->getMock();
209+
$product
210+
->expects($this->any())
211+
->method('setQuoteItemQty')
212+
->willReturnSelf();
213+
$product
214+
->expects($this->any())
215+
->method('setQuoteItemPrice')
216+
->willReturnSelf();
217+
/* @var AbstractItem|\PHPUnit_Framework_MockObject_MockObject $item */
218+
$item = $this->getMockBuilder(AbstractItem::class)
219+
->disableOriginalConstructor()
220+
->setMethods(['getProduct'])
221+
->getMockForAbstractClass();
222+
$item->expects($this->any())
223+
->method('getProduct')
224+
->willReturn($product);
225+
$this->model->setAttribute('category_ids');
226+
227+
$this->selectMock
228+
->expects($this->once())
229+
->method('where')
230+
->with($this->logicalNot($this->stringContains('visibility')), $this->anything(), $this->anything());
231+
232+
$this->model->validate($item);
233+
}
161234
}

app/code/Magento/SendFriend/Model/SendFriend.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function validate()
237237
}
238238

239239
$email = $this->getSender()->getEmail();
240-
if (empty($email) or !\Zend_Validate::is($email, 'EmailAddress')) {
240+
if (empty($email) or !\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
241241
$errors[] = __('Invalid Sender Email');
242242
}
243243

@@ -252,7 +252,7 @@ public function validate()
252252

253253
// validate recipients email addresses
254254
foreach ($this->getRecipients()->getEmails() as $email) {
255-
if (!\Zend_Validate::is($email, 'EmailAddress')) {
255+
if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
256256
$errors[] = __('Please enter a correct recipient email address.');
257257
break;
258258
}

0 commit comments

Comments
 (0)