CollectionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\ResourceModel\Rule;
  7. /**
  8. * @magentoDbIsolation enabled
  9. * @magentoAppIsolation enabled
  10. */
  11. class CollectionTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @magentoDataFixture Magento/SalesRule/_files/rules.php
  15. * @magentoDataFixture Magento/SalesRule/_files/coupons.php
  16. * @dataProvider setValidationFilterDataProvider()
  17. * @param string $couponCode
  18. * @param array $expectedItems
  19. * @magentoDbIsolation disabled
  20. */
  21. public function testSetValidationFilter($couponCode, $expectedItems)
  22. {
  23. $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  24. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  25. );
  26. $items = array_values($collection->setValidationFilter(1, 0, $couponCode)->getItems());
  27. $ids = [];
  28. $this->assertEquals(
  29. count($expectedItems),
  30. count($items),
  31. 'Invalid number of items in the result collection'
  32. );
  33. foreach ($items as $key => $item) {
  34. $this->assertEquals($expectedItems[$key], $item->getName());
  35. $this->assertFalse(
  36. in_array($item->getId(), $ids),
  37. 'Item should be unique in result collection'
  38. );
  39. $ids[] = $item->getId();
  40. }
  41. }
  42. /**
  43. * data provider for testSetValidationFilter
  44. * @return array
  45. */
  46. public function setValidationFilterDataProvider()
  47. {
  48. return [
  49. 'Check type COUPON' => ['coupon_code', ['#1', '#2', '#5']],
  50. 'Check type NO_COUPON' => ['', ['#2', '#5']],
  51. 'Check type COUPON_AUTO' => ['coupon_code_auto', ['#2', '#4', '#5']],
  52. 'Check result with auto generated coupon' => ['autogenerated_3_1', ['#2', '#3', '#5']],
  53. 'Check result with non actual previously generated coupon' => [
  54. 'autogenerated_2_1',
  55. ['#2', '#5'],
  56. ],
  57. 'Check result with wrong code' => ['wrong_code', ['#2', '#5']]
  58. ];
  59. }
  60. /**
  61. * @magentoDbIsolation disabled
  62. * @magentoAppIsolation enabled
  63. * @magentoDataFixture Magento/Checkout/_files/quote_with_shipping_method_and_items_categories.php
  64. * @magentoDataFixture Magento/SalesRule/_files/rules_group_any_categories.php
  65. */
  66. public function testSetValidationFilterWithGroup()
  67. {
  68. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  69. /** @var \Magento\SalesRule\Model\Rule $rule */
  70. $rule = $objectManager->get(\Magento\Framework\Registry::class)
  71. ->registry('_fixture/Magento_SalesRule_Group_Multiple_Categories');
  72. /** @var \Magento\Quote\Model\Quote $quote */
  73. $quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
  74. $quote->load('test_order_item_with_items', 'reserved_order_id');
  75. //gather only the existing rules that obey the validation filter
  76. /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $ruleCollection */
  77. $ruleCollection = $objectManager->create(
  78. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  79. );
  80. $appliedRulesArray = array_keys(
  81. $ruleCollection->setValidationFilter(
  82. $quote->getStore()->getWebsiteId(),
  83. 0,
  84. '',
  85. null,
  86. $quote->getShippingAddress()
  87. )->getItems()
  88. );
  89. $this->assertEquals([$rule->getRuleId()], $appliedRulesArray);
  90. }
  91. /**
  92. * @magentoDbIsolation disabled
  93. * @magentoAppIsolation enabled
  94. * @magentoDataFixture Magento/Checkout/_files/quote_with_shipping_method_and_items_categories.php
  95. * @magentoDataFixture Magento/SalesRule/_files/rules_group_any_categories.php
  96. */
  97. public function testSetValidationFilterAnyCategory()
  98. {
  99. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  100. /** @var \Magento\SalesRule\Model\Rule $rule */
  101. $rule = $objectManager->get(\Magento\Framework\Registry::class)
  102. ->registry('_fixture/Magento_SalesRule_Group_Multiple_Categories');
  103. /** @var \Magento\Quote\Model\Quote $quote */
  104. $quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
  105. $quote->load('test_order_item_with_items', 'reserved_order_id');
  106. //gather only the existing rules that obey the validation filter
  107. /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $ruleCollection */
  108. $ruleCollection = $objectManager->create(
  109. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  110. );
  111. $appliedRulesArray = array_keys(
  112. $ruleCollection->setValidationFilter(
  113. $quote->getStore()->getWebsiteId(),
  114. 0,
  115. '',
  116. null,
  117. $quote->getShippingAddress()
  118. )->getItems()
  119. );
  120. $this->assertEquals([$rule->getRuleId()], $appliedRulesArray);
  121. }
  122. /**
  123. * @magentoDbIsolation disabled
  124. * @magentoAppIsolation enabled
  125. * @magentoDataFixture Magento/Checkout/_files/quote_with_shipping_method_and_items_categories.php
  126. * @magentoDataFixture Magento/SalesRule/_files/rules_group_not_categories_sku_attr.php
  127. * @magentoDataFixture Magento/SalesRule/_files/rules_group_any_categories.php
  128. * @magentoDataFixture Magento/SalesRule/_files/rules_group_any_categories_price_attr_set_any.php
  129. */
  130. public function testSetValidationFilterOther()
  131. {
  132. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  133. /** @var \Magento\Quote\Model\Quote $quote */
  134. $quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
  135. $quote->load('test_order_item_with_items', 'reserved_order_id');
  136. //gather only the existing rules that obey the validation filter
  137. /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $ruleCollection */
  138. $ruleCollection = $objectManager->create(
  139. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  140. );
  141. $appliedRulesArray = array_keys(
  142. $ruleCollection->setValidationFilter(
  143. $quote->getStore()->getWebsiteId(),
  144. 0,
  145. '',
  146. null,
  147. $quote->getShippingAddress()
  148. )->getItems()
  149. );
  150. $this->assertEquals(3, count($appliedRulesArray));
  151. }
  152. /**
  153. * @magentoDbIsolation enabled
  154. * @magentoAppIsolation enabled
  155. * @magentoDataFixture Magento/SalesRule/_files/rules.php
  156. * @magentoDataFixture Magento/SalesRule/_files/coupons.php
  157. * @magentoDataFixture Magento/SalesRule/_files/rule_specific_date.php
  158. * @magentoConfigFixture general/locale/timezone Europe/Kiev
  159. */
  160. public function testMultiRulesWithTimezone()
  161. {
  162. $this->setSpecificTimezone('Europe/Kiev');
  163. $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  164. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  165. );
  166. $collection->addWebsiteGroupDateFilter(1, 0);
  167. $items = array_values($collection->getItems());
  168. $this->assertNotEmpty($items);
  169. }
  170. /**
  171. * @magentoDbIsolation enabled
  172. * @magentoAppIsolation enabled
  173. * @magentoDataFixture Magento/SalesRule/_files/rules.php
  174. * @magentoDataFixture Magento/SalesRule/_files/coupons.php
  175. * @magentoDataFixture Magento/SalesRule/_files/rule_specific_date.php
  176. * @magentoConfigFixture general/locale/timezone Australia/Sydney
  177. */
  178. public function testMultiRulesWithDifferentTimezone()
  179. {
  180. $this->setSpecificTimezone('Australia/Sydney');
  181. $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  182. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  183. );
  184. $collection->addWebsiteGroupDateFilter(1, 0);
  185. $items = array_values($collection->getItems());
  186. $this->assertNotEmpty($items);
  187. }
  188. protected function setSpecificTimezone($timezone)
  189. {
  190. $localeData = [
  191. 'section' => 'general',
  192. 'website' => null,
  193. 'store' => null,
  194. 'groups' => [
  195. 'locale' => [
  196. 'fields' => [
  197. 'timezone' => [
  198. 'value' => $timezone
  199. ]
  200. ]
  201. ]
  202. ]
  203. ];
  204. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Config\Model\Config\Factory::class)
  205. ->create()
  206. ->addData($localeData)
  207. ->save();
  208. }
  209. /**
  210. * Check that it's possible to find previously created rule by attribute.
  211. *
  212. * @magentoDbIsolation enabled
  213. * @magentoAppIsolation enabled
  214. * @magentoDataFixture Magento/SalesRule/_files/rule_custom_product_attribute.php
  215. */
  216. public function testAddAttributeInConditionFilterPositive()
  217. {
  218. $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  219. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  220. );
  221. $collection->addAttributeInConditionFilter('attribute_for_sales_rule_1');
  222. $item = $collection->getFirstItem();
  223. $this->assertEquals('50% Off on some attribute', $item->getName());
  224. }
  225. /**
  226. * Check that it's not possible to find previously created rule by wrong attribute.
  227. *
  228. * @magentoDbIsolation enabled
  229. * @magentoAppIsolation enabled
  230. * @magentoDataFixture Magento/SalesRule/_files/rule_custom_product_attribute.php
  231. */
  232. public function testAddAttributeInConditionFilterNegative()
  233. {
  234. $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  235. \Magento\SalesRule\Model\ResourceModel\Rule\Collection::class
  236. );
  237. $collection->addAttributeInConditionFilter('attribute_for_sales_rule_2');
  238. $this->assertEquals(0, $collection->count());
  239. }
  240. public function tearDown()
  241. {
  242. // restore default timezone
  243. $this->setSpecificTimezone('America/Los_Angeles');
  244. }
  245. }