/** * {@inheritdoc} */ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'save'); if (!$pluginInfo) { return parent::save($product, $saveOptions); } else { return $this->___callPlugins('save', func_get_args(), $pluginInfo); } }
/** * @expectedException \Magento\Framework\Exception\CouldNotSaveException * @expectedExceptionMessage Invalid product data: error1,error2 */ public function testSaveInvalidProductException() { $this->resourceModelMock->expects($this->exactly(1))->method('getIdBySku')->will($this->returnValue(null)); $this->productFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->productMock)); $this->initializationHelperMock->expects($this->never())->method('initialize')->with($this->productMock); $this->resourceModelMock->expects($this->once())->method('validate')->with($this->productMock)->willReturn(['error1', 'error2']); $this->productMock->expects($this->never())->method('getId'); $this->extensibleDataObjectConverterMock->expects($this->once())->method('toNestedArray')->will($this->returnValue($this->productData)); $this->model->save($this->productMock); }
public function testSaveExistingWithMediaGalleryEntries() { //update one entry, delete one entry $newEntries = [['id' => 5, "label" => "new_label_text", 'file' => 'filename1', 'position' => 10, 'disabled' => false, 'types' => ['image', 'small_image']]]; $existingMediaGallery = ['images' => [['value_id' => 5, "label" => "label_text", 'file' => 'filename1', 'position' => 10, 'disabled' => true], ['value_id' => 6, 'file' => 'filename2']]]; $expectedResult = [['id' => 5, 'value_id' => 5, "label" => "new_label_text", 'file' => 'filename1', 'position' => 10, 'disabled' => false, 'types' => ['image', 'small_image']], ['value_id' => 6, 'file' => 'filename2', 'removed' => true]]; $this->setupProductMocksForSave(); //media gallery data $this->productData['media_gallery_entries'] = $newEntries; $this->extensibleDataObjectConverterMock->expects($this->once())->method('toNestedArray')->will($this->returnValue($this->productData)); $this->initializedProductMock->setData('media_gallery', $existingMediaGallery); $this->initializedProductMock->expects($this->any())->method('getMediaAttributes')->willReturn(["image" => "filename1", "small_image" => "filename2"]); $this->mediaGalleryProcessor->expects($this->once())->method('clearMediaAttribute')->with($this->initializedProductMock, ['image', 'small_image']); $this->mediaGalleryProcessor->expects($this->once())->method('setMediaAttribute')->with($this->initializedProductMock, ['image', 'small_image'], 'filename1'); $this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]); $this->model->save($this->productMock); $this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images')); }
public function testSaveExistingWithMediaGalleryEntries() { //update one entry, delete one entry $newEntries = [ [ 'id' => 5, "label" => "new_label_text", 'file' => 'filename1', 'position' => 10, 'disabled' => false, 'types' => ['image', 'small_image'], ], ]; $existingMediaGallery = [ 'images' => [ [ 'value_id' => 5, "label" => "label_text", 'file' => 'filename1', 'position' => 10, 'disabled' => true, ], [ 'value_id' => 6, //will be deleted 'file' => 'filename2', ], ], ]; $expectedResult = [ [ 'id' => 5, 'value_id' => 5, "label" => "new_label_text", 'file' => 'filename1', 'position' => 10, 'disabled' => false, 'types' => ['image', 'small_image'], ], [ 'value_id' => 6, //will be deleted 'file' => 'filename2', 'removed' => true, ], ]; $this->setupProductMocksForSave(); //media gallery data $this->productData['media_gallery_entries'] = $newEntries; $this->extensibleDataObjectConverterMock ->expects($this->once()) ->method('toNestedArray') ->will($this->returnValue($this->productData)); $this->initializedProductMock->setData('media_gallery', $existingMediaGallery); $this->initializedProductMock->expects($this->any()) ->method('getMediaAttributes') ->willReturn(["image" => "filename1", "small_image" => "filename2"]); //setup media attribute backend $galleryAttributeBackendMock = $this->getMockBuilder('\Magento\Catalog\Model\Product\Attribute\Backend\Media') ->disableOriginalConstructor()->getMock(); $galleryAttributeBackendMock->expects($this->once())->method('clearMediaAttribute') ->with($this->initializedProductMock, ['image', 'small_image']); $this->initializedProductMock->expects($this->once()) ->method('getGalleryAttributeBackend') ->willReturn($galleryAttributeBackendMock); $galleryAttributeBackendMock->expects($this->once()) ->method('setMediaAttribute') ->with($this->initializedProductMock, ['image', 'small_image'], 'filename1'); $this->model->save($this->productMock); $this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images')); }