ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,151 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model;
use Magento\MediaGalleryApi\Api\Data\KeywordInterfaceFactory;
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Magento\MediaGalleryApi\Api\Data\AssetKeywordsInterfaceFactory;
use Magento\MediaGalleryApi\Api\Data\AssetKeywordsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsKeywordsInterface;
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGalleryApi\Api\SaveAssetsKeywordsInterface;
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* End to end test for working with assets and keywords
*/
class AssetEndToEndTest extends TestCase
{
/**
* @var GetAssetsKeywordsInterface
*/
private $saveAssetsKeywords;
/**
* @var GetAssetsKeywordsInterface
*/
private $getAssetsKeywords;
/**
* @var AssetKeywordsInterfaceFactory
*/
private $assetsKeywordsFactory;
/**
* @var AssetInterfaceFactory
*/
private $assetFactory;
/**
* @var KeywordInterfaceFactory
*/
private $keywordFactory;
/**
* @var SaveAssetsInterface
*/
private $saveAssets;
/**
* @var GetAssetsByIdsInterface
*/
private $getAssetsByIds;
/**
* @var GetAssetsByPathsInterface
*/
private $getAssetsByPath;
/**
* @var DeleteAssetsByPathsInterface
*/
private $deleteAssetsByPaths;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->saveAssetsKeywords = Bootstrap::getObjectManager()->get(SaveAssetsKeywordsInterface::class);
$this->getAssetsKeywords = Bootstrap::getObjectManager()->get(GetAssetsKeywordsInterface::class);
$this->assetsKeywordsFactory = Bootstrap::getObjectManager()->get(AssetKeywordsInterfaceFactory::class);
$this->assetFactory = Bootstrap::getObjectManager()->get(AssetInterfaceFactory::class);
$this->keywordFactory = Bootstrap::getObjectManager()->get(KeywordInterfaceFactory::class);
$this->saveAssets = Bootstrap::getObjectManager()->get(SaveAssetsInterface::class);
$this->getAssetsByIds = Bootstrap::getObjectManager()->get(GetAssetsByIdsInterface::class);
$this->getAssetsByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
$this->deleteAssetsByPaths = Bootstrap::getObjectManager()->get(DeleteAssetsByPathsInterface::class);
}
/**
* Testing assets keywords save and get
*/
public function testExecute(): void
{
$keyword1 = $this->keywordFactory->create(
[
'keyword' => 'pear'
]
);
$keyword2 = $this->keywordFactory->create(
[
'keyword' => 'plum'
]
);
$asset = $this->assetFactory->create(
[
'path' => 'fruit.jpg',
'title' => 'Img',
'source' => 'Local',
'contentType' => 'image/jpeg',
'width' => 420,
'height' => 240,
'size' => 12877
]
);
$this->saveAssets->execute([$asset]);
$loadedAssets = $this->getAssetsByPath->execute([$asset->getPath()]);
$loadedAsset = $loadedAssets[0];
$this->assertCount(1, $loadedAssets);
$assetKeywords = $this->assetsKeywordsFactory->create(
[
'assetId' => $loadedAsset->getId(),
'keywords' => [
$keyword1,
$keyword2
]
]
);
$this->saveAssetsKeywords->execute([$assetKeywords]);
$loadedAssetKeywords = $this->getAssetsKeywords->execute([$loadedAsset->getId()]);
$this->assertCount(1, $loadedAssetKeywords);
/** @var AssetKeywordsInterface $loadedAssetKeywords1 */
$loadedAssetKeywords1 = current($loadedAssetKeywords);
$loadedKeywords = $loadedAssetKeywords1->getKeywords();
$this->assertCount(2, $loadedKeywords);
foreach ($loadedKeywords as $theKeyword) {
$this->assertTrue(in_array($theKeyword->getKeyword(), ['pear', 'plum']));
}
$this->deleteAssetsByPaths->execute(['fruit.jpg']);
}
}
@@ -0,0 +1,192 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\Directory\Command;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Filesystem;
use Magento\MediaGalleryApi\Api\CreateDirectoriesByPathsInterface;
use Magento\MediaGalleryApi\Api\DeleteDirectoriesByPathsInterface;
use Magento\TestFramework\Helper\Bootstrap;
/**
* Test for CreateDirectoriesByPathsInterface
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreateByPathsTest extends \PHPUnit\Framework\TestCase
{
private const MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH
= 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders';
/**
* @var array
*/
private $origConfigValue;
/**
* Test directory name
*/
private const TEST_DIRECTORY_NAME = 'testDir/testCreateDirectory';
/**
* Absolute path to the media directory
*/
/**
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
*/
private $mediaDirectoryPath;
/**
* @var CreateDirectoriesByPathsInterface
*/
private $createByPaths;
/**
* @var DeleteDirectoriesByPathsInterface
*/
private $deleteByPaths;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
*/
private $mediaDirectory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->createByPaths = $this->objectManager->get(CreateDirectoriesByPathsInterface::class);
$this->deleteByPaths = $this->objectManager->get(DeleteDirectoriesByPathsInterface::class);
$this->mediaDirectory = $this->objectManager->get(Filesystem::class)
->getDirectoryWrite(DirectoryList::MEDIA);
$this->mediaDirectoryPath = $this->objectManager->get(Filesystem::class)
->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
$this->mediaDirectory->create(
$this->mediaDirectory->getRelativePath($this->mediaDirectoryPath . '/testDir')
);
$config = $this->objectManager->get(ScopeConfigInterface::class);
$this->origConfigValue = $config->getValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
'default'
);
$scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
$scopeConfig->setValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
array_merge($this->origConfigValue, ['testDir']),
);
}
protected function tearDown(): void
{
$this->mediaDirectory->delete(
$this->mediaDirectory->getRelativePath($this->mediaDirectoryPath . '/testDir')
);
$scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
$scopeConfig->setValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
$this->origConfigValue
);
}
/**
* @throws CouldNotSaveException
* @throws CouldNotDeleteException
*/
public function testCreateDirectory(): void
{
$this->createByPaths->execute([self::TEST_DIRECTORY_NAME]);
$this->assertTrue($this->mediaDirectory->isExist($this->mediaDirectoryPath . self::TEST_DIRECTORY_NAME));
$this->deleteByPaths->execute([self::TEST_DIRECTORY_NAME]);
$this->assertFalse($this->mediaDirectory->isExist($this->mediaDirectoryPath . self::TEST_DIRECTORY_NAME));
}
/**
* @param array $paths
* @throws CouldNotSaveException
* @dataProvider notAllowedPathsProvider
*/
public function testCreateDirectoryWithRelativePath(array $paths): void
{
$this->expectException(CouldNotSaveException::class);
$this->createByPaths->execute($paths);
}
/**
* Provider of paths that are not allowed for deletion
*
* @return array
*/
public function notAllowedPathsProvider(): array
{
return [
[
['../../pub/' . self::TEST_DIRECTORY_NAME]
],
[
['theme/' . self::TEST_DIRECTORY_NAME]
],
[
['../../pub/media', 'theme']
]
];
}
/**
* Test create child directory with the same name as parent
*/
public function testCreateChildDirectoryTheSameNameAsParentDirectory(): void
{
$dir = self::TEST_DIRECTORY_NAME;
$childPath = $dir . '/testCreateDirectory';
$this->createByPaths->execute([$dir]);
$this->assertTrue($this->mediaDirectory->isExist($this->mediaDirectoryPath . $dir));
$this->createByPaths->execute([$childPath]);
$this->assertTrue($this->mediaDirectory->isExist($this->mediaDirectoryPath . $childPath));
$this->deleteByPaths->execute([$dir]);
$this->assertFalse($this->mediaDirectory->isExist($this->mediaDirectoryPath . $dir));
}
/**
* @throws CouldNotSaveException
*/
public function testCreateDirectoryThatAlreadyExist(): void
{
$this->expectException(CouldNotSaveException::class);
$this->createByPaths->execute([self::TEST_DIRECTORY_NAME]);
$this->assertTrue($this->mediaDirectory->isExist($this->mediaDirectoryPath . self::TEST_DIRECTORY_NAME));
$this->createByPaths->execute([self::TEST_DIRECTORY_NAME]);
}
/**
* @throws \Magento\Framework\Exception\FileSystemException
*/
public static function tearDownAfterClass(): void
{
$filesystem = Bootstrap::getObjectManager()
->get(\Magento\Framework\Filesystem::class);
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $directory */
$directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
if ($directory->isExist(self::TEST_DIRECTORY_NAME)) {
$directory->delete(self::TEST_DIRECTORY_NAME);
}
}
}
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\Directory\Command;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\MediaGalleryApi\Api\DeleteDirectoriesByPathsInterface;
use Magento\TestFramework\Helper\Bootstrap;
/**
* Test for DeleteDirectoriesByPathsInterface
*/
class DeleteByPathsTest extends \PHPUnit\Framework\TestCase
{
private const MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH
= 'system/media_storage_configuration/allowed_resources/media_gallery_image_folders';
/**
* @var array
*/
private $origConfigValue;
/**
* @var DeleteDirectoriesByPathsInterface
*/
private $deleteByPaths;
/**
* @var string
*/
private $testDirectoryName = 'testDeleteDirectory';
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->deleteByPaths = $this->objectManager->get(DeleteDirectoriesByPathsInterface::class);
$this->filesystem = $this->objectManager->get(Filesystem::class);
$config = $this->objectManager->get(ScopeConfigInterface::class);
$this->origConfigValue = $config->getValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
'default'
);
$scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
$scopeConfig->setValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
array_merge($this->origConfigValue, [$this->testDirectoryName]),
);
}
/**
* @throws \Magento\Framework\Exception\CouldNotDeleteException
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function testDeleteDirectory(): void
{
$testDir = $this->testDirectoryName . '/testDir';
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$mediaDirectory->create($this->testDirectoryName);
$mediaDirectory->create($testDir);
$fullPath = $mediaDirectory->getAbsolutePath($testDir);
$this->assertTrue($mediaDirectory->isExist($fullPath));
$this->deleteByPaths->execute([$testDir]);
$this->assertFalse($mediaDirectory->isExist($fullPath));
}
/**
* @param array $paths
* @throws \Magento\Framework\Exception\CouldNotDeleteException
* @dataProvider notAllowedPathsProvider
*/
public function testDeleteDirectoryThatIsNotAllowed(array $paths): void
{
$this->expectException(\Magento\Framework\Exception\CouldNotDeleteException::class);
$this->deleteByPaths->execute($paths);
}
/**
* Provider of paths that are not allowed for deletion
*
* @return array
*/
public function notAllowedPathsProvider(): array
{
return [
[
['../../pub/media']
],
[
['theme']
],
[
['../../pub/media', 'theme']
]
];
}
/**
* @throws \Magento\Framework\Exception\FileSystemException
*/
protected function tearDown(): void
{
$scopeConfig = $this->objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
$scopeConfig->setValue(
self::MEDIA_GALLERY_IMAGE_FOLDERS_CONFIG_PATH,
$this->origConfigValue
);
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
if ($directory->isExist($this->testDirectoryName)) {
$directory->delete($this->testDirectoryName);
}
}
}
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\Directory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for @see \Magento\MediaGallery\Model\Directory\IsExcluded.
*/
class IsExcludedTest extends TestCase
{
/**
* @var IsExcluded
*/
private $model;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->model = Bootstrap::getObjectManager()->get(IsExcluded::class);
}
/**
* @dataProvider directoriesDataProvider
* @param string $path
* @param bool $expectedResult
* @return void
*/
public function testIsExcluded(string $path, bool $expectedResult): void
{
$actualResult = $this->model->execute($path);
$this->assertEquals($expectedResult, $actualResult);
}
/**
* @return array
*/
public function directoriesDataProvider(): array
{
return [
[
'catalog',
true
],
[
'catalog/category',
false
],
[
'customer',
true
],
[
'catalog/../customer',
true
],
];
}
}
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model;
use Magento\MediaGalleryApi\Api\IsPathExcludedInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for IsPathExcludedInterface
*/
class IsExcludedTest extends TestCase
{
/**
* @var IsPathExcludedInterface
*/
private $service;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->service = Bootstrap::getObjectManager()->get(IsPathExcludedInterface::class);
}
/**
* Testing the excluded paths
*
* @param string $path
* @param bool $isExcluded
* @dataProvider pathsProvider
*/
public function testExecute(string $path, bool $isExcluded): void
{
$this->assertEquals($isExcluded, $this->service->execute($path));
}
/**
* Provider of paths and if the path should be in the excluded list
*
* @return array
*/
public function pathsProvider(): array
{
return [
['theme', true],
['.thumbs', true],
['catalog/product/somedir', true],
['catalog/category', false]
];
}
}
@@ -0,0 +1,165 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\ResourceModel;
use Behat\Gherkin\Keywords\KeywordsInterface;
use Magento\MediaGalleryApi\Api\Data\AssetKeywordsInterface;
use Magento\MediaGalleryApi\Api\Data\AssetKeywordsInterfaceFactory;
use Magento\MediaGalleryApi\Api\Data\KeywordInterfaceFactory;
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsKeywordsInterface;
use Magento\MediaGalleryApi\Api\SaveAssetsKeywordsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Testing assets keywords operation
*/
class AssetKeywordsTest extends TestCase
{
private const FIXTURE_ASSET_PATH = 'testDirectory/path.jpg';
/**
* @var GetAssetsKeywordsInterface
*/
private $saveAssetsKeywords;
/**
* @var GetAssetsKeywordsInterface
*/
private $getAssetsKeywords;
/**
* @var AssetKeywordsInterfaceFactory
*/
private $assetsKeywordsFactory;
/**
* @var KeywordInterfaceFactory
*/
private $keywordFactory;
/**
* @var GetAssetsByPathsInterface
*/
private $getAssetsByPath;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->saveAssetsKeywords = Bootstrap::getObjectManager()->get(SaveAssetsKeywordsInterface::class);
$this->getAssetsKeywords = Bootstrap::getObjectManager()->get(GetAssetsKeywordsInterface::class);
$this->assetsKeywordsFactory = Bootstrap::getObjectManager()->get(AssetKeywordsInterfaceFactory::class);
$this->keywordFactory = Bootstrap::getObjectManager()->get(KeywordInterfaceFactory::class);
$this->getAssetsByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
}
/**
* Testing assets keywords save and get
*
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
* @dataProvider keywordsProvider
* @param string[] $keywords
* @param string[] $updatedKeywords
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function testSaveAndGetKeywords(array $keywords, array $updatedKeywords): void
{
$loadedAssets = $this->getAssetsByPath->execute([self::FIXTURE_ASSET_PATH]);
$this->assertCount(1, $loadedAssets);
$loadedAsset = current($loadedAssets);
$this->updateAssetKeywords($loadedAsset->getId(), $keywords);
$this->updateAssetKeywords($loadedAsset->getId(), $updatedKeywords);
}
/**
* Update Asset keywords
*
* @param int $assetId
* @param string[] $keywords
*/
private function updateAssetKeywords(int $assetId, array $keywords): void
{
$assetKeywords = $this->assetsKeywordsFactory->create(
[
'assetId' => $assetId,
'keywords' => $this->getKeywords($keywords)
]
);
$this->saveAssetsKeywords->execute([$assetKeywords]);
$loadedAssetKeywords = $this->getAssetsKeywords->execute([$assetId]);
if (empty($keywords)) {
$this->assertEmpty($loadedAssetKeywords);
return;
}
$this->assertCount(1, $loadedAssetKeywords);
/** @var AssetKeywordsInterface $loadedAssetKeyword */
$loadedAssetKeyword = current($loadedAssetKeywords);
$loadedKeywords = $loadedAssetKeyword->getKeywords();
$this->assertEquals(count($keywords), count($loadedKeywords));
$loadedKeywordStrings = [];
foreach ($loadedKeywords as $loadedKeywordObject) {
$loadedKeywordStrings[] = $loadedKeywordObject->getKeyword();
}
sort($loadedKeywordStrings);
sort($keywords);
$this->assertEquals($keywords, $loadedKeywordStrings);
}
/**
* Data provider of paths matching existing asset
*
* @return array
*/
public function keywordsProvider(): array
{
return [
[['one-keyword'],['plum','orange']],
[['кириллица'],[]],
[[],['plum']],
[['plum', 'pear'],['plum','pear']],
[['plum', 'pear'],['plum','orange']],
[['plum', 'pear','grape'],['plum','orange']],
[['plum', 'pear','grape'],['mango']],
[['plum', 'pear','grape'],['orange']],
[['plum', 'pear','grape'],[]],
[['plum', 'pear'],['plum', 'pear','grape','mango','orange']],
[[],[]]
];
}
/**
* Create keywords
*
* @param string[] $keywords
* @return KeywordsInterface[]
*/
private function getKeywords(array $keywords): array
{
$keywordObjects = [];
foreach ($keywords as $keyword) {
$keywordObjects[] = $this->keywordFactory->create(
[
'keyword' => $keyword
]
);
}
return $keywordObjects;
}
}
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\ResourceModel;
use Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for GetAssetsByIdsInterface
*/
class AssetsByIdsTest extends TestCase
{
private const FIXTURE_ASSET_ID = 2020;
private const FIXTURE_ASSET_PATH = 'testDirectory/path.jpg';
/**
* @var GetAssetsByIdsInterface
*/
private $getAssetsByIds;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->getAssetsByIds = Bootstrap::getObjectManager()->get(GetAssetsByIdsInterface::class);
}
/**
* Testing assets keywords save and get
*
* @throws \Magento\Framework\Exception\LocalizedException
*
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
*/
public function testExecute(): void
{
$assets = $this->getAssetsByIds->execute([self::FIXTURE_ASSET_ID]);
$this->assertCount(1, $assets);
$this->assertEquals($assets[0]->getPath(), self::FIXTURE_ASSET_PATH);
}
}
@@ -0,0 +1,188 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\ResourceModel;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
use Magento\MediaGalleryApi\Api\GetAssetsByIdsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for assets operations
*/
class AssetsTest extends TestCase
{
/**
* @var AssetInterfaceFactory
*/
private $assetFactory;
/**
* @var SaveAssetsInterface
*/
private $saveAssets;
/**
* @var GetAssetsByIdsInterface
*/
private $getAssetsByIds;
/**
* @var GetAssetsByPathsInterface
*/
private $getAssetsByPath;
/**
* @var DeleteAssetsByPathsInterface
*/
private $deleteAssetsByPaths;
/**
* @var DataObjectProcessor
*/
private $dataObjectProcessor;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->assetFactory = Bootstrap::getObjectManager()->get(AssetInterfaceFactory::class);
$this->saveAssets = Bootstrap::getObjectManager()->get(SaveAssetsInterface::class);
$this->getAssetsByIds = Bootstrap::getObjectManager()->get(GetAssetsByIdsInterface::class);
$this->getAssetsByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
$this->deleteAssetsByPaths = Bootstrap::getObjectManager()->get(DeleteAssetsByPathsInterface::class);
$this->dataObjectProcessor = Bootstrap::getObjectManager()->get(DataObjectProcessor::class);
}
/**
* Testing assets keywords save and get
*
* @param array $assetsData
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\LocalizedException
*
* @dataProvider assetsDataProvider
*/
public function testExecute(array $assetsData): void
{
$this->saveAssets->execute($this->getAssets($assetsData));
$paths = $this->getKeyValues($assetsData, 'path');
$loadedAssets = $this->getAssetsByPath->execute($paths);
$loadedPaths = $this->getFieldValues($loadedAssets, 'path');
$this->assertEquals(count($assetsData), count($loadedAssets));
sort($paths);
sort($loadedPaths);
$this->assertEquals($paths, $loadedPaths);
$this->deleteAssetsByPaths->execute($paths);
$this->assertEmpty($this->getAssetsByPath->execute($paths));
}
/**
* Data provider for testExecute
*
* @return array
*/
public function assetsDataProvider(): array
{
return [
'One asset' => [
'assetsData' => [
'asset1' => [
'path' => 'fruit.jpg',
'title' => 'Img',
'source' => 'Local',
'contentType' => 'image/jpeg',
'width' => 420,
'height' => 240,
'size' => 12877
]
]
],
'Two assets' => [
'assetsData' => [
'asset1' => [
'path' => 'image.jpg',
'title' => 'Img',
'source' => 'Local',
'contentType' => 'image/jpeg',
'width' => 420,
'height' => 240,
'size' => 12877
],
'asset2' => [
'path' => 'image2.jpg',
'title' => 'Img',
'source' => 'Local',
'contentType' => 'image/jpeg',
'width' => 420,
'height' => 240,
'size' => 12877
]
]
],
];
}
/**
* Create assets
*
* @param array $assetsData
* @return AssetInterface[]
*/
private function getAssets(array $assetsData): array
{
$assets = [];
foreach ($assetsData as $assetData) {
$assets[] = $this->assetFactory->create($assetData);
}
return $assets;
}
/**
* Get field values from assets
*
* @param AssetInterface[] $assets
* @param string $fieldName
* @return string[]
*/
private function getFieldValues(array $assets, string $fieldName): array
{
$values = [];
foreach ($assets as $asset) {
$data = $this->dataObjectProcessor->buildOutputDataArray($asset, AssetInterface::class);
$values[] = $data[$fieldName];
}
return $values;
}
/**
* Get key values from assets data array
*
* @param array $assetsData
* @param string $key
* @return string[]
*/
private function getKeyValues(array $assetsData, string $key): array
{
$values = [];
foreach ($assetsData as $assetData) {
$values[] = $assetData[$key];
}
return $values;
}
}
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model\ResourceModel;
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Testing delete assets operation
*/
class DeleteAssetsTest extends TestCase
{
private const FIXTURE_ASSET_PATH = 'testDirectory/path.jpg';
/**
* @var GetAssetsByPathsInterface
*/
private $getAssetsByPath;
/**
* @var DeleteAssetsByPathsInterface
*/
private $deleteAssetsByPaths;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->getAssetsByPath = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
$this->deleteAssetsByPaths = Bootstrap::getObjectManager()->get(DeleteAssetsByPathsInterface::class);
}
/**
* Test deletion of assets by path
*
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
*
* @param array $paths
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\LocalizedException
*
* @dataProvider matchingPathsProvider
*/
public function testAssetsAreDeleted(array $paths): void
{
$this->deleteAssetsByPaths->execute($paths);
$this->assertEmpty($this->getAssetsByPath->execute([self::FIXTURE_ASSET_PATH]));
}
/**
* Test scenarios where delete operation should not delete an asset
*
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
*
* @param array $paths
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\LocalizedException
*
* @dataProvider notMatchingPathsProvider
*/
public function testAssetsAreNotDeleted(array $paths): void
{
$this->deleteAssetsByPaths->execute($paths);
$this->assertNotEmpty($this->getAssetsByPath->execute([self::FIXTURE_ASSET_PATH]));
}
/**
* Data provider of paths matching existing asset
*
* @return array
*/
public function matchingPathsProvider(): array
{
return [
[['testDirectory/path.jpg']],
[['testDirectory/']],
[['testDirectory']]
];
}
/**
* Data provider of paths not matching existing asset
*
* @return array
*/
public function notMatchingPathsProvider(): array
{
return [
[['testDirectory/path.png']],
[['anotherDirectory/path.jpg']],
[['path.jpg']]
];
}
}
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MediaGallery\Model;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\MediaGalleryApi\Api\SearchAssetsInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Verify SearchAssets By searchCriteria
*/
class SearchAssetsTest extends TestCase
{
private const FIXTURE_ASSET_PATH = 'testDirectory/path.jpg';
/**
* @var SearchAssetsInterfcae
*/
private $searchAssets;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->filterBuilder = Bootstrap::getObjectManager()->get(FilterBuilder::class);
$this->filterGroupBuilder = Bootstrap::getObjectManager()->get(FilterGroupBuilder::class);
$this->searchCriteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
$this->searchAssets = Bootstrap::getObjectManager()->get(SearchAssetsInterface::class);
}
/**
* Verify search asstes by searching with search criteria
*
* @dataProvider searchCriteriaProvider
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
*/
public function testExecute(array $searchCriteriaData): void
{
$titleFilter = $this->filterBuilder->setField($searchCriteriaData['field'])
->setConditionType($searchCriteriaData['conditionType'])
->setValue($searchCriteriaData['value'])
->create();
$searchCriteria = $this->searchCriteriaBuilder
->setFilterGroups([$this->filterGroupBuilder->setFilters([$titleFilter])->create()])
->create();
$assets = $this->searchAssets->execute($searchCriteria);
$this->assertCount(1, $assets);
$this->assertEquals($assets[0]->getPath(), self::FIXTURE_ASSET_PATH);
}
/**
* Search criteria params provider
*
* @return array
*/
public function searchCriteriaProvider(): array
{
return [
[
['field' => 'id', 'conditionType' => 'eq', 'value' => 2020],
],
[
['field' => 'title', 'conditionType' => 'fulltext', 'value' => 'Img'],
],
[
['field' => 'content_type', 'conditionType' => 'eq', 'value' => 'image']
],
[
['field' => 'description', 'conditionType' => 'fulltext', 'value' => 'description']
]
];
}
}