Skip to content

Commit

Permalink
Cover changes with Integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
konarshankar07 committed Sep 3, 2020
1 parent d312ab5 commit c09a261
Showing 1 changed file with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
namespace Magento\Cms\Model\Wysiwyg\Images;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Test methods of class Storage
Expand All @@ -29,22 +33,27 @@ class StorageTest extends \PHPUnit\Framework\TestCase
private $objectManager;

/**
* @var \Magento\Framework\Filesystem
* @var Filesystem
*/
private $filesystem;

/**
* @var \Magento\Cms\Model\Wysiwyg\Images\Storage
* @var Storage
*/
private $storage;

/**
* @var DriverInterface
*/
private $driver;

/**
* @inheritdoc
*/
// phpcs:disable
public static function setUpBeforeClass(): void
{
self::$_baseDir = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
self::$_baseDir = Bootstrap::getObjectManager()->get(
\Magento\Cms\Helper\Wysiwyg\Images::class
)->getCurrentPath() . 'MagentoCmsModelWysiwygImagesStorageTest';
if (!file_exists(self::$_baseDir)) {
Expand All @@ -60,8 +69,8 @@ public static function setUpBeforeClass(): void
// phpcs:ignore
public static function tearDownAfterClass(): void
{
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Framework\Filesystem\Driver\File::class
Bootstrap::getObjectManager()->create(
File::class
)->deleteDirectory(
self::$_baseDir
);
Expand All @@ -72,9 +81,10 @@ public static function tearDownAfterClass(): void
*/
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->filesystem = $this->objectManager->get(\Magento\Framework\Filesystem::class);
$this->storage = $this->objectManager->create(\Magento\Cms\Model\Wysiwyg\Images\Storage::class);
$this->objectManager = Bootstrap::getObjectManager();
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->storage = $this->objectManager->create(Storage::class);
$this->driver = Bootstrap::getObjectManager()->get(DriverInterface::class);
}

/**
Expand All @@ -83,17 +93,29 @@ protected function setUp(): void
*/
public function testGetFilesCollection(): void
{
\Magento\TestFramework\Helper\Bootstrap::getInstance()
Bootstrap::getInstance()
->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
$fileName = 'magento_image.jpg';
$imagePath = realpath(__DIR__ . '/../../../../Catalog/_files/' . $fileName);
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$modifiableFilePath = $mediaDirectory->getAbsolutePath($fileName);
$this->driver->copy(
$imagePath,
$modifiableFilePath
);
$collection = $this->storage->getFilesCollection(self::$_baseDir, 'media');
$this->assertInstanceOf(\Magento\Cms\Model\Wysiwyg\Images\Storage\Collection::class, $collection);
foreach ($collection as $item) {
$this->assertInstanceOf(\Magento\Framework\DataObject::class, $item);
$this->assertStringEndsWith('/1.swf', $item->getUrl());
$this->assertStringEndsWith('/' . $fileName, $item->getUrl());
$this->assertStringMatchesFormat(
'http://%s/static/%s/adminhtml/%s/%s/Magento_Cms/images/placeholder_thumbnail.jpg',
'http://%s/static/%s/adminhtml/%s/%s/Magento_Cms/images/magento_image.jpg',
$item->getThumbUrl()
);
$this->assertEquals(
'jpg',
$item->getMimeType()
);
return;
}
}
Expand Down Expand Up @@ -121,7 +143,7 @@ public function testDeleteDirectory(): void
$this->storage->createDirectory($dir, $path);
$this->assertFileExists($fullPath);
$this->storage->deleteDirectory($fullPath);
$this->assertFileNotExists($fullPath);
$this->assertFileDoesNotExist($fullPath);
}

/**
Expand All @@ -142,7 +164,7 @@ public function testDeleteDirectoryWithExcludedDirPath(): void
public function testUploadFile(): void
{
$fileName = 'magento_small_image.jpg';
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$fixtureDir = realpath(__DIR__ . '/../../../../Catalog/_files');
Expand Down Expand Up @@ -172,7 +194,7 @@ public function testUploadFileWithExcludedDirPath(): void
);

$fileName = 'magento_small_image.jpg';
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$fixtureDir = realpath(__DIR__ . '/../../../../Catalog/_files');
Expand Down Expand Up @@ -204,7 +226,7 @@ public function testUploadFileWithWrongExtension(string $fileName, string $fileT
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$this->expectExceptionMessage('File validation failed.');

$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$fixtureDir = realpath(__DIR__ . '/../../../_files');
Expand Down Expand Up @@ -251,7 +273,7 @@ public function testUploadFileWithWrongFile(): void
$this->expectExceptionMessage('File validation failed.');

$fileName = 'file.gif';
$tmpDirectory = $this->filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::SYS_TMP);
$tmpDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::SYS_TMP);
$filePath = $tmpDirectory->getAbsolutePath($fileName);
// phpcs:disable
$file = fopen($filePath, "wb");
Expand Down

0 comments on commit c09a261

Please sign in to comment.