function it_normalizes_an_existing_product_into_mongodb_document($mongoFactory, $serializer, ProductInterface $product, \MongoId $mongoId, \MongoDate $mongoDate, Association $assoc1, Association $assoc2, CategoryInterface $category1, CategoryInterface $category2, GroupInterface $group1, GroupInterface $group2, ProductValueInterface $value1, ProductValueInterface $value2, FamilyInterface $family)
 {
     $mongoFactory->createMongoId('product1')->willReturn($mongoId);
     $mongoFactory->createMongoDate()->willReturn($mongoDate);
     $family->getId()->willReturn(36);
     $category1->getId()->willReturn(12);
     $category2->getId()->willReturn(34);
     $group1->getId()->willReturn(56);
     $group2->getId()->willReturn(78);
     $product->getId()->willReturn('product1');
     $product->getCreated()->willReturn(null);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroups()->willReturn([$group1, $group2]);
     $product->getCategories()->willReturn([$category1, $category2]);
     $product->getAssociations()->willReturn([$assoc1, $assoc2]);
     $product->getValues()->willReturn([$value1, $value2]);
     $context = ['_id' => $mongoId];
     $serializer->normalize($product, 'mongodb_json')->willReturn(['data' => 'data', 'completenesses' => 'completenesses']);
     $serializer->normalize($value1, 'mongodb_document', $context)->willReturn('my_value_1');
     $serializer->normalize($value2, 'mongodb_document', $context)->willReturn('my_value_2');
     $serializer->normalize($assoc1, 'mongodb_document', $context)->willReturn('my_assoc_1');
     $serializer->normalize($assoc2, 'mongodb_document', $context)->willReturn('my_assoc_2');
     $this->normalize($product, 'mongodb_document')->shouldReturn(['_id' => $mongoId, 'created' => $mongoDate, 'updated' => $mongoDate, 'family' => 36, 'enabled' => true, 'groupIds' => [56, 78], 'categoryIds' => [12, 34], 'associations' => ['my_assoc_1', 'my_assoc_2'], 'values' => ['my_value_1', 'my_value_2'], 'normalizedData' => ['data' => 'data'], 'completenesses' => []]);
 }
 function it_applies_a_filter_by_unclassified_products($utility, $categoryRepo, FilterDatasourceAdapterInterface $datasource, CategoryInterface $tree)
 {
     $tree->getId()->willReturn(1);
     $categoryRepo->find(1)->willReturn($tree);
     $categoryRepo->getAllChildrenIds($tree)->willReturn([2, 3]);
     $utility->applyFilter($datasource, 'categories.id', 'NOT IN', [2, 3])->shouldBeCalled();
     $this->apply($datasource, ['value' => ['categoryId' => -1, 'treeId' => 1]]);
 }
 function it_provides_formatted_batch_config_for_the_job(CategoryInterface $officeCategory, CategoryInterface $bedroomCategory)
 {
     $officeCategory->getCode()->willReturn('office_room');
     $bedroomCategory->getCode()->willReturn('bedroom');
     $this->setCategories([$officeCategory, $bedroomCategory]);
     $this->setFilters([['id', 'IN', ['49', '2']]]);
     $this->getBatchConfig()->shouldReturn('{\\"filters\\":[[\\"id\\",\\"IN\\",[\\"49\\",\\"2\\"]]],\\"actions\\":[{\\"field\\":\\"categories\\",\\"value\\":[\\"office_room\\",\\"bedroom\\"]}]}');
 }
 function it_dispatches_an_event_when_removing_a_tree($eventDispatcher, $objectManager, CategoryInterface $tree)
 {
     $tree->isRoot()->willReturn(true);
     $tree->getProducts()->willReturn([]);
     $eventDispatcher->dispatch(CategoryEvents::PRE_REMOVE_TREE, Argument::type('Symfony\\Component\\EventDispatcher\\GenericEvent'))->shouldBeCalled();
     $objectManager->remove($tree)->shouldBeCalled();
     $this->remove($tree, ['flush' => false]);
 }
 /**
  * Get id from category and Magento url.
  *
  * @param CategoryInterface $category
  * @param string            $magentoUrl
  * @param MappingCollection $categoryMapping
  *
  * @return int|null
  */
 public function getIdFromCategory(CategoryInterface $category, $magentoUrl, MappingCollection $categoryMapping = null)
 {
     if ($categoryMapping && ($categoryId = $categoryMapping->getTarget($category->getCode())) != $category->getCode()) {
         return $categoryId;
     } else {
         $categoryMapping = $this->getEntityRepository()->findOneBy(['category' => $category, 'magentoUrl' => $magentoUrl]);
         return $categoryMapping ? $categoryMapping->getMagentoCategoryId() : null;
     }
 }
 function it_dispatches_an_event_when_removing_a_tree($eventDispatcher, $objectManager, CategoryInterface $tree)
 {
     $tree->getId()->willReturn(12);
     $tree->isRoot()->willReturn(true);
     $tree->getProducts()->willReturn([]);
     $eventDispatcher->dispatch(CategoryEvents::PRE_REMOVE_TREE, Argument::type('Akeneo\\Component\\StorageUtils\\Event\\RemoveEvent'))->shouldBeCalled();
     $objectManager->remove($tree)->shouldBeCalled();
     $eventDispatcher->dispatch(CategoryEvents::POST_REMOVE_TREE, Argument::type('Akeneo\\Component\\StorageUtils\\Event\\RemoveEvent'))->shouldBeCalled();
     $this->remove($tree, ['flush' => true]);
 }
 function it_massively_insert_and_update_objects($bulkSaver, $bulkDetacher, $stepExecution, CategoryInterface $object1, CategoryInterface $object2)
 {
     $bulkSaver->saveAll([$object1, $object2]);
     $bulkDetacher->detachAll([$object1, $object2]);
     $object1->getId()->willReturn(null);
     $stepExecution->incrementSummaryInfo('create')->shouldBeCalled();
     $object2->getId()->willReturn(42);
     $stepExecution->incrementSummaryInfo('update')->shouldBeCalled();
     $this->write([$object1, $object2]);
 }
 /**
  * @param CategoryInterface $category
  * @param string            $field
  * @param mixed             $data
  */
 protected function setData(CategoryInterface $category, $field, $data)
 {
     if ('labels' === $field) {
         foreach ($data as $localeCode => $label) {
             $category->setLocale($localeCode);
             $translation = $category->getTranslation();
             $translation->setLabel($label);
         }
     } elseif ('parent' === $field) {
         $categoryParent = $this->findParent($data);
         if (null !== $categoryParent) {
             $category->setParent($categoryParent);
         } else {
             throw new \InvalidArgumentException(sprintf('The parent category "%s" does not exist', $data));
         }
     } else {
         $this->accessor->setValue($category, $field, $data);
     }
 }
 function it_updates_a_category($categoryRepository, CategoryInterface $category, CategoryInterface $categoryMaster, CategoryTranslation $translatable)
 {
     $categoryRepository->findOneByIdentifier('master')->willReturn($categoryMaster);
     $category->getTranslation()->willReturn($translatable);
     $translatable->setLabel('Ma superbe catégorie')->shouldBeCalled();
     $category->setCode('mycode')->shouldBeCalled();
     $category->setParent($categoryMaster)->shouldBeCalled();
     $category->setLocale('fr_FR')->shouldBeCalled();
     $category->getId()->willReturn(null);
     $values = ['code' => 'mycode', 'parent' => 'master', 'labels' => ['fr_FR' => 'Ma superbe catégorie']];
     $this->update($category, $values, []);
 }
 /**
  * {@inheritdoc}
  */
 public function getProductIdsInCategory(CategoryInterface $category, QueryBuilder $categoryQb = null)
 {
     $qb = $this->em->createQueryBuilder();
     $qb->select('DISTINCT p.id');
     $qb->from($this->entityName, 'p', 'p.id');
     $qb->join('p.categories', 'node');
     if (null === $categoryQb) {
         $qb->where('node.id = :nodeId');
         $qb->setParameter('nodeId', $category->getId());
     } else {
         $qb->where($categoryQb->getDqlPart('where'));
         $qb->setParameters($categoryQb->getParameters());
     }
     $products = $qb->getQuery()->execute(array(), AbstractQuery::HYDRATE_ARRAY);
     return array_keys($products);
 }
 /**
  * Get all sources
  * @param CategoryInterface $category
  *
  * @return array
  */
 public function getAllSources(CategoryInterface $category = null)
 {
     $sources = [];
     if ($this->isValid()) {
         $categories = $category === null ? $this->categoryManager->getTrees() : $category->getChildren();
         foreach ($categories as $category) {
             $sources[] = ['id' => $category->getCode(), 'text' => sprintf('%s (%s)', $category->getLabel(), $category->getCode())];
             $sources = array_merge($sources, $this->getAllSources($category));
         }
     }
     return $sources;
 }
 /**
  * {@inheritdoc}
  */
 public function findAllForCategory(CategoryInterface $category)
 {
     $qb = $this->createQueryBuilder('p');
     $qb->field('categoryIds')->in([$category->getId()]);
     return $qb->getQuery()->execute();
 }
 /**
  * Return categories ids provided by the categoryQb or by the provided category
  *
  * @param CategoryInterface $category
  * @param OrmQueryBuilder   $categoryQb
  *
  * @return array $categoryIds
  */
 protected function getCategoryIds(CategoryInterface $category, OrmQueryBuilder $categoryQb = null)
 {
     $categoryIds = array();
     if (null !== $categoryQb) {
         $categoryAlias = $categoryQb->getRootAlias();
         $categories = $categoryQb->select('PARTIAL ' . $categoryAlias . '.{id}')->getQuery()->getArrayResult();
     } else {
         $categories = array(array('id' => $category->getId()));
     }
     foreach ($categories as $category) {
         $categoryIds[] = $category['id'];
     }
     return $categoryIds;
 }
 /**
  * Update default tree of users using a tree that will be removed
  *
  * @param CategoryInterface $category
  *
  * @return null
  */
 protected function onTreeRemoved(CategoryInterface $category)
 {
     $users = $this->findUsersBy(array('defaultTree' => $category));
     $trees = $this->container->get('pim_catalog.manager.category')->getTrees();
     $defaultTree = current(array_filter($trees, function ($tree) use($category) {
         return $tree->getCode() !== $category->getCode();
     }));
     foreach ($users as $user) {
         $user->setDefaultTree($defaultTree);
         $this->computeChangeset($user);
     }
 }
