Skip to content

Commit e5759ee

Browse files
committed
MC-36956: Create automated test for "Upload Category Image"
1 parent 959047d commit e5759ee

File tree

3 files changed

+189
-31
lines changed

3 files changed

+189
-31
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Model/ImageUploaderTest.php

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,77 @@
88
namespace Magento\Catalog\Model;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\Framework\Filesystem\Directory\WriteInterface;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
1117

1218
/**
1319
* Tests for the \Magento\Catalog\Model\ImageUploader class
1420
*/
15-
class ImageUploaderTest extends \PHPUnit\Framework\TestCase
21+
class ImageUploaderTest extends TestCase
1622
{
23+
private const BASE_TMP_PATH = 'catalog/tmp/category';
24+
25+
private const BASE_PATH = 'catalog/category';
26+
1727
/**
18-
* @var \Magento\Framework\ObjectManagerInterface
28+
* @var ObjectManagerInterface
1929
*/
2030
private $objectManager;
2131

2232
/**
23-
* @var \Magento\Catalog\Model\ImageUploader
33+
* @var ImageUploader
2434
*/
2535
private $imageUploader;
2636

2737
/**
28-
* @var \Magento\Framework\Filesystem
38+
* @var Filesystem
2939
*/
3040
private $filesystem;
3141

3242
/**
33-
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
43+
* @var WriteInterface
3444
*/
3545
private $mediaDirectory;
3646

47+
/**
48+
* @var WriteInterface
49+
*/
50+
private $tmpDirectory;
51+
3752
/**
3853
* @inheritdoc
3954
*/
4055
protected function setUp(): void
4156
{
42-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
43-
/** @var \Magento\Framework\Filesystem $filesystem */
44-
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
57+
$this->objectManager = Bootstrap::getObjectManager();
58+
$this->filesystem = $this->objectManager->get(Filesystem::class);
4559
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
46-
/** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
60+
$this->tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
4761
$this->imageUploader = $this->objectManager->create(
48-
\Magento\Catalog\Model\ImageUploader::class,
62+
ImageUploader::class,
4963
[
50-
'baseTmpPath' => 'catalog/tmp/category',
51-
'basePath' => 'catalog/category',
64+
'baseTmpPath' => self::BASE_TMP_PATH,
65+
'basePath' => self::BASE_PATH,
5266
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
5367
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
5468
]
5569
);
5670
}
5771

5872
/**
73+
* @dataProvider saveFileToTmpDirProvider
74+
* @param string $fileName
75+
* @param string $expectedName
5976
* @return void
6077
*/
61-
public function testSaveFileToTmpDir(): void
78+
public function testSaveFileToTmpDir(string $fileName, string $expectedName): void
6279
{
63-
$fileName = 'magento_small_image.jpg';
64-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
6580
$fixtureDir = realpath(__DIR__ . '/../_files');
66-
$filePath = $tmpDirectory->getAbsolutePath($fileName);
81+
$filePath = $this->tmpDirectory->getAbsolutePath($fileName);
6782
copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath);
6883

6984
$_FILES['image'] = [
@@ -75,10 +90,27 @@ public function testSaveFileToTmpDir(): void
7590
];
7691

7792
$this->imageUploader->saveFileToTmpDir('image');
78-
$filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
93+
$filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR . $expectedName;
7994
$this->assertTrue(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
8095
}
8196

97+
/**
98+
* @return array
99+
*/
100+
public function saveFileToTmpDirProvider(): array
101+
{
102+
return [
103+
'image_default_name' => [
104+
'file_name' => 'magento_small_image.jpg',
105+
'expected_name' => 'magento_small_image.jpg',
106+
],
107+
'image_with_space_in_name' => [
108+
'file_name' => 'magento_image with space in name.jpg',
109+
'expected_name' => 'magento_image_with_space_in_name.jpg',
110+
],
111+
];
112+
}
113+
82114
/**
83115
* Test that method rename files when move it with the same name into base directory.
84116
*
@@ -90,7 +122,7 @@ public function testMoveFileFromTmp(): void
90122
{
91123
$expectedFilePath = $this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . 'magento_small_image_1.jpg';
92124

93-
$this->assertFileNotExists($this->mediaDirectory->getAbsolutePath($expectedFilePath));
125+
$this->assertFileDoesNotExist($this->mediaDirectory->getAbsolutePath($expectedFilePath));
94126

95127
$this->imageUploader->moveFileFromTmp('magento_small_image.jpg');
96128

@@ -102,12 +134,11 @@ public function testMoveFileFromTmp(): void
102134
*/
103135
public function testSaveFileToTmpDirWithWrongExtension(): void
104136
{
105-
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
137+
$this->expectException(LocalizedException::class);
106138
$this->expectExceptionMessage('File validation failed.');
107139

108140
$fileName = 'text.txt';
109-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
110-
$filePath = $tmpDirectory->getAbsolutePath($fileName);
141+
$filePath = $this->tmpDirectory->getAbsolutePath($fileName);
111142
$file = fopen($filePath, "wb");
112143
fwrite($file, 'just a text');
113144

@@ -120,7 +151,7 @@ public function testSaveFileToTmpDirWithWrongExtension(): void
120151
];
121152

122153
$this->imageUploader->saveFileToTmpDir('image');
123-
$filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
154+
$filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR . $fileName;
124155
$this->assertFalse(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
125156
}
126157

@@ -129,12 +160,11 @@ public function testSaveFileToTmpDirWithWrongExtension(): void
129160
*/
130161
public function testSaveFileToTmpDirWithWrongFile(): void
131162
{
132-
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
163+
$this->expectException(LocalizedException::class);
133164
$this->expectExceptionMessage('File validation failed.');
134165

135166
$fileName = 'file.gif';
136-
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
137-
$filePath = $tmpDirectory->getAbsolutePath($fileName);
167+
$filePath = $this->tmpDirectory->getAbsolutePath($fileName);
138168
$file = fopen($filePath, "wb");
139169
fwrite($file, 'just a text');
140170

@@ -147,7 +177,7 @@ public function testSaveFileToTmpDirWithWrongFile(): void
147177
];
148178

149179
$this->imageUploader->saveFileToTmpDir('image');
150-
$filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
180+
$filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR . $fileName;
151181
$this->assertFalse(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
152182
}
153183

@@ -157,11 +187,10 @@ public function testSaveFileToTmpDirWithWrongFile(): void
157187
public static function tearDownAfterClass(): void
158188
{
159189
parent::tearDownAfterClass();
160-
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
161-
\Magento\Framework\Filesystem::class
162-
);
163-
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
190+
$filesystem = Bootstrap::getObjectManager()->get(Filesystem::class);
191+
/** @var WriteInterface $mediaDirectory */
164192
$mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
165-
$mediaDirectory->delete('tmp');
193+
$mediaDirectory->delete(self::BASE_TMP_PATH);
194+
$mediaDirectory->delete(self::BASE_PATH);
166195
}
167196
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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\Catalog\Model\ResourceModel;
10+
11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
12+
use Magento\Catalog\Model\Category as CategoryModel;
13+
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
14+
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
15+
use Magento\Framework\App\Filesystem\DirectoryList;
16+
use Magento\Framework\Filesystem;
17+
use Magento\Framework\Filesystem\Directory\WriteInterface;
18+
use Magento\Framework\ObjectManagerInterface;
19+
use Magento\Framework\UrlInterface;
20+
use Magento\Store\Model\StoreManagerInterface;
21+
use Magento\TestFramework\Helper\Bootstrap;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Tests category resource model
26+
*
27+
* @see \Magento\Catalog\Model\ResourceModel\Category
28+
*/
29+
class CategoryTest extends TestCase
30+
{
31+
private const BASE_TMP_PATH = 'catalog/tmp/category';
32+
33+
private const BASE_PATH = 'catalog/category';
34+
35+
/** @var ObjectManagerInterface */
36+
private $objectManager;
37+
38+
/** @var CategoryRepositoryInterface */
39+
private $categoryRepository;
40+
41+
/** @var CategoryResource */
42+
private $categoryResource;
43+
44+
/** @var StoreManagerInterface */
45+
private $storeManager;
46+
47+
/** @var CategoryCollection */
48+
private $categoryCollection;
49+
50+
/** @var Filesystem */
51+
private $filesystem;
52+
53+
/** @var WriteInterface */
54+
private $mediaDirectory;
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
protected function setUp(): void
60+
{
61+
parent::setUp();
62+
63+
$this->objectManager = Bootstrap::getObjectManager();
64+
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
65+
$this->categoryResource = $this->objectManager->get(CategoryResource::class);
66+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
67+
$this->categoryCollection = $this->objectManager->get(CategoryCollection::class);
68+
$this->filesystem = $this->objectManager->get(Filesystem::class);
69+
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
70+
}
71+
72+
/**
73+
* @inheritdoc
74+
*/
75+
protected function tearDown(): void
76+
{
77+
$this->mediaDirectory->delete(self::BASE_PATH);
78+
79+
parent::tearDown();
80+
}
81+
82+
/**
83+
* @magentoDataFixture Magento/Catalog/_files/category.php
84+
* @magentoDataFixture Magento/Catalog/_files/catalog_tmp_category_image.php
85+
* @magentoDbIsolation disabled
86+
* @return void
87+
*/
88+
public function testAddImageForCategory(): void
89+
{
90+
$dataImage = [
91+
'name' => 'magento_small_image.jpg',
92+
'type' => 'image/jpg',
93+
'tmp_name' => '/tmp/phpDstnAx',
94+
'file' => 'magento_small_image.jpg',
95+
];
96+
$this->prepareDataImageUrl($dataImage);
97+
$imageRelativePath = self::BASE_PATH . DIRECTORY_SEPARATOR . $dataImage['file'];
98+
$expectedImage = DIRECTORY_SEPARATOR . $this->storeManager->getStore()->getBaseMediaDir()
99+
. DIRECTORY_SEPARATOR . $imageRelativePath;
100+
/** @var CategoryModel $category */
101+
$category = $this->categoryRepository->get(333);
102+
$category->setImage([$dataImage]);
103+
104+
$this->categoryResource->save($category);
105+
106+
$categoryModel = $this->categoryCollection
107+
->addAttributeToSelect('image')
108+
->addIdFilter([$category->getId()])
109+
->getFirstItem();
110+
$this->assertEquals(
111+
$expectedImage,
112+
$categoryModel->getImage(),
113+
'The path of the expected image does not match the path to the actual image.'
114+
);
115+
$this->assertFileExists($this->mediaDirectory->getAbsolutePath($imageRelativePath));
116+
}
117+
118+
/**
119+
* Add image url to image data
120+
*
121+
* @param array $dataImage
122+
* @return void
123+
*/
124+
private function prepareDataImageUrl(array &$dataImage): void
125+
{
126+
$dataImage['url'] = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA)
127+
. self::BASE_TMP_PATH . DIRECTORY_SEPARATOR . $dataImage['file'];
128+
}
129+
}
Loading

0 commit comments

Comments
 (0)