In a Module I configured my custom attribute redirect_url
for categories to be included in the collection using catalog_attributes.xml
This seems to no longer work in Magento 2.1
My module defines a plugin for Magento\Catalog\Helper\Category
public function aroundGetCategoryUrl(\Magento\Catalog\Helper\Category $subject, \Closure $proceed, $category) {
if ($category instanceof \Magento\Catalog\Model\Category) {
$redirect = $category->getRedirectUrl();
} else {
$category = $this->_categoryFactory->create()->setData($category->getData());
$redirect = $category->getRedirectUrl();
}
if ($redirect) {
return $this->_helper->buildUrl($redirect);
}
return $proceed($category);
}
I have the following integration test:
/**
* @var \Magento\Catalog\Model\Category
*/
protected $category;
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\Tree
*/
protected $tree;
protected function setUp()
{
/**
* Create category
* @source \Magento\Catalog\Model\Indexer\FlatTest::testCreateCategory
*/
/** @var \Magento\Catalog\Model\Category $category */
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
'Magento\Catalog\Model\Category'
);
$category->getResource()->load($category, 2);
/** @var \Magento\Catalog\Model\Category $categoryOne */
$categoryOne = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
'Magento\Catalog\Model\Category'
);
$categoryOne
->setName('Home Category ' . uniqid())->setPath($category->getPath())
->setIsActive(true)
->setRedirectUrl('/');
$category->getResource()->save($categoryOne);
$this->category = $categoryOne;
$this->tree = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
'Magento\Catalog\Model\ResourceModel\Category\Tree'
);
}
public function testAttributeInstalled()
{
/**
* @var $attribute \Magento\Catalog\Model\Category\Attribute
*/
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
'\Magento\Catalog\Model\Category\Attribute'
);
$attribute->loadByCode('catalog_category', 'redirect_url');
$this->assertNotNull($attribute->getId());
}
public function testRedirectUrlAttributeIsInCollection()
{
$collection = $this->tree->getCollection()->addAttributeToFilter('entity_id' , $this->category->getId());
$this->assertEquals('/', $collection->getFirstItem()->getRedirectUrl());
}
Those tests pass, but the following fails:
/**
* Check if menu items have the right URL
*
* @magentoAppArea frontend
*/
public function testUrlInMenu()
{
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
'Magento\Framework\View\DesignInterface'
)->setDesignTheme(
'Magento/blank'
);
/**
* @var $layout \Magento\Framework\View\LayoutInterface
*/
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
);
$block = $layout->addBlock(\Magento\Theme\Block\Html\Topmenu::class, 'test');
$block->setTemplate('Magento_Theme::html/topmenu.phtml');
$result = $block->toHtml();
$this->assertContains('http://localhost/index.php/foo-bar', $result);
}
Magento\Catalog\Model\ResourceModel\Category\Tree::getCollection
github.com/magento/magento2/blob/2.0/app/code/Magento/Catalog/…, This config file doesnt work for collections recived throughMagentoCatalog\Model\ResourceModel\Category
model. This behavior has no changes since 2.0. You may use direct call toaddAttributeToSelect()
to load required attribute.