Skip to content

Commit 4a49696

Browse files
committed
Merge remote-tracking branch 'origin/MC-18057' into 2.3-develop-pr96
2 parents 2d6ec0d + 4e7e763 commit 4a49696

File tree

5 files changed

+138
-6
lines changed

5 files changed

+138
-6
lines changed

app/code/Magento/Rule/Model/Condition/AbstractCondition.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public function getValueSelectOptions()
386386
public function getValueParsed()
387387
{
388388
if (!$this->hasValueParsed()) {
389-
$value = $this->getData('value');
389+
$value = $this->getValue();
390390
if (is_array($value) && count($value) === 1) {
391391
$value = reset($value);
392392
}
@@ -765,7 +765,7 @@ public function asStringRecursive($level = 0)
765765
/**
766766
* Validate product attribute value for condition
767767
*
768-
* @param object|array|int|string|float|bool $validatedValue product attribute value
768+
* @param object|array|int|string|float|bool|null $validatedValue product attribute value
769769
* @return bool
770770
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
771771
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -830,6 +830,7 @@ public function validateAttribute($validatedValue)
830830
case '{}':
831831
case '!{}':
832832
if (is_scalar($validatedValue) && is_array($value)) {
833+
$validatedValue = (string)$validatedValue;
833834
foreach ($value as $item) {
834835
if (stripos($validatedValue, (string)$item) !== false) {
835836
$result = true;
@@ -877,14 +878,15 @@ public function validateAttribute($validatedValue)
877878
/**
878879
* Case and type insensitive comparison of values
879880
*
880-
* @param string|int|float $validatedValue
881-
* @param string|int|float $value
881+
* @param string|int|float|null $validatedValue
882+
* @param string|int|float|null $value
882883
* @param bool $strict
883884
* @return bool
884885
*/
885886
protected function _compareValues($validatedValue, $value, $strict = true)
886887
{
887-
if ($strict && is_numeric($validatedValue) && is_numeric($value)) {
888+
if (null === $value || null === $validatedValue ||
889+
$strict && is_numeric($validatedValue) && is_numeric($value)) {
888890
return $validatedValue == $value;
889891
}
890892

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
include __DIR__ . '/product_date_attribute.php';
10+
11+
$attribute->setData('is_used_for_promo_rules', 1);
12+
13+
/** @var \Magento\Catalog\Model\ProductFactory $productFactory */
14+
$productFactory = $objectManager->get(Magento\Catalog\Model\ProductFactory::class);
15+
$product = $productFactory->create();
16+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
17+
->setAttributeSetId($product->getDefaultAttributeSetId())
18+
->setWebsiteIds([1])
19+
->setName('Simple Product with date')
20+
->setSku('simple_with_date')
21+
->setPrice(10)
22+
->setDescription('Description with <b>html tag</b>')
23+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
24+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
25+
->setCategoryIds([2])
26+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
27+
->setDateAttribute(date('Y-m-d'))
28+
->setUrlKey('simple_with_date')
29+
->save();
30+
31+
$product2 = $productFactory->create();
32+
$product2->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
33+
->setAttributeSetId($product->getDefaultAttributeSetId())
34+
->setWebsiteIds([1])
35+
->setName('Simple Product with date -1')
36+
->setSku('simple_with_date2')
37+
->setPrice(10)
38+
->setDescription('Description with <b>html tag</b>')
39+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
40+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
41+
->setCategoryIds([2])
42+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
43+
->setDateAttribute(date('Y-m-d', strtotime(date('Y-m-d') . '-1 day')))
44+
->setUrlKey('simple_with_date2')
45+
->save();
46+
47+
$product3 = $productFactory->create();
48+
$product3->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
49+
->setAttributeSetId($product->getDefaultAttributeSetId())
50+
->setWebsiteIds([1])
51+
->setName('Simple Product with date +1')
52+
->setSku('simple_with_date3')
53+
->setPrice(10)
54+
->setDescription('Description with <b>html tag</b>')
55+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
56+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
57+
->setCategoryIds([2])
58+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
59+
->setDateAttribute(date('Y-m-d', strtotime(date('Y-m-d') . '+1 day')))
60+
->setUrlKey('simple_with_date3')
61+
->save();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
11+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
12+
13+
/** @var \Magento\Framework\Registry $registry */
14+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
15+
16+
$registry->unregister('isSecureArea');
17+
$registry->register('isSecureArea', true);
18+
19+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
20+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
21+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
22+
23+
try {
24+
$product = $productRepository->get('simple_with_date', false, null, true);
25+
$productRepository->delete($product);
26+
} catch (NoSuchEntityException $e) {
27+
}
28+
29+
try {
30+
$product = $productRepository->get('simple_with_date2', false, null, true);
31+
$productRepository->delete($product);
32+
} catch (NoSuchEntityException $e) {
33+
}
34+
35+
try {
36+
$product = $productRepository->get('simple_with_date3', false, null, true);
37+
$productRepository->delete($product);
38+
} catch (NoSuchEntityException $e) {
39+
}
40+
41+
$registry->unregister('isSecureArea');
42+
$registry->register('isSecureArea', false);
43+
44+
include __DIR__ . '/product_date_attribute_rollback.php';

dev/tests/integration/testsuite/Magento/CatalogWidget/Block/Product/ProductListTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,29 @@ public function createCollectionForSkuDataProvider()
172172
. '`attribute`:`sku`,`operator`:`!^[^]`,`value`:`virtual`^]^]', 'product-with-xss']
173173
];
174174
}
175+
176+
/**
177+
* Check that collection returns correct result if use date attribute.
178+
*
179+
* @magentoDbIsolation disabled
180+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_date_attribute.php
181+
* @return void
182+
*/
183+
public function testProductListWithDateAttribute()
184+
{
185+
$encodedConditions = '^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,'
186+
. '`aggregator`:`all`,`value`:`1`,`new_child`:``^],'
187+
. '`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,'
188+
. '`attribute`:`date_attribute`,`operator`:`==`,`value`:`' . date('Y-m-d') . '`^]^]';
189+
$this->block->setData('conditions_encoded', $encodedConditions);
190+
191+
// Load products collection filtered using specified conditions and perform assertions
192+
$productCollection = $this->block->createCollection();
193+
$productCollection->load();
194+
$this->assertEquals(
195+
1,
196+
$productCollection->count(),
197+
"Product collection was not filtered according to the widget condition."
198+
);
199+
}
175200
}

dev/tests/integration/testsuite/Magento/Rule/Model/Condition/Sql/BuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testAttachConditionToCollection(): void
7272
$rule->loadPost($ruleConditionArray);
7373
$this->model->attachConditionToCollection($collection, $rule->getConditions());
7474

75-
$whereString = "/\(category_id IN \('3'\).+\(IFNULL\(`e`\.`entity_id`,.+\) = '2017-09-15'\)"
75+
$whereString = "/\(category_id IN \('3'\).+\(IFNULL\(`e`\.`entity_id`,.+\) = '2017-09-15 00:00:00'\)"
7676
. ".+ORDER BY \(FIELD\(`e`.`sku`, ':\(', ':\)'\)\)/";
7777
$this->assertEquals(1, preg_match($whereString, $collection->getSelectSql(true)));
7878
}

0 commit comments

Comments
 (0)