PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Tax/Controller/Adminhtml/RateTest.php

https://gitlab.com/axeltizon/magento-demopoweraccess
PHP | 299 lines | 228 code | 24 blank | 47 comment | 5 complexity | a22d01542d1ec5eed2c4d11fc30be094 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. 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'
  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'
  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'
  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' => 'Make sure all required information is valid.',
  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. // Rate empty
  146. [
  147. [
  148. 'rate' => '',
  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. // Tax zip code is empty
  160. [
  161. [
  162. 'rate' => rand(1, 10000),
  163. 'tax_country_id' => 'US',
  164. 'tax_region_id' => '0',
  165. 'code' => 'Rate ' . uniqid(),
  166. 'zip_is_range' => '0',
  167. 'zip_from' => '10000',
  168. 'zip_to' => '20000',
  169. 'tax_postcode' => '',
  170. ],
  171. $expectedData
  172. ],
  173. // All params empty
  174. [
  175. [
  176. 'rate' => '',
  177. 'tax_country_id' => '',
  178. 'tax_region_id' => '1',
  179. 'code' => '',
  180. 'zip_is_range' => '0',
  181. 'zip_from' => '',
  182. 'zip_to' => '',
  183. 'tax_postcode' => '',
  184. ],
  185. $expectedData
  186. ]
  187. ];
  188. }
  189. /**
  190. * @dataProvider ajaxSaveActionDataProvider
  191. * @magentoDbIsolation enabled
  192. *
  193. * @param array $rateClassData
  194. * @SuppressWarnings(PHPMD.NPathComplexity)
  195. */
  196. public function testAjaxLoadAction($rateClassData)
  197. {
  198. /** @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory $rateClassFactory */
  199. $rateClassFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  200. 'Magento\Tax\Api\Data\TaxRateInterfaceFactory'
  201. );
  202. $rateClass = $rateClassFactory->create();
  203. $rateClass->setRate($rateClassData['rate'])
  204. ->setTaxCountryId($rateClassData['tax_country_id'])
  205. ->setTaxRegionId($rateClassData['tax_region_id'])
  206. ->setCode($rateClassData['code'])
  207. ->setZipFrom($rateClassData['zip_from'])
  208. ->setZipIsRange($rateClassData['zip_is_range'])
  209. ->setZipFrom($rateClassData['zip_from'])
  210. ->setZipTo($rateClassData['zip_to'])
  211. ->setTaxPostcode($rateClassData['tax_postcode']);
  212. $rateClass->save($rateClass);
  213. $rateClassId=$rateClass->getTaxCalculationRateId();
  214. /** @var $class \Magento\Tax\Model\Calculation\Rate */
  215. $class = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  216. ->create('Magento\Tax\Model\Calculation\Rate')
  217. ->load($rateClassId, 'tax_calculation_rate_id');
  218. $this->assertEquals($rateClassData['tax_country_id'], $class->getTaxCountryId());
  219. $this->assertEquals($rateClassData['tax_region_id'], $class->getTaxRegionId());
  220. $this->assertEquals($rateClassData['code'], $class->getCode());
  221. $this->assertEquals($rateClassData['rate'], $class->getRate());
  222. $this->assertEquals($rateClassData['zip_is_range']==1 ? 1 : 0, $class->getZipIsRange() ? 1 : 0);
  223. if ($rateClassData['zip_is_range']=='1') {
  224. $this->assertEquals($rateClassData['zip_from'], $class->getZipFrom());
  225. $this->assertEquals($rateClassData['zip_to'], $class->getZipTo());
  226. }
  227. $postData = [ 'id' => $rateClassId ];
  228. $this->getRequest()->setPostValue($postData);
  229. $this->dispatch('backend/tax/rate/ajaxLoad');
  230. $jsonBody = $this->getResponse()->getBody();
  231. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  232. 'Magento\Framework\Json\Helper\Data'
  233. )->jsonDecode(
  234. $jsonBody
  235. );
  236. $this->assertTrue(is_array($result));
  237. $this->assertArrayHasKey('success', $result);
  238. $this->assertTrue($result['success'] == true);
  239. $this->assertArrayHasKey('result', $result);
  240. $this->assertTrue(is_array($result['result']));
  241. $this->assertEquals($result['result']['tax_country_id'], $class->getTaxCountryId());
  242. $this->assertEquals($result['result']['tax_region_id'], $class->getTaxRegionId());
  243. $this->assertEquals($result['result']['tax_postcode'], $class->getTaxPostcode());
  244. $this->assertEquals($result['result']['code'], $class->getCode());
  245. $this->assertEquals($result['result']['rate'], $class->getRate());
  246. $expectedZipIsRange=$result['result']['zip_is_range'] == 1 ? 1 : 0;
  247. $this->assertEquals($expectedZipIsRange, $class->getZipIsRange() ? 1 : 0);
  248. if ($expectedZipIsRange) {
  249. $this->assertEquals($result['result']['zip_from'], $class->getZipFrom());
  250. $this->assertEquals($result['result']['zip_to'], $class->getZipTo());
  251. }
  252. }
  253. /**
  254. * @magentoDbIsolation enabled
  255. *
  256. */
  257. public function testAjaxNonLoadAction()
  258. {
  259. $postData = [ 'id' => 99999999 ];
  260. $this->getRequest()->setPostValue($postData);
  261. $this->dispatch('backend/tax/rate/ajaxLoad');
  262. $jsonBody = $this->getResponse()->getBody();
  263. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  264. 'Magento\Framework\Json\Helper\Data'
  265. )->jsonDecode(
  266. $jsonBody
  267. );
  268. $this->assertTrue(is_array($result));
  269. $this->assertArrayHasKey('success', $result);
  270. $this->assertTrue($result['success'] == false);
  271. $this->assertTrue(!array_key_exists('result', $result));
  272. $this->assertArrayHasKey('error_message', $result);
  273. $this->assertTrue(strlen($result['error_message'])>0);
  274. }
  275. }