3838use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
3939use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
4040use Magento \Framework \Validator \UniversalFactory ;
41+ use Magento \Store \Model \Store ;
4142use Magento \Store \Model \StoreManagerInterface ;
4243use PHPUnit \Framework \MockObject \MockObject ;
4344use PHPUnit \Framework \TestCase ;
@@ -93,6 +94,11 @@ class CollectionTest extends TestCase
9394 */
9495 private $ storeManager ;
9596
97+ /**
98+ * @var ProductLimitation|MockObject
99+ */
100+ private $ productLimitationMock ;
101+
96102 /**
97103 * @var EntityFactory|MockObject
98104 */
@@ -192,7 +198,7 @@ protected function setUp(): void
192198 $ this ->entityMock ->expects ($ this ->any ())->method ('getTable ' )->willReturnArgument (0 );
193199 $ this ->connectionMock ->expects ($ this ->atLeastOnce ())->method ('select ' )->willReturn ($ this ->selectMock );
194200
195- $ productLimitationMock = $ this ->createMock (
201+ $ this -> productLimitationMock = $ this ->createMock (
196202 ProductLimitation::class
197203 );
198204 $ productLimitationFactoryMock = $ this ->getMockBuilder (
@@ -201,7 +207,7 @@ protected function setUp(): void
201207 ->setMethods (['create ' ])->getMock ();
202208
203209 $ productLimitationFactoryMock ->method ('create ' )
204- ->willReturn ($ productLimitationMock );
210+ ->willReturn ($ this -> productLimitationMock );
205211 $ this ->collection = $ this ->objectManager ->getObject (
206212 Collection::class,
207213 [
@@ -432,4 +438,44 @@ public function testGetNewEmptyItem()
432438 $ secondItem = $ this ->collection ->getNewEmptyItem ();
433439 $ this ->assertEquals ($ firstItem , $ secondItem );
434440 }
441+
442+ /**
443+ * Test to add website filter in admin area
444+ */
445+ public function testAddWebsiteFilterOnAdminStore (): void
446+ {
447+ $ websiteIds = [2 ];
448+ $ websiteTable = 'catalog_product_website ' ;
449+ $ joinCondition = 'join condition ' ;
450+ $ this ->productLimitationMock ->expects ($ this ->atLeastOnce ())
451+ ->method ('offsetSet ' )
452+ ->with ('website_ids ' , $ websiteIds );
453+ $ this ->productLimitationMock ->method ('offsetExists ' )
454+ ->with ('website_ids ' )
455+ ->willReturn (true );
456+ $ this ->productLimitationMock ->method ('offsetGet ' )
457+ ->with ('website_ids ' )
458+ ->willReturn ($ websiteIds );
459+ $ this ->connectionMock ->expects ($ this ->once ())
460+ ->method ('quoteInto ' )
461+ ->with ('product_website.website_id IN(?) ' , $ websiteIds , 'int ' )
462+ ->willReturn ($ joinCondition );
463+ $ this ->selectMock ->method ('getPart ' )->with (Select::FROM )->willReturn ([]);
464+ /** @var AbstractEntity|MockObject $eavEntity */
465+ $ eavEntity = $ this ->createMock (AbstractEntity::class);
466+ $ eavEntity ->method ('getTable ' )
467+ ->with ('catalog_product_website ' )
468+ ->willReturn ($ websiteTable );
469+ $ this ->selectMock ->expects ($ this ->once ())
470+ ->method ('join ' )
471+ ->with (
472+ ['product_website ' => $ websiteTable ],
473+ 'product_website.product_id = e.entity_id AND ' . $ joinCondition ,
474+ []
475+ );
476+
477+ $ this ->collection ->setEntity ($ eavEntity );
478+ $ this ->collection ->setStoreId (Store::DEFAULT_STORE_ID );
479+ $ this ->collection ->addWebsiteFilter ($ websiteIds );
480+ }
435481}
0 commit comments