|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Magento\ProductVideo; |
| 5 | + |
| 6 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 7 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test for \Magento\ProductVideo feature |
| 11 | + * |
| 12 | + * @magentoAppIsolation enabled |
| 13 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 14 | + */ |
| 15 | +class ProductVideoExternalSourceTest extends WebapiAbstract |
| 16 | +{ |
| 17 | + const SERVICE_NAME = 'catalogProductRepositoryV1'; |
| 18 | + const SERVICE_VERSION = 'V1'; |
| 19 | + const RESOURCE_PATH = '/V1/products'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Media gallery entries with external videos |
| 23 | + * |
| 24 | + * @return array |
| 25 | + */ |
| 26 | + public function externalVideoDataProvider(): array |
| 27 | + { |
| 28 | + return [ |
| 29 | + 'youtube-external-video' => [ |
| 30 | + [ |
| 31 | + 'media_type' => 'external-video', |
| 32 | + 'disabled' => false, |
| 33 | + 'label' => 'Test Video Created', |
| 34 | + 'types' => [], |
| 35 | + 'position' => 1, |
| 36 | + 'content' => $this->getVideoThumbnailStub(), |
| 37 | + 'extension_attributes' => [ |
| 38 | + 'video_content' => [ |
| 39 | + 'media_type' => 'external-video', |
| 40 | + 'video_provider' => 'youtube', |
| 41 | + 'video_url' => 'https://www.youtube.com/', |
| 42 | + 'video_title' => 'Video title', |
| 43 | + 'video_description' => 'Video description', |
| 44 | + 'video_metadata' => 'Video meta', |
| 45 | + ], |
| 46 | + ], |
| 47 | + ] |
| 48 | + ], |
| 49 | + 'vimeo-external-video' => [ |
| 50 | + [ |
| 51 | + 'media_type' => 'external-video', |
| 52 | + 'disabled' => false, |
| 53 | + 'label' => 'Test Video Updated', |
| 54 | + 'types' => [], |
| 55 | + 'position' => 1, |
| 56 | + 'content' => $this->getVideoThumbnailStub(), |
| 57 | + 'extension_attributes' => [ |
| 58 | + 'video_content' => [ |
| 59 | + 'media_type' => 'external-video', |
| 60 | + 'video_provider' => 'vimeo', |
| 61 | + 'video_url' => 'https://www.vimeo.com/', |
| 62 | + 'video_title' => 'Video title', |
| 63 | + 'video_description' => 'Video description', |
| 64 | + 'video_metadata' => 'Video meta', |
| 65 | + ], |
| 66 | + ], |
| 67 | + ] |
| 68 | + ] |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the array of data for Video thumbnail |
| 74 | + * |
| 75 | + * @return array|string[] |
| 76 | + */ |
| 77 | + private function getVideoThumbnailStub(): array |
| 78 | + { |
| 79 | + return [ |
| 80 | + 'type' => 'image/png', |
| 81 | + 'name' => 'thumbnail.png', |
| 82 | + 'base64_encoded_data' => 'iVBORw0KGgoAAAANSUhEUgAAAP8AAADGCAMAAAAqo6adAAAAA1BMVEUAAP79f' |
| 83 | + . '+LBAAAASElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' |
| 84 | + . 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA+BsYAAAF7hZJ0AAAAAElFTkSuQmCC', |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Test create/ update product with external video media gallery entry |
| 90 | + * |
| 91 | + * @dataProvider externalVideoDataProvider |
| 92 | + * @param array $mediaGalleryData |
| 93 | + */ |
| 94 | + public function testCreateWithExternalVideo(array $mediaGalleryData) |
| 95 | + { |
| 96 | + $simpleProductBaseData = $this->getSimpleProductData( |
| 97 | + [ |
| 98 | + ProductInterface::NAME => 'Product With Ext. Video', |
| 99 | + ProductInterface::SKU => 'prod-with-ext-video' |
| 100 | + ] |
| 101 | + ); |
| 102 | + |
| 103 | + $simpleProductBaseData['media_gallery_entries'] = [$mediaGalleryData]; |
| 104 | + |
| 105 | + $response = $this->saveProduct($simpleProductBaseData); |
| 106 | + $this->assertEquals( |
| 107 | + $simpleProductBaseData['media_gallery_entries'][0]['extension_attributes'], |
| 108 | + $response["media_gallery_entries"][0]["extension_attributes"] |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get Simple Product Data |
| 114 | + * |
| 115 | + * @param array $productData |
| 116 | + * @return array |
| 117 | + */ |
| 118 | + protected function getSimpleProductData($productData = []) |
| 119 | + { |
| 120 | + return [ |
| 121 | + ProductInterface::SKU => isset($productData[ProductInterface::SKU]) |
| 122 | + ? $productData[ProductInterface::SKU] : uniqid('sku-', true), |
| 123 | + ProductInterface::NAME => isset($productData[ProductInterface::NAME]) |
| 124 | + ? $productData[ProductInterface::NAME] : uniqid('sku-', true), |
| 125 | + ProductInterface::VISIBILITY => 4, |
| 126 | + ProductInterface::TYPE_ID => 'simple', |
| 127 | + ProductInterface::PRICE => 3.62, |
| 128 | + ProductInterface::STATUS => 1, |
| 129 | + ProductInterface::ATTRIBUTE_SET_ID => 4, |
| 130 | + 'custom_attributes' => [ |
| 131 | + ['attribute_code' => 'cost', 'value' => ''], |
| 132 | + ['attribute_code' => 'description', 'value' => 'Description'], |
| 133 | + ] |
| 134 | + ]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Save Product |
| 139 | + * |
| 140 | + * @param $product |
| 141 | + * @param string|null $storeCode |
| 142 | + * @param string|null $token |
| 143 | + * @return mixed |
| 144 | + */ |
| 145 | + protected function saveProduct($product, $storeCode = null, ?string $token = null) |
| 146 | + { |
| 147 | + if (isset($product['custom_attributes'])) { |
| 148 | + foreach ($product['custom_attributes'] as &$attribute) { |
| 149 | + if ($attribute['attribute_code'] == 'category_ids' |
| 150 | + && !is_array($attribute['value']) |
| 151 | + ) { |
| 152 | + $attribute['value'] = [""]; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + $serviceInfo = [ |
| 157 | + 'rest' => [ |
| 158 | + 'resourcePath' => self::RESOURCE_PATH, |
| 159 | + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, |
| 160 | + ], |
| 161 | + 'soap' => [ |
| 162 | + 'service' => self::SERVICE_NAME, |
| 163 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 164 | + 'operation' => self::SERVICE_NAME . 'Save', |
| 165 | + ], |
| 166 | + ]; |
| 167 | + if ($token) { |
| 168 | + $serviceInfo['rest']['token'] = $serviceInfo['soap']['token'] = $token; |
| 169 | + } |
| 170 | + $requestData = ['product' => $product]; |
| 171 | + |
| 172 | + return $this->_webApiCall($serviceInfo, $requestData, null, $storeCode); |
| 173 | + } |
| 174 | +} |
0 commit comments