Skip to content

Commit 88cf03b

Browse files
committed
Merge remote-tracking branch 'origin/2.4-develop' into MC-42212
2 parents 434dafe + f80f0d9 commit 88cf03b

File tree

7 files changed

+263
-4
lines changed

7 files changed

+263
-4
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
1112
use Magento\Framework\GraphQl\Query\ResolverInterface;
1213
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
@@ -23,8 +24,13 @@ class SpecialPrice implements ResolverInterface
2324
*/
2425
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
2526
{
27+
if (!isset($value['model'])) {
28+
throw new LocalizedException(__('"model" value should be specified'));
29+
}
30+
2631
/** @var ProductInterface $product */
2732
$product = $value['model'];
33+
2834
/** @var PricingSpecialPrice $specialPrice */
2935
$specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE);
3036

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/RequiredColumnsProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function process(
3636
ContextInterface $context = null
3737
): Collection {
3838
$collection->addAttributeToSelect('special_price');
39-
$collection->addAttributeToSelect('special_price_from');
40-
$collection->addAttributeToSelect('special_price_to');
39+
$collection->addAttributeToSelect('special_from_date');
40+
$collection->addAttributeToSelect('special_to_date');
4141
$collection->addAttributeToSelect('tax_class_id');
4242

4343
return $collection;

app/code/Magento/Cms/Block/Adminhtml/Wysiwyg/Images/Tree.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public function getTreeJson()
7676
'path' => substr($item->getFilename(), strlen($storageRoot)),
7777
'cls' => 'folder',
7878
];
79-
$nestedDirectories = $this->getMediaDirectory()->readRecursively($item->getFilename());
80-
$hasNestedDirectories = count($nestedDirectories) > 0;
79+
$hasNestedDirectories = $this->hasNestedDirectories($storageRoot, $item->getFilename());
8180

8281
// if no nested directories inside dir, add 'leaf' state so that jstree hides dropdown arrow next to dir
8382
if (!$hasNestedDirectories) {
@@ -89,6 +88,26 @@ public function getTreeJson()
8988
return $this->serializer->serialize($jsonArray);
9089
}
9190

91+
/**
92+
* Check if directory has nested directories
93+
*
94+
* @param string $storageRoot
95+
* @param string $fileName
96+
* @return bool
97+
*/
98+
private function hasNestedDirectories(string $storageRoot, string $fileName): bool
99+
{
100+
$pathList = $this->getMediaDirectory()->read($fileName);
101+
foreach ($pathList as $directoryPath) {
102+
$file = $this->_filesystem->getDirectoryReadByPath($storageRoot . $directoryPath);
103+
if ($file->isDirectory()) {
104+
return true;
105+
}
106+
}
107+
108+
return false;
109+
}
110+
92111
/**
93112
* Json source URL
94113
*
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Test\Unit\Block\Adminhtml\Wysiwyg\Images;
9+
10+
use Magento\Backend\Block\Template\Context;
11+
use Magento\Cms\Block\Adminhtml\Wysiwyg\Images\Tree;
12+
use Magento\Cms\Helper\Wysiwyg\Images;
13+
use Magento\Cms\Model\Wysiwyg\Images\Storage;
14+
use Magento\Framework\App\Filesystem\DirectoryList;
15+
use Magento\Framework\DataObject;
16+
use Magento\Framework\Filesystem;
17+
use Magento\Framework\Filesystem\Directory\Read;
18+
use Magento\Framework\Registry;
19+
use Magento\Framework\Serialize\Serializer\Json;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Get tree json test.
26+
*/
27+
class TreeTest extends TestCase
28+
{
29+
/**
30+
* @var Tree
31+
*/
32+
private $model;
33+
34+
/**
35+
* @var Registry|MockObject
36+
*/
37+
private $coreRegistryMock;
38+
39+
/**
40+
* @var Images|MockObject
41+
*/
42+
private $cmsWysiwygImagesMock;
43+
44+
/**
45+
* @var Storage|MockObject
46+
*/
47+
private $imagesStorageMock;
48+
49+
/**
50+
* @var Read|MockObject
51+
*/
52+
private $directoryMock;
53+
54+
/**
55+
* @var Filesystem|MockObject
56+
*/
57+
private $fileSystemMock;
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
protected function setUp(): void
63+
{
64+
$objectManager = new ObjectManager($this);
65+
$contextMock = $this->createMock(Context::class);
66+
$this->cmsWysiwygImagesMock = $this->createMock(Images::class);
67+
$this->coreRegistryMock = $this->createMock(Registry::class);
68+
$serializerMock = $this->createMock(Json::class);
69+
$this->imagesStorageMock = $this->createMock(Storage::class);
70+
71+
$this->directoryMock = $this->getMockBuilder(Read::class)
72+
->disableOriginalConstructor()
73+
->onlyMethods(['getRelativePath', 'isDirectory', 'getAbsolutePath', 'read'])
74+
->getMock();
75+
$this->fileSystemMock = $this->createMock(Filesystem::class);
76+
$this->fileSystemMock->method('getDirectoryRead')
77+
->with(DirectoryList::MEDIA)
78+
->willReturn($this->directoryMock);
79+
80+
$this->model = $objectManager->getObject(
81+
Tree::class,
82+
[
83+
'context' => $contextMock,
84+
'cmsWysiwygImages' => $this->cmsWysiwygImagesMock,
85+
'registry' => $this->coreRegistryMock,
86+
'serializer' => $serializerMock,
87+
'_filesystem' => $this->fileSystemMock
88+
]
89+
);
90+
}
91+
92+
/**
93+
* Test execute for get directories tree
94+
*
95+
* @return void
96+
*/
97+
public function testGetTreeJson(): void
98+
{
99+
$collection = [];
100+
$this->cmsWysiwygImagesMock->method('getStorageRoot')
101+
->willReturn('/storage/root/dir/');
102+
$this->cmsWysiwygImagesMock->method('getCurrentPath')
103+
->willReturn('/storage/root/dir/pub/media/');
104+
$fileNames = ['fileName'];
105+
foreach ($fileNames as $filename) {
106+
/** @var DataObject|MockObject $objectMock */
107+
$objectMock = $this->getMockBuilder(DataObject::class)
108+
->addMethods(['getFilename'])
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$objectMock->method('getFilename')
112+
->willReturn('/storage/root/dir/' . $filename);
113+
$collection[] = $objectMock;
114+
}
115+
//items for collection
116+
$iterator = new \ArrayIterator($collection);
117+
$this->imagesStorageMock->method('getDirsCollection')
118+
->willReturn($iterator);
119+
$this->coreRegistryMock->method('registry')->willReturn($this->imagesStorageMock);
120+
$this->directoryMock->method('read')->willReturn($fileNames);
121+
$this->fileSystemMock->expects($this->once())
122+
->method('getDirectoryReadByPath')
123+
->willReturn($this->directoryMock);
124+
$this->model->getTreeJson();
125+
}
126+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,72 @@ public function testProductWithCatalogDiscount()
945945
}
946946
}
947947

