13
13
use Magento \Catalog \Model \CategoryRepository ;
14
14
use Magento \Catalog \Model \ResourceModel \Category \Collection as CategoryCollection ;
15
15
use Magento \Framework \DataObject ;
16
+ use Magento \Framework \EntityManager \MetadataPool ;
17
+ use Magento \Store \Model \Store ;
16
18
use Magento \Store \Model \StoreManagerInterface ;
17
19
use Magento \TestFramework \ObjectManager ;
18
20
use Magento \TestFramework \TestCase \GraphQl \ResponseContainsErrorsException ;
@@ -33,10 +35,22 @@ class CategoryTest extends GraphQlAbstract
33
35
*/
34
36
private $ categoryRepository ;
35
37
38
+ /**
39
+ * @var Store
40
+ */
41
+ private $ store ;
42
+
43
+ /**
44
+ * @var MetadataPool
45
+ */
46
+ private $ metadataPool ;
47
+
36
48
protected function setUp ()
37
49
{
38
50
$ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
39
51
$ this ->categoryRepository = $ this ->objectManager ->get (CategoryRepository::class);
52
+ $ this ->store = $ this ->objectManager ->get (Store::class);
53
+ $ this ->metadataPool = $ this ->objectManager ->get (MetadataPool::class);
40
54
}
41
55
42
56
/**
@@ -211,7 +225,7 @@ public function testCategoriesTreeWithDisabledCategory()
211
225
productImagePreview: products(pageSize: 1) {
212
226
items {
213
227
id
214
- }
228
+ }
215
229
}
216
230
}
217
231
}
@@ -557,17 +571,49 @@ public function testBreadCrumbs()
557
571
/**
558
572
* Test category image is returned as full url (not relative path)
559
573
*
574
+ * @param string $imagePrefix
560
575
* @magentoApiDataFixture Magento/Catalog/_files/catalog_category_with_image.php
576
+ * @dataProvider categoryImageDataProvider
561
577
*/
562
- public function testCategoryImage ()
578
+ public function testCategoryImage (? string $ imagePrefix )
563
579
{
580
+ /** @var CategoryCollection $categoryCollection */
564
581
$ categoryCollection = $ this ->objectManager ->get (CategoryCollection::class);
565
582
$ categoryModel = $ categoryCollection
566
583
->addAttributeToSelect ('image ' )
567
584
->addAttributeToFilter ('name ' , ['eq ' => 'Parent Image Category ' ])
568
585
->getFirstItem ();
569
586
$ categoryId = $ categoryModel ->getId ();
570
587
588
+ if ($ imagePrefix !== null ) {
589
+ // update image to account for different stored image formats
590
+ $ connection = $ categoryCollection ->getConnection ();
591
+ $ productLinkField = $ this ->metadataPool
592
+ ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
593
+ ->getLinkField ();
594
+
595
+ $ defaultStoreId = $ this ->store ->getId ();
596
+
597
+ $ imageAttributeValue = $ imagePrefix . basename ($ categoryModel ->getImage ());
598
+
599
+ if (!empty ($ imageAttributeValue )) {
600
+ $ query = sprintf (
601
+ 'UPDATE %s SET `value` = "%s" ' .
602
+ 'WHERE `%s` = %d ' .
603
+ 'AND `store_id`= %d ' .
604
+ 'AND `attribute_id` = ' .
605
+ '(SELECT `ea`.`attribute_id` FROM %s ea WHERE `ea`.`attribute_code` = "image" LIMIT 1) ' ,
606
+ $ connection ->getTableName ('catalog_category_entity_varchar ' ),
607
+ $ imageAttributeValue ,
608
+ $ productLinkField ,
609
+ $ categoryModel ->getData ($ productLinkField ),
610
+ $ defaultStoreId ,
611
+ $ connection ->getTableName ('eav_attribute ' )
612
+ );
613
+ $ connection ->query ($ query );
614
+ }
615
+ }
616
+
571
617
$ query = <<<QUERY
572
618
{
573
619
categoryList(filters: {ids: {in: [" $ categoryId"]}}) {
@@ -591,7 +637,7 @@ public function testCategoryImage()
591
637
$ this ->assertNotEmpty ($ response ['categoryList ' ]);
592
638
$ categoryList = $ response ['categoryList ' ];
593
639
$ storeBaseUrl = $ this ->objectManager ->get (StoreManagerInterface::class)->getStore ()->getBaseUrl ('media ' );
594
- $ expectedImageUrl = rtrim ($ storeBaseUrl , '/ ' ). '/ ' . ltrim ($ categoryModel ->getImage (), '/ ' );
640
+ $ expectedImageUrl = rtrim ($ storeBaseUrl , '/ ' ) . '/ ' . ltrim ($ categoryModel ->getImage (), '/ ' );
595
641
596
642
$ this ->assertEquals ($ categoryId , $ categoryList [0 ]['id ' ]);
597
643
$ this ->assertEquals ('Parent Image Category ' , $ categoryList [0 ]['name ' ]);
@@ -602,6 +648,27 @@ public function testCategoryImage()
602
648
$ this ->assertEquals ($ expectedImageUrl , $ childCategory ['image ' ]);
603
649
}
604
650
651
+ /**
652
+ * @return array
653
+ */
654
+ public function categoryImageDataProvider (): array
655
+ {
656
+ return [
657
+ 'default_filename_strategy ' => [
658
+ 'image_prefix ' => null
659
+ ],
660
+ 'just_filename_strategy ' => [
661
+ 'image_prefix ' => ''
662
+ ],
663
+ 'with_pub_media_strategy ' => [
664
+ 'image_prefix ' => '/pub/media/catalog/category/ '
665
+ ],
666
+ 'catalog_category_strategy ' => [
667
+ 'image_prefix ' => 'catalog/category/ '
668
+ ],
669
+ ];
670
+ }
671
+
605
672
/**
606
673
* @param ProductInterface $product
607
674
* @param array $actualResponse
0 commit comments