Skip to content

Commit

Permalink
8011: Strip Tags from attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalevanec committed Dec 6, 2017
1 parent 276c690 commit 59cedcd
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ protected function _prepareValueOptions()
} else {
$addEmptyOption = true;
}
$selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption);
$selectOptions = $this->removeTagsFromLabel(
$attributeObject->getSource()->getAllOptions($addEmptyOption)
);
}
}

Expand Down Expand Up @@ -734,4 +736,21 @@ protected function getEavAttributeTableAlias()

return 'at_' . $attribute->getAttributeCode();
}

/**
* Remove html tags from attribute labels.
*
* @param array $selectOptions
* @return array
*/
private function removeTagsFromLabel($selectOptions)
{
foreach ($selectOptions as &$option) {
if (isset($option['label'])) {
$option['label'] = strip_tags($option['label']);
}
}

return $selectOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Rule\Model\Condition\Product;

use Magento\Backend\Helper\Data;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ProductCategoryList;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
use Magento\Framework\Locale\FormatInterface;
use Magento\Rule\Model\Condition\Context;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Provide tests for Abstract Rule product condition data model.
* @magentoAppArea adminhtml
*/
class AbstractProductTest extends \PHPUnit\Framework\TestCase
{
/**
* Test subject.
*
* @var AbstractProduct|\PHPUnit_Framework_MockObject_MockObject
*/
private $model;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$context = $objectManager->get(Context::class);
$helperData = $objectManager->get(Data::class);
$config = $objectManager->get(Config::class);
$productFactory = $objectManager->get(ProductFactory::class);
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
$productResource = $objectManager->get(Product::class);
$attributeSetCollection = $objectManager->get(Collection::class);
$localeFormat = $objectManager->get(FormatInterface::class);
$data = [];
$productCategoryList = $objectManager->get(ProductCategoryList::class);
$this->model = $this->getMockBuilder(AbstractProduct::class)
->setMethods(['getOperator', 'getFormName', 'setFormName'])
->setConstructorArgs([
$context,
$helperData,
$config,
$productFactory,
$productRepository,
$productResource,
$attributeSetCollection,
$localeFormat,
$data,
$productCategoryList
])
->getMockForAbstractClass();
}

/**
* Test Abstract Rule product condition data model shows attribute labels in more readable view
* (without html tags, if one presented).
*
* @magentoDataFixture Magento/Rule/_files/dropdown_attribute_with_html.php
*/
public function test()
{
$expectedOptions = [
[
'label' => ' ',
'value' => '',
],
[
'value' => '4',
'label' => 'Option 1',
],
[
'value' => '5',
'label' => 'Option 2',
],
[
'value' => '6',
'label' => 'Option 3',
],
];
$this->model->setAttribute('dropdown_attribute_with_html');
self::assertSame($expectedOptions, $this->model->getValueSelectOptions());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/* Create attribute */
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
);

if (!$attribute->loadByCode(4, 'dropdown_attribute_with_html')->getId()) {
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Setup\CategorySetup::class
);

$attribute->setData(
[
'attribute_code' => 'dropdown_attribute_with_html',
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
'is_global' => 0,
'is_user_defined' => 1,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 0,
'is_searchable' => 0,
'is_visible_in_advanced_search' => 0,
'is_comparable' => 0,
'is_filterable' => 0,
'is_filterable_in_search' => 0,
'is_used_for_promo_rules' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 0,
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => ['Drop-Down Attribute'],
'backend_type' => 'varchar',
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
'option' => [
'value' => [
'option_1' => ['<a href="#">Option 1</a>'],
'option_2' => ['<a href="#">Option 2</a>'],
'option_3' => ['<a href="#">Option 3</a>'],
],
'order' => [
'option_1' => 1,
'option_2' => 2,
'option_3' => 3,
],
],
]
);
$attribute->save();

/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'Attributes', $attribute->getId());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/* Delete attribute with dropdown_attribute_with_html code */

use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\TestFramework\Helper\Bootstrap;

$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);
/** @var $attribute Attribute */
$attribute = Bootstrap::getObjectManager()->create(
Attribute::class
);
$attribute->load('dropdown_attribute_with_html', 'attribute_code');
$attribute->delete();

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);

0 comments on commit 59cedcd

Please sign in to comment.