Skip to content

Admin Config Search #10335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions app/code/Magento/Backend/Model/Search/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Search;

use Magento\Backend\Model\Search\Config\Result\Builder;
use Magento\Config\Model\Config\Structure;
use Magento\Config\Model\Config\Structure\Element\AbstractComposite;
use Magento\Config\Model\Config\Structure\Element\Iterator as ElementIterator;

/**
* Search Config Model
*/
class Config extends \Magento\Framework\DataObject
{
/**
* @var \Magento\Framework\App\Config\ConfigTypeInterface
*/
private $configStructure;

/**
* @var Builder
*/
private $resultBuilder;

/**
* @param Structure $configStructure
* @param Builder $resultBuilder
*/
public function __construct(Structure $configStructure, Builder $resultBuilder)
{
$this->configStructure = $configStructure;
$this->resultBuilder = $resultBuilder;
}

/**
* @param string $query
* @return $this
*/
public function setQuery($query)
{
$this->setData('query', $query);
return $this;
}

/**
* @return string|null
*/
public function getQuery()
{
return $this->getData('query');
}

/**
* @return bool
*/
public function hasQuery()
{
return $this->hasData('query');
}

/**
* @param array $results
* @return $this
*/
public function setResults(array $results)
{
$this->setData('results', $results);
return $this;
}

/**
* @return array|null
*/
public function getResults()
{
return $this->getData('results');
}

/**
* Load search results
*
* @return $this
*/
public function load()
{
$this->findInStructure($this->configStructure->getTabs(), $this->getQuery());
$this->setResults($this->resultBuilder->getAll());
return $this;
}

/**
* @param ElementIterator $structureElementIterator
* @param string $searchTerm
* @param string $pathLabel
* @return void
* @SuppressWarnings(PHPMD.LongVariable)
*/
private function findInStructure(ElementIterator $structureElementIterator, $searchTerm, $pathLabel = '')
{
if (empty($searchTerm)) {
return;
}
foreach ($structureElementIterator as $structureElement) {
if (mb_stripos((string)$structureElement->getLabel(), $searchTerm) !== false) {
$this->resultBuilder->add($structureElement, $pathLabel);
}
$elementPathLabel = $pathLabel . ' / ' . $structureElement->getLabel();
if ($structureElement instanceof AbstractComposite && $structureElement->hasChildren()) {
$this->findInStructure($structureElement->getChildren(), $searchTerm, $elementPathLabel);
}
}
}
}
78 changes: 78 additions & 0 deletions app/code/Magento/Backend/Model/Search/Config/Result/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Search\Config\Result;

use Magento\Backend\Model\Search\Config\Structure\ElementBuilderInterface;
use Magento\Backend\Model\UrlInterface;
use Magento\Config\Model\Config\StructureElementInterface;

