|
| 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\Controller\Adminhtml\Export\File; |
| 9 | + |
| 10 | +use Magento\Backend\Model\Auth\Session; |
| 11 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 12 | +use Magento\Framework\App\Request\Http; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 17 | +use Magento\Backend\Model\UrlInterface as BackendUrl; |
| 18 | +use Magento\Backend\Model\Auth; |
| 19 | +use Magento\TestFramework\Bootstrap as TestBootstrap; |
| 20 | + |
| 21 | +/** |
| 22 | + * Test for \Magento\ImportExport\Controller\Adminhtml\Export\File\Download class. |
| 23 | + */ |
| 24 | +class DownloadTest extends AbstractBackendController |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var WriteInterface |
| 28 | + */ |
| 29 | + private $varDirectory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + private $fullDirectoryPath; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + private $fileName = 'catalog_product.csv'; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + private $filesize; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Filesystem |
| 48 | + */ |
| 49 | + private $filesystem; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var Auth |
| 53 | + */ |
| 54 | + private $auth; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var BackendUrl |
| 58 | + */ |
| 59 | + private $backendUrl; |
| 60 | + |
| 61 | + /** |
| 62 | + * @inheritdoc |
| 63 | + */ |
| 64 | + protected function setUp() |
| 65 | + { |
| 66 | + parent::setUp(); |
| 67 | + |
| 68 | + $this->filesystem = $this->_objectManager->get(Filesystem::class); |
| 69 | + $this->auth = $this->_objectManager->get(Auth::class); |
| 70 | + $this->backendUrl = $this->_objectManager->get(BackendUrl::class); |
| 71 | + $baseDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::ROOT); |
| 72 | + $this->varDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 73 | + $this->varDirectory->create($this->varDirectory->getRelativePath('export')); |
| 74 | + $this->fullDirectoryPath = $this->varDirectory->getAbsolutePath('export'); |
| 75 | + $filePath = $this->fullDirectoryPath . DIRECTORY_SEPARATOR . $this->fileName; |
| 76 | + $fixtureDir = realpath(__DIR__ . '/../../Import/_files'); |
| 77 | + $baseDirectory->copyFile($fixtureDir . '/' . $this->fileName, $filePath); |
| 78 | + $this->filesize = filesize($filePath); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check that file can be downloaded. |
| 83 | + * |
| 84 | + * @return void |
| 85 | + * @magentoConfigFixture default_store admin/security/use_form_key 1 |
| 86 | + * @magentoAppArea adminhtml |
| 87 | + */ |
| 88 | + public function testExecute(): void |
| 89 | + { |
| 90 | + $uri = 'backend/admin/export_file/download/filename/' . $this->fileName; |
| 91 | + $this->prepareRequest($uri); |
| 92 | + |
| 93 | + $this->dispatch($uri); |
| 94 | + |
| 95 | + $contentType = $this->getResponse()->getHeader('content-type'); |
| 96 | + $contentLength = $this->getResponse()->getHeader('content-length'); |
| 97 | + $contentDisposition = $this->getResponse()->getHeader('content-disposition'); |
| 98 | + |
| 99 | + $this->assertEquals(200, $this->getResponse()->getStatusCode(), 'Incorrect response status code'); |
| 100 | + $this->assertEquals( |
| 101 | + 'application/octet-stream', |
| 102 | + $contentType->getFieldValue(), |
| 103 | + 'Incorrect response header "content-type"' |
| 104 | + ); |
| 105 | + $this->assertEquals( |
| 106 | + 'attachment; filename="export/' . $this->fileName . '"', |
| 107 | + $contentDisposition->getFieldValue(), |
| 108 | + 'Incorrect response header "content-disposition"' |
| 109 | + ); |
| 110 | + $this->assertEquals( |
| 111 | + $this->filesize, |
| 112 | + $contentLength->getFieldValue(), |
| 113 | + 'Incorrect response header "content-length"' |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Prepares GET request to download file. |
| 119 | + * |
| 120 | + * @param string $uri |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + private function prepareRequest(string $uri): void |
| 124 | + { |
| 125 | + $authSession = $this->_objectManager->create(Session::class); |
| 126 | + $authSession->setIsFirstPageAfterLogin(false); |
| 127 | + $this->auth->login( |
| 128 | + TestBootstrap::ADMIN_NAME, |
| 129 | + TestBootstrap::ADMIN_PASSWORD |
| 130 | + ); |
| 131 | + $this->auth->setAuthStorage($authSession); |
| 132 | + |
| 133 | + list($routeName, $controllerName, $actionName) = explode('/', Download::URL); |
| 134 | + $request = $this->getRequest(); |
| 135 | + $request->setMethod(Http::METHOD_GET) |
| 136 | + ->setRouteName($routeName) |
| 137 | + ->setControllerName($controllerName) |
| 138 | + ->setActionName($actionName) |
| 139 | + ->setParam(BackendUrl::SECRET_KEY_PARAM_NAME, $this->backendUrl->getSecretKey()) |
| 140 | + ->setRequestUri($uri); |
| 141 | + $this->backendUrl->turnOnSecretKey(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @inheritdoc |
| 146 | + */ |
| 147 | + protected function tearDown() |
| 148 | + { |
| 149 | + $this->auth = null; |
| 150 | + |
| 151 | + parent::tearDown(); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @inheritdoc |
| 156 | + */ |
| 157 | + public static function tearDownAfterClass() |
| 158 | + { |
| 159 | + $filesystem = Bootstrap::getObjectManager()->get(Filesystem::class); |
| 160 | + /** @var WriteInterface $directory */ |
| 161 | + $directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); |
| 162 | + if ($directory->isExist('export')) { |
| 163 | + $directory->delete('export'); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments