|
| 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 | +} |
0 commit comments