UpdateHandlerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. /**
  9. * @magentoAppArea adminhtml
  10. * @magentoAppIsolation enabled
  11. */
  12. class UpdateHandlerTest extends \Magento\TestFramework\Indexer\TestCase
  13. {
  14. /**
  15. * @covers \Magento\Eav\Model\ResourceModel\UpdateHandler::execute
  16. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  17. * @dataProvider getAllStoresDataProvider
  18. * @param $code
  19. * @param $snapshotValue
  20. * @param $newValue
  21. * @param $expected
  22. * @magentoDbIsolation disabled
  23. */
  24. public function testExecuteProcessForAllStores($code, $snapshotValue, $newValue, $expected)
  25. {
  26. if ($snapshotValue !== '-') {
  27. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  28. $entity->setStoreId(0);
  29. $entity->load(1);
  30. $entity->setData($code, $snapshotValue);
  31. $entity->save();
  32. }
  33. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  34. $entity->setStoreId(0);
  35. $entity->load(1);
  36. $updateHandler = Bootstrap::getObjectManager()->create(UpdateHandler::class);
  37. $entityData = array_merge($entity->getData(), [$code => $newValue]);
  38. $updateHandler->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $entityData);
  39. $resultEntity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  40. $resultEntity->setStoreId(0);
  41. $resultEntity->load(1);
  42. $this->assertSame($expected, $resultEntity->getData($code));
  43. }
  44. /**
  45. * @covers \Magento\Eav\Model\ResourceModel\UpdateHandlerTest::execute
  46. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  47. * @magentoDataFixture Magento/Store/_files/second_store.php
  48. * @dataProvider getCustomStoreDataProvider
  49. * @param $code
  50. * @param $snapshotValue
  51. * @param $newValue
  52. * @param $expected
  53. * @magentoDbIsolation disabled
  54. */
  55. public function testExecuteProcessForCustomStore($code, $snapshotValue, $newValue, $expected)
  56. {
  57. $store = Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
  58. $store->load('fixture_second_store', 'code');
  59. Bootstrap::getObjectManager()
  60. ->create(\Magento\CatalogSearch\Model\Indexer\Fulltext\Processor::class)
  61. ->reindexAll();
  62. if ($snapshotValue !== '-') {
  63. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  64. $entity->setStoreId($store->getId());
  65. $entity->load(1);
  66. $entity->setData($code, $snapshotValue);
  67. $entity->save();
  68. }
  69. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  70. $entity->setStoreId($store->getId());
  71. $entity->load(1);
  72. $updateHandler = Bootstrap::getObjectManager()->create(UpdateHandler::class);
  73. $entityData = array_merge($entity->getData(), [$code => $newValue]);
  74. $updateHandler->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $entityData);
  75. $resultEntity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  76. $resultEntity->setStoreId($store->getId());
  77. $resultEntity->load(1);
  78. $this->assertSame($expected, $resultEntity->getData($code));
  79. }
  80. /**
  81. * @covers \Magento\Eav\Model\ResourceModel\UpdateHandlerTest::execute
  82. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  83. * @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
  84. * @magentoDataFixture Magento/Store/_files/second_store.php
  85. * @dataProvider getCustomAttributeDataProvider
  86. * @param $code
  87. * @param $defaultStoreValue
  88. * @param $snapshotValue
  89. * @param $newValue
  90. * @param $expected
  91. * @magentoDbIsolation disabled
  92. */
  93. public function testExecuteProcessForCustomAttributeInCustomStore(
  94. $code,
  95. $defaultStoreValue,
  96. $snapshotValue,
  97. $newValue,
  98. $expected
  99. ) {
  100. $store = Bootstrap::getObjectManager()->create(\Magento\Store\Model\Store::class);
  101. $store->load('fixture_second_store', 'code');
  102. Bootstrap::getObjectManager()
  103. ->create(\Magento\CatalogSearch\Model\Indexer\Fulltext\Processor::class)
  104. ->reindexAll();
  105. $attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  106. \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
  107. );
  108. $attribute->loadByCode(4, $code);
  109. $options = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  110. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
  111. );
  112. $options->setAttributeFilter($attribute->getId());
  113. $optionIds = $options->getAllIds();
  114. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  115. $entity->setStoreId(0);
  116. $entity->load(1);
  117. $entity->setData($code, $optionIds[$defaultStoreValue]);
  118. $entity->save();
  119. if ($snapshotValue !== '-') {
  120. /** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */
  121. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  122. $entity->setStoreId($store->getId());
  123. $entity->load(1);
  124. if ($snapshotValue) {
  125. $snapshotValue = $optionIds[$snapshotValue];
  126. }
  127. $entity->setData($code, $snapshotValue);
  128. $entity->save();
  129. }
  130. $entity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  131. $entity->setStoreId($store->getId());
  132. $entity->load(1);
  133. $updateHandler = Bootstrap::getObjectManager()->create(UpdateHandler::class);
  134. if ($newValue) {
  135. $newValue = $optionIds[$newValue];
  136. }
  137. $entityData = array_merge($entity->getData(), [$code => $newValue]);
  138. $updateHandler->execute(\Magento\Catalog\Api\Data\ProductInterface::class, $entityData);
  139. $resultEntity = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
  140. $resultEntity->setStoreId($store->getId());
  141. $resultEntity->load(1);
  142. if ($expected !== null) {
  143. $expected = $optionIds[$expected];
  144. }
  145. $this->assertSame($expected, $resultEntity->getData($code));
  146. }
  147. /**
  148. * @return array
  149. */
  150. public function getAllStoresDataProvider()
  151. {
  152. return [
  153. ['description', '', 'not_empty_value', 'not_empty_value'], //0
  154. ['description', '', '', null], //1
  155. ['description', '', null, null], //2
  156. ['description', '', false, null], //3
  157. ['description', 'not_empty_value', 'not_empty_value2', 'not_empty_value2'], //4
  158. ['description', 'not_empty_value', '', null], //5
  159. ['description', 'not_empty_value', null, null], //6
  160. ['description', 'not_empty_value', false, null], //7
  161. ['description', null, 'not_empty_value', 'not_empty_value'], //8
  162. ['description', null, '', null], //9
  163. ['description', null, false, null], //10
  164. ['description', false, 'not_empty_value', 'not_empty_value'], //11
  165. ['description', false, '', null], //12
  166. ['description', false, null, null], //13
  167. ];
  168. }
  169. /**
  170. * @return array
  171. */
  172. public function getCustomStoreDataProvider()
  173. {
  174. return [
  175. ['description', '', 'not_empty_value', 'not_empty_value'], //0
  176. ['description', '', '', null], //1
  177. ['description', '', null, 'Description with <b>html tag</b>'], //2
  178. ['description', '', false, 'Description with <b>html tag</b>'], //3
  179. ['description', 'not_empty_value', 'not_empty_value2', 'not_empty_value2'], //4
  180. ['description', 'not_empty_value', '', null], //5
  181. ['description', 'not_empty_value', null, 'Description with <b>html tag</b>'], //6
  182. ['description', 'not_empty_value', false, 'Description with <b>html tag</b>'], //7
  183. ['description', null, 'not_empty_value', 'not_empty_value'], //8
  184. ['description', null, '', null], //9
  185. ['description', null, false, 'Description with <b>html tag</b>'], //10
  186. ['description', false, 'not_empty_value', 'not_empty_value'], //11
  187. ['description', false, '', null], //12
  188. ['description', false, null, 'Description with <b>html tag</b>'], //13
  189. ];
  190. }
  191. /**
  192. * @return array
  193. */
  194. public function getCustomAttributeDataProvider()
  195. {
  196. return [
  197. ['dropdown_attribute', 0, '', 1, 1], //0
  198. ['dropdown_attribute', 0, '', '', null], //1
  199. ['dropdown_attribute', 0, '', null, 0], //2
  200. ['dropdown_attribute', 0, '', false, 0], //3
  201. ['dropdown_attribute', 0, 1, 2, 2], //4
  202. ['dropdown_attribute', 0, 1, '', null], //5
  203. ['dropdown_attribute', 0, 1, null, 0], //6
  204. ['dropdown_attribute', 0, 1, false, 0], //7
  205. ['dropdown_attribute', 0, null, 1, 1], //8
  206. ['dropdown_attribute', 0, null, '', null], //9
  207. ['dropdown_attribute', 0, null, false, 0], //10
  208. ['dropdown_attribute', 0, false, 1, 1], //11
  209. ['dropdown_attribute', 0, false, '', null], //12
  210. ['dropdown_attribute', 0, false, null, 0], //13
  211. ['dropdown_attribute', 0, '-', 1, 1], //14
  212. ['dropdown_attribute', 0, '-', '', null], //15
  213. ['dropdown_attribute', 0, '-', null, 0], //16
  214. ['dropdown_attribute', 0, '-', false, 0], //17
  215. ];
  216. }
  217. }