|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Unit\Service; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 6 | +use Algolia\AlgoliaSearch\Helper\Configuration\AutocompleteHelper; |
| 7 | +use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper; |
| 8 | +use Algolia\AlgoliaSearch\Registry\CurrentCategory; |
| 9 | +use Algolia\AlgoliaSearch\Service\RenderingManager; |
| 10 | +use Magento\Catalog\Model\Category; |
| 11 | +use Magento\Framework\View\Layout; |
| 12 | +use Magento\Framework\View\Layout\ProcessorInterface; |
| 13 | +use Magento\Store\Model\StoreManagerInterface; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +class RenderingManagerTest extends TestCase |
| 17 | +{ |
| 18 | + protected ?ConfigHelper $configHelper; |
| 19 | + protected ?AutocompleteHelper $autocompleteConfigHelper; |
| 20 | + protected ?InstantSearchHelper $instantSearchConfigHelper; |
| 21 | + protected ?CurrentCategory $category; |
| 22 | + protected ?StoreManagerInterface $storeManager; |
| 23 | + |
| 24 | + protected ?RenderingManager $renderingManager; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + $this->configHelper = $this->createMock(ConfigHelper::class); |
| 29 | + $this->autocompleteConfigHelper = $this->createMock(AutocompleteHelper::class); |
| 30 | + $this->instantSearchConfigHelper = $this->createMock(InstantSearchHelper::class); |
| 31 | + $this->category = $this->createMock(CurrentCategory::class); |
| 32 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 33 | + |
| 34 | + $this->renderingManager = new RenderingManager( |
| 35 | + $this->configHelper, |
| 36 | + $this->autocompleteConfigHelper, |
| 37 | + $this->instantSearchConfigHelper, |
| 38 | + $this->category, |
| 39 | + $this->storeManager |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @dataProvider frontendValuesProvider |
| 45 | + */ |
| 46 | + public function testFrontendAssets($isAutocompleteEnabled, $isInstantSearchEnabled, $isLayoutUpdated): void |
| 47 | + { |
| 48 | + $this->autocompleteConfigHelper->method('isEnabled')->willReturn($isAutocompleteEnabled); |
| 49 | + $this->instantSearchConfigHelper->method('isEnabled')->willReturn($isInstantSearchEnabled); |
| 50 | + |
| 51 | + $layout = $this->createMock(Layout::class); |
| 52 | + $update = $this->createMock(ProcessorInterface::class); |
| 53 | + $layout->method('getUpdate')->willReturn($update); |
| 54 | + |
| 55 | + if ($isLayoutUpdated) { |
| 56 | + $update->expects($this->once()) |
| 57 | + ->method('addHandle') |
| 58 | + ->with('algolia_search_handle'); |
| 59 | + } else { |
| 60 | + $update->expects($this->never()) |
| 61 | + ->method('addHandle'); |
| 62 | + } |
| 63 | + |
| 64 | + $this->renderingManager->handleFrontendAssets($layout, 0); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @dataProvider backendValuesProvider |
| 69 | + */ |
| 70 | + public function testBackendRendering($actionName, $preventBackendRendering, $isLayoutUpdated): void |
| 71 | + { |
| 72 | + $this->autocompleteConfigHelper->method('isEnabled')->willReturn(true); |
| 73 | + $this->instantSearchConfigHelper->method('isEnabled')->willReturn(true); |
| 74 | + $this->configHelper->method('preventBackendRendering')->willReturn($preventBackendRendering); |
| 75 | + |
| 76 | + $layout = $this->createMock(Layout::class); |
| 77 | + $update = $this->createMock(ProcessorInterface::class); |
| 78 | + $layout->method('getUpdate')->willReturn($update); |
| 79 | + |
| 80 | + if ($isLayoutUpdated) { |
| 81 | + $update->expects($this->once()) |
| 82 | + ->method('addHandle') |
| 83 | + ->with('algolia_search_handle_prevent_backend_rendering'); |
| 84 | + } else { |
| 85 | + $update->expects($this->never()) |
| 86 | + ->method('addHandle'); |
| 87 | + } |
| 88 | + |
| 89 | + $this->renderingManager->handleBackendRendering($layout, $actionName, 0); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dataProvider displayValuesProvider |
| 94 | + */ |
| 95 | + public function testDisplayMode($displayMode, $categoryDisplayMode, $isLayoutUpdated): void |
| 96 | + { |
| 97 | + $this->autocompleteConfigHelper->method('isEnabled')->willReturn(true); |
| 98 | + $this->instantSearchConfigHelper->method('isEnabled')->willReturn(true); |
| 99 | + $this->instantSearchConfigHelper->method('shouldReplaceCategories')->willReturn(true); |
| 100 | + $this->configHelper->method('preventBackendRendering')->willReturn(true); |
| 101 | + $this->configHelper->method('getBackendRenderingDisplayMode')->willReturn($displayMode); |
| 102 | + |
| 103 | + $currentCategory = $this->createMock(Category::class); |
| 104 | + $this->category->method('get')->willReturn($currentCategory); |
| 105 | + $currentCategory->method('getId')->willReturn(1); |
| 106 | + $currentCategory->method('getDisplayMode')->willReturn($categoryDisplayMode); |
| 107 | + |
| 108 | + $layout = $this->createMock(Layout::class); |
| 109 | + $update = $this->createMock(ProcessorInterface::class); |
| 110 | + $layout->method('getUpdate')->willReturn($update); |
| 111 | + |
| 112 | + if ($isLayoutUpdated) { |
| 113 | + $update->expects($this->once()) |
| 114 | + ->method('addHandle') |
| 115 | + ->with('algolia_search_handle_prevent_backend_rendering'); |
| 116 | + } else { |
| 117 | + $update->expects($this->never()) |
| 118 | + ->method('addHandle'); |
| 119 | + } |
| 120 | + |
| 121 | + $this->renderingManager->handleBackendRendering($layout, 'catalog_category_view', 0); |
| 122 | + } |
| 123 | + |
| 124 | + public static function frontendValuesProvider(): array |
| 125 | + { |
| 126 | + return [ |
| 127 | + ['isAutocompleteEnabled' => true, 'isInstantSearchEnabled' => true, 'isLayoutUpdated' => true], |
| 128 | + ['isAutocompleteEnabled' => false, 'isInstantSearchEnabled' => true, 'isLayoutUpdated' => true], |
| 129 | + ['isAutocompleteEnabled' => true, 'isInstantSearchEnabled' => false, 'isLayoutUpdated' => true], |
| 130 | + ['isAutocompleteEnabled' => false, 'isInstantSearchEnabled' => false, 'isLayoutUpdated' => false] |
| 131 | + ]; |
| 132 | + } |
| 133 | + |
| 134 | + public static function backendValuesProvider(): array |
| 135 | + { |
| 136 | + return [ |
| 137 | + ['actionName' => 'catalog_category_view','preventBackendRendering' => true, 'isLayoutUpdated' => true], |
| 138 | + ['actionName' => 'catalogsearch_result_index', 'preventBackendRendering' => true, 'isLayoutUpdated' => true], |
| 139 | + ['actionName' => 'foo_bar', 'preventBackendRendering' => true, 'isLayoutUpdated' => false], |
| 140 | + ['actionName' => 'catalog_category_view', 'preventBackendRendering' => false, 'isLayoutUpdated' => false] |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + public static function displayValuesProvider(): array |
| 145 | + { |
| 146 | + return [ |
| 147 | + ['displayMode' => 'all', 'categoryDisplayMode' => 'PAGE', 'isLayoutUpdated' => true], |
| 148 | + ['displayMode' => 'all', 'categoryDisplayMode' => 'PRODUCTS', 'isLayoutUpdated' => true], |
| 149 | + ['displayMode' => 'all', 'categoryDisplayMode' => 'PRODUCTS_AND_PAGE', 'isLayoutUpdated' => true], |
| 150 | + ['displayMode' => 'only_products', 'categoryDisplayMode' => 'PAGE', 'isLayoutUpdated' => false], |
| 151 | + ['displayMode' => 'only_products', 'categoryDisplayMode' => 'PRODUCTS', 'isLayoutUpdated' => true], |
| 152 | + ['displayMode' => 'only_products', 'categoryDisplayMode' => 'PRODUCTS_AND_PAGE', 'isLayoutUpdated' => true], |
| 153 | + ]; |
| 154 | + } |
| 155 | +} |
0 commit comments