Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/2.2-develop' into pr-2.2-develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mslabko committed Feb 7, 2018
2 parents 6dbe135 + 5d9ca56 commit 2482b58
Show file tree
Hide file tree
Showing 366 changed files with 7,639 additions and 2,099 deletions.
86 changes: 86 additions & 0 deletions app/code/Magento/Backend/Block/GlobalSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,61 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Block;

use Magento\Backend\Model\GlobalSearch\SearchEntityFactory;
use Magento\Backend\Model\GlobalSearch\SearchEntity;
use Magento\Framework\App\ObjectManager;

/**
* @api
* @since 100.0.2
*/
class GlobalSearch extends \Magento\Backend\Block\Template
{
/**
* @var SearchEntityFactory
*/
private $searchEntityFactory;

/**
* @var string
*/
protected $_template = 'Magento_Backend::system/search.phtml';

/**
* @var array
*/
private $entityResources;

/**
* @var array
*/
private $entityPaths;

/**
* @param Template\Context $context
* @param array $data
* @param array $entityResources
* @param array $entityPaths
* @param SearchEntityFactory|null $searchEntityFactory
*/
public function __construct(
Template\Context $context,
array $data = [],
array $entityResources = [],
array $entityPaths = [],
SearchEntityFactory $searchEntityFactory = null
) {
$this->entityResources = $entityResources;
$this->entityPaths = $entityPaths;
$this->searchEntityFactory = $searchEntityFactory ?: ObjectManager::getInstance()
->get(SearchEntityFactory::class);

parent::__construct($context, $data);
}

/**
* Get components configuration
* @return array
Expand All @@ -34,4 +76,48 @@ public function getWidgetInitOptions()
]
];
}

/**
* Get entities which are allowed to show.
*
* @return SearchEntity[]
*/
public function getEntitiesToShow()
{
$allowedEntityTypes = [];
$entitiesToShow = [];

foreach ($this->entityResources as $entityType => $resource) {
if ($this->getAuthorization()->isAllowed($resource)) {
$allowedEntityTypes[] = $entityType;
}
}

foreach ($allowedEntityTypes as $entityType) {
$url = $this->getUrlEntityType($entityType);

$searchEntity = $this->searchEntityFactory->create();
$searchEntity->setId('searchPreview' . $entityType);
$searchEntity->setTitle('in ' . $entityType);
$searchEntity->setUrl($url);

$entitiesToShow[] = $searchEntity;
}

return $entitiesToShow;
}

/**
* Get url path by entity type.
*
* @param string $entityType
*
* @return string
*/
private function getUrlEntityType(string $entityType)
{
$urlPath = $this->entityPaths[$entityType] ?? '';

return $this->getUrl($urlPath);
}
}
73 changes: 73 additions & 0 deletions app/code/Magento/Backend/Model/GlobalSearch/SearchEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Model\GlobalSearch;

/**
* Entity for global search in backend
*/
class SearchEntity extends \Magento\Framework\DataObject
{
/**
* Get id.
*
* @return string
*/
public function getId()
{
return $this->getData('id');
}

/**
* Get url.
*
* @return string
*/
public function getUrl()
{
return $this->getData('url');
}

/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->getData('title');
}

/**
* Set Id.
*
* @param string $value
*/
public function setId(string $value)
{
$this->setData('id', $value);
}

/**
* Set url.
*
* @param string $value
*/
public function setUrl(string $value)
{
$this->setData('url', $value);
}

/**
* Set title.
*
* @param string $value
*/
public function setTitle(string $value)
{
$this->setData('title', $value);
}
}
123 changes: 123 additions & 0 deletions app/code/Magento/Backend/Test/Unit/Block/GlobalSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Backend\Test\Unit\Block;

use Magento\Backend\Block\GlobalSearch;
use Magento\Backend\Model\GlobalSearch\SearchEntity;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

