Skip to content

Commit

Permalink
Merge pull request #184 from magento-goinc/MAGETWO-45453
Browse files Browse the repository at this point in the history
[GoInc] BugsFixing
  • Loading branch information
Slabko,Michael(mslabko) authored and Slabko,Michael(mslabko) committed Nov 13, 2015
2 parents c044a2d + 1553d12 commit 5923c2b
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 172 deletions.
16 changes: 0 additions & 16 deletions app/code/Magento/Catalog/Api/Data/ProductInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ interface ProductInterface extends \Magento\Framework\Api\CustomAttributesDataIn

const UPDATED_AT = 'updated_at';

const STORE_ID = 'store_id';
/**#@-*/

/**
Expand Down Expand Up @@ -83,21 +82,6 @@ public function getName();
*/
public function setName($name);

/**
* Product store id
*
* @return int|null
*/
public function getStoreId();

/**
* Set product store id
*
* @param int $storeId
* @return $this
*/
public function setStoreId($storeId);

/**
* Product attribute set id
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,28 @@ interface ProductAttributeMediaGalleryManagementInterface
*
* @param string $sku
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
* @param int $storeId
* @return int gallery entry ID
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function create(
$sku,
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry,
$storeId = 0
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
);

/**
* Update gallery entry
*
* @param string $sku
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
* @param int $storeId
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function update(
$sku,
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry,
$storeId = 0
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
);

/**
Expand Down
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
*/
const CACHE_PRODUCT_CATEGORY_TAG = 'catalog_category_product';

/**
* Product Store Id
*/
const STORE_ID = 'store_id';

/**
* @var string
*/
Expand Down Expand Up @@ -316,7 +321,6 @@ class Product extends \Magento\Catalog\Model\AbstractModel implements
ProductInterface::TYPE_ID,
ProductInterface::CREATED_AT,
ProductInterface::UPDATED_AT,
ProductInterface::STORE_ID,
'media_gallery',
'tier_price',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
Expand All @@ -34,19 +29,16 @@ class GalleryManagement implements \Magento\Catalog\Api\ProductAttributeMediaGal
protected $contentValidator;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param ImageContentValidatorInterface $contentValidator
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
ImageContentValidatorInterface $contentValidator
) {
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->contentValidator = $contentValidator;
}

Expand All @@ -69,13 +61,8 @@ protected function getGalleryAttributeBackend(Product $product)
/**
* {@inheritdoc}
*/
public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry, $storeId = 0)
public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry)
{
try {
$this->storeManager->getStore($storeId);
} catch (\Exception $exception) {
throw new NoSuchEntityException(__('There is no store with provided ID.'));
}
/** @var $entry ProductAttributeMediaGalleryEntryInterface */
$entryContent = $entry->getContent();

Expand Down Expand Up @@ -114,13 +101,8 @@ public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry,
/**
* {@inheritdoc}
*/
public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry, $storeId = 0)
public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
{
try {
$this->storeManager->getStore($storeId);
} catch (\Exception $exception) {
throw new NoSuchEntityException(__('There is no store with provided ID.'));
}
$product = $this->productRepository->get($sku);
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
if ($existingMediaGalleryEntries == null) {
Expand All @@ -138,7 +120,6 @@ public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry,
throw new NoSuchEntityException(__('There is no image with provided ID.'));
}
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
$product->setStoreId($storeId);

try {
$this->productRepository->save($product);
Expand Down
6 changes: 1 addition & 5 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,6 @@ protected function initializeProductData(array $productData, $createNew)
$product = $this->productFactory->create();
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
} elseif (isset($productData['store_id'])
&& !empty($productData['store_id'])
&& $this->storeManager->getStore($productData['store_id'])
) {
$product->setWebsiteIds([$this->storeManager->getStore($productData['store_id'])->getWebsiteId()]);
}
} else {
unset($this->instances[$productData['sku']]);
Expand Down Expand Up @@ -582,6 +577,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
$productLinks = $product->getProductLinks();
}

$productDataArray['store_id'] = (int)$this->storeManager->getStore()->getId();
$product = $this->initializeProductData($productDataArray, empty($productId));

