Skip to content

Commit

Permalink
MAGETWO-37363: [GITHUB] dist front: search is broken #1268
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksym Aposov committed May 13, 2015
1 parent b4eb24a commit 3a9946a
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
*/
namespace Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget\Form;
use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Config\Model\Config\Source\Yesno;
use Magento\Catalog\Model\Entity\Attribute;
use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker;
use Magento\Framework\Data\FormFactory;
use Magento\Framework\Registry;

class Front extends Generic
{
Expand All @@ -24,28 +28,28 @@ class Front extends Generic
protected $_yesNo;

/**
* @var array
* @var PropertyLocker
*/
private $disableSearchable;
private $propertyLocker;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param Context $context
* @param Registry $registry
* @param FormFactory $formFactory
* @param Yesno $yesNo
* @param PropertyLocker $propertyLocker
* @param array $data
* @param array $disableSearchable
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
Context $context,
Registry $registry,
FormFactory $formFactory,
Yesno $yesNo,
array $data = [],
array $disableSearchable = []
PropertyLocker $propertyLocker,
array $data = []
) {
$this->_yesNo = $yesNo;
$this->disableSearchable = $disableSearchable;
$this->propertyLocker = $propertyLocker;
parent::__construct($context, $registry, $formFactory, $data);
}

Expand All @@ -71,7 +75,6 @@ protected function _prepareForm()
['legend' => __('Frontend Properties'), 'collapsable' => $this->getRequest()->has('popup')]
);

$attrCode = $attributeObject->getAttributeCode();
$fieldset->addField(
'is_searchable',
'select',
Expand All @@ -80,7 +83,6 @@ protected function _prepareForm()
'label' => __('Use in Search'),
'title' => __('Use in Search'),
'values' => $yesnoSource,
'disabled' => isset($this->disableSearchable[$attrCode]) && $this->disableSearchable[$attrCode] ? 1 : 0
]
);

Expand Down Expand Up @@ -223,6 +225,7 @@ protected function _prepareForm()
);

$form->setValues($attributeObject->getData());
$this->propertyLocker->lock($form);
$this->setForm($form);
return parent::_prepareForm();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ protected function _prepareForm()
$response = new \Magento\Framework\Object();
$response->setTypes([]);
$this->_eventManager->dispatch('adminhtml_product_attribute_types', ['response' => $response]);
$_disabledTypes = [];
$_hiddenFields = [];
foreach ($response->getTypes() as $type) {
$additionalTypes[] = $type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public function toOptionArray()
$response = new \Magento\Framework\Object();
$response->setTypes([]);
$this->_eventManager->dispatch('adminhtml_product_attribute_types', ['response' => $response]);
$_disabledTypes = [];
$_hiddenFields = [];
foreach ($response->getTypes() as $type) {
$inputTypes[] = $type;
Expand Down
14 changes: 0 additions & 14 deletions app/code/Magento/Catalog/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,4 @@
<type name="Magento\Catalog\Model\Resource\Attribute">
<plugin name="invalidate_pagecache_after_attribute_save" type="Magento\Catalog\Plugin\Model\Resource\Attribute\Save" />
</type>
<type name="Magento\Catalog\Block\Adminhtml\Product\Attribute\Edit\Tab\Front">
<arguments>
<argument name="disableSearchable" xsi:type="array">
<item name="visibility" xsi:type="boolean">true</item>
<item name="url_key" xsi:type="boolean">true</item>
<item name="status" xsi:type="boolean">true</item>
<item name="group_price" xsi:type="boolean">true</item>
<item name="price_type" xsi:type="boolean">true</item>
<item name="category_ids" xsi:type="boolean">true</item>
<item name="media_gallery" xsi:type="boolean">true</item>
<item name="country_of_manufacture" xsi:type="boolean">true</item>
</argument>
</arguments>
</type>
</config>
19 changes: 19 additions & 0 deletions app/code/Magento/Catalog/etc/eav_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,31 @@
<field code="is_unique" locked="true" />
</attribute>
<attribute code="status">
<field code="is_searchable" locked="true" />
<field code="is_filterable" locked="true" />
<field code="is_filterable_in_search" locked="true" />
</attribute>
<attribute code="visibility">
<field code="is_searchable" locked="true" />
<field code="is_filterable" locked="true" />
<field code="is_filterable_in_search" locked="true" />
</attribute>
<attribute code="country_of_manufacture">
<field code="is_searchable" locked="true" />
<field code="is_filterable" locked="true" />
<field code="is_filterable_in_search" locked="true" />
</attribute>
<attribute code="group_price">
<field code="is_searchable" locked="true" />
</attribute>
<attribute code="price_type">
<field code="is_searchable" locked="true" />
</attribute>
<attribute code="category_ids">
<field code="is_searchable" locked="true" />
</attribute>
<attribute code="media_gallery">
<field code="is_searchable" locked="true" />
</attribute>
</entity>
</config>
1 change: 1 addition & 0 deletions app/code/Magento/CatalogUrlRewrite/etc/eav_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<entity type="catalog_product">
<attribute code="url_key">
<field code="is_unique" locked="true" />
<field code="is_searchable" locked="true" />
</attribute>
</entity>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ abstract class AbstractMain extends \Magento\Backend\Block\Widget\Form\Generic
protected $_eavData = null;

/**
* @var \Magento\Eav\Model\Entity\Attribute\Config
* @var \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker
*/
protected $_attributeConfig;
protected $propertyLocker;

/**
* @var \Magento\Config\Model\Config\Source\YesnoFactory
Expand All @@ -53,7 +53,7 @@ abstract class AbstractMain extends \Magento\Backend\Block\Widget\Form\Generic
* @param \Magento\Eav\Helper\Data $eavData
* @param \Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory
* @param \Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory
* @param \Magento\Eav\Model\Entity\Attribute\Config $attributeConfig
* @param \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker
* @param array $data
*/
public function __construct(
Expand All @@ -63,13 +63,13 @@ public function __construct(
\Magento\Eav\Helper\Data $eavData,
\Magento\Config\Model\Config\Source\YesnoFactory $yesnoFactory,
\Magento\Eav\Model\Adminhtml\System\Config\Source\InputtypeFactory $inputTypeFactory,
\Magento\Eav\Model\Entity\Attribute\Config $attributeConfig,
\Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker $propertyLocker,
array $data = []
) {
$this->_eavData = $eavData;
$this->_yesnoFactory = $yesnoFactory;
$this->_inputTypeFactory = $inputTypeFactory;
$this->_attributeConfig = $attributeConfig;
$this->propertyLocker = $propertyLocker;
parent::__construct($context, $registry, $formFactory, $data);
}

Expand Down Expand Up @@ -257,6 +257,7 @@ protected function _prepareForm()
}
}

$this->propertyLocker->lock($form);
$this->setForm($form);

return parent::_prepareForm();
Expand All @@ -277,27 +278,6 @@ protected function _initFormValues()
return parent::_initFormValues();
}

