1

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);
}
6
  • Checking on this.
    – benmarks
    Jul 6, 2016 at 9:08
  • Attributes declared in catalog_attributes.xml are used directly by 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 through MagentoCatalog\Model\ResourceModel\Category model. This behavior has no changes since 2.0. You may use direct call to addAttributeToSelect() to load required attribute. Jul 8, 2016 at 13:54
  • @AntonKaplya But it worked before .. seriously ... the problem is that I need the attribute in an observer so no chance to use addAttributeToSelect. It's for github.com/Mestrona/Mestrona_CategoryRedirect in case you have some time to have a look
    – Alex
    Jul 8, 2016 at 17:02
  • @AntonKaplya Okay, wait a minute ... my test is a bit different from the code not working in the extension - I updated my test case to use the tree collection. Still failing
    – Alex
    Jul 9, 2016 at 13:06
  • @AntonKaplya But my specific test does also not run with 2.0 - but my extension was - so I have to debug further.
    – Alex
    Jul 9, 2016 at 13:17

2 Answers 2

0

The real problem is, that in catalog 101.0.0 of Magento 2.1 in difference to Magento 2.0's catalog (100.x.x) is, that the category tree is loaded in \Magento\Catalog\Plugin\Block\Topmenu::getCategoryTree which does not use the Magento\Catalog\Model\ResourceModel\Category\Tree collection but the Magento\Catalog\Model\ResourceModel\Category\Collection which does not adhere to catalog_attributes.xml as Anton said

0

I know this is a late reply but I just answered to a similar question. Check if this answer helps.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.