Skip to content

Commit 3640e7b

Browse files
committed
Merge remote-tracking branch 'mainline/2.2-develop' into MAGETWO-69634
2 parents 1e7caef + 03bb798 commit 3640e7b

File tree

374 files changed

+7954
-2059
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

374 files changed

+7954
-2059
lines changed

app/code/Magento/Backend/Block/GlobalSearch.php

+86
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,61 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Backend\Block;
78

9+
use Magento\Backend\Model\GlobalSearch\SearchEntityFactory;
10+
use Magento\Backend\Model\GlobalSearch\SearchEntity;
11+
use Magento\Framework\App\ObjectManager;
12+
813
/**
914
* @api
1015
* @since 100.0.2
1116
*/
1217
class GlobalSearch extends \Magento\Backend\Block\Template
1318
{
19+
/**
20+
* @var SearchEntityFactory
21+
*/
22+
private $searchEntityFactory;
23+
1424
/**
1525
* @var string
1626
*/
1727
protected $_template = 'Magento_Backend::system/search.phtml';
1828

29+
/**
30+
* @var array
31+
*/
32+
private $entityResources;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $entityPaths;
38+
39+
/**
40+
* @param Template\Context $context
41+
* @param array $data
42+
* @param array $entityResources
43+
* @param array $entityPaths
44+
* @param SearchEntityFactory|null $searchEntityFactory
45+
*/
46+
public function __construct(
47+
Template\Context $context,
48+
array $data = [],
49+
array $entityResources = [],
50+
array $entityPaths = [],
51+
SearchEntityFactory $searchEntityFactory = null
52+
) {
53+
$this->entityResources = $entityResources;
54+
$this->entityPaths = $entityPaths;
55+
$this->searchEntityFactory = $searchEntityFactory ?: ObjectManager::getInstance()
56+
->get(SearchEntityFactory::class);
57+
58+
parent::__construct($context, $data);
59+
}
60+
1961
/**
2062
* Get components configuration
2163
* @return array
@@ -34,4 +76,48 @@ public function getWidgetInitOptions()
3476
]
3577
];
3678
}
79+
80+
/**
81+
* Get entities which are allowed to show.
82+
*
83+
* @return SearchEntity[]
84+
*/
85+
public function getEntitiesToShow()
86+
{
87+
$allowedEntityTypes = [];
88+
$entitiesToShow = [];
89+
90+
foreach ($this->entityResources as $entityType => $resource) {
91+
if ($this->getAuthorization()->isAllowed($resource)) {
92+
$allowedEntityTypes[] = $entityType;
93+
}
94+
}
95+
96+
foreach ($allowedEntityTypes as $entityType) {
97+
$url = $this->getUrlEntityType($entityType);
98+
99+
$searchEntity = $this->searchEntityFactory->create();
100+
$searchEntity->setId('searchPreview' . $entityType);
101+
$searchEntity->setTitle('in ' . $entityType);
102+
$searchEntity->setUrl($url);
103+
104+
$entitiesToShow[] = $searchEntity;
105+
}
106+
107+
return $entitiesToShow;
108+
}
109+
110+
/**
111+
* Get url path by entity type.
112+
*
113+
* @param string $entityType
114+
*
115+
* @return string
116+
*/
117+
private function getUrlEntityType(string $entityType)
118+
{
119+
$urlPath = $this->entityPaths[$entityType] ?? '';
120+
121+
return $this->getUrl($urlPath);
122+
}
37123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Backend\Model\GlobalSearch;
8+
9+
/**
10+
* Entity for global search in backend
11+
*/
12+
class SearchEntity extends \Magento\Framework\DataObject
13+
{
14+
/**
15+
* Get id.
16+
*
17+
* @return string
18+
*/
19+
public function getId()
20+
{
21+
return $this->getData('id');
22+
}
23+
24+
/**
25+
* Get url.
26+
*
27+
* @return string
28+
*/
29+
public function getUrl()
30+
{
31+
return $this->getData('url');
32+
}
33+
34+
/**
35+
* Get title.
36+
*
37+
* @return string
38+
*/
39+
public function getTitle()
40+
{
41+
return $this->getData('title');
42+
}
43+
44+
/**
45+
* Set Id.
46+
*
47+
* @param string $value
48+
*/
49+
public function setId(string $value)
50+
{
51+
$this->setData('id', $value);
52+
}
53+
54+
/**
55+
* Set url.
56+
*
57+
* @param string $value
58+
*/
59+
public function setUrl(string $value)
60+
{
61+
$this->setData('url', $value);
62+
}
63+
64+
/**
65+
* Set title.
66+
*
67+
* @param string $value
68+
*/
69+
public function setTitle(string $value)
70+
{
71+
$this->setData('title', $value);
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Backend\Test\Unit\Block;
8+
9+
use Magento\Backend\Block\GlobalSearch;
10+
use Magento\Backend\Model\GlobalSearch\SearchEntity;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
13+
/**
14+
* Test for @see GlobalSearch.
15+
*/
16+
class GlobalSearchTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var GlobalSearch
20+
*/
21+
private $globalSearch;
22+
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\AuthorizationInterface
25+
*/
26+
private $authorization;
27+
28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface
30+
*/
31+
private $urlBuilder;
32+
33+
/**
34+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Backend\Model\GlobalSearch\SearchEntityFactory
35+
*/
36+
private $searchEntityFactory;
37+
38+
/**
39+
* @var array
40+
*/
41+
private $entityResources = [
42+
'Products' => \Magento\Catalog\Controller\Adminhtml\Product::ADMIN_RESOURCE,
43+
'Orders' => \Magento\Sales\Controller\Adminhtml\Order::ADMIN_RESOURCE,
44+
'Customers' => \Magento\Customer\Controller\Adminhtml\Index::ADMIN_RESOURCE,
45+
'Pages' => \Magento\Cms\Controller\Adminhtml\Page\Index::ADMIN_RESOURCE,
46+
];
47+
48+
/**
49+
* @var array
50+
*/
51+
private $entityPaths = [
52+
'Products' => 'catalog/product/index/',
53+
'Orders' => 'sales/order/index/',
54+
'Customers' => 'customer/index/index',
55+
'Pages' => 'cms/page/index/',
56+
];
57+
58+
protected function setUp()
59+
{
60+
$objectManager = new ObjectManager($this);
61+
62+
$this->authorization = $this->createMock(\Magento\Framework\AuthorizationInterface::class);
63+
$this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
64+
$context = $this->createMock(\Magento\Backend\Block\Template\Context::class);
65+
66+
$context->expects($this->atLeastOnce())->method('getAuthorization')->willReturn($this->authorization);
67+
$context->expects($this->atLeastOnce())->method('getUrlBuilder')->willReturn($this->urlBuilder);
68+
69+
$this->searchEntityFactory = $this->createMock(\Magento\Backend\Model\GlobalSearch\SearchEntityFactory::class);
70+
71+
$this->globalSearch = $objectManager->getObject(
72+
GlobalSearch::class,
73+
[
74+
'context' => $context,
75+
'searchEntityFactory' => $this->searchEntityFactory,
76+
'entityResources' => $this->entityResources,
77+
'entityPaths' => $this->entityPaths,
78+
]
79+
);
80+
}
81+
82+
/**
83+
* @param array $results
84+
* @param int $expectedEntitiesQty
85+
*
86+
* @dataProvider getEntitiesToShowDataProvider
87+
*/
88+
public function testGetEntitiesToShow(array $results, int $expectedEntitiesQty)
89+
{
90+
$searchEntity = $this->createMock(SearchEntity::class);
91+
92+
$this->authorization->expects($this->exactly(count($results)))->method('isAllowed')
93+
->willReturnOnConsecutiveCalls($results[0], $results[1], $results[2], $results[3]);
94+
$this->urlBuilder->expects($this->exactly($expectedEntitiesQty))
95+
->method('getUrl')->willReturn('some/url/is/here');
96+
$this->searchEntityFactory->expects($this->exactly($expectedEntitiesQty))
97+
->method('create')->willReturn($searchEntity);
98+
99+
$searchEntity->expects($this->exactly($expectedEntitiesQty))->method('setId');
100+
$searchEntity->expects($this->exactly($expectedEntitiesQty))->method('setTitle');
101+
$searchEntity->expects($this->exactly($expectedEntitiesQty))->method('setUrl');
102+
103+
$this->assertSame($expectedEntitiesQty, count($this->globalSearch->getEntitiesToShow()));
104+
}
105+
106+
public function getEntitiesToShowDataProvider()
107+
{
108+
return [
109+
[
110+
[true, false, true, false],
111+
2,
112+
],
113+
[
114+
[true, true, true, true],
115+
4,
116+
],
117+
[
118+
[false, false, false, false],
119+
0,
120+
],
121+
];
122+
}
123+
}

app/code/Magento/Backend/etc/adminhtml/di.xml

+16
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,20 @@
155155
<argument name="defaultClass" xsi:type="string">Magento\Backend\Block\Template</argument>
156156
</arguments>
157157
</type>
158+
<type name="Magento\Backend\Block\GlobalSearch">
159+
<arguments>
160+
<argument name="entityResources" xsi:type="array">
161+
<item name="Products" xsi:type="string">Magento_Catalog::products</item>
162+
<item name="Orders" xsi:type="string">Magento_Sales::sales_order</item>
163+
<item name="Customers" xsi:type="string">Magento_Customer::manage</item>
164+
<item name="Pages" xsi:type="string">Magento_Cms::page</item>
165+
</argument>
166+
<argument name="entityPaths" xsi:type="array">
167+
<item name="Products" xsi:type="string">catalog/product/index/</item>
168+
<item name="Orders" xsi:type="string">sales/order/index/</item>
169+
<item name="Customers" xsi:type="string">customer/index/index</item>
170+
<item name="Pages" xsi:type="string">cms/page/index/</item>
171+
</argument>
172+
</arguments>
173+
</type>
158174
</config>

app/code/Magento/Backend/i18n/en_US.csv

+4
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,7 @@ Pagination,Pagination
461461
"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."
462462
"Anchor Text for Next","Anchor Text for Next"
463463
"Theme Name","Theme Name"
464+
"In Products","In Products"
465+
"In Orders","In Orders"
466+
"In Customers","In Customers"
467+
"In Pages","In Pages"

app/code/Magento/Backend/view/adminhtml/templates/system/search.phtml

+9-12
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@
2727
</form>
2828
<script data-template="search-suggest" type="text/x-magento-template">
2929
<ul class="search-global-menu">
30-
<li class="item">
31-
<a id="searchPreviewProducts" href="<?= /* @escapeNotVerified */ $block->getUrl('catalog/product/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Products</a>
32-
</li>
33-
<li class="item">
34-
<a id="searchPreviewOrders" href="<?= /* @escapeNotVerified */ $block->getUrl('sales/order/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Orders</a>
35-
</li>
36-
<li class="item">
37-
<a id="searchPreviewCustomers" href="<?= /* @escapeNotVerified */ $block->getUrl('customer/index/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Customers</a>
38-
</li>
39-
<li class="item">
40-
<a id="searchPreviewPages" href="<?= /* @escapeNotVerified */ $block->getUrl('cms/page/index/') ?>?search=<%- data.term%>" class="title">"<%- data.term%>" in Pages</a>
41-
</li>
30+
<?php foreach ($block->getEntitiesToShow() as $entity): ?>
31+
<li class="item">
32+
<a id="<?= /* @escapeNotVerified */ $entity->getId(); ?>"
33+
href="<?= /* @escapeNotVerified */ $entity->getUrl() ?>?search=<%- data.term%>"
34+
class="title">
35+
"<%- data.term%>" <?= /* @escapeNotVerified */ __($entity->getTitle()); ?>
36+
</a>
37+
</li>
38+
<?php endforeach; ?>
4239
<% if (data.items.length) { %>
4340
<% _.each(data.items, function(value){ %>
4441
<li class="item"

0 commit comments

Comments
 (0)