33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Catalog \Model \Product \Attribute \Backend ;
78
89/**
910 * Test class for \Magento\Catalog\Model\Product\Attribute\Backend\Sku.
11+ *
1012 * @magentoAppArea adminhtml
1113 */
1214class SkuTest extends \PHPUnit \Framework \TestCase
1315{
1416 /**
1517 * @magentoDataFixture Magento/Catalog/_files/product_simple.php
1618 */
17- public function testGenerateUniqueSkuExistingProduct ()
19+ public function testGenerateUniqueSkuDuplicatedProduct ()
20+ {
21+ $ repository = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
22+ \Magento \Catalog \Model \ProductRepository::class
23+ );
24+ $ product = $ repository ->get ('simple ' );
25+ /** @var \Magento\Catalog\Model\Product\Copier $copier */
26+ $ copier = $ this ->getCopier ();
27+ /** @var \Magento\Catalog\Model\Product; $product2 */
28+ $ product2 = $ copier ->copy ($ product );
29+
30+ $ this ->assertEquals ('simple ' , $ product ->getSku ());
31+ $ product2 ->getResource ()->getAttribute ('sku ' )->getBackend ()->beforeSave ($ product2 );
32+ $ this ->assertEquals ('simple-1 ' , $ product2 ->getSku ());
33+ }
34+
35+ /**
36+ * Checks if generation of unique sku is not allowed
37+ *
38+ * @magentoDataFixture Magento/Catalog/_files/product_simple.php
39+ */
40+ public function testPreventGenerateUniqueSkuExistingProduct ()
1841 {
1942 $ repository = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
2043 \Magento \Catalog \Model \ProductRepository::class
2144 );
2245 $ product = $ repository ->get ('simple ' );
2346 $ product ->setId (null );
2447 $ this ->assertEquals ('simple ' , $ product ->getSku ());
48+
49+ $ this ->expectException ('Magento\Framework\Exception\AlreadyExistsException ' );
2550 $ product ->getResource ()->getAttribute ('sku ' )->getBackend ()->beforeSave ($ product );
26- $ this ->assertEquals ('simple-1 ' , $ product ->getSku ());
51+
52+ $ this ->fail ('Unique sku generation should be allowed only for product duplication. ' );
2753 }
2854
2955 /**
3056 * @param $product \Magento\Catalog\Model\Product
57+ *
3158 * @dataProvider uniqueSkuDataProvider
3259 */
3360 public function testGenerateUniqueSkuNotExistingProduct ($ product )
@@ -42,22 +69,54 @@ public function testGenerateUniqueSkuNotExistingProduct($product)
4269 * @magentoAppArea adminhtml
4370 * @magentoDbIsolation enabled
4471 */
45- public function testGenerateUniqueLongSku ()
72+ public function testGenerateUniqueLongSkuDuplicatedProduct ()
4673 {
4774 $ repository = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
4875 \Magento \Catalog \Model \ProductRepository::class
4976 );
5077 $ product = $ repository ->get ('simple ' );
51- $ product ->setSku ('0123456789012345678901234567890123456789012345678901234567890123 ' );
78+ $ product ->setSku ('0123456789012345678901234567890123456789012345678901234567890124 ' );
79+ $ product ->save ();
80+ $ product ->getResource ()->getAttribute ('sku ' )->getBackend ()->beforeSave ($ product );
81+ $ this ->assertEquals ('0123456789012345678901234567890123456789012345678901234567890124 ' , $ product ->getSku ());
5282
5383 /** @var \Magento\Catalog\Model\Product\Copier $copier */
54- $ copier = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
55- \Magento \Catalog \Model \Product \Copier::class
84+ $ copier = $ this ->getCopier ();
85+ $ product2 = $ copier ->copy ($ product );
86+
87+ $ this ->assertEquals ('01234567890123456789012345678901234567890123456789012345678901-1 ' , $ product2 ->getSku ());
88+ $ product2 ->getResource ()->getAttribute ('sku ' )->getBackend ()->beforeSave ($ product2 );
89+ $ this ->assertEquals ('01234567890123456789012345678901234567890123456789012345678901-1 ' , $ product2 ->getSku ());
90+
91+ $ product2 ->setId (null );
92+ $ product2 ->getResource ()->getAttribute ('sku ' )->getBackend ()->beforeSave ($ product2 );
93+ $ this ->assertEquals ('01234567890123456789012345678901234567890123456789012345678901-2 ' , $ product2 ->getSku ());
94+ }
95+
96+ /**
97+ * Checks if generation of long unique sku is not allowed
98+ *
99+ * @magentoDataFixture Magento/Catalog/_files/product_simple.php
100+ * @magentoAppArea adminhtml
101+ * @magentoDbIsolation enabled
102+ */
103+ public function testPreventGenerateUniqueLongSku ()
104+ {
105+ $ repository = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (
106+ \Magento \Catalog \Model \ProductRepository::class
56107 );
108+ $ product = $ repository ->get ('simple ' );
109+ $ product ->setSku ('0123456789012345678901234567890123456789012345678901234567890123 ' );
110+
111+ /** @var \Magento\Catalog\Model\Product\Copier $copier */
112+ $ copier = $ this ->getCopier ();
113+
57114 $ copier ->copy ($ product );
58115 $ this ->assertEquals ('0123456789012345678901234567890123456789012345678901234567890123 ' , $ product ->getSku ());
116+ $ this ->expectException ('Magento\Framework\Exception\AlreadyExistsException ' );
59117 $ product ->getResource ()->getAttribute ('sku ' )->getBackend ()->beforeSave ($ product );
60- $ this ->assertEquals ('01234567890123456789012345678901234567890123456789012345678901-1 ' , $ product ->getSku ());
118+
119+ $ this ->fail ('Unique long sku generation should be allowed only for product duplication. ' );
61120 }
62121
63122 /**
@@ -68,6 +127,7 @@ public function testGenerateUniqueLongSku()
68127 public function uniqueSkuDataProvider ()
69128 {
70129 $ product = $ this ->_getProduct ();
130+
71131 return [[$ product ]];
72132 }
73133
@@ -107,6 +167,18 @@ protected function _getProduct()
107167 )->setStockData (
108168 ['use_config_manage_stock ' => 1 , 'qty ' => 100 , 'is_qty_decimal ' => 0 , 'is_in_stock ' => 1 ]
109169 );
170+
110171 return $ product ;
111172 }
173+
174+ /**
175+ * @return \Magento\Catalog\Model\Product\Copier
176+ */
177+ protected function getCopier ()
178+ {
179+ return \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
180+ \Magento \Catalog \Model \Product \Copier::class
181+ );
182+ }
183+
112184}
0 commit comments