/**
* This method is called before rendering HTML
*
* @return $this
*/
protected function _beforeToHtml()
{
parent::_beforeToHtml();
$attributeObject = $this->getAttributeObject();
if ($attributeObject->getId()) {
$form = $this->getForm();
foreach ($this->_attributeConfig->getLockedFields($attributeObject) as $field) {
if ($element = $form->getElement($field)) {
$element->setDisabled(1);
$element->setReadonly(1);
}
}
}
return $this;
}

/**
* Processing block html after rendering
* Adding js block to the end of this block
Expand Down
57 changes: 57 additions & 0 deletions app/code/Magento/Eav/Block/Adminhtml/Attribute/PropertyLocker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Block\Adminhtml\Attribute;

use Magento\Framework\Registry;
use Magento\Eav\Model\Entity\Attribute\Config;

/**
* Disable form fields
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class PropertyLocker
{
/**
* @var Config
*/
private $attributeConfig;

/**
* @var Registry
*/
protected $registry;

/**
* @param Registry $registry
* @param Config $attributeConfig
*/
public function __construct(
Registry $registry,
Config $attributeConfig
) {
$this->registry = $registry;
$this->attributeConfig = $attributeConfig;
}

/**
* @param \Magento\Framework\Data\Form $form
* @return void
*/
public function lock(\Magento\Framework\Data\Form $form)
{
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attributeObject */
$attributeObject = $this->registry->registry('entity_attribute');
if ($attributeObject->getId()) {
foreach ($this->attributeConfig->getLockedFields($attributeObject) as $field) {
if ($element = $form->getElement($field)) {
$element->setDisabled(1);
$element->setReadonly(1);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Test\Unit\Block\Adminhtml\Attribute;

use Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker;

class PropertyLockerTest extends \PHPUnit_Framework_TestCase
{
/** @var PropertyLocker */
protected $object;

/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject */
protected $attributeMock;

/** @var \Magento\Framework\Data\Form|\PHPUnit_Framework_MockObject_MockObject */
protected $formMock;

/** @var \Magento\Eav\Model\Entity\Attribute\Config|\PHPUnit_Framework_MockObject_MockObject */
protected $attributeConfigMock;

protected function setUp()
{
$this->attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute')
->setMethods(['getId'])
->disableOriginalConstructor()
->getMock();

$registryMock = $this->getMockBuilder('\Magento\Framework\Registry')
->setMethods(['registry'])
->disableOriginalConstructor()
->getMock();
$registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($this->attributeMock);

$this->attributeConfigMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Config')
->setMethods(['getLockedFields'])
->disableOriginalConstructor()
->getMock();

$this->formMock = $this->getMockBuilder('Magento\Framework\Data\Form')
->setMethods(['getElement'])
->disableOriginalConstructor()
->getMock();

$this->object = new PropertyLocker($registryMock, $this->attributeConfigMock);
}

/**
* @covers \Magento\Eav\Block\Adminhtml\Attribute\PropertyLocker::lock
*/
public function testLock()
{
$lockedFields = [
'is_searchable' => 'is_searchable',
'is_filterable' => 'is_filterable'
];
$this->attributeMock->expects($this->once())->method('getId')->willReturn(1);
$this->attributeConfigMock->expects($this->once())->method('getLockedFields')->willReturn($lockedFields);

$elementMock = $this->getMockBuilder('\Magento\Framework\Data\Form\Element\AbstractElement')
->setMethods(['setDisabled', 'setReadonly'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$elementMock->expects($this->exactly(2))->method('setDisabled');
$elementMock->expects($this->exactly(2))->method('setReadonly');
$this->formMock->expects($this->exactly(2))->method('getElement')->willReturn($elementMock);
$this->object->lock($this->formMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testToHtml($attributeCode)
{
/** @var \Magento\Catalog\Model\Resource\Eav\Attribute $model */
$model = $this->objectManager->create('Magento\Catalog\Model\Resource\Eav\Attribute');
$model->load($attributeCode, 'attribute_code');
$model->loadByCode(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);

/** @var \Magento\Framework\Registry $coreRegistry */
$coreRegistry = $this->objectManager->get('\Magento\Framework\Registry');
Expand Down

0 comments on commit 3a9946a

Please sign in to comment.