Skip to content

Commit 96371bd

Browse files
author
Oleksii Korshenko
authored
MAGETWO-85298: 8011: Strip Tags from attribute. #968
2 parents be1e93b + 4437438 commit 96371bd

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

app/code/Magento/Rule/Model/Condition/Product/AbstractProduct.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ protected function _prepareValueOptions()
241241
} else {
242242
$addEmptyOption = true;
243243
}
244-
$selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption);
244+
$selectOptions = $this->removeTagsFromLabel(
245+
$attributeObject->getSource()->getAllOptions($addEmptyOption)
246+
);
245247
}
246248
}
247249

@@ -734,4 +736,21 @@ protected function getEavAttributeTableAlias()
734736

735737
return 'at_' . $attribute->getAttributeCode();
736738
}
739+
740+
/**
741+
* Remove html tags from attribute labels.
742+
*
743+
* @param array $selectOptions
744+
* @return array
745+
*/
746+
private function removeTagsFromLabel(array $selectOptions)
747+
{
748+
foreach ($selectOptions as &$option) {
749+
if (isset($option['label'])) {
750+
$option['label'] = strip_tags($option['label']);
751+
}
752+
}
753+
754+
return $selectOptions;
755+
}
737756
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Rule\Model\Condition\Product;
8+
9+
use Magento\Backend\Helper\Data;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\ProductCategoryList;
12+
use Magento\Catalog\Model\ProductFactory;
13+
use Magento\Catalog\Model\ResourceModel\Product;
14+
use Magento\Eav\Model\Config;
15+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection;
16+
use Magento\Framework\Locale\FormatInterface;
17+
use Magento\Rule\Model\Condition\Context;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
20+
/**
21+
* Provide tests for Abstract Rule product condition data model.
22+
* @magentoAppArea adminhtml
23+
*/
24+
class AbstractProductTest extends \PHPUnit\Framework\TestCase
25+
{
26+
/**
27+
* Test subject.
28+
*
29+
* @var AbstractProduct|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $model;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$objectManager = Bootstrap::getObjectManager();
39+
$context = $objectManager->get(Context::class);
40+
$helperData = $objectManager->get(Data::class);
41+
$config = $objectManager->get(Config::class);
42+
$productFactory = $objectManager->get(ProductFactory::class);
43+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
44+
$productResource = $objectManager->get(Product::class);
45+
$attributeSetCollection = $objectManager->get(Collection::class);
46+
$localeFormat = $objectManager->get(FormatInterface::class);
47+
$data = [];
48+
$productCategoryList = $objectManager->get(ProductCategoryList::class);
49+
$this->model = $this->getMockBuilder(AbstractProduct::class)
50+
->setMethods(['getOperator', 'getFormName', 'setFormName'])
51+
->setConstructorArgs([
52+
$context,
53+
$helperData,
54+
$config,
55+
$productFactory,
56+
$productRepository,
57+
$productResource,
58+
$attributeSetCollection,
59+
$localeFormat,
60+
$data,
61+
$productCategoryList
62+
])
63+
->getMockForAbstractClass();
64+
}
65+
66+
/**
67+
* Test Abstract Rule product condition data model shows attribute labels in more readable view
68+
* (without html tags, if one presented).
69+
*
70+
* @magentoDataFixture Magento/Rule/_files/dropdown_attribute_with_html.php
71+
*/
72+
public function testGetValueSelectOptions()
73+
{
74+
$expectedLabels = [' ', 'Option 1', 'Option 2', 'Option 3'];
75+
$this->model->setAttribute('dropdown_attribute_with_html');
76+
$options = $this->model->getValueSelectOptions();
77+
$labels = [];
78+
foreach ($options as $option) {
79+
$labels[] = $option['label'];
80+
}
81+
self::assertSame($expectedLabels, $labels);
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/* Create attribute */
8+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
9+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
10+
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
11+
);
12+
13+
if (!$attribute->loadByCode(4, 'dropdown_attribute_with_html')->getId()) {
14+
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
15+
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
16+
\Magento\Catalog\Setup\CategorySetup::class
17+
);
18+
19+
$attribute->setData(
20+
[
21+
'attribute_code' => 'dropdown_attribute_with_html',
22+
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
23+
'is_global' => 0,
24+
'is_user_defined' => 1,
25+
'frontend_input' => 'select',
26+
'is_unique' => 0,
27+
'is_required' => 0,
28+
'is_searchable' => 0,
29+
'is_visible_in_advanced_search' => 0,
30+
'is_comparable' => 0,
31+
'is_filterable' => 0,
32+
'is_filterable_in_search' => 0,
33+
'is_used_for_promo_rules' => 0,
34+
'is_html_allowed_on_front' => 1,
35+
'is_visible_on_front' => 0,
36+
'used_in_product_listing' => 0,
37+
'used_for_sort_by' => 0,
38+
'frontend_label' => ['Drop-Down Attribute'],
39+
'backend_type' => 'varchar',
40+
'backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class,
41+
'option' => [
42+
'value' => [
43+
'option_1' => ['<a href="#">Option 1</a>'],
44+
'option_2' => ['<a href="#">Option 2</a>'],
45+
'option_3' => ['<a href="#">Option 3</a>'],
46+
],
47+
'order' => [
48+
'option_1' => 1,
49+
'option_2' => 2,
50+
'option_3' => 3,
51+
],
52+
],
53+
]
54+
);
55+
$attribute->save();
56+
57+
/* Assign attribute to attribute set */
58+
$installer->addAttributeToGroup('catalog_product', 'Default', 'Attributes', $attribute->getId());
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
/* Delete attribute with dropdown_attribute_with_html code */
7+
8+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
11+
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
12+
$registry->unregister('isSecureArea');
13+
$registry->register('isSecureArea', true);
14+
/** @var $attribute Attribute */
15+
$attribute = Bootstrap::getObjectManager()->create(
16+
Attribute::class
17+
);
18+
$attribute->load('dropdown_attribute_with_html', 'attribute_code');
19+
$attribute->delete();
20+
21+
$registry->unregister('isSecureArea');
22+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)