RateTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Controller\Adminhtml;
  7. /**
  8. * @magentoAppArea adminhtml
  9. */
  10. class RateTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  11. {
  12. /**
  13. * @dataProvider ajaxSaveActionDataProvider
  14. * @magentoDbIsolation enabled
  15. */
  16. public function testAjaxSaveAction($postData, $expectedData)
  17. {
  18. $this->getRequest()->setPostValue($postData);
  19. $this->dispatch('backend/tax/rate/ajaxSave');
  20. $jsonBody = $this->getResponse()->getBody();
  21. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  22. \Magento\Framework\Json\Helper\Data::class
  23. )->jsonDecode(
  24. $jsonBody
  25. );
  26. $this->assertArrayHasKey('tax_calculation_rate_id', $result);
  27. $rateId = $result['tax_calculation_rate_id'];
  28. /** @var $rate \Magento\Tax\Model\Calculation\Rate */
  29. $rate = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  30. \Magento\Tax\Model\Calculation\Rate::class
  31. )->load(
  32. $rateId,
  33. 'tax_calculation_rate_id'
  34. );
  35. $this->assertEquals($expectedData['zip_is_range'], $rate->getZipIsRange());
  36. $this->assertEquals($expectedData['zip_from'], $rate->getZipFrom());
  37. $this->assertEquals($expectedData['zip_to'], $rate->getZipTo());
  38. $this->assertEquals($expectedData['tax_postcode'], $rate->getTaxPostcode());
  39. }
  40. /**
  41. * Data provider for testAjaxSaveAction
  42. *
  43. * @return array
  44. */
  45. public function ajaxSaveActionDataProvider()
  46. {
  47. $postData = ['rate' => '10', 'tax_country_id' => 'US', 'tax_region_id' => '1'];
  48. return [
  49. [
  50. $postData + [
  51. 'code' => 'Rate ' . uniqid(rand()),
  52. 'zip_is_range' => '1',
  53. 'zip_from' => '10000',
  54. 'zip_to' => '20000',
  55. 'tax_postcode' => '*',
  56. ],
  57. ['zip_is_range' => 1, 'zip_from' => '10000', 'zip_to' => '20000', 'tax_postcode' => '10000-20000'],
  58. ],
  59. [
  60. $postData + [
  61. 'code' => 'Rate ' . uniqid(rand()),
  62. 'zip_is_range' => '0',
  63. 'zip_from' => '10000',
  64. 'zip_to' => '20000',
  65. 'tax_postcode' => '*',
  66. ],
  67. ['zip_is_range' => null, 'zip_from' => null, 'zip_to' => null, 'tax_postcode' => '*']
  68. ]
  69. ];
  70. }
  71. /**
  72. * Test wrong data conditions
  73. *
  74. * @dataProvider ajaxSaveActionDataInvalidDataProvider
  75. * @magentoDbIsolation enabled
  76. */
  77. public function testAjaxSaveActionInvalidData($postData, $expectedData)
  78. {
  79. $this->getRequest()->setPostValue($postData);
  80. $this->dispatch('backend/tax/rate/ajaxSave');
  81. $jsonBody = $this->getResponse()->getBody();
  82. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  83. \Magento\Framework\Json\Helper\Data::class
  84. )->jsonDecode(
  85. $jsonBody
  86. );
  87. $this->assertEquals($expectedData['success'], $result['success']);
  88. $this->assertArrayHasKey('error_message', $result);
  89. $this->assertGreaterThan(1, strlen($result['error_message']));
  90. }
  91. /**
  92. * Data provider for testAjaxSaveActionInvalidData
  93. *
  94. * @return array
  95. */
  96. public function ajaxSaveActionDataInvalidDataProvider()
  97. {
  98. $expectedData = [
  99. 'success' => false,
  100. 'error_message' => 'The required information is invalid. Verify the information and try again.',
  101. ];
  102. return [
  103. [
  104. // Zip as range but no range values provided
  105. [
  106. 'rate' => rand(1, 10000),
  107. 'tax_country_id' => 'US',
  108. 'tax_region_id' => '0',
  109. 'code' => 'Rate ' . uniqid(),
  110. 'zip_is_range' => '1',
  111. 'zip_from' => '',
  112. 'zip_to' => '',
  113. 'tax_postcode' => '*'
  114. ],
  115. $expectedData,
  116. ],
  117. // Code is empty
  118. [
  119. [
  120. 'rate' => rand(1, 10000),
  121. 'tax_country_id' => 'US',
  122. 'tax_region_id' => '0',
  123. 'code' => '',
  124. 'zip_is_range' => '0',
  125. 'zip_from' => '10000',
  126. 'zip_to' => '20000',
  127. 'tax_postcode' => '*',
  128. ],
  129. $expectedData
  130. ],
  131. // Country ID empty
  132. [
  133. [
  134. 'rate' => rand(1, 10000),
  135. 'tax_country_id' => '',
  136. 'tax_region_id' => '0',
  137. 'code' => 'Rate ' . uniqid(),
  138. 'zip_is_range' => '0',
  139. 'zip_from' => '10000',
  140. 'zip_to' => '20000',
  141. 'tax_postcode' => '*',
  142. ],
  143. $expectedData
  144. ],
  145. // Tax zip code is empty
  146. [
  147. [
  148. 'rate' => rand(1, 10000),
  149. 'tax_country_id' => 'US',
  150. 'tax_region_id' => '0',
  151. 'code' => 'Rate ' . uniqid(),
  152. 'zip_is_range' => '0',
  153. 'zip_from' => '10000',
  154. 'zip_to' => '20000',
  155. 'tax_postcode' => '',
  156. ],
  157. $expectedData
  158. ],
  159. // All params empty
  160. [
  161. [
  162. 'rate' => '',
  163. 'tax_country_id' => '',
  164. 'tax_region_id' => '1',
  165. 'code' => '',
  166. 'zip_is_range' => '0',
  167. 'zip_from' => '',
  168. 'zip_to' => '',
  169. 'tax_postcode' => '',
  170. ],
  171. $expectedData
  172. ]
  173. ];
  174. }
  175. /**
  176. * @dataProvider ajaxSaveActionDataProvider
  177. * @magentoDbIsolation enabled
  178. *
  179. * @param array $rateClassData
  180. * @SuppressWarnings(PHPMD.NPathComplexity)
  181. */
  182. public function testAjaxLoadAction($rateClassData)
  183. {
  184. /** @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory $rateClassFactory */
  185. $rateClassFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  186. \Magento\Tax\Api\Data\TaxRateInterfaceFactory::class
  187. );
  188. $rateClass = $rateClassFactory->create();
  189. $rateClass->setRate($rateClassData['rate'])
  190. ->setTaxCountryId($rateClassData['tax_country_id'])
  191. ->setTaxRegionId($rateClassData['tax_region_id'])
  192. ->setCode($rateClassData['code'])
  193. ->setZipFrom($rateClassData['zip_from'])
  194. ->setZipIsRange($rateClassData['zip_is_range'])
  195. ->setZipFrom($rateClassData['zip_from'])
  196. ->setZipTo($rateClassData['zip_to'])
  197. ->setTaxPostcode($rateClassData['tax_postcode']);
  198. $rateClass->save($rateClass);
  199. $rateClassId=$rateClass->getTaxCalculationRateId();
  200. /** @var $class \Magento\Tax\Model\Calculation\Rate */
  201. $class = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  202. ->create(\Magento\Tax\Model\Calculation\Rate::class)
  203. ->load($rateClassId, 'tax_calculation_rate_id');
  204. $this->assertEquals($rateClassData['tax_country_id'], $class->getTaxCountryId());
  205. $this->assertEquals($rateClassData['tax_region_id'], $class->getTaxRegionId());
  206. $this->assertEquals($rateClassData['code'], $class->getCode());
  207. $this->assertEquals($rateClassData['rate'], $class->getRate());
  208. $this->assertEquals($rateClassData['zip_is_range']==1 ? 1 : 0, $class->getZipIsRange() ? 1 : 0);
  209. if ($rateClassData['zip_is_range']=='1') {
  210. $this->assertEquals($rateClassData['zip_from'], $class->getZipFrom());
  211. $this->assertEquals($rateClassData['zip_to'], $class->getZipTo());
  212. }
  213. $postData = [ 'id' => $rateClassId ];
  214. $this->getRequest()->setPostValue($postData);
  215. $this->dispatch('backend/tax/rate/ajaxLoad');
  216. $jsonBody = $this->getResponse()->getBody();
  217. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  218. \Magento\Framework\Json\Helper\Data::class
  219. )->jsonDecode(
  220. $jsonBody
  221. );
  222. $this->assertTrue(is_array($result));
  223. $this->assertArrayHasKey('success', $result);
  224. $this->assertTrue($result['success'] == true);
  225. $this->assertArrayHasKey('result', $result);
  226. $this->assertTrue(is_array($result['result']));
  227. $this->assertEquals($result['result']['tax_country_id'], $class->getTaxCountryId());
  228. $this->assertEquals($result['result']['tax_region_id'], $class->getTaxRegionId());
  229. $this->assertEquals($result['result']['tax_postcode'], $class->getTaxPostcode());
  230. $this->assertEquals($result['result']['code'], $class->getCode());
  231. $this->assertEquals($result['result']['rate'], $class->getRate());
  232. $expectedZipIsRange=$result['result']['zip_is_range'] == 1 ? 1 : 0;
  233. $this->assertEquals($expectedZipIsRange, $class->getZipIsRange() ? 1 : 0);
  234. if ($expectedZipIsRange) {
  235. $this->assertEquals($result['result']['zip_from'], $class->getZipFrom());
  236. $this->assertEquals($result['result']['zip_to'], $class->getZipTo());
  237. }
  238. }
  239. /**
  240. * @magentoDbIsolation enabled
  241. *
  242. */
  243. public function testAjaxNonLoadAction()
  244. {
  245. $postData = [ 'id' => 99999999 ];
  246. $this->getRequest()->setPostValue($postData);
  247. $this->dispatch('backend/tax/rate/ajaxLoad');
  248. $jsonBody = $this->getResponse()->getBody();
  249. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  250. \Magento\Framework\Json\Helper\Data::class
  251. )->jsonDecode(
  252. $jsonBody
  253. );
  254. $this->assertTrue(is_array($result));
  255. $this->assertArrayHasKey('success', $result);
  256. $this->assertTrue($result['success'] == false);
  257. $this->assertTrue(!array_key_exists('result', $result));
  258. $this->assertArrayHasKey('error_message', $result);
  259. $this->assertTrue(strlen($result['error_message'])>0);
  260. }
  261. }