EavAttributeTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Test\Unit\Model\Plugin;
  7. use Magento\Swatches\Model\Plugin\EavAttribute;
  8. use Magento\Swatches\Model\Swatch;
  9. class EavAttributeTest extends \PHPUnit\Framework\TestCase
  10. {
  11. const ATTRIBUTE_ID = 123;
  12. const OPTION_ID = 'option 12';
  13. const STORE_ID = 'option 89';
  14. const ATTRIBUTE_DEFAULT_VALUE = 1;
  15. const ATTRIBUTE_OPTION_VALUE = 2;
  16. const ATTRIBUTE_SWATCH_VALUE = 3;
  17. /** @var EavAttribute */
  18. private $eavAttribute;
  19. /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject */
  20. private $attribute;
  21. /** @var \Magento\Swatches\Model\SwatchFactory|\PHPUnit_Framework_MockObject_MockObject */
  22. private $swatchFactory;
  23. /** @var \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject */
  24. private $collectionFactory;
  25. /** @var \Magento\Swatches\Helper\Data|\PHPUnit_Framework_MockObject_MockObject */
  26. private $swatchHelper;
  27. /** @var \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource|\PHPUnit_Framework_MockObject_MockObject */
  28. private $abstractSource;
  29. /** @var \Magento\Swatches\Model\Swatch|\PHPUnit_Framework_MockObject_MockObject */
  30. private $swatch;
  31. /** @var \Magento\Swatches\Model\ResourceModel\Swatch|\PHPUnit_Framework_MockObject_MockObject */
  32. private $resource;
  33. /** @var \Magento\Swatches\Model\ResourceModel\Swatch\Collection|\PHPUnit_Framework_MockObject_MockObject */
  34. private $collection;
  35. /** @var array */
  36. private $optionIds = [];
  37. /** @var array */
  38. private $allOptions = [];
  39. /** @var array */
  40. private $dependencyArray = [];
  41. protected function setUp()
  42. {
  43. $this->attribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
  44. $this->swatchFactory = $this->createPartialMock(\Magento\Swatches\Model\SwatchFactory::class, ['create']);
  45. $this->swatchHelper = $this->createMock(\Magento\Swatches\Helper\Data::class);
  46. $this->swatch = $this->createMock(\Magento\Swatches\Model\Swatch::class);
  47. $this->resource = $this->createMock(\Magento\Swatches\Model\ResourceModel\Swatch::class);
  48. $this->collection =
  49. $this->createMock(\Magento\Swatches\Model\ResourceModel\Swatch\Collection::class);
  50. $this->collectionFactory = $this->createPartialMock(
  51. \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory::class,
  52. ['create']
  53. );
  54. $this->abstractSource = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class);
  55. $serializer = $this->createPartialMock(
  56. \Magento\Framework\Serialize\Serializer\Json::class,
  57. ['serialize', 'unserialize']
  58. );
  59. $serializer->expects($this->any())
  60. ->method('serialize')->willReturnCallback(function ($parameter) {
  61. return json_encode($parameter);
  62. });
  63. $serializer->expects($this->any())
  64. ->method('unserialize')->willReturnCallback(function ($parameter) {
  65. return json_decode($parameter, true);
  66. });
  67. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  68. $this->eavAttribute = $objectManager->getObject(
  69. \Magento\Swatches\Model\Plugin\EavAttribute::class,
  70. [
  71. 'collectionFactory' => $this->collectionFactory,
  72. 'swatchFactory' => $this->swatchFactory,
  73. 'swatchHelper' => $this->swatchHelper,
  74. 'serializer' => $serializer,
  75. ]
  76. );
  77. $this->optionIds = [
  78. 'value' => ['option 89' => 'test 1', 'option 114' => 'test 2', 'option 170' => 'test 3'],
  79. 'delete' => ['option 89' => 0, 'option 114' => 1, 'option 170' => 0],
  80. ];
  81. $this->allOptions = [null, ['value' => 'option 12'], ['value' => 'option 154']];
  82. $this->dependencyArray = ['option 89', 'option 170'];
  83. }
  84. public function testBeforeSaveVisualSwatch()
  85. {
  86. $option = [
  87. 'value' => [
  88. 0 => 'option value',
  89. ]
  90. ];
  91. $this->attribute->expects($this->exactly(6))->method('getData')->withConsecutive(
  92. ['defaultvisual'],
  93. ['optionvisual'],
  94. ['swatchvisual'],
  95. ['optionvisual'],
  96. ['option/delete/0']
  97. )->will($this->onConsecutiveCalls(
  98. self::ATTRIBUTE_DEFAULT_VALUE,
  99. self::ATTRIBUTE_OPTION_VALUE,
  100. self::ATTRIBUTE_SWATCH_VALUE,
  101. $option,
  102. false
  103. ));
  104. $this->attribute->expects($this->exactly(3))->method('setData')
  105. ->withConsecutive(
  106. ['option', self::ATTRIBUTE_OPTION_VALUE],
  107. ['default', self::ATTRIBUTE_DEFAULT_VALUE],
  108. ['swatch', self::ATTRIBUTE_SWATCH_VALUE]
  109. );
  110. $this->swatchHelper->expects($this->once())->method('assembleAdditionalDataEavAttribute')
  111. ->with($this->attribute);
  112. $this->swatchHelper->expects($this->atLeastOnce())->method('isVisualSwatch')
  113. ->with($this->attribute)
  114. ->willReturn(true);
  115. $this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
  116. ->with($this->attribute)
  117. ->willReturn(true);
  118. $this->swatchHelper->expects($this->never())->method('isTextSwatch');
  119. $this->eavAttribute->beforeBeforeSave($this->attribute);
  120. }
  121. public function testBeforeSaveTextSwatch()
  122. {
  123. $option = [
  124. 'value' => [
  125. 0 => 'option value',
  126. ]
  127. ];
  128. $this->attribute->expects($this->exactly(6))->method('getData')->withConsecutive(
  129. ['defaulttext'],
  130. ['optiontext'],
  131. ['swatchtext'],
  132. ['optiontext'],
  133. ['option/delete/0']
  134. )->will(
  135. $this->onConsecutiveCalls(
  136. self::ATTRIBUTE_DEFAULT_VALUE,
  137. self::ATTRIBUTE_OPTION_VALUE,
  138. self::ATTRIBUTE_SWATCH_VALUE,
  139. $option,
  140. false
  141. )
  142. );
  143. $this->attribute->expects($this->exactly(3))->method('setData')
  144. ->withConsecutive(
  145. ['option', self::ATTRIBUTE_OPTION_VALUE],
  146. ['default', self::ATTRIBUTE_DEFAULT_VALUE],
  147. ['swatch', self::ATTRIBUTE_SWATCH_VALUE]
  148. );
  149. $this->swatchHelper->expects($this->once())->method('assembleAdditionalDataEavAttribute')
  150. ->with($this->attribute);
  151. $this->swatchHelper->expects($this->atLeastOnce())->method('isVisualSwatch')
  152. ->with($this->attribute)
  153. ->willReturn(false);
  154. $this->swatchHelper->expects($this->atLeastOnce())->method('isTextSwatch')
  155. ->with($this->attribute)
  156. ->willReturn(true);
  157. $this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
  158. ->with($this->attribute)
  159. ->willReturn(true);
  160. $this->eavAttribute->beforeBeforeSave($this->attribute);
  161. }
  162. /**
  163. * @expectedException \Magento\Framework\Exception\InputException
  164. * @expectedExceptionMessage Admin is a required field in each row
  165. */
  166. public function testBeforeSaveWithFailedValidation()
  167. {
  168. $optionText = [
  169. 'value' => [
  170. 0 => '',
  171. ]
  172. ];
  173. $this->swatchHelper->expects($this->once())
  174. ->method('isSwatchAttribute')
  175. ->with($this->attribute)
  176. ->willReturn(true);
  177. $this->swatchHelper->expects($this->atLeastOnce())
  178. ->method('isVisualSwatch')
  179. ->willReturn(true);
  180. $this->attribute->expects($this->exactly(5))
  181. ->method('getData')
  182. ->withConsecutive(
  183. ['defaultvisual'],
  184. ['optionvisual'],
  185. ['swatchvisual'],
  186. ['optionvisual'],
  187. ['option/delete/0']
  188. )
  189. ->will(
  190. $this->onConsecutiveCalls(
  191. self::ATTRIBUTE_DEFAULT_VALUE,
  192. self::ATTRIBUTE_OPTION_VALUE,
  193. self::ATTRIBUTE_SWATCH_VALUE,
  194. $optionText,
  195. false
  196. )
  197. );
  198. $this->eavAttribute->beforeBeforeSave($this->attribute);
  199. }
  200. /**
  201. * @covers \Magento\Swatches\Model\Plugin\EavAttribute::beforeBeforeSave()
  202. */
  203. public function testBeforeSaveWithDeletedOption()
  204. {
  205. $optionText = [
  206. 'value' => [
  207. 0 => '',
  208. ]
  209. ];
  210. $this->swatchHelper->expects($this->once())
  211. ->method('isSwatchAttribute')
  212. ->with($this->attribute)
  213. ->willReturn(true);
  214. $this->swatchHelper->expects($this->atLeastOnce())
  215. ->method('isVisualSwatch')
  216. ->willReturn(true);
  217. $this->attribute->expects($this->exactly(6))
  218. ->method('getData')
  219. ->withConsecutive(
  220. ['defaultvisual'],
  221. ['optionvisual'],
  222. ['swatchvisual'],
  223. ['optionvisual'],
  224. ['option/delete/0'],
  225. ['swatch_input_type']
  226. )
  227. ->will(
  228. $this->onConsecutiveCalls(
  229. self::ATTRIBUTE_DEFAULT_VALUE,
  230. self::ATTRIBUTE_OPTION_VALUE,
  231. self::ATTRIBUTE_SWATCH_VALUE,
  232. $optionText,
  233. true,
  234. false
  235. )
  236. );
  237. $this->eavAttribute->beforeBeforeSave($this->attribute);
  238. }
  239. public function testBeforeSaveNotSwatch()
  240. {
  241. $additionalData = [
  242. 'swatch_input_type' => 'visual',
  243. 'update_product_preview_image' => 1,
  244. 'use_product_image_for_swatch' => 0
  245. ];
  246. $shortAdditionalData = [
  247. 'update_product_preview_image' => 1,
  248. 'use_product_image_for_swatch' => 0
  249. ];
  250. $this->attribute->expects($this->exactly(2))->method('getData')->withConsecutive(
  251. [Swatch::SWATCH_INPUT_TYPE_KEY],
  252. ['additional_data']
  253. )->willReturnOnConsecutiveCalls(
  254. Swatch::SWATCH_INPUT_TYPE_DROPDOWN,
  255. json_encode($additionalData)
  256. );
  257. $this->attribute
  258. ->expects($this->once())
  259. ->method('setData')
  260. ->with('additional_data', json_encode($shortAdditionalData))
  261. ->will($this->returnSelf());
  262. $this->swatchHelper->expects($this->never())->method('assembleAdditionalDataEavAttribute');
  263. $this->swatchHelper->expects($this->never())->method('isVisualSwatch');
  264. $this->swatchHelper->expects($this->never())->method('isTextSwatch');
  265. $this->swatchHelper->expects($this->once())->method('isSwatchAttribute')
  266. ->with($this->attribute)
  267. ->willReturn(false);
  268. $this->eavAttribute->beforeBeforeSave($this->attribute);
  269. }
  270. /**
  271. * @return array
  272. */
  273. public function visualSwatchProvider()
  274. {
  275. return [
  276. [Swatch::SWATCH_TYPE_EMPTY, null],
  277. [Swatch::SWATCH_TYPE_VISUAL_COLOR, '#hex'],
  278. [Swatch::SWATCH_TYPE_VISUAL_IMAGE, '/path'],
  279. ];
  280. }
  281. /**
  282. * @dataProvider visualSwatchProvider
  283. *
  284. * @param $swatchType
  285. * @param $swatchValue
  286. */
  287. public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue)
  288. {
  289. $this->abstractSource->expects($this->once())->method('getAllOptions')
  290. ->willReturn($this->allOptions);
  291. $this->resource->expects($this->once())->method('saveDefaultSwatchOption')
  292. ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
  293. $this->swatch->expects($this->once())->method('getResource')
  294. ->willReturn($this->resource);
  295. $this->swatch->expects($this->once())->method('getId')
  296. ->willReturn(EavAttribute::DEFAULT_STORE_ID);
  297. $this->swatch->expects($this->once())->method('save');
  298. $this->swatch->expects($this->exactly(4))->method('setData')
  299. ->withConsecutive(
  300. ['option_id', self::OPTION_ID],
  301. ['store_id', EavAttribute::DEFAULT_STORE_ID],
  302. ['type', $swatchType],
  303. ['value', $swatchValue]
  304. );
  305. $this->collection->expects($this->exactly(2))->method('addFieldToFilter')
  306. ->withConsecutive(
  307. ['option_id', self::OPTION_ID],
  308. ['store_id', EavAttribute::DEFAULT_STORE_ID]
  309. )->willReturnSelf();
  310. $this->collection->expects($this->once())->method('getFirstItem')
  311. ->willReturn($this->swatch);
  312. $this->collectionFactory->expects($this->once())->method('create')
  313. ->willReturn($this->collection);
  314. $this->attribute->expects($this->at(0))->method('getData')
  315. ->willReturn($this->optionIds);
  316. $this->attribute->expects($this->at(1))->method('getSource')
  317. ->willReturn($this->abstractSource);
  318. $this->attribute->expects($this->at(2))->method('getData')
  319. ->with('default/0')
  320. ->willReturn($this->dependencyArray[0]);
  321. $this->attribute->expects($this->at(3))->method('getId')
  322. ->willReturn(self::ATTRIBUTE_ID);
  323. $this->attribute->expects($this->at(4))->method('getData')
  324. ->with('swatch/value')
  325. ->willReturn([self::STORE_ID => $swatchValue]);
  326. $this->attribute->expects($this->at(5))->method('getData')
  327. ->with('option/delete/' . self::OPTION_ID)
  328. ->willReturn(false);
  329. $this->swatchFactory->expects($this->exactly(1))->method('create')
  330. ->willReturn($this->swatch);
  331. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  332. ->with($this->attribute)
  333. ->willReturn(true);
  334. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  335. ->with($this->attribute)
  336. ->willReturn(true);
  337. $this->swatchHelper->expects($this->never())->method('isTextSwatch');
  338. $this->eavAttribute->afterAfterSave($this->attribute);
  339. }
  340. public function testDefaultTextualSwatchAfterSave()
  341. {
  342. $this->abstractSource->expects($this->once())->method('getAllOptions')
  343. ->willReturn($this->allOptions);
  344. $this->swatch->expects($this->any())->method('getId')
  345. ->willReturn(EavAttribute::DEFAULT_STORE_ID);
  346. $this->swatch->expects($this->any())->method('save');
  347. $this->swatch->expects($this->any())->method('isDeleted')
  348. ->with(false);
  349. $this->collection->expects($this->any())->method('addFieldToFilter')
  350. ->willReturnSelf();
  351. $this->collection->expects($this->any())->method('getFirstItem')
  352. ->willReturn($this->swatch);
  353. $this->collectionFactory->expects($this->any())->method('create')
  354. ->willReturn($this->collection);
  355. $this->attribute->expects($this->at(0))->method('getData')
  356. ->willReturn($this->optionIds);
  357. $this->attribute->expects($this->at(1))->method('getSource')
  358. ->willReturn($this->abstractSource);
  359. $this->attribute->expects($this->at(2))->method('getData')
  360. ->with('default/0')
  361. ->willReturn(null);
  362. $this->attribute->expects($this->at(3))->method('getData')
  363. ->with('swatch/value')
  364. ->willReturn(
  365. [
  366. self::STORE_ID => [
  367. 1 => "test",
  368. 2 => false,
  369. 3 => null,
  370. 4 => "",
  371. ]
  372. ]
  373. );
  374. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  375. ->with($this->attribute)
  376. ->willReturn(true);
  377. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  378. ->with($this->attribute)
  379. ->willReturn(false);
  380. $this->swatchHelper->expects($this->once())->method('isTextSwatch')
  381. ->with($this->attribute)
  382. ->willReturn(true);
  383. $this->swatch->expects($this->any())->method('setData')
  384. ->withConsecutive(
  385. ['option_id', self::OPTION_ID],
  386. ['store_id', 1],
  387. ['type', Swatch::SWATCH_TYPE_TEXTUAL],
  388. ['value', "test"]
  389. );
  390. $this->eavAttribute->afterAfterSave($this->attribute);
  391. }
  392. public function testAfterAfterSaveTextualSwatch()
  393. {
  394. $this->abstractSource->expects($this->once())->method('getAllOptions')
  395. ->willReturn($this->allOptions);
  396. $this->resource->expects($this->once())->method('saveDefaultSwatchOption')
  397. ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
  398. $this->swatch->expects($this->once())->method('getResource')
  399. ->willReturn($this->resource);
  400. $this->swatch->expects($this->once())->method('getId')
  401. ->willReturn(EavAttribute::DEFAULT_STORE_ID);
  402. $this->swatch->expects($this->once())->method('save');
  403. $this->swatch->expects($this->once())->method('isDeleted')
  404. ->with(false);
  405. $this->swatch->expects($this->exactly(4))->method('setData')
  406. ->withConsecutive(
  407. ['option_id', self::OPTION_ID],
  408. ['store_id', self::OPTION_ID],
  409. ['type', Swatch::SWATCH_TYPE_TEXTUAL],
  410. ['value', null]
  411. );
  412. $this->collection->expects($this->exactly(2))->method('addFieldToFilter')
  413. ->withConsecutive(
  414. ['option_id', self::OPTION_ID],
  415. ['store_id', self::OPTION_ID]
  416. )->willReturnSelf();
  417. $this->collection->expects($this->once())->method('getFirstItem')
  418. ->willReturn($this->swatch);
  419. $this->collectionFactory->expects($this->once())->method('create')
  420. ->willReturn($this->collection);
  421. $this->attribute->expects($this->at(0))->method('getData')
  422. ->willReturn($this->optionIds);
  423. $this->attribute->expects($this->at(1))->method('getSource')
  424. ->willReturn($this->abstractSource);
  425. $this->attribute->expects($this->at(2))->method('getData')
  426. ->with('default/0')
  427. ->willReturn($this->dependencyArray[0]);
  428. $this->attribute->expects($this->at(3))->method('getId')
  429. ->willReturn(self::ATTRIBUTE_ID);
  430. $this->attribute->expects($this->at(4))->method('getData')
  431. ->with('swatch/value')
  432. ->willReturn([self::STORE_ID => [self::OPTION_ID => null]]);
  433. $this->attribute->expects($this->at(5))->method('getData')
  434. ->with('option/delete/' . self::OPTION_ID)
  435. ->willReturn(false);
  436. $this->swatchFactory->expects($this->exactly(1))->method('create')
  437. ->willReturn($this->swatch);
  438. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  439. ->with($this->attribute)
  440. ->willReturn(true);
  441. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  442. ->with($this->attribute)
  443. ->willReturn(false);
  444. $this->swatchHelper->expects($this->once())->method('isTextSwatch')
  445. ->with($this->attribute)
  446. ->willReturn(true);
  447. $this->eavAttribute->afterAfterSave($this->attribute);
  448. }
  449. public function testAfterAfterSaveVisualSwatchIsDelete()
  450. {
  451. $this->abstractSource->expects($this->once())->method('getAllOptions')
  452. ->willReturn($this->allOptions);
  453. $this->resource->expects($this->once())->method('saveDefaultSwatchOption')
  454. ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
  455. $this->swatch->expects($this->once())->method('getResource')
  456. ->willReturn($this->resource);
  457. $this->attribute->expects($this->at(0))->method('getData')
  458. ->willReturn($this->optionIds);
  459. $this->attribute->expects($this->at(1))->method('getSource')
  460. ->willReturn($this->abstractSource);
  461. $this->attribute->expects($this->at(2))->method('getData')
  462. ->with('default/0')
  463. ->willReturn($this->dependencyArray[0]);
  464. $this->attribute->expects($this->at(3))->method('getId')
  465. ->willReturn(self::ATTRIBUTE_ID);
  466. $this->attribute->expects($this->at(4))->method('getData')
  467. ->with('swatch/value')
  468. ->willReturn([self::STORE_ID => null]);
  469. $this->attribute->expects($this->at(5))->method('getData')
  470. ->with('option/delete/' . self::OPTION_ID)
  471. ->willReturn(true);
  472. $this->swatchFactory->expects($this->once())->method('create')
  473. ->willReturn($this->swatch);
  474. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  475. ->with($this->attribute)
  476. ->willReturn(true);
  477. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  478. ->with($this->attribute)
  479. ->willReturn(true);
  480. $this->swatchHelper->expects($this->never())->method('isTextSwatch');
  481. $this->eavAttribute->afterAfterSave($this->attribute);
  482. }
  483. public function testAfterAfterSaveTextualSwatchIsDelete()
  484. {
  485. $this->abstractSource->expects($this->once())->method('getAllOptions')
  486. ->willReturn($this->allOptions);
  487. $this->resource->expects($this->once())->method('saveDefaultSwatchOption')
  488. ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
  489. $this->swatch->expects($this->once())->method('getResource')
  490. ->willReturn($this->resource);
  491. $this->attribute->expects($this->at(0))->method('getData')
  492. ->willReturn($this->optionIds);
  493. $this->attribute->expects($this->at(1))->method('getSource')
  494. ->willReturn($this->abstractSource);
  495. $this->attribute->expects($this->at(2))->method('getData')
  496. ->with('default/0')
  497. ->willReturn($this->dependencyArray[0]);
  498. $this->attribute->expects($this->at(3))->method('getId')
  499. ->willReturn(self::ATTRIBUTE_ID);
  500. $this->attribute->expects($this->at(4))->method('getData')
  501. ->with('swatch/value')
  502. ->willReturn([self::STORE_ID => [self::OPTION_ID => null]]);
  503. $this->attribute->expects($this->at(5))->method('getData')
  504. ->with('option/delete/' . self::OPTION_ID)
  505. ->willReturn(true);
  506. $this->swatchFactory->expects($this->once())->method('create')
  507. ->willReturn($this->swatch);
  508. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  509. ->with($this->attribute)
  510. ->willReturn(true);
  511. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  512. ->with($this->attribute)
  513. ->willReturn(false);
  514. $this->swatchHelper->expects($this->once())->method('isTextSwatch')
  515. ->with($this->attribute)
  516. ->willReturn(true);
  517. $this->eavAttribute->afterAfterSave($this->attribute);
  518. }
  519. public function testAfterAfterSaveIsSwatchExists()
  520. {
  521. $this->abstractSource->expects($this->once())->method('getAllOptions')
  522. ->willReturn($this->allOptions);
  523. $this->resource->expects($this->once())->method('saveDefaultSwatchOption')
  524. ->with(self::ATTRIBUTE_ID, self::OPTION_ID);
  525. $this->swatch->expects($this->once())->method('getResource')
  526. ->willReturn($this->resource);
  527. $this->swatch->expects($this->once())->method('getId')
  528. ->willReturn(1);
  529. $this->swatch->expects($this->once())->method('save');
  530. $this->swatch->expects($this->once())->method('isDeleted')
  531. ->with(false);
  532. $this->swatch->expects($this->exactly(2))->method('setData')
  533. ->withConsecutive(
  534. ['type', Swatch::SWATCH_TYPE_TEXTUAL],
  535. ['value', null]
  536. );
  537. $this->collection->expects($this->exactly(2))->method('addFieldToFilter')
  538. ->withConsecutive(
  539. ['option_id', self::OPTION_ID],
  540. ['store_id', self::OPTION_ID]
  541. )->willReturnSelf();
  542. $this->collection->expects($this->once())->method('getFirstItem')
  543. ->willReturn($this->swatch);
  544. $this->collectionFactory->expects($this->once())->method('create')
  545. ->willReturn($this->collection);
  546. $this->attribute->expects($this->at(0))->method('getData')
  547. ->willReturn($this->optionIds);
  548. $this->attribute->expects($this->at(1))->method('getSource')
  549. ->willReturn($this->abstractSource);
  550. $this->attribute->expects($this->at(2))->method('getData')
  551. ->with('default/0')
  552. ->willReturn($this->dependencyArray[0]);
  553. $this->attribute->expects($this->at(3))->method('getId')
  554. ->willReturn(self::ATTRIBUTE_ID);
  555. $this->attribute->expects($this->at(4))->method('getData')
  556. ->with('swatch/value')
  557. ->willReturn([self::STORE_ID => [self::OPTION_ID => null]]);
  558. $this->attribute->expects($this->at(5))->method('getData')
  559. ->with('option/delete/' . self::OPTION_ID)
  560. ->willReturn(false);
  561. $this->swatchFactory->expects($this->exactly(1))->method('create')
  562. ->willReturn($this->swatch);
  563. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  564. ->with($this->attribute)
  565. ->willReturn(true);
  566. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  567. ->with($this->attribute)
  568. ->willReturn(false);
  569. $this->swatchHelper->expects($this->once())->method('isTextSwatch')
  570. ->with($this->attribute)
  571. ->willReturn(true);
  572. $this->eavAttribute->afterAfterSave($this->attribute);
  573. }
  574. public function testAfterAfterSaveNotSwatchAttribute()
  575. {
  576. $this->abstractSource->expects($this->once())->method('getAllOptions')
  577. ->willReturn($this->allOptions);
  578. $this->swatch->expects($this->once())->method('getId')
  579. ->willReturn(1);
  580. $this->swatch->expects($this->once())->method('save');
  581. $this->swatch->expects($this->once())->method('isDeleted')
  582. ->with(false);
  583. $this->swatch->expects($this->exactly(2))->method('setData')
  584. ->withConsecutive(
  585. ['type', Swatch::SWATCH_TYPE_TEXTUAL],
  586. ['value', null]
  587. );
  588. $this->collection->expects($this->exactly(2))->method('addFieldToFilter')
  589. ->withConsecutive(
  590. ['option_id', self::OPTION_ID],
  591. ['store_id', self::OPTION_ID]
  592. )->willReturnSelf();
  593. $this->collection->expects($this->once())->method('getFirstItem')
  594. ->willReturn($this->swatch);
  595. $this->collectionFactory->expects($this->once())->method('create')
  596. ->willReturn($this->collection);
  597. $this->attribute->expects($this->at(0))->method('getData')
  598. ->with('option')
  599. ->willReturn($this->optionIds);
  600. $this->attribute->expects($this->at(1))->method('getSource')
  601. ->willReturn($this->abstractSource);
  602. $this->attribute->expects($this->at(2))->method('getData')
  603. ->with('swatch/value')
  604. ->willReturn([self::STORE_ID => [self::OPTION_ID => null]]);
  605. $this->attribute->expects($this->at(3))->method('getData')
  606. ->with('option/delete/' . self::OPTION_ID)
  607. ->willReturn(false);
  608. $this->swatchHelper->expects($this->exactly(2))->method('isSwatchAttribute')
  609. ->with($this->attribute)
  610. ->will($this->onConsecutiveCalls(true, false));
  611. $this->swatchHelper->expects($this->once())->method('isVisualSwatch')
  612. ->with($this->attribute)
  613. ->willReturn(false);
  614. $this->swatchHelper->expects($this->once())->method('isTextSwatch')
  615. ->with($this->attribute)
  616. ->willReturn(true);
  617. $this->eavAttribute->afterAfterSave($this->attribute);
  618. }
  619. }