Example #1
0
 /**
  * @depends testRemoveContent
  */
 public function testAddCategory(ProductModel $product)
 {
     $categories = $product->getProductCategories();
     $this->assertEquals(1, count($categories));
     $defaultCategory = $categories->getFirst();
     $category = CategoryQuery::create()->filterById($defaultCategory->getCategoryId(), Criteria::NOT_IN)->addAscendingOrderByColumn('RAND()')->findOne();
     $event = new ProductAddCategoryEvent($product, $category->getId());
     $event->setDispatcher($this->getDispatcher());
     $action = new Product();
     $action->addCategory($event);
     $product->clearProductCategories();
     $newCategories = $product->getProductCategories();
     $this->assertEquals(2, count($newCategories));
     return array('product' => $product, 'category' => $category);
 }
Example #2
0
 public function addCategory(ProductAddCategoryEvent $event)
 {
     if (ProductCategoryQuery::create()->filterByProduct($event->getProduct())->filterByCategoryId($event->getCategoryId())->count() <= 0) {
         $productCategory = new ProductCategory();
         $productCategory->setProduct($event->getProduct())->setCategoryId($event->getCategoryId())->setDefaultCategory(false)->save();
     }
 }