/** * @dataProvider validateDataProvider * @param bool $value */ public function testValidate($value) { $attributeCode = 'attr_code'; $attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute', ['getAttributeCode', 'getIsRequired', 'isValueEmpty', 'getIsUnique', 'getEntityType', '__wakeup'], [], '', false); $attributeEntity = $this->getMock('\\Magento\\Framework\\Model\\ModelResource\\AbstractResourceAbstractEntity', ['checkAttributeUniqueValue']); $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $attribute->expects($this->any())->method('getIsRequired')->will($this->returnValue(true)); $attribute->expects($this->any())->method('isValueEmpty')->will($this->returnValue($value)); $attribute->expects($this->any())->method('getIsUnique')->will($this->returnValue(true)); $attribute->expects($this->any())->method('getEntityType')->will($this->returnValue($attributeEntity)); $attributeEntity->expects($this->any())->method('checkAttributeUniqueValue')->will($this->returnValue(true)); $this->model->setAttribute($attribute); $this->dataObject->setData(['attr_code' => 'attribute data']); $this->assertEquals(!$value, $this->model->validate($this->dataObject)); }
public function testAfterSaveDeleteFiles() { $storeId = 1; $storeIds = ['store_1' => 1, 'store_2' => 2]; $attributeCode = 'test_code'; $toDelete = [1]; $mediaPath = 'catalog/media'; $filePathToRemove = $mediaPath . '/file/path'; $attributeValue = ['images' => [['removed' => true, 'value_id' => 1, 'file' => 'file/path'], ['removed' => false, 'value_id' => 1, 'file' => 'file/path2']]]; $assignedImages = [['filepath' => 'path_to_image']]; $attributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute')->disableOriginalConstructor()->getMock(); $attributeMock->expects($this->once())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->dataObject->expects($this->once())->method('getIsDuplicate')->will($this->returnValue(false)); $this->dataObject->expects($this->once())->method('isLockedAttribute')->will($this->returnValue(false)); $this->dataObject->setData($attributeCode, $attributeValue); $this->dataObject->setId(1); $this->dataObject->setStoreId($storeId); $this->dataObject->setStoreIds($storeIds); $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getAssignedImages', '__wakeup'])->getMock(); $productMock->expects($this->any())->method('getAssignedImages')->will($this->returnValue($assignedImages)); $this->productFactory->expects($this->once())->method('create')->will($this->returnValue($productMock)); $this->resourceModel->expects($this->once())->method('deleteGallery')->with($toDelete); $this->mediaConfig->expects($this->once())->method('getBaseMediaPath')->will($this->returnValue($mediaPath)); $this->mediaDirectory->expects($this->once())->method('delete')->with($filePathToRemove); $this->model->setAttribute($attributeMock); $this->assertNull($this->model->afterSave($this->dataObject)); }
/** * @dataProvider beforeSaveDataProvider * @param array $value */ public function testBeforeSave($value) { $attributeCode = 'attr_code'; $attribute = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute', ['getAttributeCode', 'getIsRequired', 'isValueEmpty', 'getIsUnique', 'getEntityType', '__wakeup'], [], '', false); $mediaAttributes = ['image' => $attribute, 'small_image' => $attribute, 'thumbnail' => $attribute]; $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->dataObject->expects($this->any())->method('getIsDuplicate')->will($this->returnValue(false)); $this->model->setAttribute($attribute); $this->dataObject->setData(['attr_code' => ['images' => $value]]); $this->dataObject->expects($this->any())->method('getMediaAttributes')->will($this->returnValue($mediaAttributes)); $this->model->beforeSave($this->dataObject); foreach ($this->dataObject['attr_code']['images'] as $imageType => $imageData) { if (isset($imageData['new_file'])) { $value[$imageType]['file'] = $imageData['file']; $value[$imageType]['new_file'] = $imageData['new_file']; } $this->assertEquals($value[$imageType], $imageData); } }
protected function setUp() { $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product\\Attribute\\Backend\\Media'); $this->_model->setAttribute(\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Eav\\Model\\Config')->getAttribute('catalog_product', 'media_gallery')); }
/** * {@inheritdoc} */ public function setAttribute($attribute) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'setAttribute'); if (!$pluginInfo) { return parent::setAttribute($attribute); } else { return $this->___callPlugins('setAttribute', func_get_args(), $pluginInfo); } }