Skip to content

Commit 96eaf30

Browse files
committed
ACP2E-2129: customer is reporting their table rates are not updating via CSV any longer
- added unit tests
1 parent 023f9cc commit 96eaf30

File tree

2 files changed

+178
-14
lines changed

2 files changed

+178
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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\OfflineShipping\Test\Unit\Model\Plugin\AsyncConfig\Model;
9+
10+
use Magento\AsyncConfig\Model\AsyncConfigPublisher;
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\App\RequestFactory;
14+
use Magento\Framework\Exception\FileSystemException;
15+
use Magento\Framework\Filesystem;
16+
use Magento\Framework\Filesystem\Directory\WriteInterface;
17+
use Magento\Framework\Filesystem\DriverInterface;
18+
use Magento\Framework\Math\Random;
19+
use Magento\OfflineShipping\Model\Plugin\AsyncConfig\Model\AsyncConfigPublisherPlugin;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
23+
class AsyncConfigPublisherPluginTest extends TestCase
24+
{
25+
/**
26+
* @var Filesystem|MockObject
27+
*/
28+
private Filesystem $filesystem;
29+
30+
/**
31+
* @var Random|MockObject
32+
*/
33+
private Random $rand;
34+
35+
/**
36+
* @var RequestFactory|MockObject
37+
*/
38+
private RequestFactory $requestFactory;
39+
40+
/**
41+
* @var AsyncConfigPublisherPlugin
42+
*/
43+
private AsyncConfigPublisherPlugin $plugin;
44+
45+
protected function setUp(): void
46+
{
47+
$this->filesystem = $this->createMock(Filesystem::class);
48+
$this->rand = $this->createMock(Random::class);
49+
$this->requestFactory = $this->createMock(RequestFactory::class);
50+
$this->plugin = new AsyncConfigPublisherPlugin($this->filesystem, $this->rand, $this->requestFactory);
51+
52+
parent::setUp();
53+
}
54+
55+
/**
56+
* @return void
57+
* @throws \Magento\Framework\Exception\FileSystemException
58+
* @throws \Magento\Framework\Exception\LocalizedException
59+
*/
60+
public function testBeforeSaveConfigDataNoImportFile(): void
61+
{
62+
$request = $this->createMock(Http::class);
63+
$request->expects($this->once())->method('getFiles')->willReturn([]);
64+
$this->requestFactory->expects($this->once())->method('create')->willReturn($request);
65+
66+
$subject = $this->createMock(AsyncConfigPublisher::class);
67+
$params = ['test'];
68+
$this->assertSame([$params], $this->plugin->beforeSaveConfigData($subject, $params));
69+
}
70+
71+
/**
72+
* @return void
73+
* @throws FileSystemException
74+
* @throws \Magento\Framework\Exception\LocalizedException
75+
*/
76+
public function testBeforeSaveConfigDataException(): void
77+
{
78+
$files['groups']['tablerate']['fields']['import']['value'] = [
79+
'tmp_name' => 'some/path/to/file/import.csv',
80+
'name' => 'import.csv'
81+
];
82+
$request = $this->createMock(Http::class);
83+
$request->expects($this->once())->method('getFiles')->willReturn($files);
84+
$this->requestFactory->expects($this->once())->method('create')->willReturn($request);
85+
$driver = $this->createMock(DriverInterface::class);
86+
$driver->expects($this->once())->method('copy')->willReturn(false);
87+
$varDir = $this->createMock(WriteInterface::class);
88+
$varDir->expects($this->once())->method('getDriver')->willReturn($driver);
89+
$this->filesystem->expects($this->once())
90+
->method('getDirectoryWrite')
91+
->with(DirectoryList::VAR_IMPORT_EXPORT)
92+
->willReturn($varDir);
93+
$this->rand->expects($this->once())->method('getRandomString')->willReturn('123456');
94+
95+
$this->expectException(FileSystemException::class);
96+
$subject = $this->createMock(AsyncConfigPublisher::class);
97+
$config['groups']['tablerate']['fields']['import']['value']['name'] = 'import.csv';
98+
$this->plugin->beforeSaveConfigData($subject, $config);
99+
}
100+
101+
/**
102+
* @return void
103+
* @throws FileSystemException
104+
* @throws \Magento\Framework\Exception\LocalizedException
105+
*/
106+
public function testBeforeSaveConfigDataSuccess(): void
107+
{
108+
$files['groups']['tablerate']['fields']['import']['value'] = [
109+
'tmp_name' => 'some/path/to/file/import.csv',
110+
'name' => 'import.csv'
111+
];
112+
$request = $this->createMock(Http::class);
113+
$request->expects($this->once())->method('getFiles')->willReturn($files);
114+
$this->requestFactory->expects($this->once())->method('create')->willReturn($request);
115+
$driver = $this->createMock(DriverInterface::class);
116+
$driver->expects($this->once())->method('copy')->willReturn(true);
117+
$varDir = $this->createMock(WriteInterface::class);
118+
$varDir->expects($this->once())->method('getDriver')->willReturn($driver);
119+
$varDir->expects($this->exactly(2))->method('getAbsolutePath')->willReturn('some/path/to/file');
120+
$this->filesystem->expects($this->once())
121+
->method('getDirectoryWrite')
122+
->with(DirectoryList::VAR_IMPORT_EXPORT)
123+
->willReturn($varDir);
124+
$this->rand->expects($this->once())->method('getRandomString')->willReturn('123456');
125+
126+
$subject = $this->createMock(AsyncConfigPublisher::class);
127+
$config['groups']['tablerate']['fields']['import']['value']['name'] = 'import.csv';
128+
$files['groups']['tablerate']['fields']['import']['value']['name'] = '123456_import.csv';
129+
$result['groups']['tablerate']['fields']['import']['value'] = [
130+
'name' => '123456_import.csv',
131+
'full_path' => 'some/path/to/file'
132+
];
133+
$this->assertSame([$result], $this->plugin->beforeSaveConfigData($subject, $config));
134+
}
135+
}

