/**
  * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  * @expectedExceptionMessage No such entity with id = 5
  */
 public function testDeleteByIdentifierWithException()
 {
     $categoryId = 5;
     $categoryMock = $this->getMock('\\Magento\\Catalog\\Model\\Category', [], [], '', false);
     $categoryMock->expects($this->once())->method('getId')->willReturn(null);
     $this->categoryFactoryMock->expects($this->once())->method('create')->willReturn($categoryMock);
     $categoryMock->expects($this->once())->method('load')->with($categoryId);
     $this->model->deleteByIdentifier($categoryId);
 }
 /**
  * {@inheritdoc}
  */
 public function deleteByIds($categoryId, $sku)
 {
     $category = $this->categoryRepository->get($categoryId);
     $product = $this->productRepository->get($sku);
     $productPositions = $category->getProductsPosition();
     $productID = $product->getId();
     if (!isset($productPositions[$productID])) {
         throw new InputException(__('Category does not contain specified product'));
     }
     $backupPosition = $productPositions[$productID];
     unset($productPositions[$productID]);
     $category->setPostedProducts($productPositions);
     try {
         $category->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save product "%product" with position %position to category %category', ["product" => $product->getId(), "position" => $backupPosition, "category" => $category->getId()]), $e);
     }
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function move($categoryId, $parentId, $afterId = null)
 {
     $model = $this->categoryRepository->get($categoryId);
     $parentCategory = $this->categoryRepository->get($parentId);
     if ($parentCategory->hasChildren()) {
         $parentChildren = $parentCategory->getChildren();
         $categoryIds = explode(',', $parentChildren);
         $lastId = array_pop($categoryIds);
         $afterId = $afterId === null || $afterId > $lastId ? $lastId : $afterId;
     }
     if (strpos($parentCategory->getPath(), $model->getPath()) === 0) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Operation do not allow to move a parent category to any of children category'));
     }
     try {
         $model->move($parentId, $afterId);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Could not move category'));
     }
     return true;
 }
Example #4
0
 /**
  * Retrieve product category
  *
  * @return \Magento\Catalog\Model\Category
  */
 public function getCategory()
 {
     $category = $this->getData('category');
     if ($category === null && $this->getCategoryId()) {
         $category = $this->categoryRepository->get($this->getCategoryId());
         $this->setCategory($category);
     }
     return $category;
 }