/**
* Config SearchResult Builder
* @SuppressWarnings(PHPMD.LongVariable)
*/
class Builder
{
/**
* @var array
*/
private $results = [];

/**
* @var UrlInterface
*/
private $urlBuilder;

/**
* @var ElementBuilderInterface[]
*/
private $structureElementTypes;

/**
* @param UrlInterface $urlBuilder
* @param array $structureElementTypes
*/
public function __construct(UrlInterface $urlBuilder, array $structureElementTypes)
{
$this->urlBuilder = $urlBuilder;
$this->structureElementTypes = $structureElementTypes;
}

/**
* @return array
*/
public function getAll()
{
return $this->results;
}

/**
* @param StructureElementInterface $structureElement
* @param string $elementPathLabel
* @return void
*/
public function add(StructureElementInterface $structureElement, $elementPathLabel)
{
$urlParams = [];
$elementData = $structureElement->getData();

if (!in_array($elementData['_elementType'], array_keys($this->structureElementTypes))) {
return;
}

if (isset($this->structureElementTypes[$elementData['_elementType']])) {
$urlParamsBuilder = $this->structureElementTypes[$elementData['_elementType']];
$urlParams = $urlParamsBuilder->build($structureElement);
}

$this->results[] = [
'id' => $structureElement->getPath(),
'type' => null,
'name' => (string)$structureElement->getLabel(),
'description' => $elementPathLabel,
'url' => $this->urlBuilder->getUrl('*/system_config/edit', $urlParams),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Search\Config\Structure\Element\Builder;

use Magento\Backend\Model\Search\Config\Structure\ElementBuilderInterface;
use Magento\Config\Model\Config\StructureElementInterface;

class Field implements ElementBuilderInterface
{
/**
* @inheritdoc
*/
public function build(StructureElementInterface $structureElement)
{
$elementPathParts = explode('/', $structureElement->getPath());
return [
'section' => $elementPathParts[0],
'group' => $elementPathParts[1],
'field' => $structureElement->getId(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Search\Config\Structure\Element\Builder;

use Magento\Backend\Model\Search\Config\Structure\ElementBuilderInterface;
use Magento\Config\Model\Config\StructureElementInterface;

class Group implements ElementBuilderInterface
{
/**
* @inheritdoc
*/
public function build(StructureElementInterface $structureElement)
{
$elementPathParts = explode('/', $structureElement->getPath());
return [
'section' => $elementPathParts[0],
'group' => $elementPathParts[1],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Search\Config\Structure\Element\Builder;

use Magento\Backend\Model\Search\Config\Structure\ElementBuilderInterface;
use Magento\Config\Model\Config\StructureElementInterface;

class Section implements ElementBuilderInterface
{
/**
* @inheritdoc
*/
public function build(StructureElementInterface $structureElement)
{
$elementPathParts = explode('/', $structureElement->getPath());
return ['section' => $elementPathParts[1]];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model\Search\Config\Structure;

use Magento\Config\Model\Config\StructureElementInterface;

interface ElementBuilderInterface
{
/**
* @param StructureElementInterface $structureElement
* @return array
*/
public function build(StructureElementInterface $structureElement);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Test\Unit\Model\Search\Config\Result;

use Magento\Backend\Model\Search\Config\Structure\ElementBuilderInterface;
use Magento\Backend\Model\UrlInterface;
use Magento\Backend\Model\Search\Config\Result\Builder;
use Magento\Config\Model\Config\StructureElementInterface;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.LongVariable)
*/
class BuilderTest extends TestCase
{
/**
* @var Builder
*/
protected $model;

/**
* @var StructureElementInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $structureElementMock;

/**
* @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $urlBuilderMock;

/**
* @var ElementBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $structureElementUrlParamsBuilderMock;

protected function setUp()
{
$this->urlBuilderMock = $this->getMockForAbstractClass(UrlInterface::class);
$this->structureElementMock = $this->getMockForAbstractClass(StructureElementInterface::class);
$this->structureElementUrlParamsBuilderMock = $this->getMockForAbstractClass(ElementBuilderInterface::class);
$this->model = new Builder($this->urlBuilderMock, ['section' => $this->structureElementUrlParamsBuilderMock]);
}

public function testAddWithNotSupportedStructureElementReturnsNothing()
{
$this->structureElementMock
->expects($this->once())
->method('getData')
->will($this->returnValue(['_elementType' => 'not_declared_structure_element_type']));
$this->model->add($this->structureElementMock, '');
$this->assertEquals([], $this->model->getAll());
}

public function testAddWithSupportedStructureElements()
{
$structureElementPath = '/section_code';
$structureElementLabel = 'Section Label';
$buildUrlParams = ['param_key' => 'param_value'];
$generatedUrl = 'http://example.com';

$expectedSearchResult = [
[
'id' => $structureElementPath,
'type' => null,
'name' => 'Section Label',
'description' => 'Section Label',
'url' => 'http://example.com',
],
];

$this->structureElementMock
->expects($this->once())
->method('getData')
->willReturn(['_elementType' => 'section']);
$this->structureElementMock
->expects($this->once())
->method('getPath')
->willReturn($structureElementPath);
$this->structureElementMock
->expects($this->once())
->method('getLabel')
->willReturn($structureElementLabel);

$this->structureElementUrlParamsBuilderMock->expects($this->once())
->method('build')
->willReturn($buildUrlParams);

$this->urlBuilderMock
->expects($this->once())
->method('getUrl')
->with('*/system_config/edit', $buildUrlParams)
->will($this->returnValue($generatedUrl));

$this->model->add($this->structureElementMock, $structureElementLabel);
$this->assertEquals($expectedSearchResult, $this->model->getAll());
}
}
Loading