ClassTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model;
  7. class ClassTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\TestFramework\ObjectManager
  11. */
  12. protected $_objectManager;
  13. protected function setUp()
  14. {
  15. /** @var $objectManager \Magento\TestFramework\ObjectManager */
  16. $this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  17. }
  18. /**
  19. * @magentoDbIsolation enabled
  20. */
  21. public function testCheckClassCanBeDeletedCustomerClassAssertException()
  22. {
  23. /** @var $model \Magento\Tax\Model\ClassModel */
  24. $model = $this->_objectManager->create(
  25. \Magento\Tax\Model\ClassModel::class
  26. )->getCollection()->setClassTypeFilter(
  27. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER
  28. )->getFirstItem();
  29. $this->expectException(\Magento\Framework\Exception\CouldNotDeleteException::class);
  30. $model->delete();
  31. }
  32. /**
  33. * @magentoDbIsolation enabled
  34. */
  35. public function testCheckClassCanBeDeletedProductClassAssertException()
  36. {
  37. /** @var $model \Magento\Tax\Model\ClassModel */
  38. $model = $this->_objectManager->create(
  39. \Magento\Tax\Model\ClassModel::class
  40. )->getCollection()->setClassTypeFilter(
  41. \Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT
  42. )->getFirstItem();
  43. $this->_objectManager->create(
  44. \Magento\Catalog\Model\Product::class
  45. )->setTypeId(
  46. 'simple'
  47. )->setAttributeSetId(
  48. 4
  49. )->setName(
  50. 'Simple Product'
  51. )->setSku(
  52. uniqid()
  53. )->setPrice(
  54. 10
  55. )->setMetaTitle(
  56. 'meta title'
  57. )->setMetaKeyword(
  58. 'meta keyword'
  59. )->setMetaDescription(
  60. 'meta description'
  61. )->setVisibility(
  62. \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
  63. )->setStatus(
  64. \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
  65. )->setTaxClassId(
  66. $model->getId()
  67. )->save();
  68. $this->expectException(\Magento\Framework\Exception\CouldNotDeleteException::class);
  69. $model->delete();
  70. }
  71. /**
  72. * @magentoDbIsolation enabled
  73. * @dataProvider classesDataProvider
  74. */
  75. public function testCheckClassCanBeDeletedPositiveResult($classType)
  76. {
  77. /** @var $model \Magento\Tax\Model\ClassModel */
  78. $model = $this->_objectManager->create(\Magento\Tax\Model\ClassModel::class);
  79. $model->setClassName('TaxClass' . uniqid())->setClassType($classType)->isObjectNew(true);
  80. $model->save();
  81. $model->delete();
  82. }
  83. public function classesDataProvider()
  84. {
  85. return [
  86. [\Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_CUSTOMER],
  87. [\Magento\Tax\Model\ClassModel::TAX_CLASS_TYPE_PRODUCT]
  88. ];
  89. }
  90. /**
  91. * @magentoAppIsolation enabled
  92. * @magentoDataFixture Magento/Tax/_files/tax_classes.php
  93. */
  94. public function testCheckClassCanBeDeletedCustomerClassUsedInTaxRule()
  95. {
  96. /** @var $registry \Magento\Framework\Registry */
  97. $registry = $this->_objectManager->get(\Magento\Framework\Registry::class);
  98. /** @var $taxRule \Magento\Tax\Model\Calculation\Rule */
  99. $taxRule = $registry->registry('_fixture/Magento_Tax_Model_Calculation_Rule');
  100. $customerClasses = $taxRule->getCustomerTaxClasses();
  101. /** @var $model \Magento\Tax\Model\ClassModel */
  102. $model = $this->_objectManager->create(\Magento\Tax\Model\ClassModel::class)->load($customerClasses[0]);
  103. $this->expectException(\Magento\Framework\Exception\CouldNotDeleteException::class);
  104. $this->expectExceptionMessage('You cannot delete this tax class because it is used in' .
  105. ' Tax Rules. You have to delete the rules it is used in first.');
  106. $model->delete();
  107. }
  108. /**
  109. * @magentoAppIsolation enabled
  110. * @magentoDataFixture Magento/Tax/_files/tax_classes.php
  111. */
  112. public function testCheckClassCanBeDeletedProductClassUsedInTaxRule()
  113. {
  114. /** @var $registry \Magento\Framework\Registry */
  115. $registry = $this->_objectManager->get(\Magento\Framework\Registry::class);
  116. /** @var $taxRule \Magento\Tax\Model\Calculation\Rule */
  117. $taxRule = $registry->registry('_fixture/Magento_Tax_Model_Calculation_Rule');
  118. $productClasses = $taxRule->getProductTaxClasses();
  119. /** @var $model \Magento\Tax\Model\ClassModel */
  120. $model = $this->_objectManager->create(\Magento\Tax\Model\ClassModel::class)->load($productClasses[0]);
  121. $this->expectException(\Magento\Framework\Exception\CouldNotDeleteException::class);
  122. $this->expectExceptionMessage('You cannot delete this tax class because it is used in' .
  123. ' Tax Rules. You have to delete the rules it is used in first.');
  124. $model->delete();
  125. }
  126. }