app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/TablerateTest.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
namespace Magento\OfflineShipping\Test\Unit\Model\ResourceModel\Carrier;
99

1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\DeploymentConfig;
12+
use Magento\Framework\App\Request\Http;
13+
use Magento\Framework\App\RequestFactory;
14+
use Magento\Framework\App\RequestInterface;
1115
use Magento\Framework\App\ResourceConnection;
1216
use Magento\Framework\DB\Adapter\AdapterInterface;
1317
use Magento\Framework\Filesystem;
1418
use Magento\Framework\Filesystem\Directory\ReadInterface;
19+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1520
use Magento\Framework\Model\ResourceModel\Db\Context;
1621
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
1722
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
@@ -32,27 +37,37 @@ class TablerateTest extends TestCase
3237
/**
3338
* @var Tablerate
3439
*/
35-
private $model;
40+
private Tablerate $model;
3641

3742
/**
38-
* @var MockObject
43+
* @var StoreManagerInterface|MockObject
3944
*/
40-
private $storeManagerMock;
45+
private StoreManagerInterface $storeManagerMock;
4146

4247
/**
43-
* @var MockObject
48+
* @var Filesystem|MockObject
4449
*/
45-
private $filesystemMock;
50+
private Filesystem $filesystemMock;
4651

4752
/**
48-
* @var MockObject
53+
* @var ResourceConnection|MockObject
4954
*/
50-
private $resource;
55+
private ResourceConnection $resource;
5156

5257
/**
53-
* @var MockObject
58+
* @var Import|MockObject
5459
*/
55-
private $importMock;
60+
private Import $importMock;
61+
62+
/**
63+
* @var DeploymentConfig|MockObject
64+
*/
65+
private DeploymentConfig $deploymentConfig;
66+
67+
/**
68+
* @var RequestFactory|MockObject
69+
*/
70+
private RequestFactory $requestFactory;
5671

5772
protected function setUp(): void
5873
{
@@ -65,6 +80,8 @@ protected function setUp(): void
6580
$this->importMock = $this->createMock(Import::class);
6681
$rateQueryFactoryMock = $this->createMock(RateQueryFactory::class);
6782
$this->resource = $this->createMock(ResourceConnection::class);
83+
$this->deploymentConfig = $this->createMock(DeploymentConfig::class);
84+
$this->requestFactory = $this->createMock(RequestFactory::class);
6885

6986
$contextMock->expects($this->once())->method('getResources')->willReturn($this->resource);
7087

@@ -76,18 +93,26 @@ protected function setUp(): void
7693
$carrierTablerateMock,
7794
$this->filesystemMock,
7895
$this->importMock,
79-
$rateQueryFactoryMock
96+
$rateQueryFactoryMock,
97+
null,
98+
$this->deploymentConfig,
99+
$this->requestFactory
80100
);
81101
}
82102

83103
public function testUploadAndImport()
84104
{
85-
$_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'] = 'some/path/to/file';
105+
$files['groups']['tablerate']['fields']['import']['value'] = [
106+
'tmp_name' => 'some/path/to/file/import.csv'
107+
];
86108
$object = $this->getMockBuilder(\Magento\OfflineShipping\Model\Config\Backend\Tablerate::class)
87109
->addMethods(['getScopeId'])
88110
->disableOriginalConstructor()
89111
->getMock();
90112

113+
$request = $this->createMock(Http::class);
114+
$request->expects($this->once())->method('getFiles')->willReturn($files);
115+
$this->requestFactory->expects($this->once())->method('create')->willReturn($request);
91116
$websiteMock = $this->getMockForAbstractClass(WebsiteInterface::class);
92117
$directoryReadMock = $this->getMockForAbstractClass(ReadInterface::class);
93118
$fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\ReadInterface::class);
@@ -97,10 +122,15 @@ public function testUploadAndImport()
97122
$object->expects($this->once())->method('getScopeId')->willReturn(1);
98123
$websiteMock->expects($this->once())->method('getId')->willReturn(1);
99124

125+
$writeMock = $this->createMock(WriteInterface::class);
126+
$writeMock->expects($this->once())->method('delete')->with('import.csv')->willReturn(true);
100127
$this->filesystemMock->expects($this->once())->method('getDirectoryReadByPath')
101-
->with('some/path/to')->willReturn($directoryReadMock);
128+
->with('some/path/to/file')->willReturn($directoryReadMock);
102129
$directoryReadMock->expects($this->once())->method('openFile')
103-
->with('file')->willReturn($fileReadMock);
130+
->with('import.csv')->willReturn($fileReadMock);
131+
$this->filesystemMock->expects($this->once())
132+
->method('getDirectoryWrite')
133+
->with('some/path/to/file')->willReturn($writeMock);
104134

105135
$this->resource->expects($this->once())->method('getConnection')->willReturn($connectionMock);
106136

@@ -112,6 +142,5 @@ public function testUploadAndImport()
112142
$this->importMock->expects($this->once())->method('getData')->willReturn([]);
113143

114144
$this->model->uploadAndImport($object);
115-
unset($_FILES['groups']);
116145
}
117146
}

0 commit comments

Comments
 (0)