Skip to content

Commit bc1315a

Browse files
authored
Merge branch '2.1-develop' into asi-1839
2 parents 605e5d7 + c2ce664 commit bc1315a

File tree

4 files changed

+322
-0
lines changed

4 files changed

+322
-0
lines changed
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+
7+
declare(strict_types=1);
8+
9+
namespace Magento\AdobeStockImage\Test\Integration\Model;
10+
11+
use Magento\Framework\Filesystem\Driver\Https;
12+
13+
/**
14+
* Class DriverMock represents overwritten methods
15+
*/
16+
class HttpsDriverMock extends Https
17+
{
18+
/**
19+
* Retrieve file contents from given path
20+
*
21+
* @param string $path
22+
* @param string|null $flags
23+
* @param resource|null $context
24+
* @return string
25+
*/
26+
public function fileGetContents($path, $flags = null, $context = null)
27+
{
28+
return file_get_contents($path, (bool)$flags, $context);
29+
}
30+
}
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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\AdobeStockImage\Test\Integration\Model;
10+
11+
use Magento\AdobeStockAssetApi\Api\AssetRepositoryInterface;
12+
use Magento\AdobeStockImageApi\Api\SaveImageInterface;
13+
use Magento\Framework\Api\AttributeValueFactory;
14+
use Magento\Framework\Api\Search\Document;
15+
use Magento\Framework\Api\SearchCriteriaBuilder;
16+
use Magento\Framework\App\Filesystem\DirectoryList;
17+
use Magento\Framework\Filesystem;
18+
use Magento\Framework\Filesystem\Driver\Https;
19+
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* Test client for communication to Adobe Stock API.
25+
*/
26+
class SaveImageTest extends TestCase
27+
{
28+
/**
29+
* @var Filesystem
30+
*/
31+
private $fileSystem;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp(): void
37+
{
38+
$this->fileSystem = Bootstrap::getObjectManager()->get(Filesystem::class);
39+
Bootstrap::getObjectManager()->configure([
40+
'preferences' => [
41+
Https::class => HttpsDriverMock::class
42+
]
43+
]);
44+
}
45+
46+
/**
47+
* Test with image.
48+
*
49+
* @param array $documentData
50+
* @param string $sourceFile
51+
* @param string $destinationPath
52+
* @return void
53+
* @dataProvider getSaveTestDataProvider
54+
*/
55+
public function testSave(array $documentData, string $sourceFile, string $destinationPath): void
56+
{
57+
$this->deleteImage($destinationPath);
58+
$document = $this->getDocument($documentData);
59+
$saveImage = Bootstrap::getObjectManager()->create(SaveImageInterface::class);
60+
$saveImage->execute(
61+
$document,
62+
$this->getImageFilePath($sourceFile),
63+
$destinationPath
64+
);
65+
$this->assertImageSavedToDirectory($destinationPath);
66+
$this->assertAssets($destinationPath, $documentData);
67+
$this->deleteImage($destinationPath);
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function getSaveTestDataProvider(): array
74+
{
75+
return [
76+
'image_save' => [
77+
'documentData' => [
78+
'id' => 1,
79+
'comp_url' => 'https://test.url/magento-logo.png',
80+
'width' => 110,
81+
'title' => 'test adobe image title',
82+
'content_type' => 'image/png',
83+
'height' => 210,
84+
'some_bool_param' => false,
85+
'some_nullable_param' => null,
86+
'extension_attributes' => [
87+
'title' => 'test adobe image title',
88+
'is_downloaded' => 0,
89+
'is_licensed_locally' => 0,
90+
'thumbnail_240_url' => 'https://test.url/magento-logo.png',
91+
'creator_id' => random_int(0, 2147483647),
92+
'creator_name' => 'Test',
93+
'path' => 'catalog/category/tmp.png',
94+
'content_type' => 'image/png',
95+
'category' => [
96+
'id' => random_int(0, 2147483647),
97+
'name' => 'Test'
98+
],
99+
]
100+
],
101+
'sourcePath' => 'magento-logo.png',
102+
'destinationPath' => 'catalog/category/adobe-stock-save-image-test.png',
103+
]
104+
];
105+
}
106+
107+
/**
108+
* Document for save.
109+
*
110+
* @param array $documentData
111+
* @return Document
112+
*/
113+
private function getDocument(array $documentData): Document
114+
{
115+
$document = new Document($documentData);
116+
$this->addAttributes($document, $documentData['extension_attributes']);
117+
return $document;
118+
}
119+
120+
/**
121+
* Check if image saved by destination path
122+
*
123+
* @param string $destinationPath
124+
* @return void
125+
*/
126+
private function assertImageSavedToDirectory(string $destinationPath): void
127+
{
128+
self::assertTrue(
129+
$this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->isExist($destinationPath),
130+
'File was not saved by destination'
131+
);
132+
}
133+
134+
/**
135+
* Assert saved assets data
136+
*
137+
* @param string $destinationPath
138+
* @param array $documentData
139+
* @return void
140+
*/
141+
private function assertAssets(string $destinationPath, array $documentData): void
142+
{
143+
$galleryAssets = Bootstrap::getObjectManager()->get(GetAssetsByPathsInterface::class);
144+
$mediaAssets = $galleryAssets->execute([$destinationPath]);
145+
self::assertCount(1, $mediaAssets, 'Wrong gallery assets count');
146+
self::assertEquals(
147+
$documentData['extension_attributes']['title'],
148+
$mediaAssets[0]->getTitle(),
149+
'Wrong gallery assets image title saved'
150+
);
151+
$criteriaBuilder = Bootstrap::getObjectManager()->get(SearchCriteriaBuilder::class);
152+
$searchCriteria = $criteriaBuilder
153+
->addFilter('media_gallery_id', $mediaAssets[0]->getId())
154+
->create();
155+
/** @var AssetRepositoryInterface $stockAssets */
156+
$stockAssets = Bootstrap::getObjectManager()->get(AssetRepositoryInterface::class);
157+
$items = $stockAssets->getList($searchCriteria)->getItems();
158+
self::assertNotEmpty(
159+
$items,
160+
'Image asset was not saved'
161+
);
162+
$item = reset($items);
163+
self::assertEquals(
164+
$documentData['extension_attributes']['creator_id'],
165+
$item->getCreatorId(),
166+
'Wrong stock asset creator id saved'
167+
);
168+
self::assertEquals(
169+
$documentData['extension_attributes']['category']['id'],
170+
$item->getCategoryId(),
171+
'Wrong stock asset category id saved'
172+
);
173+
}
174+
175+
/**
176+
* Add attributes to document
177+
*
178+
* @param Document $document
179+
* @param array $attributes [code => value]
180+
* @return Document
181+
*/
182+
private function addAttributes(Document $document, array $attributes): Document
183+
{
184+
$customAttributes = $document->getCustomAttributes();
185+
$valueFactory = Bootstrap::getObjectManager()->create(
186+
AttributeValueFactory::class
187+
);
188+
foreach ($attributes as $code => $value) {
189+
$attribute = $valueFactory->create();
190+
$attribute->setAttributeCode($code);
191+
$attribute->setValue($value);
192+
$customAttributes[$code] = $attribute;
193+
}
194+
195+
$document->setCustomAttributes($customAttributes);
196+
197+
return $document;
198+
}
199+
200+
/**
201+
* Return image file path
202+
*
203+
* @param string $filename
204+
* @return string
205+
*/
206+
private function getImageFilePath(string $filename): string
207+
{
208+
return implode(
209+
DIRECTORY_SEPARATOR,
210+
[
211+
dirname(__DIR__, 1),
212+
'_files',
213+
$filename
214+
]
215+
);
216+
}
217+
218+
/**
219+
* Delete test image if exists
220+
*
221+
* @param string $destinationPath
222+
* @return void
223+
*/
224+
private function deleteImage(string $destinationPath): void
225+
{
226+
$this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)->delete($destinationPath);
227+
}
228+
}
9.66 KB
Loading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminAdobeStockInsertRenditionImageFromGalleryFileSizeTest">
12+
<annotations>
13+
<features value="AdminAdobeStockInsertRenditionImageFromGalleryFileSizeTest"/>
14+
<useCaseId value="https://github.com/magento/adobe-stock-integration/issues/1806"/>
15+
<title value="Admin user should see correct image file size after rendition inserted from gallery"/>
16+
<testCaseId value="https://studio.cucumber.io/projects/131313/test-plan/folders/1507933/scenarios/5200023"/>
17+
<stories value="User inserts image rendition to the content from media gallery"/>
18+
<description value="Admin user should see correct image file size after rendition inserted from gallery"/>
19+
<severity value="AVERAGE"/>
20+
<group value="adobe_stock_media_gallery"/>
21+
</annotations>
22+
<before>
23+
<createData entity="SimpleSubCategory" stepKey="category"/>
24+
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
25+
<!-- Prepare configuration -->
26+
<actionGroup ref="AdminRenditionsSetImageSizeActionGroup" stepKey="prepareRenditionsConfig">
27+
<argument name="height" value="50"/>
28+
<argument name="width" value="50"/>
29+
</actionGroup>
30+
</before>
31+
<after>
32+
<!-- Restore configuration -->
33+
<actionGroup ref="AdminRenditionsSetImageSizeActionGroup" stepKey="restoreRenditionsConfig"/>
34+
<!-- Delete uploaded image -->
35+
<actionGroup ref="AdminOpenMediaGalleryFromCategoryImageUploaderActionGroup" stepKey="openMediaGalleryFromWysiwyg"/>
36+
<actionGroup ref="AdminEnhancedMediaGalleryImageDeleteActionGroup" stepKey="removeSavedPreview"/>
37+
<actionGroup ref="AdminAdobeStockMediaGalleryClearFiltersActionGroup" stepKey="clearFilters"/>
38+
<!-- Delete category -->
39+
<deleteData createDataKey="category" stepKey="deleteCategory"/>
40+
</after>
41+
42+
<!-- Open category page -->
43+
<actionGroup ref="AdminOpenCategoryGridPageActionGroup" stepKey="openCategoryPage"/>
44+
<actionGroup ref="AdminEditCategoryInGridPageActionGroup" stepKey="editCategoryItem">
45+
<argument name="categoryName" value="$category.name$"/>
46+
</actionGroup>
47+
48+
<!-- Add image to category from gallery -->
49+
<actionGroup ref="AdminOpenMediaGalleryFromCategoryImageUploaderActionGroup" stepKey="openMediaGallery"/>
50+
<actionGroup ref="AdminAdobeStockOpenFromEnhancedMediaGalleryActionGroup" stepKey="openAdobeStockPanel"/>
51+
<actionGroup ref="AdminSearchImagesOnModalActionGroup" stepKey="searchForUnlicensedImage">
52+
<argument name="query" value="{{AdobeStockUnlicensedImage.id}}"/>
53+
</actionGroup>
54+
<actionGroup ref="AdminAdobeStockExpandImagePreviewActionGroup" stepKey="expandImagePreview"/>
55+
<actionGroup ref="AdminAdobeStockSavePreviewActionGroup" stepKey="saveImagePreview"/>
56+
<actionGroup ref="AdminSaveAdobeStockImagePreviewActionGroup" stepKey="confirmSaveImagePreview"/>
57+
<actionGroup ref="AdminMediaGalleryClickAddSelectedActionGroup" stepKey="addSelectedSavedPreview"/>
58+
59+
<!-- Assert added image size -->
60+
<actionGroup ref="AdminAssertImageUploadFileSizeThanActionGroup" stepKey="assertSize">
61+
<argument name="fileSize" value="1 KB"/>
62+
</actionGroup>
63+
</test>
64+
</tests>

0 commit comments

Comments
 (0)