StoreTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. use Magento\Catalog\Model\ProductRepository;
  8. use Magento\Framework\App\Bootstrap;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. use Magento\Framework\UrlInterface;
  12. use Magento\Store\Api\StoreRepositoryInterface;
  13. use Zend\Stdlib\Parameters;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class StoreTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var array
  21. */
  22. protected $modelParams;
  23. /**
  24. * @var Store|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $model;
  27. protected function setUp()
  28. {
  29. $this->model = $this->_getStoreModel();
  30. }
  31. /**
  32. * @return \PHPUnit_Framework_MockObject_MockObject|Store
  33. */
  34. protected function _getStoreModel()
  35. {
  36. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  37. $this->modelParams = [
  38. 'context' => $objectManager->get(\Magento\Framework\Model\Context::class),
  39. 'registry' => $objectManager->get(\Magento\Framework\Registry::class),
  40. 'extensionFactory' => $objectManager->get(\Magento\Framework\Api\ExtensionAttributesFactory::class),
  41. 'customAttributeFactory' => $objectManager->get(\Magento\Framework\Api\AttributeValueFactory::class),
  42. 'resource' => $objectManager->get(\Magento\Store\Model\ResourceModel\Store::class),
  43. 'coreFileStorageDatabase' => $objectManager->get(\Magento\MediaStorage\Helper\File\Storage\Database::class),
  44. 'configCacheType' => $objectManager->get(\Magento\Framework\App\Cache\Type\Config::class),
  45. 'url' => $objectManager->get(\Magento\Framework\Url::class),
  46. 'request' => $objectManager->get(\Magento\Framework\App\RequestInterface::class),
  47. 'configDataResource' => $objectManager->get(\Magento\Config\Model\ResourceModel\Config\Data::class),
  48. 'filesystem' => $objectManager->get(\Magento\Framework\Filesystem::class),
  49. 'config' => $objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class),
  50. 'storeManager' => $objectManager->get(\Magento\Store\Model\StoreManager::class),
  51. 'sidResolver' => $objectManager->get(\Magento\Framework\Session\SidResolverInterface::class),
  52. 'httpContext' => $objectManager->get(\Magento\Framework\App\Http\Context::class),
  53. 'session' => $objectManager->get(\Magento\Framework\Session\SessionManagerInterface::class),
  54. 'currencyFactory' => $objectManager->get(\Magento\Directory\Model\CurrencyFactory::class),
  55. 'information' => $objectManager->get(\Magento\Store\Model\Information::class),
  56. 'currencyInstalled' => 'system/currency/installed',
  57. 'groupRepository' => $objectManager->get(\Magento\Store\Api\GroupRepositoryInterface::class),
  58. 'websiteRepository' => $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class),
  59. ];
  60. return $this->getMockBuilder(\Magento\Store\Model\Store::class)
  61. ->setMethods(['getUrl'])
  62. ->setConstructorArgs($this->modelParams)
  63. ->getMock();
  64. }
  65. protected function tearDown()
  66. {
  67. $this->model = null;
  68. }
  69. /**
  70. * @param $loadId
  71. * @param $expectedId
  72. * @dataProvider loadDataProvider
  73. */
  74. public function testLoad($loadId, $expectedId)
  75. {
  76. $this->model->load($loadId);
  77. $this->assertEquals($expectedId, $this->model->getId());
  78. }
  79. /**
  80. * @return array
  81. */
  82. public function loadDataProvider()
  83. {
  84. return [[1, 1], ['default', 1], ['nostore', null]];
  85. }
  86. public function testSetGetWebsite()
  87. {
  88. $this->assertFalse($this->model->getWebsite());
  89. $website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  90. \Magento\Store\Model\StoreManagerInterface::class
  91. )->getWebsite();
  92. $this->model->setWebsite($website);
  93. $actualResult = $this->model->getWebsite();
  94. $this->assertSame($website, $actualResult);
  95. }
  96. public function testSetGetGroup()
  97. {
  98. $this->assertFalse($this->model->getGroup());
  99. $storeGroup = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  100. \Magento\Store\Model\StoreManager::class
  101. )->getGroup();
  102. $this->model->setGroup($storeGroup);
  103. $actualResult = $this->model->getGroup();
  104. $this->assertSame($storeGroup, $actualResult);
  105. }
  106. /**
  107. * Isolation is enabled, as we pollute config with rewrite values
  108. *
  109. * @param string $type
  110. * @param bool $useRewrites
  111. * @param bool $useStoreCode
  112. * @param string $expected
  113. * @dataProvider getBaseUrlDataProvider
  114. * @magentoAppIsolation enabled
  115. */
  116. public function testGetBaseUrl($type, $useRewrites, $useStoreCode, $expected)
  117. {
  118. /* config operations require store to be loaded */
  119. $this->model->load('default');
  120. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  121. ->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
  122. ->setValue(Store::XML_PATH_USE_REWRITES, $useRewrites, ScopeInterface::SCOPE_STORE);
  123. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  124. ->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
  125. ->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode, ScopeInterface::SCOPE_STORE);
  126. $actual = $this->model->getBaseUrl($type);
  127. $this->assertEquals($expected, $actual);
  128. }
  129. /**
  130. * @return array
  131. */
  132. public function getBaseUrlDataProvider()
  133. {
  134. return [
  135. [UrlInterface::URL_TYPE_WEB, false, false, 'http://localhost/'],
  136. [UrlInterface::URL_TYPE_WEB, false, true, 'http://localhost/'],
  137. [UrlInterface::URL_TYPE_WEB, true, false, 'http://localhost/'],
  138. [UrlInterface::URL_TYPE_WEB, true, true, 'http://localhost/'],
  139. [UrlInterface::URL_TYPE_LINK, false, false, 'http://localhost/index.php/'],
  140. [UrlInterface::URL_TYPE_LINK, false, true, 'http://localhost/index.php/default/'],
  141. [UrlInterface::URL_TYPE_LINK, true, false, 'http://localhost/'],
  142. [UrlInterface::URL_TYPE_LINK, true, true, 'http://localhost/default/'],
  143. [UrlInterface::URL_TYPE_DIRECT_LINK, false, false, 'http://localhost/index.php/'],
  144. [UrlInterface::URL_TYPE_DIRECT_LINK, false, true, 'http://localhost/index.php/'],
  145. [UrlInterface::URL_TYPE_DIRECT_LINK, true, false, 'http://localhost/'],
  146. [UrlInterface::URL_TYPE_DIRECT_LINK, true, true, 'http://localhost/'],
  147. [UrlInterface::URL_TYPE_STATIC, false, false, 'http://localhost/pub/static/'],
  148. [UrlInterface::URL_TYPE_STATIC, false, true, 'http://localhost/pub/static/'],
  149. [UrlInterface::URL_TYPE_STATIC, true, false, 'http://localhost/pub/static/'],
  150. [UrlInterface::URL_TYPE_STATIC, true, true, 'http://localhost/pub/static/'],
  151. [UrlInterface::URL_TYPE_MEDIA, false, false, 'http://localhost/pub/media/'],
  152. [UrlInterface::URL_TYPE_MEDIA, false, true, 'http://localhost/pub/media/'],
  153. [UrlInterface::URL_TYPE_MEDIA, true, false, 'http://localhost/pub/media/'],
  154. [UrlInterface::URL_TYPE_MEDIA, true, true, 'http://localhost/pub/media/']
  155. ];
  156. }
  157. /**
  158. * @magentoAppIsolation enabled
  159. */
  160. public function testGetBaseUrlInPub()
  161. {
  162. \Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize([
  163. Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS => [
  164. DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
  165. ],
  166. ]);
  167. $this->model = $this->_getStoreModel();
  168. $this->model->load('default');
  169. $this->assertEquals('http://localhost/pub/static/', $this->model->getBaseUrl(UrlInterface::URL_TYPE_STATIC));
  170. $this->assertEquals('http://localhost/pub/media/', $this->model->getBaseUrl(UrlInterface::URL_TYPE_MEDIA));
  171. }
  172. /**
  173. * Isolation is enabled, as we pollute config with rewrite values
  174. *
  175. * @param string $type
  176. * @param bool $useCustomEntryPoint
  177. * @param bool $useStoreCode
  178. * @param string $expected
  179. * @dataProvider getBaseUrlForCustomEntryPointDataProvider
  180. * @magentoAppIsolation enabled
  181. */
  182. public function testGetBaseUrlForCustomEntryPoint($type, $useCustomEntryPoint, $useStoreCode, $expected)
  183. {
  184. /* config operations require store to be loaded */
  185. $this->model->load('default');
  186. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  187. ->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
  188. ->setValue(Store::XML_PATH_USE_REWRITES, false, ScopeInterface::SCOPE_STORE);
  189. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  190. ->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
  191. ->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode, ScopeInterface::SCOPE_STORE);
  192. // emulate custom entry point
  193. $_SERVER['SCRIPT_FILENAME'] = 'custom_entry.php';
  194. if ($useCustomEntryPoint) {
  195. $property = new \ReflectionProperty($this->model, '_isCustomEntryPoint');
  196. $property->setAccessible(true);
  197. $property->setValue($this->model, $useCustomEntryPoint);
  198. }
  199. $actual = $this->model->getBaseUrl($type);
  200. $this->assertEquals($expected, $actual);
  201. }
  202. /**
  203. * @return array
  204. */
  205. public function getBaseUrlForCustomEntryPointDataProvider()
  206. {
  207. return [
  208. [UrlInterface::URL_TYPE_LINK, false, false, 'http://localhost/custom_entry.php/'],
  209. [
  210. UrlInterface::URL_TYPE_LINK,
  211. false,
  212. true,
  213. 'http://localhost/custom_entry.php/default/'
  214. ],
  215. [UrlInterface::URL_TYPE_LINK, true, false, 'http://localhost/index.php/'],
  216. [UrlInterface::URL_TYPE_LINK, true, true, 'http://localhost/index.php/default/'],
  217. [
  218. UrlInterface::URL_TYPE_DIRECT_LINK,
  219. false,
  220. false,
  221. 'http://localhost/custom_entry.php/'
  222. ],
  223. [
  224. UrlInterface::URL_TYPE_DIRECT_LINK,
  225. false,
  226. true,
  227. 'http://localhost/custom_entry.php/'
  228. ],
  229. [UrlInterface::URL_TYPE_DIRECT_LINK, true, false, 'http://localhost/index.php/'],
  230. [UrlInterface::URL_TYPE_DIRECT_LINK, true, true, 'http://localhost/index.php/']
  231. ];
  232. }
  233. public function testGetDefaultCurrency()
  234. {
  235. /* currency operations require store to be loaded */
  236. $this->model->load('default');
  237. $this->assertEquals($this->model->getDefaultCurrencyCode(), $this->model->getDefaultCurrency()->getCode());
  238. }
  239. public function testIsCanDelete()
  240. {
  241. $this->assertFalse($this->model->isCanDelete());
  242. $this->model->load(1);
  243. $this->assertFalse($this->model->isCanDelete());
  244. $this->model->setId(100);
  245. $this->assertFalse($this->model->isCanDelete());
  246. }
  247. /**
  248. * @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
  249. * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  250. * @magentoDbIsolation disabled
  251. */
  252. public function testGetCurrentUrl()
  253. {
  254. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  255. $objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
  256. ->setValue('web/url/use_store', true, ScopeInterface::SCOPE_STORE, 'secondstore');
  257. $this->model->load('admin');
  258. $this->model
  259. ->expects($this->any())->method('getUrl')
  260. ->will($this->returnValue('http://localhost/index.php'));
  261. $this->assertStringEndsWith('default', $this->model->getCurrentUrl());
  262. $this->assertStringEndsNotWith('default', $this->model->getCurrentUrl(false));
  263. /** @var \Magento\Store\Model\Store $secondStore */
  264. $secondStore = $objectManager->get(StoreRepositoryInterface::class)->get('secondstore');
  265. /** @var \Magento\Catalog\Model\ProductRepository $productRepository */
  266. $productRepository = $objectManager->create(ProductRepository::class);
  267. $product = $productRepository->get('simple');
  268. $product->setStoreId($secondStore->getId());
  269. $url = $product->getUrlInStore();
  270. $this->assertEquals(
  271. $secondStore->getBaseUrl().'catalog/product/view/id/1/s/simple-product/',
  272. $url
  273. );
  274. $this->assertEquals(
  275. $secondStore->getBaseUrl().'?___from_store=default',
  276. $secondStore->getCurrentUrl()
  277. );
  278. $this->assertEquals(
  279. $secondStore->getBaseUrl(),
  280. $secondStore->getCurrentUrl(false)
  281. );
  282. }
  283. /**
  284. * @magentoDataFixture Magento/Store/_files/second_store.php
  285. * @magentoDataFixture Magento/Catalog/_files/category_product.php
  286. * @magentoDbIsolation disabled
  287. */
  288. public function testGetCurrentUrlWithUseStoreInUrlFalse()
  289. {
  290. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  291. $objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class)
  292. ->setValue('web/url/use_store', false, ScopeInterface::SCOPE_STORE, 'default');
  293. /** @var \Magento\Store\Model\Store $secondStore */
  294. $secondStore = $objectManager->get(StoreRepositoryInterface::class)->get('fixture_second_store');
  295. /** @var \Magento\Catalog\Model\ProductRepository $productRepository */
  296. $productRepository = $objectManager->create(ProductRepository::class);
  297. $product = $productRepository->get('simple333');
  298. $product->setStoreId($secondStore->getId());
  299. $url = $product->getUrlInStore();
  300. /** @var \Magento\Catalog\Model\CategoryRepository $categoryRepository */
  301. $categoryRepository = $objectManager->get(\Magento\Catalog\Model\CategoryRepository::class);
  302. $category = $categoryRepository->get(333, $secondStore->getStoreId());
  303. $this->assertEquals(
  304. $secondStore->getBaseUrl().'catalog/category/view/s/category-1/id/333/',
  305. $category->getUrl()
  306. );
  307. $this->assertEquals(
  308. $secondStore->getBaseUrl().
  309. 'catalog/product/view/id/333/s/simple-product-three/?___store=fixture_second_store',
  310. $url
  311. );
  312. $this->assertEquals(
  313. $secondStore->getBaseUrl().'?___store=fixture_second_store&___from_store=default',
  314. $secondStore->getCurrentUrl()
  315. );
  316. $this->assertEquals(
  317. $secondStore->getBaseUrl().'?___store=fixture_second_store',
  318. $secondStore->getCurrentUrl(false)
  319. );
  320. }
  321. /**
  322. * @magentoAppIsolation enabled
  323. * @magentoAppArea adminhtml
  324. * @magentoDbIsolation enabled
  325. */
  326. public function testCRUD()
  327. {
  328. $this->model->setData([
  329. 'code' => 'test',
  330. 'website_id' => 1,
  331. 'group_id' => 1,
  332. 'name' => 'test name',
  333. 'sort_order' => 0,
  334. 'is_active' => 1,
  335. ]);
  336. $crud = new \Magento\TestFramework\Entity(
  337. $this->model,
  338. ['name' => 'new name'],
  339. \Magento\Store\Model\Store::class
  340. );
  341. $crud->testCrud();
  342. }
  343. /**
  344. * @param array $badStoreData
  345. *
  346. * @dataProvider saveValidationDataProvider
  347. * @magentoAppIsolation enabled
  348. * @magentoAppArea adminhtml
  349. * @magentoDbIsolation enabled
  350. * @expectedException \Magento\Framework\Exception\LocalizedException
  351. */
  352. public function testSaveValidation($badStoreData)
  353. {
  354. $normalStoreData = [
  355. 'code' => 'test',
  356. 'website_id' => 1,
  357. 'group_id' => 1,
  358. 'name' => 'test name',
  359. 'sort_order' => 0,
  360. 'is_active' => 1,
  361. ];
  362. $data = array_merge($normalStoreData, $badStoreData);
  363. $this->model->setData($data);
  364. $this->model->save();
  365. }
  366. /**
  367. * @return array
  368. */
  369. public static function saveValidationDataProvider()
  370. {
  371. return [
  372. 'empty store name' => [['name' => '']],
  373. 'empty store code' => [['code' => '']],
  374. 'invalid store code' => [['code' => '^_^']]
  375. ];
  376. }
  377. /**
  378. * @param $storeInUrl
  379. * @param $disableStoreInUrl
  380. * @param $expectedResult
  381. * @dataProvider isUseStoreInUrlDataProvider
  382. */
  383. public function testIsUseStoreInUrl($storeInUrl, $disableStoreInUrl, $expectedResult)
  384. {
  385. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  386. $configMock = $this->createMock(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  387. $appStateMock = $this->createMock(\Magento\Framework\App\State::class);
  388. $params = $this->modelParams;
  389. $params['context'] = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  390. ->create(\Magento\Framework\Model\Context::class, ['appState' => $appStateMock]);
  391. $configMock->expects($this->any())
  392. ->method('getValue')
  393. ->with($this->stringContains(Store::XML_PATH_STORE_IN_URL))
  394. ->will($this->returnValue($storeInUrl));
  395. $params['config'] = $configMock;
  396. $model = $objectManager->create(\Magento\Store\Model\Store::class, $params);
  397. $model->setDisableStoreInUrl($disableStoreInUrl);
  398. $this->assertEquals($expectedResult, $model->isUseStoreInUrl());
  399. }
  400. /**
  401. * @see self::testIsUseStoreInUrl;
  402. * @return array
  403. */
  404. public function isUseStoreInUrlDataProvider()
  405. {
  406. return [
  407. [true, null, true],
  408. [false, null, false],
  409. [true, true, false],
  410. [true, false, true]
  411. ];
  412. }
  413. /**
  414. * @dataProvider isCurrentlySecureDataProvider
  415. *
  416. * @param bool $expected
  417. * @param array $serverValues
  418. * @magentoConfigFixture current_store web/secure/offloader_header X_FORWARDED_PROTO
  419. * @magentoConfigFixture current_store web/secure/base_url https://example.com:80
  420. */
  421. public function testIsCurrentlySecure($expected, $serverValues)
  422. {
  423. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  424. /** @var Store $model */
  425. $model = $objectManager->create(\Magento\Store\Model\Store::class);
  426. $request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
  427. $request->setServer(new Parameters(array_merge($_SERVER, $serverValues)));
  428. $this->assertEquals($expected, $model->isCurrentlySecure());
  429. }
  430. public function isCurrentlySecureDataProvider()
  431. {
  432. return [
  433. [true, ['HTTPS' => 'on']],
  434. [true, ['X_FORWARDED_PROTO' => 'https']],
  435. [true, ['HTTP_X_FORWARDED_PROTO' => 'https']],
  436. [true, ['HTTPS' => 'on', 'SERVER_PORT' => 80]],
  437. [false, ['SERVER_PORT' => 80]],
  438. [false, []],
  439. ];
  440. }
  441. /**
  442. * @magentoConfigFixture current_store web/secure/offloader_header SSL_OFFLOADED
  443. * @magentoConfigFixture current_store web/secure/base_url
  444. */
  445. public function testIsCurrentlySecureNoSecureBaseUrl()
  446. {
  447. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  448. /** @var Store $model */
  449. $model = $objectManager->create(\Magento\Store\Model\Store::class);
  450. $server = $_SERVER;
  451. $_SERVER['SERVER_PORT'] = 80;
  452. $this->assertFalse($model->isCurrentlySecure());
  453. $_SERVER = $server;
  454. }
  455. }