ImageUploaderTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Catalog\Model;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. /**
  10. * Tests for the \Magento\Catalog\Model\ImageUploader class
  11. */
  12. class ImageUploaderTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Framework\ObjectManagerInterface
  16. */
  17. private $objectManager;
  18. /**
  19. * @var \Magento\Catalog\Model\ImageUploader
  20. */
  21. private $imageUploader;
  22. /**
  23. * @var \Magento\Framework\Filesystem
  24. */
  25. private $filesystem;
  26. /**
  27. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  28. */
  29. private $mediaDirectory;
  30. /**
  31. * @inheritdoc
  32. */
  33. protected function setUp()
  34. {
  35. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  36. /** @var \Magento\Framework\Filesystem $filesystem */
  37. $this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
  38. $this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  39. /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
  40. $this->imageUploader = $this->objectManager->create(
  41. \Magento\Catalog\Model\ImageUploader::class,
  42. [
  43. 'baseTmpPath' => $this->mediaDirectory->getRelativePath('tmp'),
  44. 'basePath' => __DIR__,
  45. 'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
  46. 'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
  47. ]
  48. );
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function testSaveFileToTmpDir(): void
  54. {
  55. $fileName = 'magento_small_image.jpg';
  56. $tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
  57. $fixtureDir = realpath(__DIR__ . '/../_files');
  58. $filePath = $tmpDirectory->getAbsolutePath($fileName);
  59. copy($fixtureDir . DIRECTORY_SEPARATOR . $fileName, $filePath);
  60. $_FILES['image'] = [
  61. 'name' => $fileName,
  62. 'type' => 'image/jpeg',
  63. 'tmp_name' => $filePath,
  64. 'error' => 0,
  65. 'size' => 12500,
  66. ];
  67. $this->imageUploader->saveFileToTmpDir('image');
  68. $filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
  69. $this->assertTrue(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
  70. }
  71. /**
  72. * @expectedException \Magento\Framework\Exception\LocalizedException
  73. * @expectedExceptionMessage File validation failed.
  74. * @return void
  75. */
  76. public function testSaveFileToTmpDirWithWrongExtension(): void
  77. {
  78. $fileName = 'text.txt';
  79. $tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
  80. $filePath = $tmpDirectory->getAbsolutePath($fileName);
  81. $file = fopen($filePath, "wb");
  82. fwrite($file, 'just a text');
  83. $_FILES['image'] = [
  84. 'name' => $fileName,
  85. 'type' => 'text/plain',
  86. 'tmp_name' => $filePath,
  87. 'error' => 0,
  88. 'size' => 12500,
  89. ];
  90. $this->imageUploader->saveFileToTmpDir('image');
  91. $filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
  92. $this->assertFalse(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
  93. }
  94. /**
  95. * @expectedException \Magento\Framework\Exception\LocalizedException
  96. * @expectedExceptionMessage File validation failed.
  97. * @return void
  98. */
  99. public function testSaveFileToTmpDirWithWrongFile(): void
  100. {
  101. $fileName = 'file.gif';
  102. $tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
  103. $filePath = $tmpDirectory->getAbsolutePath($fileName);
  104. $file = fopen($filePath, "wb");
  105. fwrite($file, 'just a text');
  106. $_FILES['image'] = [
  107. 'name' => $fileName,
  108. 'type' => 'image/gif',
  109. 'tmp_name' => $filePath,
  110. 'error' => 0,
  111. 'size' => 12500,
  112. ];
  113. $this->imageUploader->saveFileToTmpDir('image');
  114. $filePath = $this->imageUploader->getBaseTmpPath() . DIRECTORY_SEPARATOR. $fileName;
  115. $this->assertFalse(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. public static function tearDownAfterClass()
  121. {
  122. parent::tearDownAfterClass();
  123. $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  124. \Magento\Framework\Filesystem::class
  125. );
  126. /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
  127. $mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  128. $mediaDirectory->delete('tmp');
  129. }
  130. }