|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Store\Test\Unit\App\Action\Plugin; |
| 10 | + |
| 11 | +use Magento\Framework\App\Action\AbstractAction; |
| 12 | +use Magento\Framework\App\Http\Context as HttpContext; |
| 13 | +use Magento\Framework\App\RequestInterface; |
| 14 | +use Magento\Framework\Session\Generic; |
| 15 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 16 | +use Magento\Store\Api\StoreCookieManagerInterface; |
| 17 | +use Magento\Store\App\Action\Plugin\Context; |
| 18 | +use Magento\Store\Model\Store; |
| 19 | +use Magento\Store\Model\StoreManagerInterface; |
| 20 | +use Magento\Store\Model\Website; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class ContextNonDefaultStoreDirectLinkTest |
| 25 | + * |
| 26 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 27 | + */ |
| 28 | +class ContextNonDefaultStoreDirectLinkTest extends TestCase |
| 29 | +{ |
| 30 | + const CURRENCY_SESSION = 'CNY'; |
| 31 | + const CURRENCY_DEFAULT = 'USD'; |
| 32 | + const CURRENCY_CURRENT_STORE = 'UAH'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Test for full page cache hits from new http clients if store context was specified in the URL |
| 36 | + * |
| 37 | + * @dataProvider cacheHitOnDirectLinkToNonDefaultStoreView |
| 38 | + * @param string $customStore |
| 39 | + * @param string $defaultStore |
| 40 | + * @param string $expectedDefaultStore |
| 41 | + * @param bool $useStoreInUrl |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function testCacheHitOnDirectLinkToNonDefaultStoreView( |
| 45 | + string $customStore, |
| 46 | + string $defaultStore, |
| 47 | + string $expectedDefaultStore, |
| 48 | + bool $useStoreInUrl |
| 49 | + ) { |
| 50 | + $sessionMock = $this->createPartialMock(Generic::class, ['getCurrencyCode']); |
| 51 | + $httpContextMock = $this->createMock(HttpContext::class); |
| 52 | + $storeManager = $this->createMock(StoreManagerInterface::class); |
| 53 | + $storeCookieManager = $this->createMock(StoreCookieManagerInterface::class); |
| 54 | + $storeMock = $this->createMock(Store::class); |
| 55 | + $currentStoreMock = $this->createMock(Store::class); |
| 56 | + $requestMock = $this->getMockBuilder(RequestInterface::class)->getMockForAbstractClass(); |
| 57 | + $subjectMock = $this->getMockBuilder(AbstractAction::class) |
| 58 | + ->disableOriginalConstructor() |
| 59 | + ->getMockForAbstractClass(); |
| 60 | + |
| 61 | + $httpContextMock->expects($this->once()) |
| 62 | + ->method('getValue') |
| 63 | + ->with(StoreManagerInterface::CONTEXT_STORE) |
| 64 | + ->willReturn(null); |
| 65 | + |
| 66 | + $websiteMock = $this->createPartialMock( |
| 67 | + Website::class, |
| 68 | + ['getDefaultStore', '__wakeup'] |
| 69 | + ); |
| 70 | + |
| 71 | + $plugin = (new ObjectManager($this))->getObject( |
| 72 | + Context::class, |
| 73 | + [ |
| 74 | + 'session' => $sessionMock, |
| 75 | + 'httpContext' => $httpContextMock, |
| 76 | + 'storeManager' => $storeManager, |
| 77 | + 'storeCookieManager' => $storeCookieManager, |
| 78 | + ] |
| 79 | + ); |
| 80 | + |
| 81 | + $storeManager->method('getDefaultStoreView') |
| 82 | + ->willReturn($storeMock); |
| 83 | + |
| 84 | + $storeCookieManager->expects($this->once()) |
| 85 | + ->method('getStoreCodeFromCookie') |
| 86 | + ->willReturn('storeCookie'); |
| 87 | + |
| 88 | + $currentStoreMock->expects($this->any()) |
| 89 | + ->method('getDefaultCurrencyCode') |
| 90 | + ->willReturn(self::CURRENCY_CURRENT_STORE); |
| 91 | + |
| 92 | + $currentStoreMock->expects($this->any()) |
| 93 | + ->method('getCode') |
| 94 | + ->willReturn($customStore); |
| 95 | + |
| 96 | + $currentStoreMock->method('isUseStoreInUrl')->willReturn($useStoreInUrl); |
| 97 | + |
| 98 | + $storeManager->expects($this->any()) |
| 99 | + ->method('getWebsite') |
| 100 | + ->willReturn($websiteMock); |
| 101 | + |
| 102 | + $websiteMock->expects($this->any()) |
| 103 | + ->method('getDefaultStore') |
| 104 | + ->willReturn($storeMock); |
| 105 | + |
| 106 | + $storeMock->expects($this->any()) |
| 107 | + ->method('getDefaultCurrencyCode') |
| 108 | + ->willReturn(self::CURRENCY_DEFAULT); |
| 109 | + |
| 110 | + $storeMock->expects($this->any()) |
| 111 | + ->method('getCode') |
| 112 | + ->willReturn($defaultStore); |
| 113 | + |
| 114 | + $requestMock->expects($this->any()) |
| 115 | + ->method('getParam') |
| 116 | + ->with($this->equalTo('___store')) |
| 117 | + ->willReturn($defaultStore); |
| 118 | + |
| 119 | + $storeManager->method('getStore') |
| 120 | + ->with($defaultStore) |
| 121 | + ->willReturn($currentStoreMock); |
| 122 | + |
| 123 | + $sessionMock->expects($this->any()) |
| 124 | + ->method('getCurrencyCode') |
| 125 | + ->willReturn(self::CURRENCY_SESSION); |
| 126 | + |
| 127 | + $httpContextMock->expects($this->at(1))->method( |
| 128 | + 'setValue' |
| 129 | + )->with(StoreManagerInterface::CONTEXT_STORE, $customStore, $expectedDefaultStore); |
| 130 | + |
| 131 | + $httpContextMock->expects($this->at(2))->method('setValue'); |
| 132 | + |
| 133 | + $plugin->beforeDispatch( |
| 134 | + $subjectMock, |
| 135 | + $requestMock |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + public function cacheHitOnDirectLinkToNonDefaultStoreView() |
| 140 | + { |
| 141 | + return [ |
| 142 | + [ |
| 143 | + 'custom_store', |
| 144 | + 'default', |
| 145 | + 'custom_store', |
| 146 | + true, |
| 147 | + ], |
| 148 | + [ |
| 149 | + 'custom_store', |
| 150 | + 'default', |
| 151 | + 'default', |
| 152 | + false, |
| 153 | + ], |
| 154 | + [ |
| 155 | + 'default', |
| 156 | + 'default', |
| 157 | + 'default', |
| 158 | + true, |
| 159 | + ], |
| 160 | + ]; |
| 161 | + } |
| 162 | +} |
0 commit comments