if (isset($productDataArray['options'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class GalleryManagementTest extends \PHPUnit_Framework_TestCase
*/
protected $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -48,7 +43,6 @@ class GalleryManagementTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->storeManagerMock = $this->getMock('\Magento\Store\Model\StoreManagerInterface');
$this->productRepositoryMock = $this->getMock('\Magento\Catalog\Api\ProductRepositoryInterface');
$this->contentValidatorMock = $this->getMock('\Magento\Framework\Api\ImageContentValidatorInterface');
$this->productMock = $this->getMock(
Expand All @@ -69,7 +63,6 @@ protected function setUp()
$this->mediaGalleryEntryMock =
$this->getMock('Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$this->model = new \Magento\Catalog\Model\Product\Gallery\GalleryManagement(
$this->storeManagerMock,
$this->productRepositoryMock,
$this->contentValidatorMock
);
Expand All @@ -78,17 +71,6 @@ protected function setUp()
->getMock();
}

/**
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
* @expectedExceptionMessage There is no store with provided ID.
*/
public function testCreateWithNoStoreException()
{
$this->storeManagerMock->expects($this->once())->method('getStore')
->willThrowException(new \Exception());
$this->model->create('sku', $this->mediaGalleryEntryMock);
}

/**
* @expectedException \Magento\Framework\Exception\InputException
* @expectedExceptionMessage The image content is not valid.
Expand All @@ -100,13 +82,10 @@ public function testCreateWithInvalidImageException()
->getMock();
$this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);

$storeId = 0;

$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
->willReturn(false);

$this->model->create("sku", $this->mediaGalleryEntryMock, $storeId);
$this->model->create("sku", $this->mediaGalleryEntryMock);
}

/**
Expand All @@ -120,20 +99,17 @@ public function testCreateWithCannotSaveException()
->disableOriginalConstructor()
->getMock();;
$this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);

$storeId = 0;
$this->productRepositoryMock->expects($this->once())
->method('get')
->with($productSku)
->willReturn($this->productMock);

$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
->willReturn(true);

$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
->willThrowException(new \Exception());
$this->model->create($productSku, $this->mediaGalleryEntryMock, $storeId);
$this->model->create($productSku, $this->mediaGalleryEntryMock);
}

public function testCreate()
Expand All @@ -144,8 +120,6 @@ public function testCreate()
);
$this->mediaGalleryEntryMock->expects($this->any())->method('getContent')->willReturn($entryContentMock);

$storeId = 0;

$this->productRepositoryMock->expects($this->once())
->method('get')
->with($productSku)
Expand All @@ -155,7 +129,6 @@ public function testCreate()
->with($this->productMock)
->willReturn($this->productMock);

$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
$this->contentValidatorMock->expects($this->once())->method('isValid')->with($entryContentMock)
->willReturn(true);

Expand All @@ -166,21 +139,7 @@ public function testCreate()
$this->productMock->expects($this->once())->method('setMediaGalleryEntries')
->with([$this->mediaGalleryEntryMock]);

$this->assertEquals(42, $this->model->create($productSku, $this->mediaGalleryEntryMock, $storeId));
}

/**
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
* @expectedExceptionMessage There is no store with provided ID.
*/
public function testUpdateWithNonExistingStore()
{
$productSku = 'testProduct';
$entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$storeId = 0;
$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId)
->willThrowException(new \Exception());
$this->model->update($productSku, $entryMock, $storeId);
$this->assertEquals(42, $this->model->create($productSku, $this->mediaGalleryEntryMock));
}

/**
Expand All @@ -191,17 +150,15 @@ public function testUpdateWithNonExistingImage()
{
$productSku = 'testProduct';
$entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$storeId = 0;
$entryId = 42;
$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
->willReturn($this->productMock);
$existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$existingEntryMock->expects($this->once())->method('getId')->willReturn(43);
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
->willReturn([$existingEntryMock]);
$entryMock->expects($this->once())->method('getId')->willReturn($entryId);
$this->model->update($productSku, $entryMock, $storeId);
$this->model->update($productSku, $entryMock);
}

/**
Expand All @@ -212,29 +169,24 @@ public function testUpdateWithCannotSaveException()
{
$productSku = 'testProduct';
$entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$storeId = 0;
$entryId = 42;
$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
->willReturn($this->productMock);
$existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$existingEntryMock->expects($this->once())->method('getId')->willReturn($entryId);
$this->productMock->expects($this->once())->method('getMediaGalleryEntries')
->willReturn([$existingEntryMock]);
$entryMock->expects($this->once())->method('getId')->willReturn($entryId);
$this->productMock->expects($this->once())->method('setStoreId')->with($storeId);
$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock)
->willThrowException(new \Exception());
$this->model->update($productSku, $entryMock, $storeId);
$this->model->update($productSku, $entryMock);
}

public function testUpdate()
{
$productSku = 'testProduct';
$entryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
$storeId = 0;
$entryId = 42;
$this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId);
$this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)
->willReturn($this->productMock);
$existingEntryMock = $this->getMock('\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface');
Expand All @@ -245,9 +197,8 @@ public function testUpdate()

$this->productMock->expects($this->once())->method('setMediaGalleryEntries')
->willReturn([$entryMock]);
$this->productMock->expects($this->once())->method('setStoreId')->with($storeId);
$this->productRepositoryMock->expects($this->once())->method('save')->with($this->productMock);
$this->assertTrue($this->model->update($productSku, $entryMock, $storeId));
$this->assertTrue($this->model->update($productSku, $entryMock));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setUp()
->getMock();
$this->productRepositoryMock = $this->getMock('Magento\Catalog\Api\ProductRepositoryInterface');
$this->productMock = $this->getMock('\Magento\Catalog\Api\Data\ProductInterface');
$this->savedProductMock = $this->getMock('\Magento\Catalog\Api\Data\ProductInterface');
$this->savedProductMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
$this->closureMock = function () {
return $this->savedProductMock;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected function setStoreToProduct(\Magento\Catalog\Model\Product $product, ar
&& ($storeId = $this->import->getStoreIdByCode($rowData[ImportProduct::COL_STORE]))
) {
$product->setStoreId($storeId);
} elseif (!$product->hasData(\Magento\Catalog\Api\Data\ProductInterface::STORE_ID)) {
} elseif (!$product->hasData(\Magento\Catalog\Model\Product::STORE_ID)) {
$product->setStoreId(Store::DEFAULT_STORE_ID);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function setUp()
$this->linkRepositoryMock = $this->getMock('Magento\Downloadable\Api\LinkRepositoryInterface');
$this->sampleRepositoryMock = $this->getMock('Magento\Downloadable\Api\SampleRepositoryInterface');
$this->productMock = $this->getMock('\Magento\Catalog\Api\Data\ProductInterface');
$this->savedProductMock = $this->getMock('\Magento\Catalog\Api\Data\ProductInterface');
$this->savedProductMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
$this->closureMock = function () {
return $this->savedProductMock;
};
Expand Down
Loading

0 comments on commit 5923c2b

Please sign in to comment.