TaxTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. use Magento\Framework\Exception\NoSuchEntityException;
  8. /**
  9. * @magentoAppArea adminhtml
  10. */
  11. class TaxTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  12. {
  13. /**
  14. * @dataProvider ajaxActionDataProvider
  15. * @magentoDbIsolation enabled
  16. *
  17. * @param array $postData
  18. * @param array $expectedData
  19. */
  20. public function testAjaxSaveAction($postData, $expectedData)
  21. {
  22. $this->getRequest()->setPostValue($postData);
  23. $this->dispatch('backend/tax/tax/ajaxSave');
  24. $jsonBody = $this->getResponse()->getBody();
  25. $result = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  26. \Magento\Framework\Json\Helper\Data::class
  27. )->jsonDecode(
  28. $jsonBody
  29. );
  30. $this->assertArrayHasKey('class_id', $result);
  31. $classId = $result['class_id'];
  32. /** @var $class \Magento\Tax\Model\ClassModel */
  33. $class = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  34. \Magento\Tax\Model\ClassModel::class
  35. )->load($classId, 'class_id');
  36. $this->assertEquals($expectedData['class_name'], $class->getClassName());
  37. }
  38. /**
  39. * @dataProvider ajaxActionDataProvider
  40. * @magentoDbIsolation enabled
  41. *
  42. * @param array $taxClassData
  43. */
  44. public function testAjaxDeleteAction($taxClassData)
  45. {
  46. /** @var \Magento\Tax\Api\TaxClassRepositoryInterface $taxClassService */
  47. $taxClassService = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  48. \Magento\Tax\Api\TaxClassRepositoryInterface::class
  49. );
  50. $taxClassFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  51. \Magento\Tax\Api\Data\TaxClassInterfaceFactory::class
  52. );
  53. $taxClass = $taxClassFactory->create();
  54. $taxClass->setClassName($taxClassData['class_name'])
  55. ->setClassType($taxClassData['class_type']);
  56. $taxClassId = $taxClassService->save($taxClass);
  57. /** @var $class \Magento\Tax\Model\ClassModel */
  58. $class = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  59. \Magento\Tax\Model\ClassModel::class
  60. )->load($taxClassId, 'class_id');
  61. $this->assertEquals($taxClassData['class_name'], $class->getClassName());
  62. $this->assertEquals($taxClassData['class_type'], $class->getClassType());
  63. $postData = [ 'class_id' => $taxClassId ];
  64. $this->getRequest()->setPostValue($postData);
  65. $this->dispatch('backend/tax/tax/ajaxDelete');
  66. $isFound = true;
  67. try {
  68. $taxClassId = $taxClassService->get($taxClassId);
  69. } catch (NoSuchEntityException $e) {
  70. $isFound = false;
  71. }
  72. $this->assertFalse($isFound, "Tax Class was found when it should have been deleted.");
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function ajaxActionDataProvider()
  78. {
  79. return [
  80. [
  81. ['class_type' => 'CUSTOMER', 'class_name' => 'Class Name'],
  82. ['class_name' => 'Class Name'],
  83. ],
  84. [
  85. ['class_type' => 'PRODUCT', 'class_name' => '11111<22222'],
  86. ['class_name' => '11111<22222']
  87. ],
  88. [
  89. ['class_type' => 'CUSTOMER', 'class_name' => ' 12<>sa&df '],
  90. ['class_name' => '12<>sa&df']
  91. ]
  92. ];
  93. }
  94. }