StoreTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Test\Unit\Model;
  7. use Magento\Framework\App\Config\ReinitableConfigInterface;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\Session\SessionManagerInterface;
  10. use Magento\Store\Model\ScopeInterface;
  11. use Magento\Store\Model\Store;
  12. /**
  13. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  14. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class StoreTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Store\Model\Store
  21. */
  22. protected $store;
  23. /**
  24. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  25. */
  26. protected $objectManagerHelper;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface
  29. */
  30. protected $requestMock;
  31. /**
  32. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $filesystemMock;
  35. /**
  36. * @var ReinitableConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $configMock;
  39. /**
  40. * @var SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $sessionMock;
  43. /**
  44. * @var \Magento\Framework\Url\ModifierInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $urlModifierMock;
  47. /**
  48. * @return void
  49. */
  50. protected function setUp()
  51. {
  52. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  53. $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, [
  54. 'getRequestString',
  55. 'getModuleName',
  56. 'setModuleName',
  57. 'getActionName',
  58. 'setActionName',
  59. 'getParam',
  60. 'getQueryValue',
  61. 'getDistroBaseUrl',
  62. 'isSecure',
  63. 'getServer',
  64. ]);
  65. $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->configMock = $this->getMockBuilder(ReinitableConfigInterface::class)
  69. ->getMock();
  70. $this->sessionMock = $this->getMockBuilder(SessionManagerInterface::class)
  71. ->setMethods(['getCurrencyCode'])
  72. ->getMockForAbstractClass();
  73. $this->store = $this->objectManagerHelper->getObject(
  74. \Magento\Store\Model\Store::class,
  75. [
  76. 'filesystem' => $this->filesystemMock,
  77. 'config' => $this->configMock,
  78. 'session' => $this->sessionMock,
  79. ]
  80. );
  81. $this->urlModifierMock = $this->createMock(\Magento\Framework\Url\ModifierInterface::class);
  82. $this->urlModifierMock->expects($this->any())
  83. ->method('execute')
  84. ->willReturnArgument(0);
  85. }
  86. /**
  87. * @dataProvider loadDataProvider
  88. *
  89. * @param string|int $key
  90. * @param string $field
  91. */
  92. public function testLoad($key, $field)
  93. {
  94. /** @var \Magento\Store\Model\ResourceModel\Store $resource */
  95. $resource = $this->createPartialMock(
  96. \Magento\Store\Model\ResourceModel\Store::class,
  97. ['load', 'getIdFieldName', '__wakeup']
  98. );
  99. $resource->expects($this->atLeastOnce())->method('load')
  100. ->with($this->isInstanceOf(\Magento\Store\Model\Store::class), $this->equalTo($key), $this->equalTo($field))
  101. ->will($this->returnSelf());
  102. $resource->expects($this->atLeastOnce())->method('getIdFieldName')->will($this->returnValue('store_id'));
  103. /** @var \Magento\Store\Model\Store $model */
  104. $model = $this->objectManagerHelper->getObject(\Magento\Store\Model\Store::class, ['resource' => $resource]);
  105. $model->load($key);
  106. }
  107. /**
  108. * @return array
  109. */
  110. public function loadDataProvider()
  111. {
  112. return [
  113. [1, null],
  114. ['default', 'code'],
  115. ];
  116. }
  117. /**
  118. * @return void
  119. */
  120. public function testSetWebsite()
  121. {
  122. $website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getId', '__wakeup']);
  123. $website->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(2));
  124. /** @var \Magento\Store\Model\Store $model */
  125. $model = $this->objectManagerHelper->getObject(\Magento\Store\Model\Store::class);
  126. $model->setWebsite($website);
  127. $this->assertEquals(2, $model->getWebsiteId());
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function testGetWebsite()
  133. {
  134. $websiteId = 2;
  135. $website = $this->createMock(\Magento\Store\Api\Data\WebsiteInterface::class);
  136. $websiteRepository = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class)
  137. ->setMethods(['getById'])
  138. ->getMockForAbstractClass();
  139. $websiteRepository->expects($this->once())
  140. ->method('getById')
  141. ->with($websiteId)
  142. ->willReturn($website);
  143. /** @var \Magento\Store\Model\Store $model */
  144. $model = $this->objectManagerHelper->getObject(
  145. \Magento\Store\Model\Store::class,
  146. ['websiteRepository' => $websiteRepository,]
  147. );
  148. $model->setWebsiteId($websiteId);
  149. $this->assertEquals($website, $model->getWebsite());
  150. }
  151. /**
  152. * @return void
  153. */
  154. public function testGetWebsiteIfWebsiteIsNotExist()
  155. {
  156. $websiteRepository = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class)
  157. ->setMethods(['getById'])
  158. ->getMockForAbstractClass();
  159. $websiteRepository->expects($this->never())
  160. ->method('getById');
  161. /** @var \Magento\Store\Model\Store $model */
  162. $model = $this->objectManagerHelper->getObject(
  163. \Magento\Store\Model\Store::class,
  164. ['websiteRepository' => $websiteRepository,]
  165. );
  166. $model->setWebsiteId(null);
  167. $this->assertFalse($model->getWebsite());
  168. }
  169. /**
  170. * @return void
  171. */
  172. public function testGetGroup()
  173. {
  174. $groupId = 2;
  175. $group = $this->createMock(\Magento\Store\Api\Data\GroupInterface::class);
  176. $groupRepository = $this->getMockBuilder(\Magento\Store\Api\GroupRepositoryInterface::class)
  177. ->setMethods(['get'])
  178. ->getMockForAbstractClass();
  179. $groupRepository->expects($this->once())
  180. ->method('get')
  181. ->with($groupId)
  182. ->willReturn($group);
  183. /** @var \Magento\Store\Model\Store $model */
  184. $model = $this->objectManagerHelper->getObject(
  185. \Magento\Store\Model\Store::class,
  186. ['groupRepository' => $groupRepository,]
  187. );
  188. $model->setGroupId($groupId);
  189. $this->assertEquals($group, $model->getGroup());
  190. }
  191. /**
  192. * @return void
  193. */
  194. public function testGetGroupIfGroupIsNotExist()
  195. {
  196. $groupRepository = $this->getMockBuilder(\Magento\Store\Api\GroupRepositoryInterface::class)
  197. ->setMethods(['getById'])
  198. ->getMockForAbstractClass();
  199. $groupRepository->expects($this->never())
  200. ->method('getById');
  201. /** @var \Magento\Store\Model\Store $model */
  202. $model = $this->objectManagerHelper->getObject(
  203. \Magento\Store\Model\Store::class,
  204. ['groupRepository' => $groupRepository,]
  205. );
  206. $model->setGroupId(null);
  207. $this->assertFalse($model->getGroup());
  208. }
  209. /**
  210. * @return void
  211. */
  212. public function testGetUrl()
  213. {
  214. $params = ['_scope_to_url' => true];
  215. $defaultStore = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId', '__wakeup']);
  216. $defaultStore->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(5));
  217. $url = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
  218. $url->expects($this->atLeastOnce())->method('setScope')->will($this->returnSelf());
  219. $url->expects($this->atLeastOnce())->method('getUrl')
  220. ->with($this->equalTo('test/route'), $this->equalTo($params))
  221. ->will($this->returnValue('http://test/url'));
  222. $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
  223. $storeManager->expects($this->any())
  224. ->method('getStore')
  225. ->will($this->returnValue($defaultStore));
  226. /** @var \Magento\Store\Model\Store $model */
  227. $model = $this->objectManagerHelper->getObject(
  228. \Magento\Store\Model\Store::class,
  229. ['storeManager' => $storeManager, 'url' => $url]
  230. );
  231. $model->setStoreId(2);
  232. $this->assertEquals('http://test/url', $model->getUrl('test/route'));
  233. }
  234. /**
  235. * @dataProvider getBaseUrlDataProvider
  236. *
  237. * @covers \Magento\Store\Model\Store::getBaseUrl
  238. * @covers \Magento\Store\Model\Store::getCode
  239. * @covers \Magento\Store\Model\Store::_updatePathUseRewrites
  240. * @covers \Magento\Store\Model\Store::getConfig
  241. *
  242. * @param string $type
  243. * @param boolean $secure
  244. * @param string $expectedPath
  245. * @param string $expectedBaseUrl
  246. */
  247. public function testGetBaseUrl($type, $secure, $expectedPath, $expectedBaseUrl)
  248. {
  249. $this->requestMock->expects($this->any())
  250. ->method('getDistroBaseUrl')
  251. ->will($this->returnValue('http://distro.com/'));
  252. /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $configMock */
  253. $configMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  254. $configMock->expects($this->atLeastOnce())
  255. ->method('getValue')
  256. ->will($this->returnCallback(
  257. function ($path, $scope, $scopeCode) use ($secure, $expectedPath) {
  258. $url = $secure ? '{{base_url}}' : 'http://domain.com/';
  259. return $expectedPath == $path ? $url . $path . '/' : null;
  260. }
  261. ));
  262. /** @var \Magento\Store\Model\Store $model */
  263. $model = $this->objectManagerHelper->getObject(
  264. \Magento\Store\Model\Store::class,
  265. [
  266. 'config' => $configMock,
  267. 'request' => $this->requestMock,
  268. 'isCustomEntryPoint' => !$secure,
  269. ]
  270. );
  271. $model->setCode('scopeCode');
  272. $this->setUrlModifier($model);
  273. $this->assertEquals($expectedBaseUrl, $model->getBaseUrl($type, $secure));
  274. }
  275. /**
  276. * @return array
  277. */
  278. public function getBaseUrlDataProvider()
  279. {
  280. return [
  281. [
  282. \Magento\Framework\UrlInterface::URL_TYPE_WEB,
  283. false,
  284. 'web/unsecure/base_url',
  285. 'http://domain.com/web/unsecure/base_url/'
  286. ],
  287. [
  288. \Magento\Framework\UrlInterface::URL_TYPE_LINK,
  289. false,
  290. 'web/unsecure/base_link_url',
  291. 'http://domain.com/web/unsecure/base_link_url/index.php/'
  292. ],
  293. [
  294. \Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK,
  295. false,
  296. 'web/unsecure/base_link_url',
  297. 'http://domain.com/web/unsecure/base_link_url/index.php/'
  298. ],
  299. [
  300. \Magento\Framework\UrlInterface::URL_TYPE_MEDIA,
  301. false,
  302. 'web/unsecure/base_media_url',
  303. 'http://domain.com/web/unsecure/base_media_url/'
  304. ],
  305. [
  306. \Magento\Framework\UrlInterface::URL_TYPE_STATIC,
  307. false,
  308. 'web/unsecure/base_static_url',
  309. 'http://domain.com/web/unsecure/base_static_url/'
  310. ],
  311. [
  312. \Magento\Framework\UrlInterface::URL_TYPE_MEDIA,
  313. false,
  314. 'web/unsecure/base_url',
  315. 'http://domain.com/web/unsecure/base_url/'
  316. ],
  317. [
  318. \Magento\Framework\UrlInterface::URL_TYPE_STATIC,
  319. false,
  320. 'web/unsecure/base_url',
  321. 'http://domain.com/web/unsecure/base_url/'
  322. ],
  323. [
  324. \Magento\Framework\UrlInterface::URL_TYPE_WEB,
  325. true,
  326. 'web/secure/base_url',
  327. 'http://distro.com/web/secure/base_url/'
  328. ],
  329. ];
  330. }
  331. /**
  332. * @return void
  333. */
  334. public function testGetBaseUrlEntryPoint()
  335. {
  336. $expectedPath = 'web/unsecure/base_link_url';
  337. $expectedBaseUrl = 'http://domain.com/web/unsecure/base_link_url/test_script.php/';
  338. /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $configMock */
  339. $configMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  340. $configMock->expects($this->atLeastOnce())
  341. ->method('getValue')
  342. ->will($this->returnCallback(
  343. function ($path, $scope, $scopeCode) use ($expectedPath) {
  344. return $expectedPath == $path ? 'http://domain.com/' . $path . '/' : null;
  345. }
  346. ));
  347. /** @var \Magento\Store\Model\Store $model */
  348. $model = $this->objectManagerHelper->getObject(
  349. \Magento\Store\Model\Store::class,
  350. [
  351. 'config' => $configMock,
  352. 'isCustomEntryPoint' => false,
  353. ]
  354. );
  355. $model->setCode('scopeCode');
  356. $this->setUrlModifier($model);
  357. $server = $_SERVER;
  358. $_SERVER['SCRIPT_FILENAME'] = 'test_script.php';
  359. $this->assertEquals(
  360. $expectedBaseUrl,
  361. $model->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, false)
  362. );
  363. $_SERVER = $server;
  364. }
  365. /**
  366. * @expectedException \InvalidArgumentException
  367. */
  368. public function testGetBaseUrlWrongType()
  369. {
  370. /** @var \Magento\Store\Model\Store $model */
  371. $model = $this->objectManagerHelper->getObject(
  372. \Magento\Store\Model\Store::class
  373. );
  374. $model->getBaseUrl('unexpected url type');
  375. }
  376. /**
  377. * @dataProvider getCurrentUrlDataProvider
  378. *
  379. * @param boolean $secure
  380. * @param string $url
  381. * @param string $expected
  382. * @param bool|string $fromStore
  383. */
  384. public function testGetCurrentUrl($secure, $url, $expected, $fromStore)
  385. {
  386. $defaultStore = $this->createPartialMock(Store::class, [
  387. 'getId',
  388. 'isCurrentlySecure',
  389. '__wakeup'
  390. ]);
  391. $defaultStore->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(5));
  392. $defaultStore->expects($this->atLeastOnce())->method('isCurrentlySecure')->will($this->returnValue($secure));
  393. $sidResolver = $this->getMockForAbstractClass(\Magento\Framework\Session\SidResolverInterface::class);
  394. $sidResolver->expects($this->any())->method('getSessionIdQueryParam')->will($this->returnValue('SID'));
  395. $config = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  396. $requestString = preg_replace(
  397. '/http(s?)\:\/\/[a-z0-9\-]+\//i',
  398. '',
  399. $url
  400. );
  401. $this->requestMock
  402. ->expects($this->atLeastOnce())
  403. ->method('getRequestString')
  404. ->willReturn($requestString);
  405. $this->requestMock->expects($this->atLeastOnce())->method('getQueryValue')->will($this->returnValue([
  406. 'SID' => 'sid'
  407. ]));
  408. $urlMock = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
  409. $urlMock
  410. ->expects($this->atLeastOnce())
  411. ->method('setScope')
  412. ->will($this->returnSelf());
  413. $urlMock->expects($this->any())
  414. ->method('getUrl')
  415. ->will($this->returnValue(str_replace($requestString, '', $url)));
  416. $urlMock
  417. ->expects($this->atLeastOnce())
  418. ->method('escape')
  419. ->willReturnArgument(0);
  420. $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
  421. $storeManager->expects($this->any())
  422. ->method('getStore')
  423. ->will($this->returnValue($defaultStore));
  424. /** @var \Magento\Store\Model\Store $model */
  425. $model = $this->objectManagerHelper->getObject(
  426. \Magento\Store\Model\Store::class,
  427. ['storeManager' => $storeManager, 'url' => $urlMock, 'request' => $this->requestMock, 'config' => $config]
  428. );
  429. $model->setStoreId(2);
  430. $model->setCode('scope_code');
  431. $this->assertEquals($expected, $model->getCurrentUrl($fromStore));
  432. }
  433. /**
  434. * @return array
  435. */
  436. public function getCurrentUrlDataProvider()
  437. {
  438. return [
  439. [
  440. true,
  441. 'http://test/url',
  442. 'http://test/url?SID=sid&___store=scope_code',
  443. false
  444. ],
  445. [
  446. true,
  447. 'http://test/url?SID=sid1&___store=scope',
  448. 'http://test/url?SID=sid&___store=scope_code',
  449. false
  450. ],
  451. [
  452. false,
  453. 'https://test/url',
  454. 'https://test/url?SID=sid&___store=scope_code',
  455. false
  456. ],
  457. [
  458. true,
  459. 'http://test/u/u.2?___store=scope_code',
  460. 'http://test/u/u.2?'
  461. . '___store=scope_code&SID=sid&___from_store=old-store',
  462. 'old-store'
  463. ]
  464. ];
  465. }
  466. /**
  467. * @dataProvider getBaseCurrencyDataProvider
  468. *
  469. * @param int $priceScope
  470. * @param string $currencyCode
  471. */
  472. public function testGetBaseCurrency($priceScope, $currencyCode)
  473. {
  474. /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
  475. $config = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  476. $config->expects($this->any())
  477. ->method('getValue')
  478. ->will($this->returnValueMap([
  479. ['catalog/price/scope', ScopeInterface::SCOPE_STORE, 'scope_code', $priceScope],
  480. [
  481. \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
  482. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  483. null,
  484. 'USD'
  485. ],
  486. [
  487. \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
  488. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  489. 'scope_code',
  490. 'UAH'
  491. ],
  492. ]));
  493. $currency = $this->createMock(\Magento\Directory\Model\Currency::class);
  494. $currency->expects($this->any())->method('load')->with($currencyCode)->will($this->returnSelf());
  495. $currencyFactory = $this->createPartialMock(\Magento\Directory\Model\CurrencyFactory::class, ['create']);
  496. $currencyFactory->expects($this->any())->method('create')->will($this->returnValue($currency));
  497. $appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['isInstalled']);
  498. $appState->expects($this->any())->method('isInstalled')->will($this->returnValue(true));
  499. /** @var \Magento\Store\Model\Store $model */
  500. $model = $this->objectManagerHelper->getObject(
  501. \Magento\Store\Model\Store::class,
  502. ['currencyFactory' => $currencyFactory, 'config' => $config, 'appState' => $appState]
  503. );
  504. $model->setCode('scope_code');
  505. $this->assertEquals($currency, $model->getBaseCurrency());
  506. }
  507. /**
  508. * @return array
  509. */
  510. public function getBaseCurrencyDataProvider()
  511. {
  512. return [
  513. [0, 'USD'],
  514. [1, 'UAH'],
  515. ];
  516. }
  517. /**
  518. * @return void
  519. */
  520. public function testGetAllowedCurrencies()
  521. {
  522. $currencyPath = 'cur/ren/cy/path';
  523. $expectedResult = ['EUR', 'USD'];
  524. $configMock = $this->getMockForAbstractClass(
  525. \Magento\Framework\App\Config\ReinitableConfigInterface::class,
  526. [],
  527. '',
  528. false
  529. );
  530. $configMock->expects($this->once())
  531. ->method('getValue')
  532. ->with($currencyPath, 'store', null)
  533. ->will($this->returnValue('EUR,USD'));
  534. /** @var \Magento\Store\Model\Store $model */
  535. $model = $this->objectManagerHelper->getObject(
  536. \Magento\Store\Model\Store::class,
  537. ['config' => $configMock, 'currencyInstalled' => $currencyPath,]
  538. );
  539. $this->assertEquals($expectedResult, $model->getAllowedCurrencies());
  540. }
  541. /**
  542. * @dataProvider isCurrentlySecureDataProvider
  543. *
  544. * @param bool $expected
  545. * @param array $value
  546. * @param bool $requestSecure
  547. * @param bool $useSecureInFrontend
  548. * @param string|null $secureBaseUrl
  549. */
  550. public function testIsCurrentlySecure(
  551. $expected,
  552. $value,
  553. $requestSecure = false,
  554. $useSecureInFrontend = true,
  555. $secureBaseUrl = 'https://example.com:443'
  556. ) {
  557. /* @var ReinitableConfigInterface|PHPUnit_Framework_MockObject_MockObject $configMock */
  558. $configMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  559. $configMock->expects($this->any())
  560. ->method('getValue')
  561. ->will($this->returnValueMap([
  562. [
  563. Store::XML_PATH_SECURE_BASE_URL,
  564. ScopeInterface::SCOPE_STORE,
  565. null,
  566. $secureBaseUrl
  567. ],
  568. [
  569. Store::XML_PATH_SECURE_IN_FRONTEND,
  570. ScopeInterface::SCOPE_STORE,
  571. null,
  572. $useSecureInFrontend
  573. ]
  574. ]));
  575. $this->requestMock->expects($this->any())
  576. ->method('isSecure')
  577. ->willReturn($requestSecure);
  578. $this->requestMock->expects($this->any())
  579. ->method('getServer')
  580. ->with($this->equalTo('SERVER_PORT'))
  581. ->willReturn($value);
  582. /** @var \Magento\Store\Model\Store $model */
  583. $model = $this->objectManagerHelper->getObject(
  584. \Magento\Store\Model\Store::class,
  585. ['config' => $configMock, 'request' => $this->requestMock]
  586. );
  587. if ($expected) {
  588. $this->assertTrue($model->isCurrentlySecure(), "Was expecting this test to show as secure, but it wasn't");
  589. } else {
  590. $this->assertFalse($model->isCurrentlySecure(), "Was expecting this test to show as not secure!");
  591. }
  592. }
  593. /**
  594. * @return array
  595. */
  596. public function isCurrentlySecureDataProvider()
  597. {
  598. return [
  599. 'secure request, no server setting' => [true, [], true],
  600. 'unsecure request, using registered port' => [true, 443],
  601. 'unsecure request, no secure base url registered' => [false, 443, false, true, null],
  602. 'unsecure request, not using registered port' => [false, 80],
  603. 'unsecure request, using registered port, not using secure in frontend' => [false, 443, false, false],
  604. 'unsecure request, no secure base url registered, not using secure in frontend' =>
  605. [false, 443, false, false, null],
  606. 'unsecure request, not using registered port, not using secure in frontend' => [false, 80, false, false],
  607. ];
  608. }
  609. /**
  610. * @covers \Magento\Store\Model\Store::getBaseMediaDir
  611. */
  612. public function testGetBaseMediaDir()
  613. {
  614. $expectedResult = 'pub/media';
  615. $this->filesystemMock->expects($this->once())
  616. ->method('getUri')
  617. ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
  618. ->willReturn($expectedResult);
  619. $this->assertEquals($expectedResult, $this->store->getBaseMediaDir());
  620. }
  621. /**
  622. * @covers \Magento\Store\Model\Store::getBaseStaticDir
  623. */
  624. public function testGetBaseStaticDir()
  625. {
  626. $expectedResult = 'pub/static';
  627. $this->filesystemMock->expects($this->once())
  628. ->method('getUri')
  629. ->with(\Magento\Framework\App\Filesystem\DirectoryList::STATIC_VIEW)
  630. ->willReturn($expectedResult);
  631. $this->assertEquals($expectedResult, $this->store->getBaseStaticDir());
  632. }
  633. /**
  634. * @return void
  635. */
  636. public function testGetScopeType()
  637. {
  638. $this->assertEquals(ScopeInterface::SCOPE_STORE, $this->store->getScopeType());
  639. }
  640. /**
  641. * @return void
  642. */
  643. public function testGetScopeTypeName()
  644. {
  645. $this->assertEquals('Store View', $this->store->getScopeTypeName());
  646. }
  647. /**
  648. * @param array $availableCodes
  649. * @param string $currencyCode
  650. * @param string $defaultCode
  651. * @param string $expectedCode
  652. * @return void
  653. * @dataProvider currencyCodeDataProvider
  654. */
  655. public function testGetCurrentCurrencyCode(
  656. array $availableCodes,
  657. string $currencyCode,
  658. string $defaultCode,
  659. string $expectedCode
  660. ): void {
  661. $this->store->setData('available_currency_codes', $availableCodes);
  662. $this->sessionMock->method('getCurrencyCode')
  663. ->willReturn($currencyCode);
  664. $this->configMock->method('getValue')
  665. ->with(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_DEFAULT)
  666. ->willReturn($defaultCode);
  667. $code = $this->store->getCurrentCurrencyCode();
  668. $this->assertEquals($expectedCode, $code);
  669. }
  670. /**
  671. * @return array
  672. */
  673. public function currencyCodeDataProvider(): array
  674. {
  675. return [
  676. [
  677. [
  678. 'USD',
  679. ],
  680. 'USD',
  681. 'USD',
  682. 'USD',
  683. ],
  684. [
  685. [
  686. 'USD',
  687. 'EUR',
  688. ],
  689. 'EUR',
  690. 'USD',
  691. 'EUR',
  692. ],
  693. [
  694. [
  695. 'EUR',
  696. 'USD',
  697. ],
  698. 'GBP',
  699. 'USD',
  700. 'USD',
  701. ],
  702. [
  703. [
  704. 'USD',
  705. ],
  706. 'GBP',
  707. 'EUR',
  708. 'USD',
  709. ],
  710. [
  711. [],
  712. 'GBP',
  713. 'EUR',
  714. 'EUR',
  715. ],
  716. ];
  717. }
  718. /**
  719. * @param \Magento\Store\Model\Store $model
  720. */
  721. private function setUrlModifier(\Magento\Store\Model\Store $model)
  722. {
  723. $property = (new \ReflectionClass(get_class($model)))
  724. ->getProperty('urlModifier');
  725. $property->setAccessible(true);
  726. $property->setValue($model, $this->urlModifierMock);
  727. }
  728. }