948+
/**
949+
* Check if the special price visible if the current date is in the date range set
950+
* for the special price
951+
*
952+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
953+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price.php
954+
*/
955+
public function testSpecialPriceVisibleIfInDateRange()
956+
{
957+
$query = <<<QUERY
958+
{
959+
products(filter: {sku: {eq: "simple_product"}}) {
960+
items {
961+
price_range {
962+
minimum_price {
963+
regular_price {
964+
value
965+
}
966+
}
967+
}
968+
special_price
969+
}
970+
}
971+
}
972+
QUERY;
973+
$result = $this->graphQlQuery($query);
974+
$productInformation = $result['products']['items'][0];
975+
$productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value'];
976+
977+
self::assertEquals('10', $productRegularPrice);
978+
self::assertEquals('5.99', $productInformation['special_price']);
979+
}
980+
981+
/**
982+
* Check if the special price is not visible if the current date is not in the date range set
983+
* for the special price
984+
*
985+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
986+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_special_price_future_date.php
987+
*/
988+
public function testSpecialPriceNotVisibleIfNotInDateRange()
989+
{
990+
$query = <<<QUERY
991+
{
992+
products(filter: {sku: {eq: "simple_product"}}) {
993+
items {
994+
price_range {
995+
minimum_price {
996+
regular_price {
997+
value
998+
}
999+
}
1000+
}
1001+
special_price
1002+
}
1003+
}
1004+
}
1005+
QUERY;
1006+
$result = $this->graphQlQuery($query);
1007+
$productInformation = $result['products']['items'][0];
1008+
$productRegularPrice = $productInformation['price_range']['minimum_price']['regular_price']['value'];
1009+
1010+
self::assertEquals('10', $productRegularPrice);
1011+
self::assertEquals(null, $productInformation['special_price']);
1012+
}
1013+
9481014
/**
9491015
* Get GraphQl query to fetch products by sku
9501016
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
$objectManager = Bootstrap::getObjectManager();
12+
/** @var ProductRepositoryInterface $productRepository */
13+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
14+
15+
$product = $productRepository->get('simple_product');
16+
$product->setSpecialPrice('5.99');
17+
18+
$product->setSpecialFromDate(date('Y-m-d', strtotime('-1 day')));
19+
$product->setSpecialToDate(date('Y-m-d', strtotime('+1 day')));
20+
21+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
$objectManager = Bootstrap::getObjectManager();
12+
/** @var ProductRepositoryInterface $productRepository */
13+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
14+
15+
$product = $productRepository->get('simple_product');
16+
$product->setSpecialPrice('5.99');
17+
18+
$product->setSpecialFromDate(date('Y-m-d', strtotime('+3 day')));
19+
$product->setSpecialToDate(date('Y-m-d', strtotime('+5 day')));
20+
21+
$productRepository->save($product);

0 commit comments

Comments
 (0)