|
| 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\Filesystem\Directory\WriteInterface; |
| 13 | +use Magento\Framework\Message\MessageInterface; |
| 14 | +use Magento\Framework\View\LayoutInterface; |
| 15 | +use Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid; |
| 16 | +use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Collection; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use Magento\TestFramework\ObjectManager; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\Framework\Filesystem; |
| 21 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test tablerates import and export. |
| 25 | + * |
| 26 | + * @magentoAppArea adminhtml |
| 27 | + */ |
| 28 | +class ImportExportTableratesTest extends \Magento\TestFramework\TestCase\AbstractBackendController |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var ObjectManager |
| 32 | + */ |
| 33 | + private $objectManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Filesystem |
| 37 | + */ |
| 38 | + private $fileSystem; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var StoreManagerInterface |
| 42 | + */ |
| 43 | + private $storeManager; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var int |
| 47 | + */ |
| 48 | + private $websiteId; |
| 49 | + |
| 50 | + /** |
| 51 | + * @inheritdoc |
| 52 | + */ |
| 53 | + protected function setUp() |
| 54 | + { |
| 55 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 56 | + $this->fileSystem = $this->objectManager->get(Filesystem::class); |
| 57 | + $this->storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 58 | + $this->websiteId = $this->storeManager->getWebsite()->getId(); |
| 59 | + |
| 60 | + parent::setUp(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Import Table Rates to be used in Configuration Settings. |
| 65 | + * |
| 66 | + * @magentoDataFixture Magento/OfflineShipping/_files/tablerate_create_file_in_tmp.php |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function testImportExportTablerates(): void |
| 70 | + { |
| 71 | + $importCsv = 'tablerates.csv'; |
| 72 | + $tmpDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::SYS_TMP); |
| 73 | + $importCsvPath = $tmpDirectory->getAbsolutePath($importCsv); |
| 74 | + |
| 75 | + $_FILES['groups'] = [ |
| 76 | + 'name' => ['tablerate' => ['fields' => ['import' => ['value' => $importCsv]]]], |
| 77 | + 'type' => ['tablerate' => ['fields' => ['import' => ['value' => 'text/csv']]]], |
| 78 | + 'tmp_name' => ['tablerate' => ['fields' => ['import' => ['value' => $importCsvPath]]]], |
| 79 | + 'error'=> ['tablerate' => ['fields' => ['import' => ['value' => 0]]]], |
| 80 | + 'size' => ['tablerate' => ['fields' => ['import' => ['value' => 102]]]], |
| 81 | + ]; |
| 82 | + |
| 83 | + $this->getRequest()->setPostValue( |
| 84 | + [ |
| 85 | + 'groups' => [ |
| 86 | + 'tablerate' => [ |
| 87 | + 'fields' => [ |
| 88 | + 'condition_name' => ['value' => 'package_weight'], |
| 89 | + 'import' => ['value' => microtime(true)], |
| 90 | + ], |
| 91 | + ], |
| 92 | + ], |
| 93 | + ] |
| 94 | + )->setMethod(HttpRequest::METHOD_POST); |
| 95 | + |
| 96 | + $this->dispatch('backend/admin/system_config/save/section/carriers/website/' . $this->websiteId . '/'); |
| 97 | + $this->assertSessionMessages( |
| 98 | + $this->equalTo([(string)__('You saved the configuration.')]), |
| 99 | + MessageInterface::TYPE_SUCCESS |
| 100 | + ); |
| 101 | + |
| 102 | + /** @var Collection $tablerateCollection */ |
| 103 | + $tablerateCollection = $this->objectManager->create(Collection::class); |
| 104 | + $tablerateData = $tablerateCollection->setConditionFilter('package_weight')->getItems()[0]->getData(); |
| 105 | + $this->assertEquals('666.0000', $tablerateData['price']); |
| 106 | + $this->assertEquals('USA', $tablerateData['dest_country']); |
| 107 | + $this->assertEquals('10.0000', $tablerateData['condition_value']); |
| 108 | + |
| 109 | + $exportCsvContent = $this->getTablerateCsv(); |
| 110 | + $importCsvContent = $tmpDirectory->openFile($importCsvPath, 'r')->readAll(); |
| 111 | + |
| 112 | + $this->assertEquals($importCsvContent, $exportCsvContent); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + private function getTablerateCsv(): string |
| 119 | + { |
| 120 | + /** @var WriteInterface $varDirectory */ |
| 121 | + $varDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 122 | + |
| 123 | + /** @var Grid $gridBlock */ |
| 124 | + $gridBlock = $this->objectManager->get(LayoutInterface::class)->createBlock(Grid::class); |
| 125 | + $exportCsv = $gridBlock->setWebsiteId($this->websiteId)->setConditionName('package_weight')->getCsvFile(); |
| 126 | + $exportCsvContent = $varDirectory->openFile($exportCsv['value'], 'r')->readAll(); |
| 127 | + |
| 128 | + return $exportCsvContent; |
| 129 | + } |
| 130 | +} |
0 commit comments