Skip to content

Commit 5d17c62

Browse files
committed
MC-20423: [Integration Test] Import Table Rates to be used in Configuration Settings
1 parent b169e62 commit 5d17c62

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\OfflineShipping\Controller\Adminhtml\System\Config;
10+
11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
use Magento\Framework\Message\MessageInterface;
13+
use Magento\Framework\View\LayoutInterface;
14+
use Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid;
15+
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
16+
use Magento\TestFramework\ObjectManager;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use Magento\Framework\Filesystem;
19+
use Magento\Framework\App\Filesystem\DirectoryList;
20+
21+
/**
22+
* Test tablerates import and export.
23+
*
24+
* @magentoAppArea adminhtml
25+
*/
26+
class ImportExportTableratesTest extends \Magento\TestFramework\TestCase\AbstractBackendController
27+
{
28+
/**
29+
* @var ObjectManager
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @var Filesystem
35+
*/
36+
private $fileSystem;
37+
38+
/**
39+
* @var DirectoryList
40+
*/
41+
private $varDirectory;
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp()
47+
{
48+
$this->objectManager = Bootstrap::getObjectManager();
49+
$this->fileSystem = $this->objectManager->get(Filesystem::class);
50+
$this->varDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
51+
52+
parent::setUp();
53+
}
54+
55+
/**
56+
* Import Table Rates to be used in Configuration Settings.
57+
*
58+
* @magentoDataFixture Magento/OfflineShipping/_files/tablerate_create_file_in_tmp.php
59+
* @return void
60+
*/
61+
public function testImportExportTablerates(): void
62+
{
63+
$importCsv = 'tablerates.csv';
64+
$tmpDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::SYS_TMP);
65+
$importCsvPath = $tmpDirectory->getAbsolutePath($importCsv);
66+
67+
$_FILES['groups'] = [
68+
'name' => ['tablerate' => ['fields' => ['import' => ['value' => $importCsv]]]],
69+
'type' => ['tablerate' => ['fields' => ['import' => ['value' => 'text/csv']]]],
70+
'tmp_name' => ['tablerate' => ['fields' => ['import' => ['value' => $importCsvPath]]]],
71+
'error'=> ['tablerate' => ['fields' => ['import' => ['value' => 0]]]],
72+
'size' => ['tablerate' => ['fields' => ['import' => ['value' => 102]]]],
73+
];
74+
75+
$this->getRequest()->setPostValue(
76+
[
77+
'groups' => [
78+
'tablerate' => [
79+
'fields' => [
80+
'condition_name' => ['value' => 'package_weight'],
81+
'import' => ['value' => microtime(true)],
82+
],
83+
],
84+
],
85+
]
86+
)->setMethod(HttpRequest::METHOD_POST);
87+
88+
$this->dispatch('backend/admin/system_config/save/section/carriers/website/1/');
89+
$this->assertSessionMessages(
90+
$this->equalTo([(string)__('You saved the configuration.')]),
91+
MessageInterface::TYPE_SUCCESS
92+
);
93+
94+
$tablerateResourceModel = $this->objectManager->create(Tablerate::class);
95+
$connection = $tablerateResourceModel->getConnection();
96+
97+
$selectData = $connection->select()->from($tablerateResourceModel->getTable('shipping_tablerate'));
98+
$this->assertNotEmpty($connection->fetchRow($selectData));
99+
100+
$exportCsv = $this->getTablerateCsv();
101+
$exportCsvContent = $this->varDirectory->openFile($exportCsv['value'], 'r')->readAll();
102+
$importCsvContent = $tmpDirectory->openFile($importCsvPath, 'r')->readAll();
103+
104+
$this->assertEquals($importCsvContent, $exportCsvContent);
105+
}
106+
107+
/**
108+
* @return array
109+
*/
110+
private function getTablerateCsv(): array
111+
{
112+
/** @var Grid $gridBlock */
113+
$gridBlock = $this->objectManager->get(LayoutInterface::class)->createBlock(Grid::class);
114+
$exportCsv = $gridBlock->setWebsiteId(1)->setConditionName('package_weight')->getCsvFile();
115+
116+
return $exportCsv;
117+
}
118+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$importCsv = 'tablerates.csv';
13+
$objectManager = Bootstrap::getObjectManager();
14+
$fileSystem = $objectManager->get(Filesystem::class);
15+
16+
$tmpDirectory = $fileSystem->getDirectoryWrite(DirectoryList::SYS_TMP);
17+
$importCsvPath = $tmpDirectory->getAbsolutePath($importCsv);
18+
19+
$fixtureDir = realpath(__DIR__);
20+
copy($fixtureDir . DIRECTORY_SEPARATOR . $importCsv, $importCsvPath);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
$fileSystem = $objectManager->get(Filesystem::class);
14+
$tmpDirectory = $fileSystem->getDirectoryWrite(DirectoryList::SYS_TMP);
15+
$fileName = 'tablerates.csv';
16+
17+
unlink($tmpDirectory->getAbsolutePath($fileName));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Country,Region/State,"Zip/Postal Code","Weight (and above)","Shipping Price"
2+
USA,*,*,10.0000,666.0000

0 commit comments

Comments
 (0)