Example #15
0
 function it_normalizes_a_root_category(CategoryInterface $category)
 {
     $category->getCode()->willReturn('foo');
     $category->getParent()->willReturn(null);
     $this->normalize($category, 'json')->shouldReturn(['code' => 'foo']);
 }
 /**
  * Define the state of a category
  *
  * @param CategoryInterface $category
  * @param bool              $hasChild
  * @param array             $selectedIds
  *
  * @return string
  */
 protected function defineCategoryState(CategoryInterface $category, $hasChild = false, array $selectedIds = [])
 {
     $state = $category->hasChildren() ? 'closed' : 'leaf';
     if ($hasChild === true) {
         $state = 'open';
     }
     if (in_array($category->getId(), $selectedIds)) {
         $state .= ' toselect jstree-checked';
     }
     if ($category->isRoot()) {
         $state .= ' jstree-root';
     }
     return $state;
 }
 /**
  * {@inheritdoc}
  */
 public function addChild(CategoryInterface $child)
 {
     $child->setParent($this);
     $this->children[] = $child;
     return $this;
 }
 /**
  * Provides a tree filled up to the categories provided, with all their ancestors
  * and ancestors sibligns are filled too, in order to be able to display the tree
  * directly without loading other data.
  *
  * @param CategoryInterface $root       Tree root category
  * @param Collection        $categories categories
  *
  * @return array Multi-dimensional array representing the tree
  *
  * TODO: To MOVE in the Repository. Try it in SQL.
  */
 public function getFilledTree(CategoryInterface $root, Collection $categories)
 {
     $parentsIds = [];
     foreach ($categories as $category) {
         $categoryParentsIds = [];
         $path = $this->getEntityRepository()->getPath($category);
         if ($path[0]->getId() === $root->getId()) {
             foreach ($path as $pathItem) {
                 $categoryParentsIds[] = $pathItem->getId();
             }
         }
         $parentsIds = array_merge($parentsIds, $categoryParentsIds);
     }
     $parentsIds = array_unique($parentsIds);
     return $this->getEntityRepository()->getTreeFromParents($parentsIds);
 }
 /**
  * Generate url key for category name and code
  * The code is included to make sure the url_key is unique, as required in Prestashop.
  *
  * @param CategoryInterface $category
  * @param string            $localeCode
  *
  * @return string
  */
 protected function generateUrlKey(CategoryInterface $category, $localeCode)
 {
     $code = $category->getCode();
     $label = $this->getCategoryLabel($category, $localeCode);
     $url = Urlizer::urlize($label . '-' . $code);
     return $url;
 }
 /**
  * Get category label
  * @param CategoryInterface $category
  * @param string            $localeCode
  *
  * @return string
  */
 protected function getCategoryLabel(CategoryInterface $category, $localeCode)
 {
     $category->setLocale($localeCode);
     return $category->getLabel();
 }
 /**
  * Check if a parent node is an ancestor of a child node
  *
  * @param CategoryInterface $parentNode
  * @param CategoryInterface $childNode
  *
  * @return boolean
  */
 public function isAncestor(CategoryInterface $parentNode, CategoryInterface $childNode)
 {
     $childPath = $this->getEntityRepository()->getPath($childNode);
     //Removing last part of the path as it's the node itself
     //which cannot be is own ancestor
     array_pop($childPath);
     $childCount = 0;
     $parentFound = false;
     while ($childCount < count($childPath) && !$parentFound) {
         $parentFound = $childPath[$childCount]->getId() === $parentNode->getId();
         $childCount++;
     }
     return $parentFound;
 }
 /**
  * Get all categories in order.
  *
  * @return array
  */
 public function findOrderedCategories(CategoryInterface $rootCategory)
 {
     return $this->createQueryBuilder('c')->select('c')->andWhere('c.root = ' . $rootCategory->getId())->orderBy('c.level, c.left', 'ASC');
 }
 function it_does_not_mark_products_as_updated_when_a_category_is_updated(EntityManager $em, ProductInterface $foo, ProductInterface $bar, CategoryInterface $category)
 {
     $category->getProducts()->willReturn([$foo, $bar]);
     $this->guessUpdates($em, $category, UpdateGuesserInterface::ACTION_UPDATE_ENTITY)->shouldReturn([]);
 }