RuleTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogRule\Test\Unit\Model;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class RuleTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\CatalogRule\Model\Rule */
  13. protected $rule;
  14. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager */
  15. private $objectManager;
  16. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $storeManager;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject */
  19. protected $combineFactory;
  20. /** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $storeModel;
  22. /** @var \Magento\Store\Model\Website|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $websiteModel;
  24. /** @var \Magento\Rule\Model\Condition\Combine|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $condition;
  26. /**
  27. * @var \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $_ruleProductProcessor;
  30. /**
  31. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $_productCollectionFactory;
  34. /**
  35. * @var \Magento\Framework\Model\ResourceModel\Iterator|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $_resourceIterator;
  38. /**
  39. * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $productModel;
  42. /**
  43. * Set up before test
  44. *
  45. * @return void
  46. */
  47. protected function setUp()
  48. {
  49. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  50. $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  51. $this->storeModel = $this->createPartialMock(\Magento\Store\Model\Store::class, ['__wakeup', 'getId']);
  52. $this->combineFactory = $this->createPartialMock(
  53. \Magento\CatalogRule\Model\Rule\Condition\CombineFactory::class,
  54. [
  55. 'create'
  56. ]
  57. );
  58. $this->productModel = $this->createPartialMock(
  59. \Magento\Catalog\Model\Product::class,
  60. [
  61. '__wakeup',
  62. 'getId',
  63. 'setData'
  64. ]
  65. );
  66. $this->condition = $this->createPartialMock(
  67. \Magento\Rule\Model\Condition\Combine::class,
  68. [
  69. 'setRule',
  70. 'validate'
  71. ]
  72. );
  73. $this->websiteModel = $this->createPartialMock(
  74. \Magento\Store\Model\Website::class,
  75. [
  76. '__wakeup',
  77. 'getId',
  78. 'getDefaultStore'
  79. ]
  80. );
  81. $this->_ruleProductProcessor = $this->createMock(
  82. \Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor::class
  83. );
  84. $this->_productCollectionFactory = $this->createPartialMock(
  85. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class,
  86. ['create']
  87. );
  88. $this->_resourceIterator = $this->createPartialMock(
  89. \Magento\Framework\Model\ResourceModel\Iterator::class,
  90. ['walk']
  91. );
  92. $extensionFactoryMock = $this->createMock(\Magento\Framework\Api\ExtensionAttributesFactory::class);
  93. $attributeValueFactoryMock = $this->createMock(\Magento\Framework\Api\AttributeValueFactory::class);
  94. $this->rule = $this->objectManager->getObject(
  95. \Magento\CatalogRule\Model\Rule::class,
  96. [
  97. 'storeManager' => $this->storeManager,
  98. 'combineFactory' => $this->combineFactory,
  99. 'ruleProductProcessor' => $this->_ruleProductProcessor,
  100. 'productCollectionFactory' => $this->_productCollectionFactory,
  101. 'resourceIterator' => $this->_resourceIterator,
  102. 'extensionFactory' => $extensionFactoryMock,
  103. 'customAttributeFactory' => $attributeValueFactoryMock,
  104. 'serializer' => $this->getSerializerMock(),
  105. ]
  106. );
  107. }
  108. /**
  109. * Get mock for serializer
  110. *
  111. * @return \PHPUnit_Framework_MockObject_MockObject
  112. */
  113. private function getSerializerMock()
  114. {
  115. $serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  116. ->disableOriginalConstructor()
  117. ->setMethods(['serialize', 'unserialize'])
  118. ->getMock();
  119. $serializerMock->expects($this->any())
  120. ->method('serialize')
  121. ->will(
  122. $this->returnCallback(
  123. function ($value) {
  124. return json_encode($value);
  125. }
  126. )
  127. );
  128. $serializerMock->expects($this->any())
  129. ->method('unserialize')
  130. ->will(
  131. $this->returnCallback(
  132. function ($value) {
  133. return json_decode($value, true);
  134. }
  135. )
  136. );
  137. return $serializerMock;
  138. }
  139. /**
  140. * @dataProvider dataProviderCallbackValidateProduct
  141. * @param bool $validate
  142. *
  143. * @return void
  144. */
  145. public function testCallbackValidateProduct($validate)
  146. {
  147. $args['product'] = $this->productModel;
  148. $args['attributes'] = [];
  149. $args['idx'] = 0;
  150. $args['row'] = [
  151. 'entity_id' => '1',
  152. 'entity_type_id' => '4',
  153. 'attribute_set_id' => '4',
  154. 'type_id' => 'simple',
  155. 'sku' => 'Product',
  156. 'has_options' => '0',
  157. 'required_options' => '0',
  158. 'created_at' => '2014-06-25 13:14:30',
  159. 'updated_at' => '2014-06-25 14:37:15'
  160. ];
  161. $this->storeManager->expects($this->any())->method('getWebsites')->with(false)
  162. ->will($this->returnValue([$this->websiteModel, $this->websiteModel]));
  163. $this->websiteModel->expects($this->at(0))->method('getId')
  164. ->will($this->returnValue('1'));
  165. $this->websiteModel->expects($this->at(2))->method('getId')
  166. ->will($this->returnValue('2'));
  167. $this->websiteModel->expects($this->any())->method('getDefaultStore')
  168. ->will($this->returnValue($this->storeModel));
  169. $this->storeModel->expects($this->at(0))->method('getId')
  170. ->will($this->returnValue('1'));
  171. $this->storeModel->expects($this->at(1))->method('getId')
  172. ->will($this->returnValue('2'));
  173. $this->combineFactory->expects($this->any())->method('create')
  174. ->will($this->returnValue($this->condition));
  175. $this->condition->expects($this->any())->method('validate')
  176. ->will($this->returnValue($validate));
  177. $this->condition->expects($this->any())->method('setRule')
  178. ->will($this->returnSelf());
  179. $this->productModel->expects($this->any())->method('getId')
  180. ->will($this->returnValue(1));
  181. $this->rule->callbackValidateProduct($args);
  182. $matchingProducts = $this->rule->getMatchingProductIds();
  183. foreach ($matchingProducts['1'] as $matchingRules) {
  184. $this->assertEquals($validate, $matchingRules);
  185. }
  186. }
  187. /**
  188. * Data provider for callbackValidateProduct test
  189. *
  190. * @return array
  191. */
  192. public function dataProviderCallbackValidateProduct()
  193. {
  194. return [
  195. [false],
  196. [true],
  197. ];
  198. }
  199. /**
  200. * Test validateData action
  201. *
  202. * @dataProvider validateDataDataProvider
  203. * @param array $data Data for the rule actions
  204. * @param bool|array $expected True or an array of errors
  205. *
  206. * @return void
  207. */
  208. public function testValidateData($data, $expected)
  209. {
  210. $result = $this->rule->validateData(new \Magento\Framework\DataObject($data));
  211. $this->assertEquals($result, $expected);
  212. }
  213. /**
  214. * Data provider for testValidateData test
  215. *
  216. * @return array
  217. */
  218. public function validateDataDataProvider()
  219. {
  220. return [
  221. [
  222. [
  223. 'simple_action' => 'by_fixed',
  224. 'discount_amount' => '123',
  225. ],
  226. true
  227. ],
  228. [
  229. [
  230. 'simple_action' => 'by_percent',
  231. 'discount_amount' => '9,99',
  232. ],
  233. true
  234. ],
  235. [
  236. [
  237. 'simple_action' => 'by_percent',
  238. 'discount_amount' => '123.12',
  239. ],
  240. [
  241. 'Percentage discount should be between 0 and 100.',
  242. ]
  243. ],
  244. [
  245. [
  246. 'simple_action' => 'to_percent',
  247. 'discount_amount' => '-12',
  248. ],
  249. [
  250. 'Percentage discount should be between 0 and 100.',
  251. ]
  252. ],
  253. [
  254. [
  255. 'simple_action' => 'to_fixed',
  256. 'discount_amount' => '-1234567890',
  257. ],
  258. [
  259. 'Discount value should be 0 or greater.',
  260. ]
  261. ],
  262. [
  263. [
  264. 'simple_action' => 'invalid action',
  265. 'discount_amount' => '12',
  266. ],
  267. [
  268. 'Unknown action.',
  269. ]
  270. ],
  271. ];
  272. }
  273. /**
  274. * Test after delete action
  275. *
  276. * @return void
  277. */
  278. public function testAfterDelete()
  279. {
  280. $indexer = $this->createMock(\Magento\Framework\Indexer\IndexerInterface::class);
  281. $indexer->expects($this->once())->method('invalidate');
  282. $this->_ruleProductProcessor->expects($this->once())->method('getIndexer')->will($this->returnValue($indexer));
  283. $this->rule->afterDelete();
  284. }
  285. /**
  286. * Test after update action
  287. *
  288. * @return void
  289. */
  290. public function testAfterUpdate()
  291. {
  292. $this->rule->isObjectNew(false);
  293. $indexer = $this->createMock(\Magento\Framework\Indexer\IndexerInterface::class);
  294. $indexer->expects($this->once())->method('invalidate');
  295. $this->_ruleProductProcessor->expects($this->once())->method('getIndexer')->will($this->returnValue($indexer));
  296. $this->rule->afterSave();
  297. }
  298. /**
  299. * Test isRuleBehaviorChanged action
  300. *
  301. * @dataProvider isRuleBehaviorChangedDataProvider
  302. *
  303. * @param array $dataArray
  304. * @param array $originDataArray
  305. * @param bool $isObjectNew
  306. * @param bool $result
  307. *
  308. * @return void
  309. */
  310. public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjectNew, $result)
  311. {
  312. $this->rule->setData('website_ids', []);
  313. $this->rule->isObjectNew($isObjectNew);
  314. $indexer = $this->createMock(\Magento\Framework\Indexer\IndexerInterface::class);
  315. $indexer->expects($this->any())->method('invalidate');
  316. $this->_ruleProductProcessor->expects($this->any())->method('getIndexer')->will($this->returnValue($indexer));
  317. foreach ($dataArray as $data) {
  318. $this->rule->setData($data);
  319. }
  320. $this->rule->afterSave();
  321. foreach ($originDataArray as $data) {
  322. $this->rule->setOrigData($data);
  323. }
  324. $this->assertEquals($result, $this->rule->isRuleBehaviorChanged());
  325. }
  326. /**
  327. * Data provider for testIsRuleBehaviorChanged test
  328. *
  329. * @return array
  330. */
  331. public function isRuleBehaviorChangedDataProvider()
  332. {
  333. return [
  334. [['new name', 'new description'], ['name', 'description'], false, false],
  335. [['name', 'description'], ['name', 'description'], false, false],
  336. [['name', 'important_data'], ['name', 'important_data'], false, false],
  337. [['name', 'new important_data'], ['name', 'important_data'], false, true],
  338. [['name', 'description'], ['name', 'description'], true, true],
  339. [['name', 'description'], ['name', 'important_data'], true, true],
  340. ];
  341. }
  342. public function testGetConditionsFieldSetId()
  343. {
  344. $formName = 'form_name';
  345. $this->rule->setId(100);
  346. $expectedResult = 'form_namerule_conditions_fieldset_100';
  347. $this->assertEquals($expectedResult, $this->rule->getConditionsFieldSetId($formName));
  348. }
  349. public function testReindex()
  350. {
  351. $this->_ruleProductProcessor->expects($this->once())->method('reindexList');
  352. $this->rule->reindex();
  353. }
  354. }