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