|
10 | 10 |
|
11 | 11 | use Magento\Catalog\Test\Fixture\Product as ProductFixture;
|
12 | 12 | use Magento\Framework\Api\Data\ImageContentInterface;
|
| 13 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 14 | +use Magento\Framework\Filesystem; |
| 15 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
13 | 16 | use Magento\Store\Test\Fixture\Store as StoreFixture;
|
14 | 17 | use Magento\TestFramework\Fixture\DataFixture;
|
15 | 18 | use Magento\TestFramework\Fixture\DataFixtureStorage;
|
@@ -69,6 +72,11 @@ class ProductAttributeMediaGalleryManagementInterfaceTest extends WebapiAbstract
|
69 | 72 | */
|
70 | 73 | private $fixtures;
|
71 | 74 |
|
| 75 | + /** |
| 76 | + * @var WriteInterface |
| 77 | + */ |
| 78 | + private $mediaDirectory; |
| 79 | + |
72 | 80 | /**
|
73 | 81 | * @inheritDoc
|
74 | 82 | */
|
@@ -113,6 +121,8 @@ protected function setUp(): void
|
113 | 121 |
|
114 | 122 | $this->testImagePath = __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test_image.jpg';
|
115 | 123 | $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
|
| 124 | + $this->mediaDirectory = $this->objectManager->get(Filesystem::class) |
| 125 | + ->getDirectoryWrite(DirectoryList::MEDIA); |
116 | 126 | }
|
117 | 127 |
|
118 | 128 | /**
|
@@ -895,4 +905,44 @@ public function testContentAttributeInGet(): void
|
895 | 905 | $response = $this->_webApiCall($serviceInfo, $requestData);
|
896 | 906 | $this->assertArrayHasKey('content', $response);
|
897 | 907 | }
|
| 908 | + |
| 909 | + /** |
| 910 | + * Test update() method when existing image gets overwritten |
| 911 | + * |
| 912 | + * @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php |
| 913 | + */ |
| 914 | + public function testUpdateExistingImage() |
| 915 | + { |
| 916 | + $productRepository = $this->objectManager->create(ProductRepositoryInterface::class); |
| 917 | + /** @var \Magento\Catalog\Api\Data\ProductInterface $product */ |
| 918 | + $product = $productRepository->get('simple'); |
| 919 | + $imageId = (int)$product->getMediaGalleryImages()->getFirstItem()->getValueId(); |
| 920 | + $originalImagePath = $product->getMediaGalleryImages()->getFirstItem()->getFile(); |
| 921 | + $requestData = [ |
| 922 | + 'sku' => 'simple', |
| 923 | + 'entry' => [ |
| 924 | + 'id' => $this->getTargetGalleryEntryId(), |
| 925 | + 'label' => 'Updated Image Text', |
| 926 | + 'position' => 10, |
| 927 | + 'types' => ['thumbnail'], |
| 928 | + 'disabled' => true, |
| 929 | + 'media_type' => 'image', |
| 930 | + 'content' => [ |
| 931 | + 'base64_encoded_data' => base64_encode(file_get_contents($this->testImagePath)), |
| 932 | + 'type' => 'image/jpeg', |
| 933 | + 'name' => 'test_image.jpg', |
| 934 | + ] |
| 935 | + ] |
| 936 | + ]; |
| 937 | + |
| 938 | + $this->updateServiceInfo['rest']['resourcePath'] = $this->updateServiceInfo['rest']['resourcePath'] |
| 939 | + . '/' . $this->getTargetGalleryEntryId(); |
| 940 | + |
| 941 | + $this->assertTrue($this->_webApiCall($this->updateServiceInfo, $requestData, null, 'all')); |
| 942 | + $updatedImage = $this->assertMediaGalleryData($imageId, '/t/e/test_image.jpg', 'Updated Image Text'); |
| 943 | + $this->assertEquals(10, $updatedImage['position_default']); |
| 944 | + $this->assertEquals(1, $updatedImage['disabled_default']); |
| 945 | + $this->assertStringStartsWith('/t/e/test_image.jpg', $updatedImage['file']); |
| 946 | + $this->assertFalse($this->mediaDirectory->isExist($originalImagePath)); |
| 947 | + } |
898 | 948 | }
|
0 commit comments