This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,210 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Framework\File;
use Magento\Customer\Model\FileProcessor;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Driver\File;
/**
* Test for \Magento\MediaStorage\Model\File\Uploader
*/
class MediaStorageUploaderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*/
private $uploaderFactory;
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->uploaderFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\MediaStorage\Model\File\UploaderFactory::class);
$this->filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\Filesystem::class);
}
/**
* @return void
*/
public function testUploadFileFromAllowedFolder(): void
{
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$fileName = 'text.txt';
$uploader = $this->createUploader($fileName);
$uploader->save($mediaDirectory->getAbsolutePath(FileProcessor::TMP_DIR));
$this->assertTrue($mediaDirectory->isFile(FileProcessor::TMP_DIR . DIRECTORY_SEPARATOR . $fileName));
}
/**
* @return void
*/
public function testUploadFileFromNotAllowedFolder(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid parameter given. A valid $fileId[tmp_name] is expected.');
$fileName = 'text.txt';
$tmpDir = 'tmp';
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::LOG);
$filePath = $tmpDirectory->getAbsolutePath() . $tmpDir . DIRECTORY_SEPARATOR . $fileName;
$tmpDirectory->writeFile($tmpDir . DIRECTORY_SEPARATOR . $fileName, 'just a text');
$type = [
'tmp_name' => $filePath,
'name' => $fileName,
];
$this->uploaderFactory->create(['fileId' => $type]);
}
/**
* @return void
*/
public function testUploadFileWithExcessiveFolderName(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Destination folder path is too long; must be 255 characters or less');
$uploader = $this->createUploader('text.txt');
$longStringFilePath = __DIR__ . '/_files/fixture_with_long_string.txt';
$longDirectoryFolderName = file_get_contents($longStringFilePath);
$uploader->save($longDirectoryFolderName);
}
/**
* Upload file test to 'var' directory
*
* @magentoConfigFixture system/media_gallery/enabled 1
* @magentoAppArea adminhtml
*
* @return void
*/
public function testUploadFileToVar(): void
{
$destinationDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$fileName = 'file.txt';
$destinationDir = 'tmp';
$filePath = $tmpDirectory->getAbsolutePath($fileName);
$tmpDirectory->writeFile($fileName, 'some data');
$type = [
'tmp_name' => $filePath,
'name' => $fileName,
];
$uploader = $this->uploaderFactory->create(['fileId' => $type]);
$uploader->save($destinationDirectory->getAbsolutePath($destinationDir));
// Uploader doesn't save file to local var if remote storage is configured
if ($this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)->getDriver() instanceof File) {
$this->assertTrue($destinationDirectory->isFile($destinationDir . DIRECTORY_SEPARATOR . $fileName));
} else {
$this->assertFalse($destinationDirectory->isFile($destinationDir . DIRECTORY_SEPARATOR . $fileName));
}
}
/**
* Upload file test when `Old Media Gallery` is disabled
*
* @magentoConfigFixture system/media_gallery/enabled 1
* @magentoAppArea adminhtml
* @dataProvider dirCodeDataProvider
*
* @param string $directoryCode
* @return void
*/
public function testUploadFileWhenOldMediaGalleryDisabled(string $directoryCode): void
{
$destinationDirectory = $this->filesystem->getDirectoryWrite($directoryCode);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$fileName = 'file.txt';
$destinationDir = 'tmp';
$filePath = $tmpDirectory->getAbsolutePath($fileName);
$tmpDirectory->writeFile($fileName, 'some data');
$type = [
'tmp_name' => $filePath,
'name' => $fileName,
];
$uploader = $this->uploaderFactory->create(['fileId' => $type]);
$uploader->save($destinationDirectory->getAbsolutePath($destinationDir));
$this->assertTrue($destinationDirectory->isFile($destinationDir . DIRECTORY_SEPARATOR . $fileName));
}
/**
* DataProvider for testUploadFileWhenOldMediaGalleryDisabled
*
* @return array
*/
public function dirCodeDataProvider(): array
{
return [
'media destination' => [DirectoryList::MEDIA],
'non-media destination' => [DirectoryList::VAR_IMPORT_EXPORT],
];
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
parent::tearDown();
$tmpDir = 'tmp';
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$mediaDirectory->delete($tmpDir);
$logDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::LOG);
$logDirectory->delete($tmpDir);
}
/**
* Create uploader instance for testing purposes.
*
* @param string $fileName
*
* @return Uploader
*/
private function createUploader(string $fileName): Uploader
{
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
$tmpDirectory->writeFile($fileName, 'just a text');
$type = [
'tmp_name' => $filePath,
'name' => $fileName,
];
return $this->uploaderFactory->create(['fileId' => $type]);
}
}
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Framework\File;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Filesystem;
/**
* Test for \Magento\Framework\File\Name
*/
class NameTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Name
*/
private $nameModel;
/**
* @var Filesystem
*/
private $fileSystem;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->fileSystem = $objectManager->get(\Magento\Framework\Filesystem::class);
$this->nameModel = $objectManager->get(Name::class);
}
/**
* @param string $destinationFile
* @param string $expectedFileName
* @return void
*
* @throws \Magento\Framework\Exception\FileSystemException
*
* @magentoDataFixture Magento/Framework/File/_files/framework_file_name.php
* @dataProvider getNewFileNameDataProvider
*/
public function testGetNewFileName($directory, $destinationFile, $expectedFileName)
{
$directory = $this->fileSystem->getDirectoryWrite($directory);
$path = $directory->getAbsolutePath('image/' . $destinationFile);
$name = $this->nameModel->getNewFileName($path);
$this->assertEquals($expectedFileName, $name);
}
/**
* Data provider for testGetNewFileName
* @return array
*/
public function getNewFileNameDataProvider()
{
return [
[DirectoryList::VAR_DIR, 'image.jpg', 'image.jpg'],
[DirectoryList::VAR_DIR, 'image_one.jpg', 'image_one_1.jpg'],
[DirectoryList::MEDIA, 'image.jpg', 'image.jpg'],
[DirectoryList::MEDIA, 'image_one.jpg', 'image_one_1.jpg'],
[DirectoryList::MEDIA, 'image_two.jpg', 'image_two_2.jpg']
];
}
}
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Magento file size test
*/
namespace Magento\Framework\File;
class SizeTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\File\Size
*/
protected $_fileSize;
protected function setUp(): void
{
$this->_fileSize = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\File\Size::class);
}
/**
* @backupStaticAttributes
*/
public function testGetMaxFileSize()
{
$this->assertGreaterThanOrEqual(0, $this->_fileSize->getMaxFileSize());
$this->assertGreaterThanOrEqual(0, $this->_fileSize->getMaxFileSizeInMb());
}
}
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Framework\File;
use Magento\Framework\App\Filesystem\DirectoryList;
use PHPUnit\Framework\TestCase;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\Filesystem;
/**
* Test for \Magento\Framework\File\Uploader
*/
class UploaderTest extends TestCase
{
/**
* @var UploaderFactory
*/
private $uploaderFactory;
/**
* @var Filesystem\File\WriteInterface
*/
private $mediaDirectory;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->uploaderFactory = $objectManager->get(UploaderFactory::class);
$filesystem = $objectManager->get(Filesystem::class);
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
}
/**
* @inheritDoc
*/
public function tearDown(): void
{
$this->mediaDirectory->delete('customer_address');
}
/**
* @dataProvider uploadDataProvider
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function testUpload(string $expectedFile, ?string $newImageName = null): void
{
$this->mediaDirectory->delete('customer_address');
$this->mediaDirectory->create($this->mediaDirectory->getRelativePath('customer_address/tmp/'));
$tmpFilePath = $this->mediaDirectory->getAbsolutePath('customer_address/tmp/magento.jpg');
$this->mediaDirectory->getDriver()->filePutContents(
$tmpFilePath,
file_get_contents(__DIR__ . '/_files/magento.jpg')
);
$fileData = [
'name' => 'magento.jpg',
'type' => 'image/jpeg',
'tmp_name' => $tmpFilePath,
'error' => 0,
'size' => 139416,
];
$uploader = $this->uploaderFactory->create(['fileId' => $fileData]);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$uploader->save($this->mediaDirectory->getAbsolutePath('customer_address'), $newImageName);
self::assertEquals($newImageName ?? 'magento.jpg', $uploader->getUploadedFileName());
self::assertTrue($this->mediaDirectory->isExist($expectedFile));
}
/**
* @return array
*/
public function uploadDataProvider(): array
{
return [
[
'customer_address/magento.jpg',
null,
],
[
'customer_address/new_magento.jpg',
'new_magento.jpg',
]
];
}
}
@@ -0,0 +1 @@
Sr2P3cdxfaP54hNgR6D25JuT3elyye4PiuJIjBQYbqWUVDLGoyJ4k3kHYjyJ6ogTBfE6Oqy4H4dJnrORTdgZ7EiQDki2sBCmXiYyzyY0d1uFP5n019WrySrZ4xalNIdbMtukAQ7D4Oi4VkECopVgcCMbVGH91L6ra8Ev8TFK7x4togk12Np77vG4j08787Y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rjcYqqiVoCdn3UY6vXFWoLstw9trsFKPJpCQOFws48aVxYKlLHUxG3iaA3lklBBEdb7srLu1gL14duXyDrOe7sTNQY8g2laMAmufXIVXkV6pYYg3UVa56rcoLRk0U2G4yfFFLuU1wxsgSNUDDR9skJy8T3fVYeqBcHD7DR8UcQdSrMcZHNGcODkKrSpzPOj41kFT9si17R5U202hhUvFqJTRNSPBCaOqkEDjBfPV6i26dSgjVyI9gjegghoF7buRaNPH4RPdtkoRx88ouWumEGvcKXJBxKUUOQH7GuFPIXLg6xIZvGyOIIIrO45gW0WL29b1fzzEZns3J1cQZH4QhpmsP3fi9HdMSWfNNVgzZsf5fMcKCSJTY1GQhbhCLBEh6Ldxk8GjEV2zCfecuwp6bYvRHFtI7Lc8x3flb6Up8cakcjsZEG0ocgznnQYGvoV8Ttpgj3frbWqZ3fuPSBl3Fzz3lqD22jslcNJPBpr9wD0nvmvj1FjeYunkngP1foTbE6VDQvHyajW5Id4PGE0WGnlXsXELrnyA5dlktNqvErW99O3ncXU0tutsJ6qPS2LHaZUIF5qXytzWZC58dxG32GUM91
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Framework\App\Filesystem\DirectoryList;
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
$mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)->getDirectoryWrite(DirectoryList::MEDIA);
/** @var $varDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
$varDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)->getDirectoryWrite(DirectoryList::VAR_DIR);
$dir = 'image';
$mediaDirectory->create($dir);
$imageContent = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'magento_small_image.jpg');
$mediaDriver = $mediaDirectory->getDriver();
$mediaDriver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_one.jpg'), $imageContent);
$mediaDriver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_two.jpg'), $imageContent);
$mediaDriver->filePutContents($mediaDirectory->getAbsolutePath($dir . '/image_two_1.jpg'), $imageContent);
$varDirectory->create($dir);
$varDirectory->getDriver()->filePutContents($varDirectory->getAbsolutePath($dir . '/image_one.jpg'), $imageContent);
unset($imageContent);
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
use Magento\Framework\App\Filesystem\DirectoryList;
$fileSystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\Filesystem::class
);
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
$mediaDirectory = $fileSystem->getDirectoryWrite(DirectoryList::MEDIA);
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $varDirectory */
$varDirectory = $fileSystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$mediaDirectory->delete('image');
$varDirectory->delete('image');
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB