public function testGetCustomAttributes()
 {
     $nameAttributeCode = 'name';
     $descriptionAttributeCode = 'description';
     $interfaceAttribute = $this->getMock('\\Magento\\Framework\\Api\\MetadataObjectInterface');
     $interfaceAttribute->expects($this->once())->method('getAttributeCode')->willReturn($nameAttributeCode);
     $descriptionAttribute = $this->getMock('\\Magento\\Framework\\Api\\MetadataObjectInterface');
     $descriptionAttribute->expects($this->once())->method('getAttributeCode')->willReturn($descriptionAttributeCode);
     $customAttributesMetadata = [$interfaceAttribute, $descriptionAttribute];
     $this->metadataServiceMock->expects($this->once())->method('getCustomAttributesMetadata')->willReturn($customAttributesMetadata);
     $this->category->setData($nameAttributeCode, "sub");
     //The color attribute is not set, expect empty custom attribute array
     $this->assertEquals([], $this->category->getCustomAttributes());
     //Set the color attribute;
     $this->category->setData($descriptionAttributeCode, "description");
     $attributeValue = new \Magento\Framework\Api\AttributeValue();
     $attributeValue2 = new \Magento\Framework\Api\AttributeValue();
     $this->attributeValueFactory->expects($this->exactly(2))->method('create')->willReturnOnConsecutiveCalls($attributeValue, $attributeValue2);
     $this->assertEquals(1, count($this->category->getCustomAttributes()));
     $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
     $this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue());
     //Change the attribute value, should reflect in getCustomAttribute
     $this->category->setData($descriptionAttributeCode, "new description");
     $this->assertEquals(1, count($this->category->getCustomAttributes()));
     $this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
     $this->assertEquals("new description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue());
 }
 /**
  * Get url for category data
  *
  * @param Category $category
  * @return string
  */
 public function getCategoryUrl($category)
 {
     if ($category instanceof Category) {
         $url = $category->getUrl();
     } else {
         $url = $this->_categoryInstance->setData($category->getData())->getUrl();
     }
     return $url;
 }
 /**
  * @param $productScheduled
  * @param $expectedProductReindexCall
  *
  * @dataProvider reindexFlatDisabledTestDataProvider
  */
 public function testReindexFlatDisabled($productScheduled, $expectedProductReindexCall)
 {
     $affectedProductIds = ["1", "2"];
     $this->category->setAffectedProductIds($affectedProductIds);
     $pathIds = ['path/1/2', 'path/2/3'];
     $this->category->setData('path_ids', $pathIds);
     $this->category->setId('123');
     $this->flatState->expects($this->any())->method('isFlatEnabled')->will($this->returnValue(false));
     $this->productIndexer->expects($this->exactly(1))->method('isScheduled')->will($this->returnValue($productScheduled));
     $this->productIndexer->expects($this->exactly($expectedProductReindexCall))->method('reindexList')->with($pathIds);
     $this->indexerRegistry->expects($this->at(0))->method('get')->with(Indexer\Category\Product::INDEXER_ID)->will($this->returnValue($this->productIndexer));
     $this->category->reindex();
 }
 /**
  * Validate category process
  *
  * @param  Category $category
  * @return void
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function validateCategory(Category $category)
 {
     $useConfigFields = [];
     foreach ($this->useConfigFields as $field) {
         if (!$category->getData($field)) {
             $useConfigFields[] = $field;
         }
     }
     $category->setData('use_post_data_config', $useConfigFields);
     $validate = $category->validate();
     if ($validate !== true) {
         foreach ($validate as $code => $error) {
             if ($error === true) {
                 $attribute = $this->categoryResource->getAttribute($code)->getFrontend()->getLabel();
                 throw new \Magento\Framework\Exception\LocalizedException(__('Attribute "%1" is required.', $attribute));
             } else {
                 throw new \Magento\Framework\Exception\LocalizedException(__($error));
             }
         }
     }
     $category->unsetData('use_post_data_config');
 }
 public function testGetLevel()
 {
     $this->assertEquals(0, $this->_model->getLevel());
     $this->_model->setData('level', 1);
     $this->assertEquals(1, $this->_model->getLevel());
 }
 public function testGetAvailableSortBy()
 {
     $this->assertEquals([], $this->_model->getAvailableSortBy());
     $this->_model->setData('available_sort_by', 'test,and,test');
     $this->assertEquals(['test', 'and', 'test'], $this->_model->getAvailableSortBy());
 }
 /**
  * {@inheritdoc}
  */
 public function setData($key, $value = null)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'setData');
     if (!$pluginInfo) {
         return parent::setData($key, $value);
     } else {
         return $this->___callPlugins('setData', func_get_args(), $pluginInfo);
     }
 }
Example #8
0
 /**
  * Adds url_path property for non-root category - to ensure that url path is not empty.
  *
  * Sometimes attribute 'url_path' can be empty, because url_path hasn't been generated yet,
  * in this case category is loaded with empty url_path and we should generate it manually.
  *
  * @param \Magento\Framework\Object $category
  * @return void
  */
 protected function _addCategoryUrlPath($category)
 {
     if (!$category instanceof \Magento\Framework\Object || $category->getUrlPath()) {
         return;
     }
     // This routine is not intended to be used with root categories,
     // but handle 'em gracefully - ensure them to have empty path.
     if ($category->getLevel() <= 1) {
         $category->setUrlPath('');
         return;
     }
     if (self::$_categoryForUrlPath === null) {
         self::$_categoryForUrlPath = $this->_categoryFactory->create();
     }
     // Generate url_path
     $urlPath = self::$_categoryForUrlPath->setData($category->getData())->getUrlPath();
     $category->setUrlPath($urlPath);
 }