Skip to content

Commit d4cc3ad

Browse files
authored
Merge branch '2.4-develop' into B2B-2606
2 parents 12dca6f + 3d49b24 commit d4cc3ad

File tree

7 files changed

+333
-17
lines changed

7 files changed

+333
-17
lines changed

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/OptionValueProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public function __construct(ResourceConnection $connection)
3232
/**
3333
* Get EAV attribute option value by option id
3434
*
35-
* @param int $valueId
35+
* @param int $optionId
3636
* @return string|null
3737
*/
38-
public function get(int $valueId): ?string
38+
public function get(int $optionId): ?string
3939
{
4040
$select = $this->connection->select()
4141
->from($this->connection->getTableName('eav_attribute_option_value'), 'value')
42-
->where('value_id = ?', $valueId);
42+
->where('option_id = ?', $optionId);
4343

4444
$result = $this->connection->fetchOne($select);
4545

app/code/Magento/GraphQlCache/Controller/Plugin/GraphQl.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\GraphQlCache\Controller\Plugin;
99

1010
use Magento\Framework\App\FrontControllerInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\App\RequestInterface;
1213
use Magento\Framework\App\Response\Http as ResponseHttp;
1314
use Magento\Framework\Controller\ResultInterface;
@@ -16,9 +17,11 @@
1617
use Magento\GraphQlCache\Model\CacheableQuery;
1718
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
1819
use Magento\PageCache\Model\Config;
20+
use Psr\Log\LoggerInterface;
1921

2022
/**
2123
* Plugin for handling controller after controller tags and pre-controller validation.
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2225
*/
2326
class GraphQl
2427
{
@@ -52,28 +55,37 @@ class GraphQl
5255
*/
5356
private $cacheIdCalculator;
5457

58+
/**
59+
* @var LoggerInterface $logger
60+
*/
61+
private $logger;
62+
5563
/**
5664
* @param CacheableQuery $cacheableQuery
65+
* @param CacheIdCalculator $cacheIdCalculator
5766
* @param Config $config
58-
* @param ResponseHttp $response
67+
* @param LoggerInterface $logger
5968
* @param HttpRequestProcessor $requestProcessor
69+
* @param ResponseHttp $response
6070
* @param Registry $registry
61-
* @param CacheIdCalculator $cacheIdCalculator
6271
*/
6372
public function __construct(
6473
CacheableQuery $cacheableQuery,
74+
CacheIdCalculator $cacheIdCalculator,
6575
Config $config,
66-
ResponseHttp $response,
76+
LoggerInterface $logger,
6777
HttpRequestProcessor $requestProcessor,
68-
Registry $registry,
69-
CacheIdCalculator $cacheIdCalculator
78+
ResponseHttp $response,
79+
Registry $registry = null
7080
) {
7181
$this->cacheableQuery = $cacheableQuery;
82+
$this->cacheIdCalculator = $cacheIdCalculator;
7283
$this->config = $config;
73-
$this->response = $response;
84+
$this->logger = $logger;
7485
$this->requestProcessor = $requestProcessor;
75-
$this->registry = $registry;
76-
$this->cacheIdCalculator = $cacheIdCalculator;
86+
$this->response = $response;
87+
$this->registry = $registry ?: ObjectManager::getInstance()
88+
->get(Registry::class);
7789
}
7890

7991
/**
@@ -87,7 +99,12 @@ public function __construct(
8799
public function beforeDispatch(
88100
FrontControllerInterface $subject,
89101
RequestInterface $request
90-
) {
102+
): void {
103+
try {
104+
$this->requestProcessor->validateRequest($request);
105+
} catch (\Exception $error) {
106+
$this->logger->critical($error->getMessage());
107+
}
91108
/** @var \Magento\Framework\App\Request\Http $request */
92109
$this->requestProcessor->processHeaders($request);
93110
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\GraphQlCache\Test\Unit\Controller\Plugin;
9+
10+
use Magento\Framework\App\FrontControllerInterface;
11+
use Magento\Framework\App\Request\Http;
12+
use Magento\Framework\App\Response\Http as ResponseHttp;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\GraphQl\Controller\HttpRequestProcessor;
15+
use Magento\GraphQlCache\Controller\Plugin\GraphQl;
16+
use Magento\GraphQlCache\Model\CacheableQuery;
17+
use Magento\GraphQlCache\Model\CacheId\CacheIdCalculator;
18+
use Magento\PageCache\Model\Config;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
use Psr\Log\LoggerInterface;
22+
23+
/**
24+
* Test beforeDispatch
25+
*/
26+
class GraphQlTest extends TestCase
27+
{
28+
/**
29+
* @var GraphQl
30+
*/
31+
private $graphql;
32+
33+
/**
34+
* @var CacheableQuery|MockObject
35+
*/
36+
private $cacheableQueryMock;
37+
38+
/**
39+
* @var Config|MockObject
40+
*/
41+
private $configMock;
42+
43+
/**
44+
* @var ResponseHttp|MockObject
45+
*/
46+
private $responseMock;
47+
48+
/**
49+
* @var HttpRequestProcessor|MockObject
50+
*/
51+
private $requestProcessorMock;
52+
53+
/**
54+
* @var CacheIdCalculator|MockObject
55+
*/
56+
private $cacheIdCalculatorMock;
57+
58+
/**
59+
* @var LoggerInterface|MockObject
60+
*/
61+
private $loggerMock;
62+
63+
/**
64+
* @var FrontControllerInterface|MockObject
65+
*/
66+
private $subjectMock;
67+
68+
/**
69+
* @var Http|MockObject
70+
*/
71+
private $requestMock;
72+
73+
protected function setUp(): void
74+
{
75+
$this->cacheableQueryMock = $this->createMock(CacheableQuery::class);
76+
$this->cacheIdCalculatorMock = $this->createMock(CacheIdCalculator::class);
77+
$this->configMock = $this->createMock(Config::class);
78+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
79+
->onlyMethods(['critical'])
80+
->disableOriginalConstructor()
81+
->getMockForAbstractClass();
82+
$this->requestProcessorMock = $this->getMockBuilder(HttpRequestProcessor::class)
83+
->onlyMethods(['validateRequest','processHeaders'])
84+
->disableOriginalConstructor()
85+
->getMockForAbstractClass();
86+
$this->responseMock = $this->createMock(ResponseHttp::class);
87+
$this->subjectMock = $this->createMock(FrontControllerInterface::class);
88+
$this->requestMock = $this->createMock(Http::class);
89+
$this->graphql = new GraphQl(
90+
$this->cacheableQueryMock,
91+
$this->cacheIdCalculatorMock,
92+
$this->configMock,
93+
$this->loggerMock,
94+
$this->requestProcessorMock,
95+
$this->responseMock
96+
);
97+
}
98+
99+
/**
100+
* test beforeDispatch function for validation purpose
101+
*/
102+
public function testBeforeDispatch(): void
103+
{
104+
$this->requestProcessorMock
105+
->expects($this->any())
106+
->method('validateRequest');
107+
$this->requestProcessorMock
108+
->expects($this->any())
109+
->method('processHeaders');
110+
$this->loggerMock
111+
->expects($this->any())
112+
->method('critical');
113+
$this->assertNull($this->graphql->beforeDispatch($this->subjectMock, $this->requestMock));
114+
}
115+
}

app/code/Magento/ImportExport/Model/Source/Upload.php

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

88
namespace Magento\ImportExport\Model\Source;
99

10+
use Laminas\File\Transfer\Adapter\Http;
11+
use Laminas\Validator\File\Upload as FileUploadValidator;
1012
use Magento\Framework\App\Filesystem\DirectoryList;
1113
use Magento\Framework\Exception\LocalizedException;
1214
use Magento\Framework\Filesystem;
@@ -74,19 +76,23 @@ public function __construct(
7476
*/
7577
public function uploadSource(string $entity)
7678
{
77-
/** @var $adapter \Zend_File_Transfer_Adapter_Http */
79+
/**
80+
* @var $adapter Http
81+
*/
7882
$adapter = $this->httpFactory->create();
7983
if (!$adapter->isValid(Import::FIELD_NAME_SOURCE_FILE)) {
8084
$errors = $adapter->getErrors();
81-
if ($errors[0] == \Zend_Validate_File_Upload::INI_SIZE) {
85+
if ($errors[0] == FileUploadValidator::INI_SIZE) {
8286
$errorMessage = $this->importExportData->getMaxUploadSizeMessage();
8387
} else {
8488
$errorMessage = __('The file was not uploaded.');
8589
}
8690
throw new LocalizedException($errorMessage);
8791
}
8892

89-
/** @var $uploader Uploader */
93+
/**
94+
* @var $uploader Uploader
95+
*/
9096
$uploader = $this->uploaderFactory->create(['fileId' => Import::FIELD_NAME_SOURCE_FILE]);
9197
$uploader->setAllowedExtensions(['csv', 'zip']);
9298
$uploader->skipDbProcessing(true);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\ImportExport\Test\Unit\Model\Source;
9+
10+
use Laminas\File\Transfer\Adapter\Http;
11+
use Magento\Framework\Filesystem;
12+
use Magento\Framework\Filesystem\Directory\WriteInterface;
13+
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
14+
use Magento\Framework\Math\Random;
15+
use Magento\ImportExport\Helper\Data as DataHelper;
16+
use Magento\ImportExport\Model\Source\Upload;
17+
use Magento\MediaStorage\Model\File\Uploader;
18+
use Magento\MediaStorage\Model\File\UploaderFactory;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class UploadTest extends TestCase
22+
{
23+
/**
24+
* @var Upload
25+
*/
26+
private Upload $upload;
27+
28+
/**
29+
* @var FileTransferFactory
30+
*/
31+
protected FileTransferFactory $httpFactoryMock;
32+
33+
/**
34+
* @var DataHelper
35+
*/
36+
private DataHelper $importExportDataMock;
37+
38+
/**
39+
* @var UploaderFactory
40+
*/
41+
private UploaderFactory $uploaderFactoryMock;
42+
43+
/**
44+
* @var Random
45+
*/
46+
private Random $randomMock;
47+
48+
/**
49+
* @var Filesystem
50+
*/
51+
protected Filesystem $filesystemMock;
52+
53+
/**
54+
* @var Http
55+
*/
56+
private Http $adapterMock;
57+
58+
/**
59+
* @var Uploader
60+
*/
61+
private Uploader $uploaderMock;
62+
63+
protected function setUp(): void
64+
{
65+
$directoryAbsolutePath = 'importexport/';
66+
$this->httpFactoryMock = $this->createPartialMock(FileTransferFactory::class, ['create']);
67+
$this->importExportDataMock = $this->createMock(DataHelper::class);
68+
$this->uploaderFactoryMock = $this->getMockBuilder(UploaderFactory::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->randomMock = $this->getMockBuilder(Random::class)
72+
->disableOriginalConstructor()
73+
->getMock();
74+
$this->filesystemMock = $this->createMock(Filesystem::class);
75+
$this->adapterMock = $this->createMock(Http::class);
76+
$directoryWriteMock = $this->getMockBuilder(WriteInterface::class)
77+
->disableOriginalConstructor()
78+
->getMock();
79+
$directoryWriteMock->expects($this->once())->method('getAbsolutePath')->willReturn($directoryAbsolutePath);
80+
$this->filesystemMock->expects($this->once())->method('getDirectoryWrite')->willReturn($directoryWriteMock);
81+
$this->upload = new Upload(
82+
$this->httpFactoryMock,
83+
$this->importExportDataMock,
84+
$this->uploaderFactoryMock,
85+
$this->randomMock,
86+
$this->filesystemMock
87+
);
88+
}
89+
90+
/**
91+
* @return void
92+
*/
93+
public function testValidateFileUploadReturnsSavedFileArray(): void
94+
{
95+
$allowedExtensions = ['csv', 'zip'];
96+
$savedFileName = 'testString';
97+
$importFileId = 'import_file';
98+
$randomStringLength=32;
99+
$this->adapterMock->method('isValid')->willReturn(true);
100+
$this->httpFactoryMock->method('create')->willReturn($this->adapterMock);
101+
$this->uploaderMock = $this->createMock(Uploader::class);
102+
$this->uploaderMock->method('setAllowedExtensions')->with($allowedExtensions);
103+
$this->uploaderMock->method('skipDbProcessing')->with(true);
104+
$this->uploaderFactoryMock->method('create')
105+
->with(['fileId' => $importFileId])
106+
->willReturn($this->uploaderMock);
107+
$this->randomMock->method('getRandomString')->with($randomStringLength);
108+
$this->uploaderMock->method('save')->willReturn(['file' => $savedFileName]);
109+
$result = $this->upload->uploadSource($savedFileName);
110+
$this->assertIsArray($result);
111+
$this->assertEquals($savedFileName, $result['file']);
112+
}
113+
}

0 commit comments

Comments
 (0)