|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Test\Unit\Plugin; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 6 | +use Algolia\AlgoliaSearch\Plugin\RenderingCacheContextPlugin; |
| 7 | +use Magento\Framework\App\Http\Context as HttpContext; |
| 8 | +use Magento\Framework\App\Request\Http; |
| 9 | +use Magento\Store\Api\Data\StoreInterface; |
| 10 | +use Magento\Store\Model\StoreManagerInterface; |
| 11 | +use Magento\UrlRewrite\Model\UrlFinderInterface; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class RenderingCacheContextPluginTest extends TestCase |
| 15 | +{ |
| 16 | + protected ?RenderingCacheContextPlugin $plugin; |
| 17 | + protected ?ConfigHelper $configHelper; |
| 18 | + protected ?StoreManagerInterface $storeManager; |
| 19 | + protected ?Http $request; |
| 20 | + protected ?UrlFinderInterface $urlFinder; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->configHelper = $this->createMock(ConfigHelper::class); |
| 25 | + $this->storeManager = $this->createMock(StoreManagerInterface::class); |
| 26 | + $this->request = $this->createMock(Http::class); |
| 27 | + $this->urlFinder = $this->createMock(UrlFinderInterface::class); |
| 28 | + |
| 29 | + $this->plugin = new RenderingCacheContextPluginTestable( |
| 30 | + $this->configHelper, |
| 31 | + $this->storeManager, |
| 32 | + $this->request, |
| 33 | + $this->urlFinder |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + protected function getStoreMock(): StoreInterface |
| 38 | + { |
| 39 | + $store = $this->createMock(StoreInterface::class); |
| 40 | + $store->method('getId')->willReturn(1); |
| 41 | + return $store; |
| 42 | + } |
| 43 | + |
| 44 | + public function testAfterGetDataAddsRenderingContextNoBackendRender(): void |
| 45 | + { |
| 46 | + $this->configHelper->method('preventBackendRendering')->willReturn(true); |
| 47 | + $this->storeManager->method('getStore')->willReturn($this->getStoreMock()); |
| 48 | + |
| 49 | + $this->request->method('getControllerName')->willReturn('category'); |
| 50 | + $this->configHelper->method('replaceCategories')->willReturn(true); |
| 51 | + |
| 52 | + $result = $this->plugin->afterGetData( |
| 53 | + $this->createMock(HttpContext::class), |
| 54 | + [] |
| 55 | + ); |
| 56 | + |
| 57 | + $this->assertArrayHasKey(RenderingCacheContextPlugin::RENDERING_CONTEXT, $result); |
| 58 | + $this->assertEquals(RenderingCacheContextPlugin::RENDERING_WITHOUT_BACKEND, $result[RenderingCacheContextPlugin::RENDERING_CONTEXT]); |
| 59 | + } |
| 60 | + |
| 61 | + public function testAfterGetDataAddsRenderingContextWithBackendRender(): void |
| 62 | + { |
| 63 | + $subject = $this->createMock(HttpContext::class); |
| 64 | + |
| 65 | + $this->configHelper->method('preventBackendRendering')->willReturn(false); |
| 66 | + $this->storeManager->method('getStore')->willReturn($this->getStoreMock()); |
| 67 | + |
| 68 | + $this->request->method('getControllerName')->willReturn('category'); |
| 69 | + $this->configHelper->method('replaceCategories')->willReturn(true); |
| 70 | + |
| 71 | + $result = $this->plugin->afterGetData( |
| 72 | + $this->createMock(HttpContext::class), |
| 73 | + [] |
| 74 | + ); |
| 75 | + |
| 76 | + $this->assertArrayHasKey(RenderingCacheContextPlugin::RENDERING_CONTEXT, $result); |
| 77 | + $this->assertEquals(RenderingCacheContextPlugin::RENDERING_WITH_BACKEND, $result[RenderingCacheContextPlugin::RENDERING_CONTEXT]); |
| 78 | + } |
| 79 | + |
| 80 | + public function testAfterGetDataDoesNotModifyDataIfNotApplicable(): void |
| 81 | + { |
| 82 | + $subject = $this->createMock(HttpContext::class); |
| 83 | + |
| 84 | + $this->configHelper->method('preventBackendRendering')->willReturn(false); |
| 85 | + $this->storeManager->method('getStore')->willReturn($this->getStoreMock()); |
| 86 | + |
| 87 | + $this->request->method('getControllerName')->willReturn('product'); |
| 88 | + $this->request->method('getRequestUri')->willReturn('some-product.html'); |
| 89 | + $this->configHelper->method('replaceCategories')->willReturn(false); |
| 90 | + |
| 91 | + $data = ['existing_key' => 'existing_value']; |
| 92 | + $result = $this->plugin->afterGetData($subject, $data); |
| 93 | + |
| 94 | + $this->assertEquals($data, $result); |
| 95 | + } |
| 96 | + |
| 97 | + public function testIsCategoryRoute(): void |
| 98 | + { |
| 99 | + $this->assertTrue($this->plugin->isCategoryRoute('catalog/category/view')); |
| 100 | + $this->assertFalse($this->plugin->isCategoryRoute('some/other/route')); |
| 101 | + } |
| 102 | + |
| 103 | + public function testGetOriginalRoute(): void |
| 104 | + { |
| 105 | + $storeId = 1; |
| 106 | + $requestUri = '/some-path'; |
| 107 | + $targetPath = 'catalog/category/view/id/42'; |
| 108 | + |
| 109 | + $this->request->method('getRequestUri')->willReturn($requestUri); |
| 110 | + |
| 111 | + $urlRewrite = $this->createMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class); |
| 112 | + $urlRewrite->method('getTargetPath')->willReturn($targetPath); |
| 113 | + |
| 114 | + $this->urlFinder->method('findOneByData')->willReturn($urlRewrite); |
| 115 | + |
| 116 | + $this->assertEquals($targetPath, $this->plugin->getOriginalRoute($storeId)); |
| 117 | + } |
| 118 | + |
| 119 | + public function testShouldApplyCacheContext(): void |
| 120 | + { |
| 121 | + $this->storeManager->method('getStore')->willReturn($this->getStoreMock()); |
| 122 | + |
| 123 | + $this->request->method('getControllerName')->willReturn('category'); |
| 124 | + $this->configHelper->method('replaceCategories')->willReturn(true); |
| 125 | + |
| 126 | + $this->assertTrue($this->plugin->shouldApplyCacheContext()); |
| 127 | + } |
| 128 | +} |
0 commit comments