|
6 | 6 |
|
7 | 7 | namespace Magento\SalesRule\Model\Rule\Condition;
|
8 | 8 |
|
| 9 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 10 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 11 | +use Magento\Quote\Api\Data\CartInterface; |
| 12 | +use Magento\SalesRule\Api\RuleRepositoryInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 16 | + */ |
9 | 17 | class ProductTest extends \PHPUnit\Framework\TestCase
|
10 | 18 | {
|
11 | 19 | /**
|
@@ -98,4 +106,71 @@ public function validateProductConditionDataProvider()
|
98 | 106 | ]
|
99 | 107 | ];
|
100 | 108 | }
|
| 109 | + |
| 110 | + /** |
| 111 | + * Ensure that SalesRules filtering on quote items quantity validates configurable product correctly |
| 112 | + * |
| 113 | + * 1. Load a quote with a configured product and a sales rule set to filter items with quantity 2. |
| 114 | + * 2. Attempt to validate the sales rule against the quote and assert the output is negative. |
| 115 | + * |
| 116 | + * @magentoDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php |
| 117 | + * @magentoDataFixture Magento/SalesRule/_files/cart_rule_10_percent_off.php |
| 118 | + */ |
| 119 | + public function testValidateQtySalesRuleWithConfigurable() |
| 120 | + { |
| 121 | + // Load the quote that contains a child of a configurable product with quantity 1. |
| 122 | + $quote = $this->getQuote('test_cart_with_configurable'); |
| 123 | + |
| 124 | + // Load the SalesRule looking for products with quantity 2. |
| 125 | + $rule = $this->getSalesRule('10% Off on orders with two items'); |
| 126 | + |
| 127 | + $this->assertFalse( |
| 128 | + $rule->validate($quote->getBillingAddress()) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Gets quote by reserved order id. |
| 134 | + * |
| 135 | + * @param string $reservedOrderId |
| 136 | + * @return CartInterface |
| 137 | + */ |
| 138 | + private function getQuote($reservedOrderId) |
| 139 | + { |
| 140 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 141 | + $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 142 | + $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId) |
| 143 | + ->create(); |
| 144 | + |
| 145 | + /** @var CartRepositoryInterface $quoteRepository */ |
| 146 | + $quoteRepository = $this->objectManager->get(CartRepositoryInterface::class); |
| 147 | + $items = $quoteRepository->getList($searchCriteria)->getItems(); |
| 148 | + return array_pop($items); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Gets rule by name. |
| 153 | + * |
| 154 | + * @param string $name |
| 155 | + * @return \Magento\SalesRule\Model\Rule |
| 156 | + * @throws \Magento\Framework\Exception\InputException |
| 157 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 158 | + */ |
| 159 | + private function getSalesRule(string $name): \Magento\SalesRule\Model\Rule |
| 160 | + { |
| 161 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 162 | + $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 163 | + $searchCriteria = $searchCriteriaBuilder->addFilter('name', $name) |
| 164 | + ->create(); |
| 165 | + |
| 166 | + /** @var CartRepositoryInterface $quoteRepository */ |
| 167 | + $ruleRepository = $this->objectManager->get(RuleRepositoryInterface::class); |
| 168 | + $items = $ruleRepository->getList($searchCriteria)->getItems(); |
| 169 | + |
| 170 | + $rule = array_pop($items); |
| 171 | + /** @var \Magento\SalesRule\Model\Converter\ToModel $converter */ |
| 172 | + $converter = $this->objectManager->get(\Magento\SalesRule\Model\Converter\ToModel::class); |
| 173 | + |
| 174 | + return $converter->toModel($rule); |
| 175 | + } |
101 | 176 | }
|
0 commit comments