@@ -76,16 +76,24 @@ class WebsitesTest extends AbstractModifierTest
7676 */
7777 protected $ storeViewMock ;
7878
79+ /**
80+ * @var array
81+ */
82+ private $ websitesList ;
83+
84+ /**
85+ * @var int
86+ */
87+ private $ productId ;
88+
7989 /**
8090 * @inheritdoc
8191 */
8292 protected function setUp (): void
8393 {
8494 parent ::setUp ();
85- $ this ->productMock ->expects ($ this ->any ())
86- ->method ('getId ' )
87- ->willReturn (self ::PRODUCT_ID );
8895 $ this ->assignedWebsites = [self ::SECOND_WEBSITE_ID ];
96+ $ this ->productId = self ::PRODUCT_ID ;
8997 $ this ->websiteMock = $ this ->getMockBuilder (Website::class)
9098 ->setMethods (['getId ' , 'getName ' ])
9199 ->disableOriginalConstructor ()
@@ -94,6 +102,7 @@ protected function setUp(): void
94102 ->setMethods (['getId ' , 'getName ' ])
95103 ->disableOriginalConstructor ()
96104 ->getMock ();
105+ $ this ->websitesList = [$ this ->websiteMock , $ this ->secondWebsiteMock ];
97106 $ this ->websiteRepositoryMock = $ this ->getMockBuilder (WebsiteRepositoryInterface::class)
98107 ->setMethods (['getList ' ])
99108 ->getMockForAbstractClass ();
@@ -106,17 +115,9 @@ protected function setUp(): void
106115 $ this ->storeRepositoryMock = $ this ->getMockBuilder (StoreRepositoryInterface::class)
107116 ->setMethods (['getList ' ])
108117 ->getMockForAbstractClass ();
109- $ this ->productMock = $ this ->getMockBuilder (ProductInterface::class)
110- ->setMethods (['getId ' ])
111- ->getMockForAbstractClass ();
112- $ this ->locatorMock ->expects ($ this ->any ())
113- ->method ('getWebsiteIds ' )
114- ->willReturn ($ this ->assignedWebsites );
115118 $ this ->storeManagerMock = $ this ->getMockBuilder (StoreManagerInterface::class)
116119 ->setMethods (['isSingleStoreMode ' , 'getWebsites ' ])
117120 ->getMockForAbstractClass ();
118- $ this ->storeManagerMock ->method ('getWebsites ' )
119- ->willReturn ([$ this ->websiteMock , $ this ->secondWebsiteMock ]);
120121 $ this ->storeManagerMock ->expects ($ this ->any ())
121122 ->method ('isSingleStoreMode ' )
122123 ->willReturn (false );
@@ -174,12 +175,30 @@ protected function createModel()
174175 );
175176 }
176177
178+ /**
179+ * Initialize return values
180+ * @return void
181+ */
182+ private function init ()
183+ {
184+ $ this ->productMock ->expects ($ this ->any ())
185+ ->method ('getId ' )
186+ ->willReturn ($ this ->productId );
187+ $ this ->locatorMock ->expects ($ this ->any ())
188+ ->method ('getWebsiteIds ' )
189+ ->willReturn ($ this ->assignedWebsites );
190+ $ this ->storeManagerMock ->method ('getWebsites ' )
191+ ->willReturn ($ this ->websitesList );
192+ }
193+
177194 /**
178195 * @return void
179196 */
180197 public function testModifyMeta ()
181198 {
199+ $ this ->init ();
182200 $ meta = $ this ->getModel ()->modifyMeta ([]);
201+
183202 $ this ->assertArrayHasKey ('websites ' , $ meta );
184203 $ this ->assertArrayHasKey (self ::SECOND_WEBSITE_ID , $ meta ['websites ' ]['children ' ]);
185204 $ this ->assertArrayHasKey (self ::WEBSITE_ID , $ meta ['websites ' ]['children ' ]);
@@ -190,7 +209,7 @@ public function testModifyMeta()
190209 );
191210 $ this ->assertEquals (
192211 $ meta ['websites ' ]['children ' ][self ::WEBSITE_ID ]['arguments ' ]['data ' ]['config ' ]['value ' ],
193- " 0 "
212+ ' 0 '
194213 );
195214 }
196215
@@ -214,10 +233,44 @@ public function testModifyData()
214233 ]
215234 ],
216235 ];
236+ $ this ->init ();
217237
218238 $ this ->assertEquals (
219239 $ expectedData ,
220240 $ this ->getModel ()->modifyData ([])
221241 );
222242 }
243+
244+ public function testModifyDataNoWebsitesExistingProduct ()
245+ {
246+ $ this ->assignedWebsites = [];
247+ $ this ->websitesList = [$ this ->websiteMock ];
248+ $ this ->init ();
249+
250+ $ meta = $ this ->getModel ()->modifyMeta ([]);
251+
252+ $ this ->assertArrayHasKey (self ::WEBSITE_ID , $ meta ['websites ' ]['children ' ]);
253+ $ this ->assertArrayHasKey ('copy_to_stores. ' . self ::WEBSITE_ID , $ meta ['websites ' ]['children ' ]);
254+ $ this ->assertEquals ('0 ' ,
255+ $ meta ['websites ' ]['children ' ][self ::WEBSITE_ID ]['arguments ' ]['data ' ]['config ' ]['value ' ]
256+ );
257+ }
258+
259+ public function testModifyDataNoWebsitesNewProduct ()
260+ {
261+ $ this ->assignedWebsites = [];
262+ $ this ->websitesList = [$ this ->websiteMock ];
263+ $ this ->productId = false ;
264+ $ this ->init ();
265+ $ this ->productMock ->expects ($ this ->any ())
266+ ->method ('getId ' )
267+ ->willReturn (false );
268+
269+ $ meta = $ this ->getModel ()->modifyMeta ([]);
270+
271+ $ this ->assertArrayHasKey (self ::WEBSITE_ID , $ meta ['websites ' ]['children ' ]);
272+ $ this ->assertEquals ('1 ' ,
273+ $ meta ['websites ' ]['children ' ][self ::WEBSITE_ID ]['arguments ' ]['data ' ]['config ' ]['value ' ]
274+ );
275+ }
223276}
0 commit comments