Skip to content

Commit 79cc0e4

Browse files
committed
#1504: [2.1-develop] Insert rendition images to the content from media gallery instead of original images - Integration test coverage for GetAssetIdsByContentFieldTest for rendition.
1 parent 72ecf87 commit 79cc0e4

File tree

9 files changed

+400
-0
lines changed

9 files changed

+400
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Magento\MediaGalleryRenditions\Test\Integration\Model\ResourceModel;
10+
11+
use Magento\Framework\Exception\InvalidArgumentException;
12+
use Magento\MediaContentApi\Api\GetAssetIdsByContentFieldInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* GetAssetIdsByContentFieldTest for MediaGalleryRenditions
18+
*/
19+
class GetAssetIdsByContentFieldTest extends TestCase
20+
{
21+
private const STORE_FIELD = 'store_id';
22+
private const STATUS_FIELD = 'content_status';
23+
private const STATUS_ENABLED = '1';
24+
private const STATUS_DISABLED = '0';
25+
private const FIXTURE_ASSET_ID = 2020;
26+
private const DEFAULT_STORE_ID = '1';
27+
private const ADMIN_STORE_ID = '0';
28+
29+
/**
30+
* @var GetAssetIdsByContentFieldInterface
31+
*/
32+
private $getAssetIdsByContentField;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp(): void
38+
{
39+
$objectManager = Bootstrap::getObjectManager();
40+
$this->getAssetIdsByContentField = $objectManager->get(GetAssetIdsByContentFieldInterface::class);
41+
}
42+
43+
/**
44+
* Test for getting asset id by block field
45+
*
46+
* @dataProvider dataProvider
47+
* @magentoConfigFixture system/media_gallery/enabled 1
48+
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
49+
* @magentoDataFixture Magento_MediaGalleryRenditions::Test/Integration/_files/cms/block_with_asset.php
50+
*
51+
* @param string $field
52+
* @param string $value
53+
* @param array $expectedAssetIds
54+
* @throws InvalidArgumentException
55+
*/
56+
public function testBlockFields(string $field, string $value, array $expectedAssetIds): void
57+
{
58+
$this->assertEquals(
59+
$expectedAssetIds,
60+
$this->getAssetIdsByContentField->execute($field, $value)
61+
);
62+
}
63+
64+
/**
65+
* Test for getting asset id by page field
66+
*
67+
* @dataProvider pageDataProvider
68+
* @magentoConfigFixture system/media_gallery/enabled 1
69+
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
70+
* @magentoDataFixture Magento_MediaGalleryRenditions::Test/Integration/_files/cms/page_with_asset.php
71+
*
72+
* @param string $field
73+
* @param string $value
74+
* @param array $expectedAssetIds
75+
* @throws InvalidArgumentException
76+
*/
77+
public function testPageFields(string $field, string $value, array $expectedAssetIds): void
78+
{
79+
$this->assertEquals(
80+
$expectedAssetIds,
81+
$this->getAssetIdsByContentField->execute($field, $value)
82+
);
83+
}
84+
85+
/**
86+
* Test for getting asset id by category fields
87+
*
88+
* @dataProvider dataProvider
89+
* @magentoConfigFixture system/media_gallery/enabled 1
90+
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
91+
* @magentoDataFixture Magento_MediaGalleryRenditions::Test/Integration/_files/catalog/category_with_asset.php
92+
*
93+
* @param string $field
94+
* @param string $value
95+
* @param array $expectedAssetIds
96+
* @throws InvalidArgumentException
97+
*/
98+
public function testCategoryFields(string $field, string $value, array $expectedAssetIds): void
99+
{
100+
$this->assertEquals(
101+
$expectedAssetIds,
102+
$this->getAssetIdsByContentField->execute($field, $value)
103+
);
104+
}
105+
106+
/**
107+
* Test for getting asset id by product fields
108+
*
109+
* @dataProvider dataProvider
110+
* @magentoConfigFixture system/media_gallery/enabled 1
111+
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
112+
* @magentoDataFixture Magento_MediaGalleryRenditions::Test/Integration/_files/catalog/product_with_asset.php
113+
* @param string $field
114+
* @param string $value
115+
* @param array $expectedAssetIds
116+
* @throws InvalidArgumentException
117+
*/
118+
public function testProductFields(string $field, string $value, array $expectedAssetIds): void
119+
{
120+
$this->assertEquals(
121+
$expectedAssetIds,
122+
$this->getAssetIdsByContentField->execute($field, $value)
123+
);
124+
}
125+
126+
/**
127+
* Test for getting asset when media gallery disabled
128+
*
129+
* @magentoConfigFixture system/media_gallery/enabled 0
130+
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
131+
* @magentoDataFixture Magento_MediaGalleryRenditions::Test/Integration/_files/catalog/product_with_asset.php
132+
* @throws InvalidArgumentException
133+
*/
134+
public function testProductFieldsWithDisabledMediaGallery(): void
135+
{
136+
$this->assertEquals(
137+
[],
138+
$this->getAssetIdsByContentField->execute(self::STATUS_FIELD, self::STATUS_ENABLED)
139+
);
140+
}
141+
142+
/**
143+
* Data provider for block, category and product tests
144+
*
145+
* @return array
146+
*/
147+
public static function dataProvider(): array
148+
{
149+
return [
150+
[self::STATUS_FIELD, self::STATUS_ENABLED, [self::FIXTURE_ASSET_ID]],
151+
[self::STATUS_FIELD, self::STATUS_DISABLED, []],
152+
[self::STORE_FIELD, self::DEFAULT_STORE_ID, [self::FIXTURE_ASSET_ID]],
153+
];
154+
}
155+
156+
/**
157+
* Data provider for page tests
158+
*
159+
* @return array
160+
*/
161+
public static function pageDataProvider(): array
162+
{
163+
return [
164+
[self::STATUS_FIELD, self::STATUS_ENABLED, [self::FIXTURE_ASSET_ID]],
165+
[self::STATUS_FIELD, self::STATUS_DISABLED, []],
166+
[self::STORE_FIELD, self::ADMIN_STORE_ID, [self::FIXTURE_ASSET_ID]],
167+
];
168+
}
169+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Model\Category;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
/** @var Category $category */
11+
$category = Bootstrap::getObjectManager()->create(Category::class);
12+
$category->isObjectNew(true);
13+
$category->setId(
14+
28767
15+
)->setCreatedAt(
16+
'2014-06-23 09:50:07'
17+
)->setName(
18+
'Category 1'
19+
)->setDescription(
20+
'content {{media url=".renditions/testDirectory/path.jpg"}} content'
21+
)->setParentId(
22+
2
23+
)->setPath(
24+
'1/2/333'
25+
)->setLevel(
26+
2
27+
)->setAvailableSortBy(
28+
['position', 'name']
29+
)->setDefaultSortBy(
30+
'name'
31+
)->setIsActive(
32+
true
33+
)->setPosition(
34+
1
35+
)->save();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\Registry $registry */
8+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
9+
$registry->unregister('isSecureArea');
10+
$registry->register('isSecureArea', true);
11+
12+
/** @var $category \Magento\Catalog\Model\Category */
13+
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
14+
$category->load(28767);
15+
if ($category->getId()) {
16+
$category->delete();
17+
}
18+
19+
$registry->unregister('isSecureArea');
20+
$registry->register('isSecureArea', false);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
10+
use Magento\Catalog\Model\Product\Type;
11+
use Magento\Catalog\Model\Product\Visibility;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\ObjectManager;
14+
15+
/** @var ObjectManager $objectManager */
16+
$objectManager = Bootstrap::getObjectManager();
17+
18+
/** @var $product Product */
19+
$product = $objectManager->create(Product::class);
20+
$product->isObjectNew(true);
21+
$product->setTypeId(Type::TYPE_SIMPLE)
22+
->setId(1567)
23+
->setAttributeSetId(4)
24+
->setName('Simple Product')
25+
->setSku('simple_with_asset')
26+
->setPrice(10)
27+
->setWeight(1)
28+
->setShortDescription('content {{media url=".renditions/testDirectory/path.jpg"}} content')
29+
->setTaxClassId(0)
30+
->setDescription('content {{media url=".renditions/testDirectory/path.jpg"}} content')
31+
->setMetaTitle('meta title')
32+
->setMetaKeyword('meta keyword')
33+
->setMetaDescription('meta description')
34+
->setVisibility(Visibility::VISIBILITY_BOTH)
35+
->setStatus(Status::STATUS_ENABLED)
36+
->setStockData(
37+
[
38+
'use_config_manage_stock' => 1,
39+
'qty' => 100,
40+
'is_qty_decimal' => 0,
41+
'is_in_stock' => 1,
42+
]
43+
);
44+
45+
/** @var ProductRepositoryInterface $productRepository */
46+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
47+
$productRepository->save($product);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\Framework\Exception\NoSuchEntityException;
7+
8+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
9+
10+
/** @var \Magento\Framework\Registry $registry */
11+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
12+
13+
$registry->unregister('isSecureArea');
14+
$registry->register('isSecureArea', true);
15+
16+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
17+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
18+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
19+
try {
20+
$product = $productRepository->get('simple_with_asset', false, null, true);
21+
$productRepository->delete($product);
22+
} catch (NoSuchEntityException $e) {
23+
}
24+
$registry->unregister('isSecureArea');
25+
$registry->register('isSecureArea', false);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Cms\Model\Block;
8+
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
/** @var $block Block */
12+
$block = Bootstrap::getObjectManager()->create(Block::class);
13+
$block->setTitle(
14+
'CMS Block Title'
15+
)->setIdentifier(
16+
'fixture_block_with_asset'
17+
)->setContent(
18+
'content {{media url=".renditions/testDirectory/path.jpg"}} content'
19+
)->setIsActive(
20+
1
21+
)->setStores(
22+
[
23+
Bootstrap::getObjectManager()->get(StoreManagerInterface::class)->getStore()->getId()
24+
]
25+
)->save();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Cms\Api\BlockRepositoryInterface;
9+
use Magento\Cms\Api\Data\BlockInterface;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
$objectManager = Bootstrap::getObjectManager();
14+
15+
/** @var BlockRepositoryInterface $blockRepository */
16+
$blockRepository = $objectManager->get(BlockRepositoryInterface::class);
17+
18+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
19+
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
20+
$searchCriteria = $searchCriteriaBuilder->addFilter(BlockInterface::IDENTIFIER, 'fixture_block_with_asset')
21+
->create();
22+
$result = $blockRepository->getList($searchCriteria);
23+
24+
/**
25+
* Tests which are wrapped with MySQL transaction clear all data by transaction rollback.
26+
* In that case there is "if" which checks that "fixture_block_with_asset" still exists in database.
27+
*/
28+
foreach ($result->getItems() as $item) {
29+
$blockRepository->delete($item);
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $page \Magento\Cms\Model\Page */
8+
$page = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Cms\Model\Page::class);
9+
$page->setTitle('Cms Page 100')
10+
->setIdentifier('fixture_page_with_asset')
11+
->setStores([0])
12+
->setIsActive(1)
13+
->setContent('content {{media url=".renditions/testDirectory/path.jpg"}} content')
14+
->setContentHeading('<h2>Cms Page 100 Title</h2>')
15+
->setMetaTitle('Cms Meta title for page100')
16+
->setMetaKeywords('Cms Meta Keywords for page100')
17+
->setMetaDescription('Cms Meta Description for page100')
18+
->setPageLayout('1column')
19+
->save();

0 commit comments

Comments
 (0)