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,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']]
];
}
}