|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Unit\Block; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Block\Configuration as ConfigurationBlock; |
| 6 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 7 | +use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper; |
| 8 | +use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper; |
| 9 | +use Algolia\AlgoliaSearch\Helper\Configuration\PersonalizationHelper; |
| 10 | +use Algolia\AlgoliaSearch\Helper\Data as CoreHelper; |
| 11 | +use Algolia\AlgoliaSearch\Helper\Entity\CategoryHelper; |
| 12 | +use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper; |
| 13 | +use Algolia\AlgoliaSearch\Helper\Entity\SuggestionHelper; |
| 14 | +use Algolia\AlgoliaSearch\Helper\LandingPageHelper; |
| 15 | +use Algolia\AlgoliaSearch\Registry\CurrentCategory; |
| 16 | +use Algolia\AlgoliaSearch\Registry\CurrentProduct; |
| 17 | +use Algolia\AlgoliaSearch\Service\AlgoliaConnector; |
| 18 | +use Algolia\AlgoliaSearch\Service\Product\SortingTransformer; |
| 19 | +use Magento\Catalog\Model\Category; |
| 20 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 21 | +use Magento\Framework\App\Http\Context as HttpContext; |
| 22 | +use Magento\Framework\App\Request\Http; |
| 23 | +use Magento\Framework\Data\Form\FormKey; |
| 24 | +use Magento\Framework\Locale\Currency; |
| 25 | +use Magento\Framework\Locale\Format; |
| 26 | +use Magento\Framework\Stdlib\DateTime\DateTime; |
| 27 | +use Magento\Framework\Url\Helper\Data as UrlHelper; |
| 28 | +use Magento\Framework\View\Element\Template\Context; |
| 29 | +use Magento\Search\Helper\Data as CatalogSearchHelper; |
| 30 | +use PHPUnit\Framework\TestCase; |
| 31 | + |
| 32 | +class ConfigurationTest extends TestCase |
| 33 | +{ |
| 34 | + protected ?ConfigurationBlock $configurationBlock; |
| 35 | + |
| 36 | + protected ?ConfigHelper $config; |
| 37 | + protected ?AutocompleteHelper $autocompleteConfig; |
| 38 | + protected ?InstantSearchHelper $instantSearchConfig; |
| 39 | + protected ?PersonalizationHelper $personalizationHelper; |
| 40 | + protected ?CatalogSearchHelper $catalogSearchHelper; |
| 41 | + protected ?ProductHelper $productHelper; |
| 42 | + protected ?Currency $currency; |
| 43 | + protected ?Format $format; |
| 44 | + protected ?CurrentProduct $currentProduct; |
| 45 | + protected ?AlgoliaConnector $algoliaConnector; |
| 46 | + protected ?UrlHelper $urlHelper; |
| 47 | + protected ?FormKey $formKey; |
| 48 | + protected ?HttpContext $httpContext; |
| 49 | + protected ?CoreHelper $coreHelper; |
| 50 | + protected ?CategoryHelper $categoryHelper; |
| 51 | + protected ?SuggestionHelper $suggestionHelper; |
| 52 | + protected ?LandingPageHelper $landingPageHelper; |
| 53 | + protected ?CheckoutSession $checkoutSession; |
| 54 | + protected ?DateTime $date; |
| 55 | + protected ?CurrentCategory $currentCategory; |
| 56 | + protected ?SortingTransformer $sortingTransformer; |
| 57 | + protected ?Context $context; |
| 58 | + |
| 59 | + protected ?Http $request; |
| 60 | + |
| 61 | + protected function setUp(): void |
| 62 | + { |
| 63 | + $this->config = $this->createMock(ConfigHelper::class); |
| 64 | + $this->autocompleteConfig = $this->createMock(AutocompleteHelper::class); |
| 65 | + $this->instantSearchConfig = $this->createMock(InstantSearchHelper::class); |
| 66 | + $this->personalizationHelper = $this->createMock(PersonalizationHelper::class); |
| 67 | + $this->catalogSearchHelper = $this->createMock(CatalogSearchHelper::class); |
| 68 | + $this->productHelper = $this->createMock(ProductHelper::class); |
| 69 | + $this->currency = $this->createMock(Currency::class); |
| 70 | + $this->format = $this->createMock(Format::class); |
| 71 | + $this->currentProduct = $this->createMock(CurrentProduct::class); |
| 72 | + $this->algoliaConnector = $this->createMock(AlgoliaConnector::class); |
| 73 | + $this->urlHelper = $this->createMock(UrlHelper::class); |
| 74 | + $this->formKey = $this->createMock(FormKey::class); |
| 75 | + $this->httpContext = $this->createMock(HttpContext::class); |
| 76 | + $this->coreHelper = $this->createMock(CoreHelper::class); |
| 77 | + $this->categoryHelper = $this->createMock(CategoryHelper::class); |
| 78 | + $this->suggestionHelper = $this->createMock(SuggestionHelper::class); |
| 79 | + $this->landingPageHelper = $this->createMock(LandingPageHelper::class); |
| 80 | + $this->checkoutSession = $this->createMock(CheckoutSession::class); |
| 81 | + $this->date = $this->createMock(DateTime::class); |
| 82 | + $this->currentCategory = $this->createMock(CurrentCategory::class); |
| 83 | + $this->sortingTransformer = $this->createMock(SortingTransformer::class); |
| 84 | + $this->context = $this->createMock(Context::class); |
| 85 | + |
| 86 | + $this->request = $this->createMock(Http::class); |
| 87 | + $this->context->method('getRequest')->willReturn($this->request); |
| 88 | + |
| 89 | + $this->configurationBlock = new ConfigurationBlock( |
| 90 | + $this->config, |
| 91 | + $this->autocompleteConfig , |
| 92 | + $this->instantSearchConfig , |
| 93 | + $this->personalizationHelper , |
| 94 | + $this->catalogSearchHelper , |
| 95 | + $this->productHelper, |
| 96 | + $this->currency , |
| 97 | + $this->format , |
| 98 | + $this->currentProduct , |
| 99 | + $this->algoliaConnector , |
| 100 | + $this->urlHelper , |
| 101 | + $this->formKey , |
| 102 | + $this->httpContext , |
| 103 | + $this->coreHelper , |
| 104 | + $this->categoryHelper, |
| 105 | + $this->suggestionHelper , |
| 106 | + $this->landingPageHelper , |
| 107 | + $this->checkoutSession, |
| 108 | + $this->date , |
| 109 | + $this->currentCategory , |
| 110 | + $this->sortingTransformer , |
| 111 | + $this->context, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @dataProvider searchPageDataProvider |
| 117 | + */ |
| 118 | + public function testIsSearchPage($action, $categoryId, $categoryDisplayMode, $expectedResult): void |
| 119 | + { |
| 120 | + $this->config->method('isInstantEnabled')->willReturn(true); |
| 121 | + $this->request->method('getFullActionName')->willReturn($action); |
| 122 | + |
| 123 | + $controller = explode('_', $action); |
| 124 | + $controller = $controller[1]; |
| 125 | + |
| 126 | + $this->request->method('getControllerName')->willReturn($controller); |
| 127 | + $this->config->method('replaceCategories')->willReturn(true); |
| 128 | + |
| 129 | + $category = $this->createMock(Category::class); |
| 130 | + $category->method('getId')->willReturn($categoryId); |
| 131 | + $category->method('getDisplayMode')->willReturn($categoryDisplayMode); |
| 132 | + $this->currentCategory->method('get')->willReturn($category); |
| 133 | + |
| 134 | + $this->assertEquals($expectedResult, $this->configurationBlock->isSearchPage()); |
| 135 | + } |
| 136 | + |
| 137 | + public static function searchPageDataProvider(): array |
| 138 | + { |
| 139 | + return [ |
| 140 | + [ // true if category has an ID |
| 141 | + 'action' => 'catalog_category_view', |
| 142 | + 'categoryId' => 1, |
| 143 | + 'categoryDisplayMode' => 'PRODUCT', |
| 144 | + 'expectedResult' => true |
| 145 | + ], |
| 146 | + [ // false if category has no ID |
| 147 | + 'action' => 'catalog_category_view', |
| 148 | + 'categoryId' => null, |
| 149 | + 'categoryDisplayMode' => 'PRODUCT', |
| 150 | + 'expectedResult' => false |
| 151 | + ], |
| 152 | + [ // false if category has a PAGE as display mode |
| 153 | + 'action' => 'catalog_category_view', |
| 154 | + 'categoryId' => 1, |
| 155 | + 'categoryDisplayMode' => 'PAGE', |
| 156 | + 'expectedResult' => false |
| 157 | + ], |
| 158 | + [ // true if catalogsearch |
| 159 | + 'action' => 'catalogsearch_result_index', |
| 160 | + 'categoryId' => null, |
| 161 | + 'categoryDisplayMode' => 'FOO', |
| 162 | + 'expectedResult' => true |
| 163 | + ], |
| 164 | + [ // true if landing page |
| 165 | + 'action' => 'algolia_landingpage_view', |
| 166 | + 'categoryId' => null, |
| 167 | + 'categoryDisplayMode' => 'FOO', |
| 168 | + 'expectedResult' => true |
| 169 | + ] |
| 170 | + ]; |
| 171 | + } |
| 172 | +} |
0 commit comments