ContextPluginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Test\Unit\App\Action;
  7. /**
  8. * Class ContextPluginTest
  9. *
  10. * @package Magento\Weee\Test\Unit\App\Action
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class ContextPluginTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Tax\Helper\Data
  17. */
  18. protected $taxHelperMock;
  19. /**
  20. * @var \Magento\Weee\Helper\Data
  21. */
  22. protected $weeeHelperMock;
  23. /**
  24. * @var \Magento\Weee\Model\Tax
  25. */
  26. protected $weeeTaxMock;
  27. /**
  28. * @var \Magento\Framework\App\Http\Context
  29. */
  30. protected $httpContextMock;
  31. /**
  32. * @var \Magento\Tax\Model\Calculation\Proxy
  33. */
  34. protected $taxCalculationMock;
  35. /**
  36. * @var \Magento\Framework\Module\Manager
  37. */
  38. protected $moduleManagerMock;
  39. /**
  40. * @var \Magento\PageCache\Model\Config
  41. */
  42. protected $cacheConfigMock;
  43. /**
  44. * @var \Magento\Store\Model\StoreManager
  45. */
  46. protected $storeManageMock;
  47. /**
  48. * @var \Magento\Framework\App\Config\ScopeConfig
  49. */
  50. protected $scopeConfigMock;
  51. /**
  52. * @var \Magento\Tax\Model\App\Action\ContextPlugin
  53. */
  54. protected $contextPlugin;
  55. protected function setUp()
  56. {
  57. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  58. $this->taxHelperMock = $this->getMockBuilder(\Magento\Tax\Helper\Data::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->weeeHelperMock = $this->getMockBuilder(\Magento\Weee\Helper\Data::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->weeeTaxMock = $this->getMockBuilder(\Magento\Weee\Model\Tax::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->httpContextMock = $this->getMockBuilder(\Magento\Framework\App\Http\Context::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->customerSessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  71. ->disableOriginalConstructor()
  72. ->setMethods([
  73. 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId',
  74. 'getWebsiteId', 'isLoggedIn'
  75. ])
  76. ->getMock();
  77. $this->moduleManagerMock = $this->getMockBuilder(\Magento\Framework\Module\Manager::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->cacheConfigMock = $this->getMockBuilder(\Magento\PageCache\Model\Config::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManager::class)
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config::class)
  87. ->disableOriginalConstructor()
  88. ->getMock();
  89. $this->contextPlugin = $this->objectManager->getObject(
  90. \Magento\Weee\Model\App\Action\ContextPlugin::class,
  91. [
  92. 'customerSession' => $this->customerSessionMock,
  93. 'httpContext' => $this->httpContextMock,
  94. 'weeeTax' => $this->weeeTaxMock,
  95. 'taxHelper' => $this->taxHelperMock,
  96. 'weeeHelper' => $this->weeeHelperMock,
  97. 'moduleManager' => $this->moduleManagerMock,
  98. 'cacheConfig' => $this->cacheConfigMock,
  99. 'storeManager' => $this->storeManagerMock,
  100. 'scopeConfig' => $this->scopeConfigMock
  101. ]
  102. );
  103. }
  104. public function testBeforeDispatchBasedOnDefault()
  105. {
  106. $this->customerSessionMock->expects($this->once())
  107. ->method('isLoggedIn')
  108. ->willReturn(true);
  109. $this->moduleManagerMock->expects($this->once())
  110. ->method('isEnabled')
  111. ->with('Magento_PageCache')
  112. ->willReturn(true);
  113. $this->cacheConfigMock->expects($this->once())
  114. ->method('isEnabled')
  115. ->willReturn(true);
  116. $this->weeeHelperMock->expects($this->once())
  117. ->method('isEnabled')
  118. ->willReturn(true);
  119. $this->taxHelperMock->expects($this->once())
  120. ->method('getTaxBasedOn')
  121. ->willReturn('billing');
  122. $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. $storeMock->expects($this->once())
  126. ->method('getWebsiteId')
  127. ->willReturn(1);
  128. $this->storeManagerMock->expects($this->once())
  129. ->method('getStore')
  130. ->willReturn($storeMock);
  131. $this->scopeConfigMock->expects($this->at(0))
  132. ->method('getValue')
  133. ->with(
  134. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
  135. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  136. null
  137. )
  138. ->willReturn('US');
  139. $this->scopeConfigMock->expects($this->at(1))
  140. ->method('getValue')
  141. ->with(
  142. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
  143. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  144. null
  145. )
  146. ->willReturn(0);
  147. $this->weeeTaxMock->expects($this->once())
  148. ->method('isWeeeInLocation')
  149. ->with('US', 0, 1)
  150. ->willReturn(true);
  151. $this->httpContextMock->expects($this->once())
  152. ->method('setValue')
  153. ->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 0], 0);
  154. $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class);
  155. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getActionName']);
  156. $this->contextPlugin->beforeDispatch($action, $request);
  157. }
  158. public function testBeforeDispatchBasedOnOrigin()
  159. {
  160. $this->customerSessionMock->expects($this->once())
  161. ->method('isLoggedIn')
  162. ->willReturn(true);
  163. $this->moduleManagerMock->expects($this->once())
  164. ->method('isEnabled')
  165. ->with('Magento_PageCache')
  166. ->willReturn(true);
  167. $this->cacheConfigMock->expects($this->once())
  168. ->method('isEnabled')
  169. ->willReturn(true);
  170. $this->weeeHelperMock->expects($this->once())
  171. ->method('isEnabled')
  172. ->willReturn(true);
  173. $this->taxHelperMock->expects($this->once())
  174. ->method('getTaxBasedOn')
  175. ->willReturn('origin');
  176. $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class);
  177. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getActionName']);
  178. $this->contextPlugin->beforeDispatch($action, $request);
  179. }
  180. public function testBeforeDispatchBasedOnBilling()
  181. {
  182. $this->customerSessionMock->expects($this->once())
  183. ->method('isLoggedIn')
  184. ->willReturn(true);
  185. $this->moduleManagerMock->expects($this->once())
  186. ->method('isEnabled')
  187. ->with('Magento_PageCache')
  188. ->willReturn(true);
  189. $this->cacheConfigMock->expects($this->once())
  190. ->method('isEnabled')
  191. ->willReturn(true);
  192. $this->weeeHelperMock->expects($this->once())
  193. ->method('isEnabled')
  194. ->willReturn(true);
  195. $this->taxHelperMock->expects($this->once())
  196. ->method('getTaxBasedOn')
  197. ->willReturn('billing');
  198. $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  199. ->disableOriginalConstructor()
  200. ->getMock();
  201. $storeMock->expects($this->once())
  202. ->method('getWebsiteId')
  203. ->willReturn(1);
  204. $this->storeManagerMock->expects($this->once())
  205. ->method('getStore')
  206. ->willReturn($storeMock);
  207. $this->scopeConfigMock->expects($this->at(0))
  208. ->method('getValue')
  209. ->with(
  210. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
  211. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  212. null
  213. )
  214. ->willReturn('US');
  215. $this->scopeConfigMock->expects($this->at(1))
  216. ->method('getValue')
  217. ->with(
  218. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
  219. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  220. null
  221. )
  222. ->willReturn(0);
  223. $this->customerSessionMock->expects($this->once())
  224. ->method('getDefaultTaxBillingAddress')
  225. ->willReturn(['country_id' => 'US', 'region_id' => 1]);
  226. $this->weeeTaxMock->expects($this->once())
  227. ->method('isWeeeInLocation')
  228. ->with('US', 1, 1)
  229. ->willReturn(true);
  230. $this->httpContextMock->expects($this->once())
  231. ->method('setValue')
  232. ->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 1], 0);
  233. $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class);
  234. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getActionName']);
  235. $this->contextPlugin->beforeDispatch($action, $request);
  236. }
  237. public function testBeforeDispatchBasedOnShipping()
  238. {
  239. $this->customerSessionMock->expects($this->once())
  240. ->method('isLoggedIn')
  241. ->willReturn(true);
  242. $this->moduleManagerMock->expects($this->once())
  243. ->method('isEnabled')
  244. ->with('Magento_PageCache')
  245. ->willReturn(true);
  246. $this->cacheConfigMock->expects($this->once())
  247. ->method('isEnabled')
  248. ->willReturn(true);
  249. $this->weeeHelperMock->expects($this->once())
  250. ->method('isEnabled')
  251. ->willReturn(true);
  252. $this->taxHelperMock->expects($this->once())
  253. ->method('getTaxBasedOn')
  254. ->willReturn('shipping');
  255. $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  256. ->disableOriginalConstructor()
  257. ->getMock();
  258. $storeMock->expects($this->once())
  259. ->method('getWebsiteId')
  260. ->willReturn(1);
  261. $this->storeManagerMock->expects($this->once())
  262. ->method('getStore')
  263. ->willReturn($storeMock);
  264. $this->scopeConfigMock->expects($this->at(0))
  265. ->method('getValue')
  266. ->with(
  267. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
  268. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  269. null
  270. )
  271. ->willReturn('US');
  272. $this->scopeConfigMock->expects($this->at(1))
  273. ->method('getValue')
  274. ->with(
  275. \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
  276. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  277. null
  278. )
  279. ->willReturn(0);
  280. $this->customerSessionMock->expects($this->once())
  281. ->method('getDefaultTaxShippingAddress')
  282. ->willReturn(['country_id' => 'US', 'region_id' => 1]);
  283. $this->weeeTaxMock->expects($this->once())
  284. ->method('isWeeeInLocation')
  285. ->with('US', 1, 1)
  286. ->willReturn(true);
  287. $this->httpContextMock->expects($this->once())
  288. ->method('setValue')
  289. ->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 1], 0);
  290. $action = $this->objectManager->getObject(\Magento\Framework\App\Test\Unit\Action\Stub\ActionStub::class);
  291. $request = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getActionName']);
  292. $this->contextPlugin->beforeDispatch($action, $request);
  293. }
  294. }