init
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model\App;
|
||||
|
||||
class EmulationTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Store\Model\App\Emulation
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
/**
|
||||
* @covers \Magento\Store\Model\App\Emulation::startEnvironmentEmulation
|
||||
* @covers \Magento\Store\Model\App\Emulation::stopEnvironmentEmulation
|
||||
*/
|
||||
public function testEnvironmentEmulation()
|
||||
{
|
||||
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Store\Model\App\Emulation::class);
|
||||
\Magento\TestFramework\Helper\Bootstrap::getInstance()
|
||||
->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
|
||||
$design = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\View\DesignInterface::class);
|
||||
|
||||
$this->_model->startEnvironmentEmulation(1);
|
||||
$this->_model->stopEnvironmentEmulation();
|
||||
$this->assertEquals(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, $design->getArea());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
/**
|
||||
* Dummy layout argument data source object
|
||||
*/
|
||||
class DataSource extends \Magento\Framework\Data\Collection
|
||||
{
|
||||
/**
|
||||
* Property which stores all updater calls
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_calls = [];
|
||||
|
||||
/**
|
||||
* Return current updater calls
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUpdaterCall()
|
||||
{
|
||||
return $this->_calls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set updater calls
|
||||
*
|
||||
* @param array $calls
|
||||
* @return \Magento\Store\Model\DataSource
|
||||
*/
|
||||
public function setUpdaterCall(array $calls)
|
||||
{
|
||||
$this->_calls = $calls;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class GroupTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Store\Model\Store
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_model = Bootstrap::getObjectManager()->create(\Magento\Store\Model\Group::class);
|
||||
}
|
||||
|
||||
public function testSetGetWebsite()
|
||||
{
|
||||
$this->assertFalse($this->_model->getWebsite());
|
||||
$website = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class)->getWebsite();
|
||||
$this->_model->setWebsite($website);
|
||||
$actualResult = $this->_model->getWebsite();
|
||||
$this->assertSame($website, $actualResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that getWebsite returns the default site when defaults are passed in for id
|
||||
*/
|
||||
public function testGetWebsiteDefault()
|
||||
{
|
||||
$this->assertFalse($this->_model->getWebsite());
|
||||
$website = Bootstrap::getObjectManager()->get(\Magento\Store\Model\StoreManagerInterface::class)->getWebsite();
|
||||
$this->_model->setWebsite($website);
|
||||
// Empty string should get treated like no parameter
|
||||
$actualResult = $this->_model->getWebsite('');
|
||||
$this->assertSame($website, $actualResult);
|
||||
// Null string should get treated like no parameter
|
||||
$actualResult = $this->_model->getWebsite(null);
|
||||
$this->assertSame($website, $actualResult);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\Customer\Test\Fixture\Customer;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Newsletter\Model\Subscriber;
|
||||
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
|
||||
use Magento\Store\Test\Fixture\Store as StoreFixture;
|
||||
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
|
||||
use Magento\TestFramework\Fixture\Config as ConfigFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixture;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorage;
|
||||
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
|
||||
use Magento\TestFramework\Fixture\DbIsolation;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
* phpcs:disable Magento2.Security.Superglobal
|
||||
*/
|
||||
class MultiStoreTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var DataFixtureStorage
|
||||
*/
|
||||
private $fixtures;
|
||||
|
||||
/**
|
||||
* @inheridoc
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws LocalizedException
|
||||
* @throws \Magento\Framework\Exception\MailException
|
||||
*/
|
||||
#[
|
||||
DbIsolation(false),
|
||||
ConfigFixture('system/smtp/transport', 'smtp', 'store'),
|
||||
DataFixture(WebsiteFixture::class, as: 'website2'),
|
||||
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
|
||||
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
|
||||
DataFixture(
|
||||
Customer::class,
|
||||
[
|
||||
'store_id' => '$store2.id$',
|
||||
'website_id' => '$website2.id$',
|
||||
'addresses' => [[]]
|
||||
],
|
||||
as: 'customer1'
|
||||
),
|
||||
DataFixture(WebsiteFixture::class, as: 'website3'),
|
||||
DataFixture(StoreGroupFixture::class, ['website_id' => '$website3.id$'], 'store_group3'),
|
||||
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group3.id$'], 'store3'),
|
||||
DataFixture(
|
||||
Customer::class,
|
||||
[
|
||||
'store_id' => '$store3.id$',
|
||||
'website_id' => '$website3.id$',
|
||||
'addresses' => [[]]
|
||||
],
|
||||
as: 'customer2'
|
||||
),
|
||||
]
|
||||
public function testStoreSpecificEmailInFromHeader() :void
|
||||
{
|
||||
$customerOne = $this->fixtures->get('customer1');
|
||||
$storeOne = $this->fixtures->get('store2');
|
||||
$customerOneData = [
|
||||
'email' => $customerOne->getDataByKey('email'),
|
||||
'storeId' => $storeOne->getData('store_id'),
|
||||
'storeEmail' => 'store_one@example.com'
|
||||
];
|
||||
|
||||
$this->subscribeNewsLetterAndAssertFromHeader($customerOneData);
|
||||
|
||||
$customerTwo = $this->fixtures->get('customer2');
|
||||
$storeTwo = $this->fixtures->get('store3');
|
||||
$customerTwoData = [
|
||||
'email' => $customerTwo->getDataByKey('email'),
|
||||
'storeId' => $storeTwo->getData('store_id'),
|
||||
'storeEmail' => 'store_two@example.com'
|
||||
];
|
||||
|
||||
$this->subscribeNewsLetterAndAssertFromHeader($customerTwoData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $customerData
|
||||
* @return void
|
||||
* @throws LocalizedException
|
||||
* @throws \Magento\Framework\Exception\MailException
|
||||
*/
|
||||
private function subscribeNewsLetterAndAssertFromHeader(
|
||||
$customerData
|
||||
) :void {
|
||||
/** @var Subscriber $subscriber */
|
||||
$subscriber = $this->objectManager->create(Subscriber::class);
|
||||
$subscriber->subscribe($customerData['email']);
|
||||
|
||||
/** @var TransportBuilderMock $transportBuilderMock */
|
||||
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
|
||||
$transportBuilderMock->setTemplateIdentifier(
|
||||
'customer_password_reset_password_template'
|
||||
)->setTemplateVars([
|
||||
'subscriber_data' => [
|
||||
'confirmation_link' => $subscriber->getConfirmationLink(),
|
||||
],
|
||||
])->setTemplateOptions([
|
||||
'area' => Area::AREA_FRONTEND,
|
||||
'store' => (int) $customerData['storeId']
|
||||
])
|
||||
->setFromByScope(
|
||||
[
|
||||
'email' => $customerData['storeEmail'],
|
||||
'name' => 'Store Email Name'
|
||||
],
|
||||
(int) $customerData['storeId']
|
||||
)
|
||||
->addTo($customerData['email'])
|
||||
->getTransport();
|
||||
|
||||
$headers = $transportBuilderMock->getSentMessage()->getHeaders();
|
||||
|
||||
$this->assertNotNull($transportBuilderMock->getSentMessage());
|
||||
$this->assertStringContainsString($customerData['storeEmail'], $headers['From']);
|
||||
}
|
||||
}
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Store\Model\ResourceModel\Store;
|
||||
|
||||
class CollectionTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Store\Model\ResourceModel\Store\Collection
|
||||
*/
|
||||
protected $_collection;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Store\Model\ResourceModel\Store\Collection::class
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetGetLoadDefault()
|
||||
{
|
||||
$this->assertFalse($this->_collection->getLoadDefault());
|
||||
|
||||
$this->_collection->setLoadDefault(true);
|
||||
$this->assertTrue($this->_collection->getLoadDefault());
|
||||
|
||||
$this->_collection->setLoadDefault(false);
|
||||
$this->assertFalse($this->_collection->getLoadDefault());
|
||||
}
|
||||
|
||||
public function testSetWithoutDefaultFilter()
|
||||
{
|
||||
$this->_collection->setWithoutDefaultFilter();
|
||||
$quote = $this->_getQuoteIdentifierSymbol();
|
||||
|
||||
$this->assertStringContainsString("{$quote}store_id{$quote} > 0", (string)$this->_collection->getSelect());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Store\Model\ResourceModel\Store\Collection::addGroupFilter
|
||||
* @covers \Magento\Store\Model\ResourceModel\Store\Collection::addIdFilter
|
||||
* @covers \Magento\Store\Model\ResourceModel\Store\Collection::addWebsiteFilter
|
||||
* @covers \Magento\Store\Model\ResourceModel\Store\Collection::addCategoryFilter
|
||||
*/
|
||||
public function testAddFilters()
|
||||
{
|
||||
$this->_collection->addGroupFilter(1);
|
||||
$quote = $this->_getQuoteIdentifierSymbol();
|
||||
$this->assertStringContainsString(
|
||||
"{$quote}group_id{$quote} IN",
|
||||
(string)$this->_collection->getSelect(),
|
||||
'Group filter'
|
||||
);
|
||||
|
||||
$this->_collection->addIdFilter(1);
|
||||
$this->assertStringContainsString(
|
||||
"{$quote}store_id{$quote} IN",
|
||||
(string)$this->_collection->getSelect(),
|
||||
'Id filter'
|
||||
);
|
||||
|
||||
$this->_collection->addWebsiteFilter(1);
|
||||
$this->assertStringContainsString(
|
||||
"{$quote}website_id{$quote} IN",
|
||||
(string)$this->_collection->getSelect(),
|
||||
'Website filter'
|
||||
);
|
||||
|
||||
$this->_collection->addCategoryFilter(1);
|
||||
$this->assertStringContainsString(
|
||||
"{$quote}root_category_id{$quote} IN",
|
||||
(string)$this->_collection->getSelect(),
|
||||
'Category filter'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get quote symbol from adapter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _getQuoteIdentifierSymbol()
|
||||
{
|
||||
return $this->_collection->getConnection()->getQuoteIdentifierSymbol();
|
||||
}
|
||||
|
||||
public function testToOptionArrayHash()
|
||||
{
|
||||
$this->assertIsArray($this->_collection->toOptionArray());
|
||||
$this->assertNotEmpty($this->_collection->toOptionArray());
|
||||
|
||||
$this->assertIsArray($this->_collection->toOptionHash());
|
||||
$this->assertNotEmpty($this->_collection->toOptionHash());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::addFieldToSelect
|
||||
* @covers \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::removeFieldFromSelect
|
||||
*/
|
||||
public function testAddRemoveFieldToSelect()
|
||||
{
|
||||
$this->_collection->addFieldToSelect(['store_id']);
|
||||
$this->assertStringContainsString('store_id', (string)$this->_collection->getSelect());
|
||||
$this->_collection->addFieldToSelect('*');
|
||||
$this->assertStringContainsString('*', (string)$this->_collection->getSelect());
|
||||
|
||||
$this->_collection->addFieldToSelect('test_field', 'test_alias');
|
||||
$this->assertStringContainsString('test_field', (string)$this->_collection->getSelect());
|
||||
$this->assertStringContainsString('test_alias', (string)$this->_collection->getSelect());
|
||||
|
||||
$this->_collection->removeFieldFromSelect('test_field');
|
||||
$this->_collection->addFieldToSelect('store_id');
|
||||
$this->assertStringNotContainsString('test_field', (string)$this->_collection->getSelect());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::addExpressionFieldToSelect
|
||||
*/
|
||||
public function testAddExpressionFieldToSelect()
|
||||
{
|
||||
$this->_collection->addExpressionFieldToSelect('test_alias', 'SUM({{store_id}})', 'store_id');
|
||||
$this->assertStringContainsString('SUM(store_id)', (string)$this->_collection->getSelect());
|
||||
$this->assertStringContainsString('test_alias', (string)$this->_collection->getSelect());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::getAllIds
|
||||
*/
|
||||
public function testGetAllIds()
|
||||
{
|
||||
$this->assertContainsEquals(\Magento\Store\Model\Store::DISTRO_STORE_ID, $this->_collection->getAllIds());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::getData
|
||||
*/
|
||||
public function testGetData()
|
||||
{
|
||||
$this->assertNotEmpty($this->_collection->getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::join
|
||||
*/
|
||||
public function testJoin()
|
||||
{
|
||||
$this->_collection->join(['w' => 'store_website'], 'main_table.website_id=w.website_id');
|
||||
$this->assertStringContainsString('store_website', (string)$this->_collection->getSelect());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model\ResourceModel;
|
||||
|
||||
class StoreTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testCountAll()
|
||||
{
|
||||
/** @var $model \Magento\Store\Model\ResourceModel\Store */
|
||||
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Store\Model\ResourceModel\Store::class
|
||||
);
|
||||
$this->assertEquals(1, $model->countAll());
|
||||
$this->assertEquals(1, $model->countAll(false));
|
||||
$this->assertEquals(2, $model->countAll(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model\ResourceModel;
|
||||
|
||||
class WebsiteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testCountAll()
|
||||
{
|
||||
/** @var $model \Magento\Store\Model\ResourceModel\Website */
|
||||
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Store\Model\ResourceModel\Website::class
|
||||
);
|
||||
$this->assertEquals(1, $model->countAll());
|
||||
$this->assertEquals(1, $model->countAll(false));
|
||||
$this->assertEquals(2, $model->countAll(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class StoreCookieManagerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreCookieManager
|
||||
*/
|
||||
protected $storeCookieManager;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $existingCookies;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->storeCookieManager = Bootstrap::getObjectManager()->create(
|
||||
\Magento\Store\Model\StoreCookieManager::class
|
||||
);
|
||||
$this->existingCookies = $_COOKIE;
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$_COOKIE = $this->existingCookies;
|
||||
}
|
||||
|
||||
public function testSetCookie()
|
||||
{
|
||||
$storeCode = 'store code';
|
||||
$store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStorePath', 'getCode']);
|
||||
$store->expects($this->once())->method('getStorePath')->willReturn('/');
|
||||
$store->expects($this->once())->method('getCode')->willReturn($storeCode);
|
||||
|
||||
$this->assertArrayNotHasKey(StoreCookieManager::COOKIE_NAME, $_COOKIE);
|
||||
$this->storeCookieManager->setStoreCookie($store);
|
||||
$this->assertArrayHasKey(StoreCookieManager::COOKIE_NAME, $_COOKIE);
|
||||
$this->assertEquals($storeCode, $_COOKIE[StoreCookieManager::COOKIE_NAME]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\Framework\ObjectManagerInterface as ObjectManager;
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class StoreManagerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* Class dependencies initialization
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that behavior of setting and getting store into StoreManager is correct
|
||||
* Setting: Magento\Store\Model\StoreManagerInterface::setCurrentStore
|
||||
* Getting: Magento\Store\Model\StoreManagerInterface::getStore
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testDefaultStoreIdIsSetCorrectly()
|
||||
{
|
||||
$this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
|
||||
$this->assertEquals(Store::DEFAULT_STORE_ID, $this->storeManager->getStore()->getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\TestFramework\Helper\CacheCleaner;
|
||||
|
||||
class StoreResolverTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \Magento\TestFramework\ObjectManager */
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
public function testGetStoreData()
|
||||
{
|
||||
$methodGetStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'getStoresData');
|
||||
$methodGetStoresData->setAccessible(true);
|
||||
$methodReadStoresData = new \ReflectionMethod(\Magento\Store\Model\StoreResolver::class, 'readStoresData');
|
||||
$methodReadStoresData->setAccessible(true);
|
||||
|
||||
$storeResolver = $this->objectManager->get(\Magento\Store\Model\StoreResolver::class);
|
||||
|
||||
$storesDataRead = $methodReadStoresData->invoke($storeResolver);
|
||||
$storesData = $methodGetStoresData->invoke($storeResolver);
|
||||
$storesDataCached = $methodGetStoresData->invoke($storeResolver);
|
||||
$this->assertEquals($storesDataRead, $storesData);
|
||||
$this->assertEquals($storesDataRead, $storesDataCached);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\Framework\ObjectManagerInterface as ObjectManager;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class StoreSwitcherTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var StoreSwitcher
|
||||
*/
|
||||
private $storeSwitcher;
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* Class dependencies initialization
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->storeSwitcher = $this->objectManager->get(StoreSwitcher::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Store/_files/store.php
|
||||
* @magentoDataFixture Magento/Store/_files/second_store.php
|
||||
* @return void
|
||||
* @throws StoreSwitcher\CannotSwitchStoreException
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
*/
|
||||
public function testSwitch(): void
|
||||
{
|
||||
$redirectUrl = "http://domain.com/?___store=fixture_second_store";
|
||||
$expectedUrl = "http://domain.com/";
|
||||
$fromStoreCode = 'test';
|
||||
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
|
||||
$storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
|
||||
$fromStore = $storeRepository->get($fromStoreCode);
|
||||
|
||||
$toStoreCode = 'fixture_second_store';
|
||||
/** @var \Magento\Store\Api\StoreRepositoryInterface $storeRepository */
|
||||
$storeRepository = $this->objectManager->create(\Magento\Store\Api\StoreRepositoryInterface::class);
|
||||
$toStore = $storeRepository->get($toStoreCode);
|
||||
|
||||
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,543 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Laminas\Stdlib\Parameters;
|
||||
use Magento\Catalog\Model\ProductRepository;
|
||||
use Magento\Framework\App\Bootstrap;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Session\SidResolverInterface;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
|
||||
/**
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
* phpcs:disable Magento2.Security.Superglobal
|
||||
*/
|
||||
class StoreTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $modelParams;
|
||||
|
||||
/**
|
||||
* @var Store|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* @var HttpRequest
|
||||
*/
|
||||
private $request;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->model = $this->_getStoreModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \PHPUnit\Framework\MockObject\MockObject|Store
|
||||
*/
|
||||
protected function _getStoreModel()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->request = $objectManager->get(RequestInterface::class);
|
||||
$this->modelParams = [
|
||||
'context' => $objectManager->get(\Magento\Framework\Model\Context::class),
|
||||
'registry' => $objectManager->get(\Magento\Framework\Registry::class),
|
||||
'extensionFactory' => $objectManager->get(\Magento\Framework\Api\ExtensionAttributesFactory::class),
|
||||
'customAttributeFactory' => $objectManager->get(\Magento\Framework\Api\AttributeValueFactory::class),
|
||||
'resource' => $objectManager->get(\Magento\Store\Model\ResourceModel\Store::class),
|
||||
'coreFileStorageDatabase' => $objectManager->get(\Magento\MediaStorage\Helper\File\Storage\Database::class),
|
||||
'configCacheType' => $objectManager->get(\Magento\Framework\App\Cache\Type\Config::class),
|
||||
'url' => $objectManager->get(\Magento\Framework\Url::class),
|
||||
'request' => $this->request,
|
||||
'configDataResource' => $objectManager->get(\Magento\Config\Model\ResourceModel\Config\Data::class),
|
||||
'filesystem' => $objectManager->get(\Magento\Framework\Filesystem::class),
|
||||
'config' => $objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class),
|
||||
'storeManager' => $objectManager->get(\Magento\Store\Model\StoreManager::class),
|
||||
'sidResolver' => $objectManager->get(\Magento\Framework\Session\SidResolverInterface::class),
|
||||
'httpContext' => $objectManager->get(\Magento\Framework\App\Http\Context::class),
|
||||
'session' => $objectManager->get(\Magento\Framework\Session\SessionManagerInterface::class),
|
||||
'currencyFactory' => $objectManager->get(\Magento\Directory\Model\CurrencyFactory::class),
|
||||
'information' => $objectManager->get(\Magento\Store\Model\Information::class),
|
||||
'currencyInstalled' => 'system/currency/installed',
|
||||
'groupRepository' => $objectManager->get(\Magento\Store\Api\GroupRepositoryInterface::class),
|
||||
'websiteRepository' => $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class),
|
||||
];
|
||||
|
||||
return $this->getMockBuilder(\Magento\Store\Model\Store::class)
|
||||
->setMethods(['getUrl'])
|
||||
->setConstructorArgs($this->modelParams)
|
||||
->getMock();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->model = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $loadId
|
||||
* @param $expectedId
|
||||
* @dataProvider loadDataProvider
|
||||
*/
|
||||
public function testLoad($loadId, $expectedId)
|
||||
{
|
||||
$this->model->load($loadId);
|
||||
$this->assertEquals($expectedId, $this->model->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function loadDataProvider()
|
||||
{
|
||||
return [[1, 1], ['default', 1], ['nostore', null]];
|
||||
}
|
||||
|
||||
public function testSetGetWebsite()
|
||||
{
|
||||
$this->assertFalse($this->model->getWebsite());
|
||||
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Store\Model\StoreManagerInterface::class
|
||||
)->getWebsite();
|
||||
$this->model->setWebsite($website);
|
||||
$actualResult = $this->model->getWebsite();
|
||||
$this->assertSame($website, $actualResult);
|
||||
}
|
||||
|
||||
public function testSetGetGroup()
|
||||
{
|
||||
$this->assertFalse($this->model->getGroup());
|
||||
$storeGroup = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Store\Model\StoreManager::class
|
||||
)->getGroup();
|
||||
$this->model->setGroup($storeGroup);
|
||||
$actualResult = $this->model->getGroup();
|
||||
$this->assertSame($storeGroup, $actualResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* Isolation is enabled, as we pollute config with rewrite values
|
||||
*
|
||||
* @param string $type
|
||||
* @param bool $useRewrites
|
||||
* @param bool $useStoreCode
|
||||
* @param string $expected
|
||||
* @dataProvider getBaseUrlDataProvider
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetBaseUrl($type, $useRewrites, $useStoreCode, $expected)
|
||||
{
|
||||
/* config operations require store to be loaded */
|
||||
$this->model->load('default');
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
|
||||
->setValue(Store::XML_PATH_USE_REWRITES, $useRewrites, ScopeInterface::SCOPE_STORE);
|
||||
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
|
||||
->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode, ScopeInterface::SCOPE_STORE);
|
||||
|
||||
$actual = $this->model->getBaseUrl($type);
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBaseUrlDataProvider()
|
||||
{
|
||||
return [
|
||||
[UrlInterface::URL_TYPE_WEB, false, false, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_WEB, false, true, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_WEB, true, false, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_WEB, true, true, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_LINK, false, false, 'http://localhost/index.php/'],
|
||||
[UrlInterface::URL_TYPE_LINK, false, true, 'http://localhost/index.php/default/'],
|
||||
[UrlInterface::URL_TYPE_LINK, true, false, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_LINK, true, true, 'http://localhost/default/'],
|
||||
[UrlInterface::URL_TYPE_DIRECT_LINK, false, false, 'http://localhost/index.php/'],
|
||||
[UrlInterface::URL_TYPE_DIRECT_LINK, false, true, 'http://localhost/index.php/'],
|
||||
[UrlInterface::URL_TYPE_DIRECT_LINK, true, false, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_DIRECT_LINK, true, true, 'http://localhost/'],
|
||||
[UrlInterface::URL_TYPE_STATIC, false, false, 'http://localhost/static/'],
|
||||
[UrlInterface::URL_TYPE_STATIC, false, true, 'http://localhost/static/'],
|
||||
[UrlInterface::URL_TYPE_STATIC, true, false, 'http://localhost/static/'],
|
||||
[UrlInterface::URL_TYPE_STATIC, true, true, 'http://localhost/static/'],
|
||||
[UrlInterface::URL_TYPE_MEDIA, false, false, 'http://localhost/media/'],
|
||||
[UrlInterface::URL_TYPE_MEDIA, false, true, 'http://localhost/media/'],
|
||||
[UrlInterface::URL_TYPE_MEDIA, true, false, 'http://localhost/media/'],
|
||||
[UrlInterface::URL_TYPE_MEDIA, true, true, 'http://localhost/media/']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetBaseUrlInPub()
|
||||
{
|
||||
\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize(
|
||||
[
|
||||
Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS => [
|
||||
DirectoryList::PUB => [DirectoryList::URL_PATH => ''],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$this->model = $this->_getStoreModel();
|
||||
$this->model->load('default');
|
||||
|
||||
$this->assertEquals('http://localhost/static/', $this->model->getBaseUrl(UrlInterface::URL_TYPE_STATIC));
|
||||
$this->assertEquals('http://localhost/media/', $this->model->getBaseUrl(UrlInterface::URL_TYPE_MEDIA));
|
||||
}
|
||||
|
||||
/**
|
||||
* Isolation is enabled, as we pollute config with rewrite values
|
||||
*
|
||||
* @param string $type
|
||||
* @param bool $useCustomEntryPoint
|
||||
* @param bool $useStoreCode
|
||||
* @param string $expected
|
||||
* @dataProvider getBaseUrlForCustomEntryPointDataProvider
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetBaseUrlForCustomEntryPoint($type, $useCustomEntryPoint, $useStoreCode, $expected)
|
||||
{
|
||||
/* config operations require store to be loaded */
|
||||
$this->model->load('default');
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
|
||||
->setValue(Store::XML_PATH_USE_REWRITES, false, ScopeInterface::SCOPE_STORE);
|
||||
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
|
||||
->setValue(Store::XML_PATH_STORE_IN_URL, $useStoreCode, ScopeInterface::SCOPE_STORE);
|
||||
|
||||
// emulate custom entry point
|
||||
$_SERVER['SCRIPT_FILENAME'] = 'custom_entry.php';
|
||||
$request = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->get(\Magento\Framework\App\RequestInterface::class);
|
||||
$request->setServer(new Parameters($_SERVER));
|
||||
|
||||
if ($useCustomEntryPoint) {
|
||||
$property = new \ReflectionProperty($this->model, '_isCustomEntryPoint');
|
||||
$property->setAccessible(true);
|
||||
$property->setValue($this->model, $useCustomEntryPoint);
|
||||
}
|
||||
$actual = $this->model->getBaseUrl($type);
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBaseUrlForCustomEntryPointDataProvider()
|
||||
{
|
||||
return [
|
||||
[UrlInterface::URL_TYPE_LINK, false, false, 'http://localhost/custom_entry.php/'],
|
||||
[
|
||||
UrlInterface::URL_TYPE_LINK,
|
||||
false,
|
||||
true,
|
||||
'http://localhost/custom_entry.php/default/'
|
||||
],
|
||||
[UrlInterface::URL_TYPE_LINK, true, false, 'http://localhost/index.php/'],
|
||||
[UrlInterface::URL_TYPE_LINK, true, true, 'http://localhost/index.php/default/'],
|
||||
[
|
||||
UrlInterface::URL_TYPE_DIRECT_LINK,
|
||||
false,
|
||||
false,
|
||||
'http://localhost/custom_entry.php/'
|
||||
],
|
||||
[
|
||||
UrlInterface::URL_TYPE_DIRECT_LINK,
|
||||
false,
|
||||
true,
|
||||
'http://localhost/custom_entry.php/'
|
||||
],
|
||||
[UrlInterface::URL_TYPE_DIRECT_LINK, true, false, 'http://localhost/index.php/'],
|
||||
[UrlInterface::URL_TYPE_DIRECT_LINK, true, true, 'http://localhost/index.php/']
|
||||
];
|
||||
}
|
||||
|
||||
public function testGetDefaultCurrency()
|
||||
{
|
||||
/* currency operations require store to be loaded */
|
||||
$this->model->load('default');
|
||||
$this->assertEquals($this->model->getDefaultCurrencyCode(), $this->model->getDefaultCurrency()->getCode());
|
||||
}
|
||||
|
||||
public function testIsCanDelete()
|
||||
{
|
||||
$this->assertFalse($this->model->isCanDelete());
|
||||
$this->model->load(1);
|
||||
$this->assertFalse($this->model->isCanDelete());
|
||||
$this->model->setId(100);
|
||||
$this->assertFalse($this->model->isCanDelete());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetCurrentUrl()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class)
|
||||
->setValue('web/url/use_store', true, ScopeInterface::SCOPE_STORE, 'secondstore');
|
||||
|
||||
$this->model->load('admin');
|
||||
$this->model
|
||||
->expects($this->any())->method('getUrl')
|
||||
->willReturn('http://localhost/index.php');
|
||||
$this->assertStringEndsWith('default', $this->model->getCurrentUrl());
|
||||
$this->assertStringEndsNotWith('default', $this->model->getCurrentUrl(false));
|
||||
|
||||
$this->model
|
||||
->expects($this->any())->method('getUrl')
|
||||
->willReturn('http://localhost/index.php?' . SidResolverInterface::SESSION_ID_QUERY_PARAM . '=12345');
|
||||
$this->request->setParams([SidResolverInterface::SESSION_ID_QUERY_PARAM, '12345']);
|
||||
$this->request->setQueryValue(SidResolverInterface::SESSION_ID_QUERY_PARAM, '12345');
|
||||
$this->assertStringContainsString(
|
||||
SidResolverInterface::SESSION_ID_QUERY_PARAM . '=12345',
|
||||
$this->model->getCurrentUrl()
|
||||
);
|
||||
|
||||
/** @var \Magento\Store\Model\Store $secondStore */
|
||||
$secondStore = $objectManager->get(StoreRepositoryInterface::class)->get('secondstore');
|
||||
|
||||
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
|
||||
$productRepository = $objectManager->create(ProductRepository::class);
|
||||
$product = $productRepository->get('simple');
|
||||
$product->setStoreId($secondStore->getId());
|
||||
$url = $product->getUrlInStore();
|
||||
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() . 'catalog/product/view/id/1/s/simple-product/',
|
||||
$url
|
||||
);
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() . '?SID=12345&___from_store=default',
|
||||
$secondStore->getCurrentUrl()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() . '?SID=12345',
|
||||
$secondStore->getCurrentUrl(false)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testGetCurrentUrlWithUseStoreInUrlFalse()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class)
|
||||
->setValue('web/url/use_store', false, ScopeInterface::SCOPE_STORE, 'default');
|
||||
|
||||
/** @var \Magento\Store\Model\Store $secondStore */
|
||||
$secondStore = $objectManager->get(StoreRepositoryInterface::class)->get('secondstore');
|
||||
|
||||
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
|
||||
$productRepository = $objectManager->create(ProductRepository::class);
|
||||
$product = $productRepository->get('simple');
|
||||
|
||||
$product->setStoreId($secondStore->getId());
|
||||
$url = $product->getUrlInStore();
|
||||
|
||||
/** @var \Magento\Catalog\Model\CategoryRepository $categoryRepository */
|
||||
$categoryRepository = $objectManager->get(\Magento\Catalog\Model\CategoryRepository::class);
|
||||
$category = $categoryRepository->get(2, $secondStore->getStoreId());
|
||||
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() . 'catalog/category/view/s/default-category/id/2/',
|
||||
$category->getUrl()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() .
|
||||
'catalog/product/view/id/1/s/simple-product/?___store=secondstore',
|
||||
$url
|
||||
);
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() . '?___store=secondstore&___from_store=default',
|
||||
$secondStore->getCurrentUrl()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$secondStore->getBaseUrl() . '?___store=secondstore',
|
||||
$secondStore->getCurrentUrl(false)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testCRUD()
|
||||
{
|
||||
$this->model->setData(
|
||||
[
|
||||
'code' => 'test',
|
||||
'website_id' => 1,
|
||||
'group_id' => 1,
|
||||
'name' => 'test name',
|
||||
'sort_order' => 0,
|
||||
'is_active' => 1,
|
||||
]
|
||||
);
|
||||
$crud = new \Magento\TestFramework\Entity(
|
||||
$this->model,
|
||||
['name' => 'new name'],
|
||||
\Magento\Store\Model\Store::class
|
||||
);
|
||||
$crud->testCrud();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $badStoreData
|
||||
*
|
||||
* @dataProvider saveValidationDataProvider
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testSaveValidation($badStoreData)
|
||||
{
|
||||
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
|
||||
|
||||
$normalStoreData = [
|
||||
'code' => 'test',
|
||||
'website_id' => 1,
|
||||
'group_id' => 1,
|
||||
'name' => 'test name',
|
||||
'sort_order' => 0,
|
||||
'is_active' => 1,
|
||||
];
|
||||
$data = array_merge($normalStoreData, $badStoreData);
|
||||
$this->model->setData($data);
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function saveValidationDataProvider()
|
||||
{
|
||||
return [
|
||||
'empty store name' => [['name' => '']],
|
||||
'empty store code' => [['code' => '']],
|
||||
'invalid store code' => [['code' => '^_^']]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $storeInUrl
|
||||
* @param $disableStoreInUrl
|
||||
* @param $singleStoreModeEnabled
|
||||
* @param $expectedResult
|
||||
* @dataProvider isUseStoreInUrlDataProvider
|
||||
*/
|
||||
public function testIsUseStoreInUrl($storeInUrl, $disableStoreInUrl, $singleStoreModeEnabled, $expectedResult)
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$configMock = $this->createMock(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
|
||||
$appStateMock = $this->createMock(\Magento\Framework\App\State::class);
|
||||
|
||||
$params = $this->modelParams;
|
||||
$params['context'] = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Framework\Model\Context::class, ['appState' => $appStateMock]);
|
||||
|
||||
$configMock
|
||||
->method('getValue')
|
||||
->withConsecutive(
|
||||
[$this->stringContains(StoreManager::XML_PATH_SINGLE_STORE_MODE_ENABLED)],
|
||||
[$this->stringContains(Store::XML_PATH_STORE_IN_URL)]
|
||||
)
|
||||
->willReturnOnConsecutiveCalls($singleStoreModeEnabled, $storeInUrl);
|
||||
|
||||
$params['config'] = $configMock;
|
||||
$model = $objectManager->create(\Magento\Store\Model\Store::class, $params);
|
||||
$model->setDisableStoreInUrl($disableStoreInUrl);
|
||||
$this->assertEquals($expectedResult, $model->isUseStoreInUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @see self::testIsUseStoreInUrl;
|
||||
*/
|
||||
public function isUseStoreInUrlDataProvider()
|
||||
{
|
||||
return [
|
||||
[true, null, false, true],
|
||||
[false, null, false, false],
|
||||
[true, true, false, false],
|
||||
[true, false, false, true],
|
||||
[true, null, true, false],
|
||||
[false, null, true, false],
|
||||
[true, true, true, false],
|
||||
[true, false, true, false]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isCurrentlySecureDataProvider
|
||||
*
|
||||
* @param bool $expected
|
||||
* @param array $serverValues
|
||||
* @magentoConfigFixture current_store web/secure/offloader_header X_FORWARDED_PROTO
|
||||
* @magentoConfigFixture current_store web/secure/base_url https://example.com:80
|
||||
*/
|
||||
public function testIsCurrentlySecure($expected, $serverValues)
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var Store $model */
|
||||
$model = $objectManager->create(\Magento\Store\Model\Store::class);
|
||||
|
||||
$request = $objectManager->get(\Magento\Framework\App\RequestInterface::class);
|
||||
$request->setServer(new Parameters(array_merge($_SERVER, $serverValues)));
|
||||
|
||||
$this->assertEquals($expected, $model->isCurrentlySecure());
|
||||
}
|
||||
|
||||
public function isCurrentlySecureDataProvider()
|
||||
{
|
||||
return [
|
||||
[true, ['HTTPS' => 'on']],
|
||||
[true, ['X_FORWARDED_PROTO' => 'https']],
|
||||
[true, ['HTTP_X_FORWARDED_PROTO' => 'https']],
|
||||
[true, ['HTTPS' => 'on', 'SERVER_PORT' => 80]],
|
||||
[false, ['SERVER_PORT' => 80]],
|
||||
[false, []],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store web/secure/offloader_header SSL_OFFLOADED
|
||||
* @magentoConfigFixture current_store web/secure/base_url
|
||||
*/
|
||||
public function testIsCurrentlySecureNoSecureBaseUrl()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var Store $model */
|
||||
$model = $objectManager->create(\Magento\Store\Model\Store::class);
|
||||
|
||||
$server = $_SERVER;
|
||||
$_SERVER['SERVER_PORT'] = 80;
|
||||
|
||||
$this->assertFalse($model->isCurrentlySecure());
|
||||
$_SERVER = $server;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Store\Model\Validation;
|
||||
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StoreValidatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var StoreValidator
|
||||
*/
|
||||
private $storeValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->storeValidator = Bootstrap::getObjectManager()->create(StoreValidator::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider isValidDataProvider
|
||||
* @param Store $store
|
||||
* @param bool $isValid
|
||||
*/
|
||||
public function testIsValid(Store $store, bool $isValid): void
|
||||
{
|
||||
$result = $this->storeValidator->isValid($store);
|
||||
$this->assertEquals($isValid, $result);
|
||||
}
|
||||
|
||||
public function isValidDataProvider(): array
|
||||
{
|
||||
$validStore = Bootstrap::getObjectManager()->create(Store::class);
|
||||
$validStore->setName('name');
|
||||
$validStore->setCode('code');
|
||||
$emptyStore = Bootstrap::getObjectManager()->create(Store::class);
|
||||
$storeWithEmptyName = Bootstrap::getObjectManager()->create(Store::class);
|
||||
$storeWithEmptyName->setCode('code');
|
||||
$storeWithEmptyCode = Bootstrap::getObjectManager()->create(Store::class);
|
||||
$storeWithEmptyCode->setName('name');
|
||||
$storeWithInvalidCode = Bootstrap::getObjectManager()->create(Store::class);
|
||||
$storeWithInvalidCode->setName('name');
|
||||
$storeWithInvalidCode->setCode('5');
|
||||
|
||||
return [
|
||||
[$validStore, true],
|
||||
[$emptyStore, false],
|
||||
[$storeWithEmptyName, false],
|
||||
[$storeWithEmptyCode, false],
|
||||
[$storeWithInvalidCode, false],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Store\Model;
|
||||
|
||||
use Magento\Framework\App\Cache\Type\Config;
|
||||
use Magento\Framework\App\Cache\TypeListInterface;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\PageCache\Model\Cache\Type;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
class WebsiteTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Store\Model\Website
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
/**
|
||||
* @var TypeListInterface
|
||||
*/
|
||||
private $typeList;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->typeList = $this->objectManager->create(TypeListInterface::class);
|
||||
$this->_model = $this->objectManager->create(\Magento\Store\Model\Website::class);
|
||||
$this->_model->load(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testLoadById()
|
||||
{
|
||||
$this->assertEquals(1, $this->_model->getId());
|
||||
$this->assertEquals('base', $this->_model->getCode());
|
||||
$this->assertEquals('Main Website', $this->_model->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
public function testLoadByCode()
|
||||
{
|
||||
$this->_model->load('admin');
|
||||
$this->assertEquals(0, $this->_model->getId());
|
||||
$this->assertEquals('admin', $this->_model->getCode());
|
||||
$this->assertEquals('Admin', $this->_model->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Store\Model\Website::setGroups
|
||||
* @covers \Magento\Store\Model\Website::setStores
|
||||
* @covers \Magento\Store\Model\Website::getStores
|
||||
*/
|
||||
public function testSetGroupsAndStores()
|
||||
{
|
||||
/* Groups */
|
||||
$expectedGroup = $this->objectManager->create(\Magento\Store\Model\Group::class);
|
||||
$expectedGroup->setId(123);
|
||||
$this->_model->setDefaultGroupId($expectedGroup->getId());
|
||||
$this->_model->setGroups([$expectedGroup]);
|
||||
|
||||
$groups = $this->_model->getGroups();
|
||||
$this->assertSame($expectedGroup, reset($groups));
|
||||
|
||||
/* Stores */
|
||||
$expectedStore = $this->objectManager->create(\Magento\Store\Model\Store::class);
|
||||
$expectedStore->setId(456);
|
||||
$expectedGroup->setDefaultStoreId($expectedStore->getId());
|
||||
$this->_model->setStores([$expectedStore]);
|
||||
|
||||
$stores = $this->_model->getStores();
|
||||
$this->assertSame($expectedStore, reset($stores));
|
||||
}
|
||||
|
||||
public function testGetGroups()
|
||||
{
|
||||
$groups = $this->_model->getGroups();
|
||||
$this->assertEquals([1], array_keys($groups));
|
||||
$this->assertInstanceOf(\Magento\Store\Model\Group::class, $groups[1]);
|
||||
$this->assertEquals(1, $groups[1]->getId());
|
||||
}
|
||||
|
||||
public function testGetGroupIds()
|
||||
{
|
||||
$this->assertEquals([1 => 1], $this->_model->getGroupIds());
|
||||
}
|
||||
|
||||
public function testGetGroupsCount()
|
||||
{
|
||||
$this->assertEquals(1, $this->_model->getGroupsCount());
|
||||
}
|
||||
|
||||
public function testGetDefaultGroup()
|
||||
{
|
||||
$defaultGroup = $this->_model->getDefaultGroup();
|
||||
$this->assertInstanceOf(\Magento\Store\Model\Group::class, $defaultGroup);
|
||||
$this->assertEquals(1, $defaultGroup->getId());
|
||||
|
||||
$this->_model->setDefaultGroupId(null);
|
||||
$this->assertFalse($this->_model->getDefaultGroup());
|
||||
}
|
||||
|
||||
public function testGetStores()
|
||||
{
|
||||
$stores = $this->_model->getStores();
|
||||
$this->assertEquals([1], array_keys($stores));
|
||||
$this->assertInstanceOf(\Magento\Store\Model\Store::class, $stores[1]);
|
||||
$this->assertEquals(1, $stores[1]->getId());
|
||||
}
|
||||
|
||||
public function testGetStoreIds()
|
||||
{
|
||||
$this->assertEquals([1 => 1], $this->_model->getStoreIds());
|
||||
}
|
||||
|
||||
public function testGetStoreCodes()
|
||||
{
|
||||
$this->assertEquals([1 => 'default'], $this->_model->getStoreCodes());
|
||||
}
|
||||
|
||||
public function testGetStoresCount()
|
||||
{
|
||||
$this->assertEquals(1, $this->_model->getStoresCount());
|
||||
}
|
||||
|
||||
public function testIsCanDelete()
|
||||
{
|
||||
$this->assertFalse($this->_model->isCanDelete());
|
||||
$this->_model->isReadOnly(true);
|
||||
$this->assertFalse($this->_model->isCanDelete());
|
||||
}
|
||||
|
||||
public function testGetWebsiteGroupStore()
|
||||
{
|
||||
$this->assertEquals('1--', $this->_model->getWebsiteGroupStore());
|
||||
$this->_model->setGroupId(123);
|
||||
$this->_model->setStoreId(456);
|
||||
$this->assertEquals('1-123-456', $this->_model->getWebsiteGroupStore());
|
||||
}
|
||||
|
||||
public function testGetDefaultGroupId()
|
||||
{
|
||||
$this->assertEquals(1, $this->_model->getDefaultGroupId());
|
||||
}
|
||||
|
||||
public function testGetBaseCurrency()
|
||||
{
|
||||
$currency = $this->_model->getBaseCurrency();
|
||||
$this->assertInstanceOf(\Magento\Directory\Model\Currency::class, $currency);
|
||||
$this->assertEquals('USD', $currency->getCode());
|
||||
}
|
||||
|
||||
public function testGetDefaultStore()
|
||||
{
|
||||
$defaultStore = $this->_model->getDefaultStore();
|
||||
$this->assertInstanceOf(\Magento\Store\Model\Store::class, $defaultStore);
|
||||
$this->assertEquals(1, $defaultStore->getId());
|
||||
}
|
||||
|
||||
public function testGetDefaultStoresSelect()
|
||||
{
|
||||
$this->assertInstanceOf(\Magento\Framework\DB\Select::class, $this->_model->getDefaultStoresSelect());
|
||||
}
|
||||
|
||||
public function testIsReadonly()
|
||||
{
|
||||
$this->assertFalse($this->_model->isReadOnly());
|
||||
$this->_model->isReadOnly(true);
|
||||
$this->assertTrue($this->_model->isReadOnly());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
public function testCRUD()
|
||||
{
|
||||
$this->_model->setData(['code' => 'test_website', 'name' => 'test website', 'default_group_id' => 1]);
|
||||
|
||||
/* emulate admin store */
|
||||
$crud = new \Magento\TestFramework\Entity($this->_model, ['name' => 'new name']);
|
||||
$crud->testCrud();
|
||||
}
|
||||
|
||||
public function testCollection()
|
||||
{
|
||||
$collection = $this->_model->getCollection()->joinGroupAndStore()->addIdFilter(1);
|
||||
$this->assertCount(1, $collection->getItems());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Store/_files/website.php
|
||||
* @magentoCache full_page enabled
|
||||
* @magentoDbIsolation disabled
|
||||
*/
|
||||
public function testCacheInvalidationOnWebsiteUpdateAndDeletion()
|
||||
{
|
||||
$this->typeList->cleanType(Type::TYPE_IDENTIFIER);
|
||||
$this->typeList->cleanType(Config::TYPE_IDENTIFIER);
|
||||
|
||||
$this->assertCacheStatusAfterAction(
|
||||
$this->typeList->getInvalidated(),
|
||||
0,
|
||||
'should be clean before website update.'
|
||||
);
|
||||
|
||||
$website = $this->objectManager->create(\Magento\Store\Model\Website::class);
|
||||
$website->load('test', 'code');
|
||||
$website->setName('Test Website 1');
|
||||
$website->save();
|
||||
|
||||
$this->assertEquals('Test Website 1', $website->getName());
|
||||
|
||||
$this->assertCacheStatusAfterAction(
|
||||
$this->typeList->getInvalidated(),
|
||||
1,
|
||||
'was not invalidated after website update.'
|
||||
);
|
||||
|
||||
/** Marks area as secure to allow website removal */
|
||||
$registry = $this->objectManager->get(Registry::class);
|
||||
$isSecuredAreaSystemState = $registry->registry('isSecuredArea');
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
$website->delete();
|
||||
|
||||
/** Revert mark area secured */
|
||||
$registry->unregister('isSecuredArea');
|
||||
$registry->register('isSecuredArea', $isSecuredAreaSystemState);
|
||||
|
||||
$this->assertCacheStatusAfterAction(
|
||||
$this->typeList->getInvalidated(),
|
||||
0,
|
||||
'should be clean after website removal.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $invalidatedCacheTypes
|
||||
* @param int $expectedStatus
|
||||
* @param string $messageEnd
|
||||
* @return void
|
||||
*/
|
||||
private function assertCacheStatusAfterAction(
|
||||
array $invalidatedCacheTypes,
|
||||
int $expectedStatus,
|
||||
string $messageEnd
|
||||
): void {
|
||||
if (array_key_exists(Type::TYPE_IDENTIFIER, $invalidatedCacheTypes)) {
|
||||
$this->assertEquals(
|
||||
$expectedStatus,
|
||||
$invalidatedCacheTypes[Type::TYPE_IDENTIFIER]->getData('status'),
|
||||
"Full page cache " . $messageEnd
|
||||
);
|
||||
}
|
||||
|
||||
if (array_key_exists(Config::TYPE_IDENTIFIER, $invalidatedCacheTypes)) {
|
||||
$this->assertEquals(
|
||||
$expectedStatus,
|
||||
$invalidatedCacheTypes[Config::TYPE_IDENTIFIER]->getData('status'),
|
||||
"Configuration cache " . $messageEnd
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user