Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreTest.php
Go to the documentation of this file.
1 <?php
8 
13 
19 class StoreTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $store;
25 
30 
34  protected $requestMock;
35 
39  protected $filesystemMock;
40 
44  private $urlModifierMock;
45 
49  protected function setUp()
50  {
51  $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
52  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, [
53  'getRequestString',
54  'getModuleName',
55  'setModuleName',
56  'getActionName',
57  'setActionName',
58  'getParam',
59  'getQueryValue',
60  'getDistroBaseUrl',
61  'isSecure',
62  'getServer',
63  ]);
64  $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->store = $this->objectManagerHelper->getObject(
68  \Magento\Store\Model\Store::class,
69  ['filesystem' => $this->filesystemMock]
70  );
71 
72  $this->urlModifierMock = $this->createMock(\Magento\Framework\Url\ModifierInterface::class);
73  $this->urlModifierMock->expects($this->any())
74  ->method('execute')
75  ->willReturnArgument(0);
76  }
77 
84  public function testLoad($key, $field)
85  {
87  $resource = $this->createPartialMock(
88  \Magento\Store\Model\ResourceModel\Store::class,
89  ['load', 'getIdFieldName', '__wakeup']
90  );
91  $resource->expects($this->atLeastOnce())->method('load')
92  ->with($this->isInstanceOf(\Magento\Store\Model\Store::class), $this->equalTo($key), $this->equalTo($field))
93  ->will($this->returnSelf());
94  $resource->expects($this->atLeastOnce())->method('getIdFieldName')->will($this->returnValue('store_id'));
96  $model = $this->objectManagerHelper->getObject(\Magento\Store\Model\Store::class, ['resource' => $resource]);
97  $model->load($key);
98  }
99 
103  public function loadDataProvider()
104  {
105  return [
106  [1, null],
107  ['default', 'code'],
108  ];
109  }
110 
114  public function testSetWebsite()
115  {
116  $website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getId', '__wakeup']);
117  $website->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(2));
119  $model = $this->objectManagerHelper->getObject(\Magento\Store\Model\Store::class);
120  $model->setWebsite($website);
121  $this->assertEquals(2, $model->getWebsiteId());
122  }
123 
127  public function testGetWebsite()
128  {
129  $websiteId = 2;
130  $website = $this->createMock(\Magento\Store\Api\Data\WebsiteInterface::class);
131 
132  $websiteRepository = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class)
133  ->setMethods(['getById'])
134  ->getMockForAbstractClass();
135  $websiteRepository->expects($this->once())
136  ->method('getById')
137  ->with($websiteId)
138  ->willReturn($website);
139 
141  $model = $this->objectManagerHelper->getObject(
142  \Magento\Store\Model\Store::class,
143  ['websiteRepository' => $websiteRepository,]
144  );
145  $model->setWebsiteId($websiteId);
146 
147  $this->assertEquals($website, $model->getWebsite());
148  }
149 
153  public function testGetWebsiteIfWebsiteIsNotExist()
154  {
155  $websiteRepository = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class)
156  ->setMethods(['getById'])
157  ->getMockForAbstractClass();
158  $websiteRepository->expects($this->never())
159  ->method('getById');
160 
162  $model = $this->objectManagerHelper->getObject(
163  \Magento\Store\Model\Store::class,
164  ['websiteRepository' => $websiteRepository,]
165  );
166  $model->setWebsiteId(null);
167 
168  $this->assertFalse($model->getWebsite());
169  }
170 
174  public function testGetGroup()
175  {
176  $groupId = 2;
177  $group = $this->createMock(\Magento\Store\Api\Data\GroupInterface::class);
178 
179  $groupRepository = $this->getMockBuilder(\Magento\Store\Api\GroupRepositoryInterface::class)
180  ->setMethods(['get'])
181  ->getMockForAbstractClass();
182  $groupRepository->expects($this->once())
183  ->method('get')
184  ->with($groupId)
185  ->willReturn($group);
186 
188  $model = $this->objectManagerHelper->getObject(
189  \Magento\Store\Model\Store::class,
190  ['groupRepository' => $groupRepository,]
191  );
192  $model->setGroupId($groupId);
193 
194  $this->assertEquals($group, $model->getGroup());
195  }
196 
200  public function testGetGroupIfGroupIsNotExist()
201  {
202  $groupRepository = $this->getMockBuilder(\Magento\Store\Api\GroupRepositoryInterface::class)
203  ->setMethods(['getById'])
204  ->getMockForAbstractClass();
205  $groupRepository->expects($this->never())
206  ->method('getById');
207 
209  $model = $this->objectManagerHelper->getObject(
210  \Magento\Store\Model\Store::class,
211  ['groupRepository' => $groupRepository,]
212  );
213  $model->setGroupId(null);
214 
215  $this->assertFalse($model->getGroup());
216  }
217 
221  public function testGetUrl()
222  {
223  $params = ['_scope_to_url' => true];
224  $defaultStore = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId', '__wakeup']);
225  $defaultStore->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(5));
226 
227  $url = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
228  $url->expects($this->atLeastOnce())->method('setScope')->will($this->returnSelf());
229  $url->expects($this->atLeastOnce())->method('getUrl')
230  ->with($this->equalTo('test/route'), $this->equalTo($params))
231  ->will($this->returnValue('http://test/url'));
232 
233  $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
234  $storeManager->expects($this->any())
235  ->method('getStore')
236  ->will($this->returnValue($defaultStore));
237 
239  $model = $this->objectManagerHelper->getObject(
240  \Magento\Store\Model\Store::class,
241  ['storeManager' => $storeManager, 'url' => $url]
242  );
243  $model->setStoreId(2);
244  $this->assertEquals('http://test/url', $model->getUrl('test/route'));
245  }
246 
260  public function testGetBaseUrl($type, $secure, $expectedPath, $expectedBaseUrl)
261  {
262  $this->requestMock->expects($this->any())
263  ->method('getDistroBaseUrl')
264  ->will($this->returnValue('http://distro.com/'));
265 
267  $configMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
268  $configMock->expects($this->atLeastOnce())
269  ->method('getValue')
270  ->will($this->returnCallback(
271  function ($path, $scope, $scopeCode) use ($secure, $expectedPath) {
272  $url = $secure ? '{{base_url}}' : 'http://domain.com/';
273  return $expectedPath == $path ? $url . $path . '/' : null;
274  }
275  ));
277  $model = $this->objectManagerHelper->getObject(
278  \Magento\Store\Model\Store::class,
279  [
280  'config' => $configMock,
281  'request' => $this->requestMock,
282  'isCustomEntryPoint' => !$secure,
283  ]
284  );
285  $model->setCode('scopeCode');
286 
287  $this->setUrlModifier($model);
288 
289  $this->assertEquals($expectedBaseUrl, $model->getBaseUrl($type, $secure));
290  }
291 
295  public function getBaseUrlDataProvider()
296  {
297  return [
298  [
300  false,
301  'web/unsecure/base_url',
302  'http://domain.com/web/unsecure/base_url/'
303  ],
304  [
306  false,
307  'web/unsecure/base_link_url',
308  'http://domain.com/web/unsecure/base_link_url/index.php/'
309  ],
310  [
312  false,
313  'web/unsecure/base_link_url',
314  'http://domain.com/web/unsecure/base_link_url/index.php/'
315  ],
316  [
318  false,
319  'web/unsecure/base_media_url',
320  'http://domain.com/web/unsecure/base_media_url/'
321  ],
322  [
324  false,
325  'web/unsecure/base_static_url',
326  'http://domain.com/web/unsecure/base_static_url/'
327  ],
328  [
330  false,
331  'web/unsecure/base_url',
332  'http://domain.com/web/unsecure/base_url/'
333  ],
334  [
336  false,
337  'web/unsecure/base_url',
338  'http://domain.com/web/unsecure/base_url/'
339  ],
340  [
342  true,
343  'web/secure/base_url',
344  'http://distro.com/web/secure/base_url/'
345  ],
346  ];
347  }
348 
352  public function testGetBaseUrlEntryPoint()
353  {
354  $expectedPath = 'web/unsecure/base_link_url';
355  $expectedBaseUrl = 'http://domain.com/web/unsecure/base_link_url/test_script.php/';
357  $configMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
358  $configMock->expects($this->atLeastOnce())
359  ->method('getValue')
360  ->will($this->returnCallback(
361  function ($path, $scope, $scopeCode) use ($expectedPath) {
362  return $expectedPath == $path ? 'http://domain.com/' . $path . '/' : null;
363  }
364  ));
366  $model = $this->objectManagerHelper->getObject(
367  \Magento\Store\Model\Store::class,
368  [
369  'config' => $configMock,
370  'isCustomEntryPoint' => false,
371  ]
372  );
373  $model->setCode('scopeCode');
374 
375  $this->setUrlModifier($model);
376 
377  $server = $_SERVER;
378  $_SERVER['SCRIPT_FILENAME'] = 'test_script.php';
379  $this->assertEquals(
380  $expectedBaseUrl,
381  $model->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK, false)
382  );
383  $_SERVER = $server;
384  }
385 
389  public function testGetBaseUrlWrongType()
390  {
392  $model = $this->objectManagerHelper->getObject(
393  \Magento\Store\Model\Store::class
394  );
395  $model->getBaseUrl('unexpected url type');
396  }
397 
406  public function testGetCurrentUrl($secure, $url, $expected, $fromStore)
407  {
408  $defaultStore = $this->createPartialMock(Store::class, [
409  'getId',
410  'isCurrentlySecure',
411  '__wakeup'
412  ]);
413  $defaultStore->expects($this->atLeastOnce())->method('getId')->will($this->returnValue(5));
414  $defaultStore->expects($this->atLeastOnce())->method('isCurrentlySecure')->will($this->returnValue($secure));
415 
416  $sidResolver = $this->getMockForAbstractClass(\Magento\Framework\Session\SidResolverInterface::class);
417  $sidResolver->expects($this->any())->method('getSessionIdQueryParam')->will($this->returnValue('SID'));
418 
419  $config = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
420 
421  $requestString = preg_replace(
422  '/http(s?)\:\/\/[a-z0-9\-]+\//i',
423  '',
424  $url
425  );
426  $this->requestMock
427  ->expects($this->atLeastOnce())
428  ->method('getRequestString')
429  ->willReturn($requestString);
430  $this->requestMock->expects($this->atLeastOnce())->method('getQueryValue')->will($this->returnValue([
431  'SID' => 'sid'
432  ]));
433 
434  $urlMock = $this->getMockForAbstractClass(\Magento\Framework\UrlInterface::class);
435  $urlMock
436  ->expects($this->atLeastOnce())
437  ->method('setScope')
438  ->will($this->returnSelf());
439  $urlMock->expects($this->any())
440  ->method('getUrl')
441  ->will($this->returnValue(str_replace($requestString, '', $url)));
442  $urlMock
443  ->expects($this->atLeastOnce())
444  ->method('escape')
445  ->willReturnArgument(0);
446 
447  $storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
448  $storeManager->expects($this->any())
449  ->method('getStore')
450  ->will($this->returnValue($defaultStore));
451 
453  $model = $this->objectManagerHelper->getObject(
454  \Magento\Store\Model\Store::class,
455  ['storeManager' => $storeManager, 'url' => $urlMock, 'request' => $this->requestMock, 'config' => $config]
456  );
457  $model->setStoreId(2);
458  $model->setCode('scope_code');
459 
460  $this->assertEquals($expected, $model->getCurrentUrl($fromStore));
461  }
462 
466  public function getCurrentUrlDataProvider()
467  {
468  return [
469  [
470  true,
471  'http://test/url',
472  'http://test/url?SID=sid&___store=scope_code',
473  false
474  ],
475  [
476  true,
477  'http://test/url?SID=sid1&___store=scope',
478  'http://test/url?SID=sid&___store=scope_code',
479  false
480  ],
481  [
482  false,
483  'https://test/url',
484  'https://test/url?SID=sid&___store=scope_code',
485  false
486  ],
487  [
488  true,
489  'http://test/u/u.2?___store=scope_code',
490  'http://test/u/u.2?'
491  . '___store=scope_code&SID=sid&___from_store=old-store',
492  'old-store'
493  ]
494  ];
495  }
496 
503  public function testGetBaseCurrency($priceScope, $currencyCode)
504  {
506  $config = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
507  $config->expects($this->any())
508  ->method('getValue')
509  ->will($this->returnValueMap([
510  ['catalog/price/scope', ScopeInterface::SCOPE_STORE, 'scope_code', $priceScope],
511  [
512  \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
514  null,
515  'USD'
516  ],
517  [
518  \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
520  'scope_code',
521  'UAH'
522  ],
523  ]));
524 
525  $currency = $this->createMock(\Magento\Directory\Model\Currency::class);
526  $currency->expects($this->any())->method('load')->with($currencyCode)->will($this->returnSelf());
527 
528  $currencyFactory = $this->createPartialMock(\Magento\Directory\Model\CurrencyFactory::class, ['create']);
529  $currencyFactory->expects($this->any())->method('create')->will($this->returnValue($currency));
530 
531  $appState = $this->createPartialMock(\Magento\Framework\App\State::class, ['isInstalled']);
532  $appState->expects($this->any())->method('isInstalled')->will($this->returnValue(true));
534  $model = $this->objectManagerHelper->getObject(
535  \Magento\Store\Model\Store::class,
536  ['currencyFactory' => $currencyFactory, 'config' => $config, 'appState' => $appState]
537  );
538  $model->setCode('scope_code');
539  $this->assertEquals($currency, $model->getBaseCurrency());
540  }
541 
545  public function getBaseCurrencyDataProvider()
546  {
547  return [
548  [0, 'USD'],
549  [1, 'UAH'],
550  ];
551  }
552 
556  public function testGetAllowedCurrencies()
557  {
558  $currencyPath = 'cur/ren/cy/path';
559  $expectedResult = ['EUR', 'USD'];
560 
561  $configMock = $this->getMockForAbstractClass(
562  \Magento\Framework\App\Config\ReinitableConfigInterface::class,
563  [],
564  '',
565  false
566  );
567  $configMock->expects($this->once())
568  ->method('getValue')
569  ->with($currencyPath, 'store', null)
570  ->will($this->returnValue('EUR,USD'));
571 
573  $model = $this->objectManagerHelper->getObject(
574  \Magento\Store\Model\Store::class,
575  ['config' => $configMock, 'currencyInstalled' => $currencyPath,]
576  );
577 
578  $this->assertEquals($expectedResult, $model->getAllowedCurrencies());
579  }
580 
590  public function testIsCurrentlySecure(
591  $expected,
592  $value,
593  $requestSecure = false,
594  $useSecureInFrontend = true,
595  $secureBaseUrl = 'https://example.com:443'
596  ) {
597  /* @var ReinitableConfigInterface|PHPUnit_Framework_MockObject_MockObject $configMock */
598  $configMock = $this->getMockForAbstractClass(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
599  $configMock->expects($this->any())
600  ->method('getValue')
601  ->will($this->returnValueMap([
602  [
605  null,
606  $secureBaseUrl
607  ],
608  [
611  null,
612  $useSecureInFrontend
613  ]
614  ]));
615 
616  $this->requestMock->expects($this->any())
617  ->method('isSecure')
618  ->willReturn($requestSecure);
619 
620  $this->requestMock->expects($this->any())
621  ->method('getServer')
622  ->with($this->equalTo('SERVER_PORT'))
623  ->willReturn($value);
624 
626  $model = $this->objectManagerHelper->getObject(
627  \Magento\Store\Model\Store::class,
628  ['config' => $configMock, 'request' => $this->requestMock]
629  );
630 
631  if ($expected) {
632  $this->assertTrue($model->isCurrentlySecure(), "Was expecting this test to show as secure, but it wasn't");
633  } else {
634  $this->assertFalse($model->isCurrentlySecure(), "Was expecting this test to show as not secure!");
635  }
636  }
637 
642  {
643  return [
644  'secure request, no server setting' => [true, [], true],
645  'unsecure request, using registered port' => [true, 443],
646  'unsecure request, no secure base url registered' => [false, 443, false, true, null],
647  'unsecure request, not using registered port' => [false, 80],
648  'unsecure request, using registered port, not using secure in frontend' => [false, 443, false, false],
649  'unsecure request, no secure base url registered, not using secure in frontend' =>
650  [false, 443, false, false, null],
651  'unsecure request, not using registered port, not using secure in frontend' => [false, 80, false, false],
652  ];
653  }
654 
658  public function testGetBaseMediaDir()
659  {
660  $expectedResult = 'pub/media';
661  $this->filesystemMock->expects($this->once())
662  ->method('getUri')
663  ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
664  ->willReturn($expectedResult);
665  $this->assertEquals($expectedResult, $this->store->getBaseMediaDir());
666  }
667 
671  public function testGetBaseStaticDir()
672  {
673  $expectedResult = 'pub/static';
674  $this->filesystemMock->expects($this->once())
675  ->method('getUri')
677  ->willReturn($expectedResult);
678  $this->assertEquals($expectedResult, $this->store->getBaseStaticDir());
679  }
680 
684  public function testGetScopeType()
685  {
686  $this->assertEquals(ScopeInterface::SCOPE_STORE, $this->store->getScopeType());
687  }
688 
692  public function testGetScopeTypeName()
693  {
694  $this->assertEquals('Store View', $this->store->getScopeTypeName());
695  }
696 
700  private function setUrlModifier(\Magento\Store\Model\Store $model)
701  {
702  $property = (new \ReflectionClass(get_class($model)))
703  ->getProperty('urlModifier');
704 
705  $property->setAccessible(true);
706  $property->setValue($model, $this->urlModifierMock);
707  }
708 }
$groupRepository
return false
Definition: gallery.phtml:36
$config
Definition: fraud_order.php:17
$group
Definition: sections.phtml:16
$storeManager
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
const XML_PATH_SECURE_IN_FRONTEND
Definition: Store.php:70
const XML_PATH_SECURE_BASE_URL
Definition: Store.php:68
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18