/**
* Test for @see GlobalSearch.
*/
class GlobalSearchTest extends \PHPUnit\Framework\TestCase
{
/**
* @var GlobalSearch
*/
private $globalSearch;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\AuthorizationInterface
*/
private $authorization;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface
*/
private $urlBuilder;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Backend\Model\GlobalSearch\SearchEntityFactory
*/
private $searchEntityFactory;

/**
* @var array
*/
private $entityResources = [
'Products' => \Magento\Catalog\Controller\Adminhtml\Product::ADMIN_RESOURCE,
'Orders' => \Magento\Sales\Controller\Adminhtml\Order::ADMIN_RESOURCE,
'Customers' => \Magento\Customer\Controller\Adminhtml\Index::ADMIN_RESOURCE,
'Pages' => \Magento\Cms\Controller\Adminhtml\Page\Index::ADMIN_RESOURCE,
];

/**
* @var array
*/
private $entityPaths = [
'Products' => 'catalog/product/index/',
'Orders' => 'sales/order/index/',
'Customers' => 'customer/index/index',
'Pages' => 'cms/page/index/',
];

protected function setUp()
{
$objectManager = new ObjectManager($this);

$this->authorization = $this->createMock(\Magento\Framework\AuthorizationInterface::class);
$this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
$context = $this->createMock(\Magento\Backend\Block\Template\Context::class);

$context->expects($this->atLeastOnce())->method('getAuthorization')->willReturn($this->authorization);
$context->expects($this->atLeastOnce())->method('getUrlBuilder')->willReturn($this->urlBuilder);

$this->searchEntityFactory = $this->createMock(\Magento\Backend\Model\GlobalSearch\SearchEntityFactory::class);

$this->globalSearch = $objectManager->getObject(
GlobalSearch::class,
[
'context' => $context,
'searchEntityFactory' => $this->searchEntityFactory,
'entityResources' => $this->entityResources,
'entityPaths' => $this->entityPaths,
]
);
}

/**
* @param array $results
* @param int $expectedEntitiesQty
*
* @dataProvider getEntitiesToShowDataProvider
*/
public function testGetEntitiesToShow(array $results, int $expectedEntitiesQty)
{
$searchEntity = $this->createMock(SearchEntity::class);

$this->authorization->expects($this->exactly(count($results)))->method('isAllowed')
->willReturnOnConsecutiveCalls($results[0], $results[1], $results[2], $results[3]);
$this->urlBuilder->expects($this->exactly($expectedEntitiesQty))
->method('getUrl')->willReturn('some/url/is/here');
$this->searchEntityFactory->expects($this->exactly($expectedEntitiesQty))
->method('create')->willReturn($searchEntity);

$searchEntity->expects($this->exactly($expectedEntitiesQty))->method('setId');
$searchEntity->expects($this->exactly($expectedEntitiesQty))->method('setTitle');
$searchEntity->expects($this->exactly($expectedEntitiesQty))->method('setUrl');

$this->assertSame($expectedEntitiesQty, count($this->globalSearch->getEntitiesToShow()));
}

public function getEntitiesToShowDataProvider()
{
return [
[
[true, false, true, false],
2,
],
[
[true, true, true, true],
4,
],
[
[false, false, false, false],
0,
],
];
}
}
16 changes: 16 additions & 0 deletions app/code/Magento/Backend/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,20 @@
<argument name="defaultClass" xsi:type="string">Magento\Backend\Block\Template</argument>
</arguments>
</type>
<type name="Magento\Backend\Block\GlobalSearch">
<arguments>
<argument name="entityResources" xsi:type="array">
<item name="Products" xsi:type="string">Magento_Catalog::products</item>
<item name="Orders" xsi:type="string">Magento_Sales::sales_order</item>
<item name="Customers" xsi:type="string">Magento_Customer::manage</item>
<item name="Pages" xsi:type="string">Magento_Cms::page</item>
</argument>
<argument name="entityPaths" xsi:type="array">
<item name="Products" xsi:type="string">catalog/product/index/</item>
<item name="Orders" xsi:type="string">sales/order/index/</item>
<item name="Customers" xsi:type="string">customer/index/index</item>
<item name="Pages" xsi:type="string">cms/page/index/</item>
</argument>
</arguments>
</type>
</config>
4 changes: 4 additions & 0 deletions app/code/Magento/Backend/i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,7 @@ Pagination,Pagination
"Alternative text for the next pages link in the pagination menu. If empty, default arrow image is used.","Alternative text for the next pages link in the pagination menu. If empty, default arrow image is used."
"Anchor Text for Next","Anchor Text for Next"
"Theme Name","Theme Name"
"In Products","In Products"
"In Orders","In Orders"
"In Customers","In Customers"
"In Pages","In Pages"
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@
</form>
<script data-template="search-suggest" type="text/x-magento-template">
<ul class="search-global-menu">
<li class="item">
<a id="searchPreviewProducts" href="<?= /* @escapeNotVerified */ $block->getUrl('catalog/product/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Products</a>
</li>
<li class="item">
<a id="searchPreviewOrders" href="<?= /* @escapeNotVerified */ $block->getUrl('sales/order/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Orders</a>
</li>
<li class="item">
<a id="searchPreviewCustomers" href="<?= /* @escapeNotVerified */ $block->getUrl('customer/index/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Customers</a>
</li>
<li class="item">
<a id="searchPreviewPages" href="<?= /* @escapeNotVerified */ $block->getUrl('cms/page/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Pages</a>
</li>
<?php foreach ($block->getEntitiesToShow() as $entity): ?>
<li class="item">
<a id="<?= /* @escapeNotVerified */ $entity->getId(); ?>"
href="<?= /* @escapeNotVerified */ $entity->getUrl() ?>?search=<%- data.term%>"
class="title">
"<%- data.term%>" <?= /* @escapeNotVerified */ __($entity->getTitle()); ?>
</a>
</li>
<?php endforeach; ?>
<% if (data.items.length) { %>
<% _.each(data.items, function(value){ %>
<li class="item"
Expand Down
Loading

0 comments on commit 2482b58

Please sign in to comment.