|
| 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\Catalog\Controller\Adminhtml\Category\Image; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 14 | +use Magento\Framework\Serialize\SerializerInterface; |
| 15 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test cases related to upload category image. |
| 19 | + * |
| 20 | + * @see \Magento\Catalog\Controller\Adminhtml\Category\Image\Upload |
| 21 | + * @magentoAppArea adminhtml |
| 22 | + * @magentoDbIsolation enabled |
| 23 | + */ |
| 24 | +class UploadTest extends AbstractBackendController |
| 25 | +{ |
| 26 | + /** @var Filesystem */ |
| 27 | + private $filesystem; |
| 28 | + |
| 29 | + /** @var WriteInterface */ |
| 30 | + private $tmpDirectory; |
| 31 | + |
| 32 | + /** @var SerializerInterface */ |
| 33 | + private $json; |
| 34 | + |
| 35 | + /** @var string */ |
| 36 | + private $fileToRemove; |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->filesystem = $this->_objectManager->get(Filesystem::class); |
| 46 | + $this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::TMP); |
| 47 | + $this->json = $this->_objectManager->get(SerializerInterface::class); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @inheritdoc |
| 52 | + */ |
| 53 | + protected function tearDown(): void |
| 54 | + { |
| 55 | + if (file_exists($this->fileToRemove)) { |
| 56 | + unlink($this->fileToRemove); |
| 57 | + } |
| 58 | + |
| 59 | + parent::tearDown(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function testWithNotAllowedFileExtension(): void |
| 66 | + { |
| 67 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 68 | + $this->getRequest()->setPostValue('param_name', 'image'); |
| 69 | + $this->prepareFile('empty.csv'); |
| 70 | + $this->dispatch('backend/catalog/category_image/upload'); |
| 71 | + $responseBody = $this->json->unserialize($this->getResponse()->getBody()); |
| 72 | + $this->assertNotEmpty($responseBody['error']); |
| 73 | + $this->assertStringContainsString((string)__('File validation failed.'), $responseBody['error']); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Prepare file |
| 78 | + * |
| 79 | + * @param string $fileName |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + private function prepareFile(string $fileName): void |
| 83 | + { |
| 84 | + $this->tmpDirectory->create($this->tmpDirectory->getAbsolutePath()); |
| 85 | + $filePath = $this->tmpDirectory->getAbsolutePath($fileName); |
| 86 | + $this->fileToRemove = $filePath; |
| 87 | + $fixtureDir = realpath(__DIR__ . '/../../../../_files'); |
| 88 | + $this->tmpDirectory->getDriver()->copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath); |
| 89 | + $_FILES['image'] = [ |
| 90 | + 'name' => $fileName, |
| 91 | + 'type' => 'image', |
| 92 | + 'tmp_name' => $filePath, |
| 93 | + 'error' => 0, |
| 94 | + 'size' => 12500, |
| 95 | + ]; |
| 96 | + } |
| 97 | +} |
0 commit comments