From 322cb25455530a349f86716af6b415da83b3266a Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Wed, 5 Aug 2020 16:04:40 +0300 Subject: [PATCH 01/10] Added cli command to create store view --- .../Console/Command/StoreCreateCommand.php | 166 ++++++++++++++++++ app/code/Magento/Store/etc/di.xml | 1 + 2 files changed, 167 insertions(+) create mode 100644 app/code/Magento/Store/Console/Command/StoreCreateCommand.php diff --git a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php new file mode 100644 index 0000000000000..180c9099ce397 --- /dev/null +++ b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php @@ -0,0 +1,166 @@ +websiteFactory = $websiteFactory; + $this->groupFactory = $groupFactory; + $this->storeFactory = $storeFactory; + $this->filterManager = $filterManager; + parent::__construct($name); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this->setName('store:create') + ->setDescription('Create new store view (see list: bin/magento store:list)') + ->addArgument(self::INPUT_ARGUMENT_NAME, InputArgument::REQUIRED, 'Put the store view name you want to create') + ->addArgument(self::INPUT_ARGUMENT_CODE, InputArgument::REQUIRED, 'Put the store view code') + ->addArgument( + self::INPUT_ARGUMENT_IS_ACTIVE, + InputArgument::OPTIONAL, + 'Status (enable/disable)', + true + ) + ->addArgument( + self::INPUT_ARGUMENT_SORT_ORDER, + InputArgument::OPTIONAL, + 'Sort Order', + 0 + ) + ; + $groups = $this->getStoreGroups(); + $defaultGroup = current($groups); + + $this->addOption( + self::INPUT_OPTION_GROUP, + 'g', + InputOption::VALUE_OPTIONAL, + 'Group ID (php bin/magento store:list).', + $defaultGroup + ); + + parent::configure(); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + try { + $groupId = (int) $input->getOption(self::INPUT_OPTION_GROUP); + $storeViewName = $input->getArgument(self::INPUT_ARGUMENT_NAME); + $storeViewCode = $input->getArgument(self::INPUT_ARGUMENT_CODE); + $isActive = (bool) $input->getArgument(self::INPUT_ARGUMENT_IS_ACTIVE); + $sortOrder = (int) $input->getArgument(self::INPUT_ARGUMENT_SORT_ORDER); + + $data = [ + self::INPUT_OPTION_GROUP => $groupId, + self::INPUT_ARGUMENT_NAME => $storeViewName, + self::INPUT_ARGUMENT_CODE => $storeViewCode, + self::INPUT_ARGUMENT_IS_ACTIVE => $isActive, + self::INPUT_ARGUMENT_SORT_ORDER => $sortOrder + ]; + /** @var \Magento\Store\Model\Store $storeModel */ + $storeModel = $this->storeFactory->create(); + $data['name'] = $this->filterManager->removeTags($data['name']); + + $storeModel->setData($data); + $groupModel = $this->groupFactory->create()->load( + $storeModel->getGroupId() + ); + $storeModel->setWebsiteId($groupModel->getWebsiteId()); + $storeModel->save(); + $io->success('You created the store view.'); + + return \Magento\Framework\Console\Cli::RETURN_SUCCESS; + } catch (\Exception $e) { + $io->error($e->getMessage()); + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { + $io->comment($e->getTraceAsString()); + } + + return \Magento\Framework\Console\Cli::RETURN_FAILURE; + } + } + + /** + * Retrieve list of store groups + * + * @return array + */ + private function getStoreGroups() + { + $websites = $this->websiteFactory->create()->getCollection(); + $allgroups = $this->groupFactory->create()->getCollection(); + $groups = []; + foreach ($websites as $website) { + foreach ($allgroups as $group) { + if ($group->getWebsiteId() == $website->getId()) { + $groups[$group->getWebsiteId() . '-' . $group->getId()] = $group->getId(); + } + } + } + + return $groups; + } +} diff --git a/app/code/Magento/Store/etc/di.xml b/app/code/Magento/Store/etc/di.xml index 2da9e91e1fddd..94e76e81ee0a2 100644 --- a/app/code/Magento/Store/etc/di.xml +++ b/app/code/Magento/Store/etc/di.xml @@ -380,6 +380,7 @@ Magento\Store\Console\Command\StoreListCommand + Magento\Store\Console\Command\StoreCreateCommand Magento\Store\Console\Command\WebsiteListCommand From 10352e9baee9e3734cd7ac7b5ae86b6afc1fdd86 Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Wed, 5 Aug 2020 16:18:00 +0300 Subject: [PATCH 02/10] Sterilized name and code arguments --- .../Magento/Store/Console/Command/StoreCreateCommand.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php index 180c9099ce397..f049654c92997 100644 --- a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php +++ b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php @@ -122,7 +122,12 @@ protected function execute(InputInterface $input, OutputInterface $output) ]; /** @var \Magento\Store\Model\Store $storeModel */ $storeModel = $this->storeFactory->create(); - $data['name'] = $this->filterManager->removeTags($data['name']); + $data[self::INPUT_ARGUMENT_NAME] = $this->filterManager->removeTags( + $data[self::INPUT_ARGUMENT_NAME] + ); + $data[self::INPUT_ARGUMENT_CODE] = $this->filterManager->removeTags( + $data[self::INPUT_ARGUMENT_CODE] + ); $storeModel->setData($data); $groupModel = $this->groupFactory->create()->load( From 050d238a1b04b2b5c5b706d62674273b1f65f7c3 Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Thu, 22 Oct 2020 14:28:17 +0300 Subject: [PATCH 03/10] Add StoreCreateManagment model --- .../Api/StoreCreateManagementInterface.php | 19 ++++ .../Console/Command/StoreCreateCommand.php | 65 +++---------- .../Store/Model/StoreCreateManagement.php | 93 +++++++++++++++++++ 3 files changed, 123 insertions(+), 54 deletions(-) create mode 100644 app/code/Magento/Store/Api/StoreCreateManagementInterface.php create mode 100644 app/code/Magento/Store/Model/StoreCreateManagement.php diff --git a/app/code/Magento/Store/Api/StoreCreateManagementInterface.php b/app/code/Magento/Store/Api/StoreCreateManagementInterface.php new file mode 100644 index 0000000000000..1fcac8a5aa9ce --- /dev/null +++ b/app/code/Magento/Store/Api/StoreCreateManagementInterface.php @@ -0,0 +1,19 @@ +websiteFactory = $websiteFactory; - $this->groupFactory = $groupFactory; - $this->storeFactory = $storeFactory; + $this->storeCreateManagement = $storeCreateManagement; $this->filterManager = $filterManager; parent::__construct($name); } @@ -85,8 +70,7 @@ protected function configure() 0 ) ; - $groups = $this->getStoreGroups(); - $defaultGroup = current($groups); + $defaultGroup = $this->storeCreateManagement->getDefaultGroupId(); $this->addOption( self::INPUT_OPTION_GROUP, @@ -108,8 +92,8 @@ protected function execute(InputInterface $input, OutputInterface $output) try { $groupId = (int) $input->getOption(self::INPUT_OPTION_GROUP); - $storeViewName = $input->getArgument(self::INPUT_ARGUMENT_NAME); - $storeViewCode = $input->getArgument(self::INPUT_ARGUMENT_CODE); + $storeViewName = (string) $input->getArgument(self::INPUT_ARGUMENT_NAME); + $storeViewCode = (string) $input->getArgument(self::INPUT_ARGUMENT_CODE); $isActive = (bool) $input->getArgument(self::INPUT_ARGUMENT_IS_ACTIVE); $sortOrder = (int) $input->getArgument(self::INPUT_ARGUMENT_SORT_ORDER); @@ -120,8 +104,6 @@ protected function execute(InputInterface $input, OutputInterface $output) self::INPUT_ARGUMENT_IS_ACTIVE => $isActive, self::INPUT_ARGUMENT_SORT_ORDER => $sortOrder ]; - /** @var \Magento\Store\Model\Store $storeModel */ - $storeModel = $this->storeFactory->create(); $data[self::INPUT_ARGUMENT_NAME] = $this->filterManager->removeTags( $data[self::INPUT_ARGUMENT_NAME] ); @@ -129,12 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $data[self::INPUT_ARGUMENT_CODE] ); - $storeModel->setData($data); - $groupModel = $this->groupFactory->create()->load( - $storeModel->getGroupId() - ); - $storeModel->setWebsiteId($groupModel->getWebsiteId()); - $storeModel->save(); + $this->storeCreateManagement->create($data); + $io->success('You created the store view.'); return \Magento\Framework\Console\Cli::RETURN_SUCCESS; @@ -147,25 +125,4 @@ protected function execute(InputInterface $input, OutputInterface $output) return \Magento\Framework\Console\Cli::RETURN_FAILURE; } } - - /** - * Retrieve list of store groups - * - * @return array - */ - private function getStoreGroups() - { - $websites = $this->websiteFactory->create()->getCollection(); - $allgroups = $this->groupFactory->create()->getCollection(); - $groups = []; - foreach ($websites as $website) { - foreach ($allgroups as $group) { - if ($group->getWebsiteId() == $website->getId()) { - $groups[$group->getWebsiteId() . '-' . $group->getId()] = $group->getId(); - } - } - } - - return $groups; - } } diff --git a/app/code/Magento/Store/Model/StoreCreateManagement.php b/app/code/Magento/Store/Model/StoreCreateManagement.php new file mode 100644 index 0000000000000..906afd23e10b2 --- /dev/null +++ b/app/code/Magento/Store/Model/StoreCreateManagement.php @@ -0,0 +1,93 @@ +websiteFactory = $websiteFactory; + $this->groupFactory = $groupFactory; + $this->storeFactory = $storeFactory; + } + + /** + * + * @param array $data + * @return \Magento\Store\Model\Store + */ + public function create($data) + { + /** @var \Magento\Store\Model\Store $storeModel */ + $storeModel = $this->storeFactory->create(); + $storeModel->setData($data); + + /** @var \Magento\Store\Model\Group $groupModel */ + $groupModel = $this->groupFactory->create() + ->load($storeModel->getGroupId()); + + $storeModel->setWebsiteId($groupModel->getWebsiteId()); + $storeModel->save(); + + return $storeModel; + } + + /** + * + * @return int + */ + public function getDefaultGroupId() + { + $groups = $this->getAllStoreGroups(); + + return current($groups); + } + + + /** + * Retrieve list of store groups + * + * @return array + */ + private function getAllStoreGroups() + { + $websites = $this->websiteFactory->create()->getCollection(); + $allgroups = $this->groupFactory->create()->getCollection(); + $groups = []; + foreach ($websites as $website) { + foreach ($allgroups as $group) { + if ($group->getWebsiteId() == $website->getId()) { + $groups[$group->getWebsiteId() . '-' . $group->getId()] = $group->getId(); + } + } + } + + return $groups; + } +} From 86c4b55d574e1172cd412d84d17944e533fa0603 Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Thu, 29 Oct 2020 14:57:21 +0200 Subject: [PATCH 04/10] Applied some of @sivaschenko's suggestions --- .../Api/StoreCreateManagementInterface.php | 19 ---- .../Console/Command/StoreCreateCommand.php | 64 ++++++++----- app/code/Magento/Store/Model/CreateStore.php | 60 ++++++++++++ .../Store/Model/GetDefaultStoreGroup.php | 74 +++++++++++++++ .../Store/Model/StoreCreateManagement.php | 93 ------------------- 5 files changed, 177 insertions(+), 133 deletions(-) delete mode 100644 app/code/Magento/Store/Api/StoreCreateManagementInterface.php create mode 100644 app/code/Magento/Store/Model/CreateStore.php create mode 100644 app/code/Magento/Store/Model/GetDefaultStoreGroup.php delete mode 100644 app/code/Magento/Store/Model/StoreCreateManagement.php diff --git a/app/code/Magento/Store/Api/StoreCreateManagementInterface.php b/app/code/Magento/Store/Api/StoreCreateManagementInterface.php deleted file mode 100644 index 1fcac8a5aa9ce..0000000000000 --- a/app/code/Magento/Store/Api/StoreCreateManagementInterface.php +++ /dev/null @@ -1,19 +0,0 @@ -storeCreateManagement = $storeCreateManagement; + $this->createStore = $createStore; + $this->getDefaultStoreGroup = $getDefaultStoreGroup; $this->filterManager = $filterManager; parent::__construct($name); } @@ -70,14 +86,16 @@ protected function configure() 0 ) ; - $defaultGroup = $this->storeCreateManagement->getDefaultGroupId(); + + /** @var \Magento\Store\Api\Data\GroupInterface $defaultGroup */ + $defaultGroup = $this->getDefaultStoreGroup->execute(); $this->addOption( self::INPUT_OPTION_GROUP, 'g', InputOption::VALUE_OPTIONAL, 'Group ID (php bin/magento store:list).', - $defaultGroup + $defaultGroup->getId() ); parent::configure(); @@ -91,18 +109,22 @@ protected function execute(InputInterface $input, OutputInterface $output) $io = new SymfonyStyle($input, $output); try { - $groupId = (int) $input->getOption(self::INPUT_OPTION_GROUP); - $storeViewName = (string) $input->getArgument(self::INPUT_ARGUMENT_NAME); - $storeViewCode = (string) $input->getArgument(self::INPUT_ARGUMENT_CODE); - $isActive = (bool) $input->getArgument(self::INPUT_ARGUMENT_IS_ACTIVE); - $sortOrder = (int) $input->getArgument(self::INPUT_ARGUMENT_SORT_ORDER); - $data = [ - self::INPUT_OPTION_GROUP => $groupId, - self::INPUT_ARGUMENT_NAME => $storeViewName, - self::INPUT_ARGUMENT_CODE => $storeViewCode, - self::INPUT_ARGUMENT_IS_ACTIVE => $isActive, - self::INPUT_ARGUMENT_SORT_ORDER => $sortOrder + self::INPUT_OPTION_GROUP => (int) $input->getOption( + self::INPUT_OPTION_GROUP + ), + self::INPUT_ARGUMENT_NAME => (string) $input->getArgument( + self::INPUT_ARGUMENT_NAME + ), + self::INPUT_ARGUMENT_CODE => (string) $input->getArgument( + self::INPUT_ARGUMENT_CODE + ), + self::INPUT_ARGUMENT_IS_ACTIVE => (bool) $input->getArgument( + self::INPUT_ARGUMENT_IS_ACTIVE + ), + self::INPUT_ARGUMENT_SORT_ORDER => (int) $input->getArgument( + self::INPUT_ARGUMENT_SORT_ORDER + ) ]; $data[self::INPUT_ARGUMENT_NAME] = $this->filterManager->removeTags( $data[self::INPUT_ARGUMENT_NAME] @@ -111,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $data[self::INPUT_ARGUMENT_CODE] ); - $this->storeCreateManagement->create($data); + $this->createStore->create($data); $io->success('You created the store view.'); diff --git a/app/code/Magento/Store/Model/CreateStore.php b/app/code/Magento/Store/Model/CreateStore.php new file mode 100644 index 0000000000000..edb152e0e3651 --- /dev/null +++ b/app/code/Magento/Store/Model/CreateStore.php @@ -0,0 +1,60 @@ +groupFactory = $groupFactory; + $this->storeFactory = $storeFactory; + } + + /** + * + * @param array $data + * @return \Magento\Store\Api\Data\StoreInterface + */ + public function execute(array $data): StoreInterface + { + /** @var \Magento\Store\Model\Storev $storeModel */ + $storeModel = $this->storeFactory->create(['data' => $data]); + + /** @var \Magento\Store\Model\Group $groupModel */ + $groupModel = $this->groupFactory->create() + ->load($storeModel->getGroupId()); + + $storeModel->setWebsiteId($groupModel->getWebsiteId()); + $storeModel->save(); + + return $storeModel; + } +} diff --git a/app/code/Magento/Store/Model/GetDefaultStoreGroup.php b/app/code/Magento/Store/Model/GetDefaultStoreGroup.php new file mode 100644 index 0000000000000..a432f41019a89 --- /dev/null +++ b/app/code/Magento/Store/Model/GetDefaultStoreGroup.php @@ -0,0 +1,74 @@ +websiteFactory = $websiteFactory; + $this->groupFactory = $groupFactory; + } + + /** + * Get default group + * + * @return GroupInterface + */ + public function execute() : GroupInterface + { + $groups = $this->getAllStoreGroups(); + + return current($groups); + } + + + /** + * Retrieve list of store groups + * + * @return array + */ + private function getAllStoreGroups() : array + { + $websites = $this->websiteFactory->create()->getCollection(); + $allgroups = $this->groupFactory->create()->getCollection(); + $groups = []; + foreach ($websites as $website) { + foreach ($allgroups as $group) { + if ($group->getWebsiteId() == $website->getId()) { + $groups[$group->getWebsiteId() . '-' . $group->getId()] = $group; + } + } + } + + return $groups; + } +} diff --git a/app/code/Magento/Store/Model/StoreCreateManagement.php b/app/code/Magento/Store/Model/StoreCreateManagement.php deleted file mode 100644 index 906afd23e10b2..0000000000000 --- a/app/code/Magento/Store/Model/StoreCreateManagement.php +++ /dev/null @@ -1,93 +0,0 @@ -websiteFactory = $websiteFactory; - $this->groupFactory = $groupFactory; - $this->storeFactory = $storeFactory; - } - - /** - * - * @param array $data - * @return \Magento\Store\Model\Store - */ - public function create($data) - { - /** @var \Magento\Store\Model\Store $storeModel */ - $storeModel = $this->storeFactory->create(); - $storeModel->setData($data); - - /** @var \Magento\Store\Model\Group $groupModel */ - $groupModel = $this->groupFactory->create() - ->load($storeModel->getGroupId()); - - $storeModel->setWebsiteId($groupModel->getWebsiteId()); - $storeModel->save(); - - return $storeModel; - } - - /** - * - * @return int - */ - public function getDefaultGroupId() - { - $groups = $this->getAllStoreGroups(); - - return current($groups); - } - - - /** - * Retrieve list of store groups - * - * @return array - */ - private function getAllStoreGroups() - { - $websites = $this->websiteFactory->create()->getCollection(); - $allgroups = $this->groupFactory->create()->getCollection(); - $groups = []; - foreach ($websites as $website) { - foreach ($allgroups as $group) { - if ($group->getWebsiteId() == $website->getId()) { - $groups[$group->getWebsiteId() . '-' . $group->getId()] = $group->getId(); - } - } - } - - return $groups; - } -} From ed4a1fe479ab921339363b6c918b3271d78b4e36 Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Tue, 1 Dec 2020 15:45:48 +0200 Subject: [PATCH 05/10] Prevent integration test failing (Table store_website doesn\'t exist) --- .../Store/Console/Command/StoreCreateCommand.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php index 0593f38975334..cc9441b9de012 100644 --- a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php +++ b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php @@ -87,15 +87,11 @@ protected function configure() ) ; - /** @var \Magento\Store\Api\Data\GroupInterface $defaultGroup */ - $defaultGroup = $this->getDefaultStoreGroup->execute(); - $this->addOption( self::INPUT_OPTION_GROUP, 'g', InputOption::VALUE_OPTIONAL, - 'Group ID (php bin/magento store:list).', - $defaultGroup->getId() + 'Group ID (php bin/magento store:list).' ); parent::configure(); @@ -109,6 +105,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $io = new SymfonyStyle($input, $output); try { + $defaultGroupId = $input->getOption(self::INPUT_OPTION_GROUP); + if ($defaultGroupId === null) { + /** @var \Magento\Store\Api\Data\GroupInterface $defaultGroup */ + $defaultGroup = $this->getDefaultStoreGroup->execute(); + $defaultGroupId = $defaultGroup->getId(); + } + $defaultGroupId = (int) $defaultGroupId; $data = [ self::INPUT_OPTION_GROUP => (int) $input->getOption( self::INPUT_OPTION_GROUP From d2e1f407d45e2859db292e769f6f16d51f396f2c Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Tue, 1 Dec 2020 17:56:48 +0200 Subject: [PATCH 06/10] Add integration test for CreateStore --- .../Magento/Store/Model/CreateStoreTest.php | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php new file mode 100644 index 0000000000000..08971b059833f --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php @@ -0,0 +1,96 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + $this->createStoreModel = $this->objectManager->get( + \Magento\Store\Model\CreateStore::class + ); + + $this->websiteModel = $this->objectManager->create( + \Magento\Store\Model\Website::class + ); + $this->websiteModel->load(1); + } + + /** + * @inheritdoc + */ + protected function tearDown(): void + { + $this->objectManager = null; + $this->createStoreModel = null; + $this->websiteModel = null; + } + + /** + * @param $code + * @param $data + * @dataProvider loadCreateDataProvider + */ + public function testExecute($code, $data) + { + $defaultGroupId = $this->websiteModel->getDefaultGroupId(); + $data['group_id'] = $defaultGroupId; + + $store = $this->createStoreModel->execute($data); + $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $store); + $this->assertNotNull($store->getId(), 'Store was not saved correctly'); + $this->assertEquals($code, $store->getCode()); + + /** @var \Magento\Framework\Registry $registry */ + $registry = $this->objectManager->get(\Magento\Framework\Registry::class); + + $registry->unregister('isSecureArea'); + $registry->register('isSecureArea', true); + + $store->delete(); + } + + /** + * @return array + */ + public function loadCreateDataProvider() + { + return [['code', + [ + 'name' => 'code', + 'code' => 'code', + 'is_active' => 0, + 'sort_order' => 10, + ] + ]]; + } +} From 5e8ec7894e9756ba500514c113b7b8b1e7128c6b Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Wed, 2 Dec 2020 11:11:31 +0200 Subject: [PATCH 07/10] Add GetDefaultStoreGroupTest --- .../Store/Model/GetDefaultStoreGroupTest.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php new file mode 100644 index 0000000000000..07976f1b47525 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php @@ -0,0 +1,51 @@ +getDefaultStoreGroupModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\Store\Model\GetDefaultStoreGroup::class + ); + } + + /** + * @inheritdoc + */ + protected function tearDown(): void + { + $this->getDefaultStoreGroupModel = null; + } + + /** + * @magentoDataFixture Magento/Store/_files/multiple_websites_with_store_groups_stores.php + * @magentoAppIsolation enabled + * @magentoDbIsolation enabled + */ + public function testExecute() + { + $group = $this->getDefaultStoreGroupModel->execute(); + + $this->assertInstanceOf(\Magento\Store\Api\Data\GroupInterface::class, $group); + $this->assertNotNull($group->getId()); + $this->assertEquals("main_website_store", $group->getCode()); + } +} From 024fe1d4f768085ce10c6baae05379e27d07ad3b Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Wed, 6 Jan 2021 10:35:23 +0200 Subject: [PATCH 08/10] Applied some of @sivaschenko's suggestions --- .../Magento/Store/Model/CreateStoreTest.php | 25 ++++--------- .../Store/Model/GetDefaultStoreGroupTest.php | 35 ++++--------------- 2 files changed, 12 insertions(+), 48 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php index 08971b059833f..0a0c601ef889d 100644 --- a/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php +++ b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php @@ -6,13 +6,8 @@ namespace Magento\Store\Model; -/** - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - * phpcs:disable Magento2.Security.Superglobal - */ class CreateStoreTest extends \PHPUnit\Framework\TestCase { - /** * @var \Magento\Framework\ObjectManagerInterface */ @@ -46,21 +41,10 @@ protected function setUp(): void } /** - * @inheritdoc - */ - protected function tearDown(): void - { - $this->objectManager = null; - $this->createStoreModel = null; - $this->websiteModel = null; - } - - /** - * @param $code * @param $data * @dataProvider loadCreateDataProvider */ - public function testExecute($code, $data) + public function testExecute($data) { $defaultGroupId = $this->websiteModel->getDefaultGroupId(); $data['group_id'] = $defaultGroupId; @@ -68,7 +52,7 @@ public function testExecute($code, $data) $store = $this->createStoreModel->execute($data); $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $store); $this->assertNotNull($store->getId(), 'Store was not saved correctly'); - $this->assertEquals($code, $store->getCode()); + $this->assertEquals($data['code'], $store->getCode()); /** @var \Magento\Framework\Registry $registry */ $registry = $this->objectManager->get(\Magento\Framework\Registry::class); @@ -77,6 +61,9 @@ public function testExecute($code, $data) $registry->register('isSecureArea', true); $store->delete(); + + $registry->unregister('isSecureArea'); + $registry->register('isSecureArea', false); } /** @@ -84,7 +71,7 @@ public function testExecute($code, $data) */ public function loadCreateDataProvider() { - return [['code', + return [[ [ 'name' => 'code', 'code' => 'code', diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php index 07976f1b47525..8be7ae584fea5 100644 --- a/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php +++ b/dev/tests/integration/testsuite/Magento/Store/Model/GetDefaultStoreGroupTest.php @@ -6,35 +6,8 @@ namespace Magento\Store\Model; -/** - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - * phpcs:disable Magento2.Security.Superglobal - */ class GetDefaultStoreGroupTest extends \PHPUnit\Framework\TestCase { - /** - * @var \Magento\Store\Model\GetDefaultStoreGroup - */ - protected $getDefaultStoreGroupModel; - - /** - * @inheritdoc - */ - protected function setUp(): void - { - $this->getDefaultStoreGroupModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( - \Magento\Store\Model\GetDefaultStoreGroup::class - ); - } - - /** - * @inheritdoc - */ - protected function tearDown(): void - { - $this->getDefaultStoreGroupModel = null; - } - /** * @magentoDataFixture Magento/Store/_files/multiple_websites_with_store_groups_stores.php * @magentoAppIsolation enabled @@ -42,10 +15,14 @@ protected function tearDown(): void */ public function testExecute() { - $group = $this->getDefaultStoreGroupModel->execute(); + $getDefaultStoreGroupModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get( + \Magento\Store\Model\GetDefaultStoreGroup::class + ); + + $group = $getDefaultStoreGroupModel->execute(); $this->assertInstanceOf(\Magento\Store\Api\Data\GroupInterface::class, $group); $this->assertNotNull($group->getId()); - $this->assertEquals("main_website_store", $group->getCode()); + $this->assertEquals('main_website_store', $group->getCode()); } } From 0d528ea26612ffb1ccbea575e39156e7fcc44fbf Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Fri, 8 Jan 2021 11:40:07 +0200 Subject: [PATCH 09/10] CreateStore::execute accept a StoreInterface instead of array --- .../Console/Command/StoreCreateCommand.php | 13 ++++++++++++- app/code/Magento/Store/Model/CreateStore.php | 17 +++-------------- .../Magento/Store/Model/CreateStoreTest.php | 12 +++++++++++- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php index cc9441b9de012..5ff661756bc39 100644 --- a/app/code/Magento/Store/Console/Command/StoreCreateCommand.php +++ b/app/code/Magento/Store/Console/Command/StoreCreateCommand.php @@ -28,6 +28,11 @@ class StoreCreateCommand extends Command const INPUT_OPTION_GROUP = 'group_id'; + /** + * @var \Magento\Store\Api\Data\StoreInterfaceFactory + */ + private $storeFactory; + /** * * @var \Magento\Store\Model\CreateStore @@ -47,17 +52,20 @@ class StoreCreateCommand extends Command /** * + * @param \Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory * @param \Magento\Store\Model\CreateStore $createStore * @param \Magento\Store\Model\GetDefaultStoreGroup $getDefaultStoreGroup * @param \Magento\Framework\Filter\FilterManager $filterManager * @param string $name */ public function __construct( + \Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory, \Magento\Store\Model\CreateStore $createStore, \Magento\Store\Model\GetDefaultStoreGroup $getDefaultStoreGroup, \Magento\Framework\Filter\FilterManager $filterManager, $name = null ) { + $this->storeFactory = $storeFactory; $this->createStore = $createStore; $this->getDefaultStoreGroup = $getDefaultStoreGroup; $this->filterManager = $filterManager; @@ -136,7 +144,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $data[self::INPUT_ARGUMENT_CODE] ); - $this->createStore->create($data); + /** @var \Magento\Store\Model\Storev $storeModel */ + $storeModel = $this->storeFactory->create(['data' => $data]); + + $this->createStore->create($storeModel); $io->success('You created the store view.'); diff --git a/app/code/Magento/Store/Model/CreateStore.php b/app/code/Magento/Store/Model/CreateStore.php index edb152e0e3651..c17845aa5d176 100644 --- a/app/code/Magento/Store/Model/CreateStore.php +++ b/app/code/Magento/Store/Model/CreateStore.php @@ -20,34 +20,23 @@ class CreateStore */ private $groupFactory; - /** - * @var \Magento\Store\Api\Data\StoreInterfaceFactory - */ - private $storeFactory; - /** * * @param \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory - * @param \Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory */ public function __construct( - \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory, - \Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory + \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory ) { $this->groupFactory = $groupFactory; - $this->storeFactory = $storeFactory; } /** * - * @param array $data + * @param \Magento\Store\Api\Data\StoreInterface $data * @return \Magento\Store\Api\Data\StoreInterface */ - public function execute(array $data): StoreInterface + public function execute(StoreInterface $storeModel): StoreInterface { - /** @var \Magento\Store\Model\Storev $storeModel */ - $storeModel = $this->storeFactory->create(['data' => $data]); - /** @var \Magento\Store\Model\Group $groupModel */ $groupModel = $this->groupFactory->create() ->load($storeModel->getGroupId()); diff --git a/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php index 0a0c601ef889d..306e6036bdc75 100644 --- a/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php +++ b/dev/tests/integration/testsuite/Magento/Store/Model/CreateStoreTest.php @@ -13,6 +13,11 @@ class CreateStoreTest extends \PHPUnit\Framework\TestCase */ protected $objectManager; + /** + * @var \Magento\Store\Api\Data\StoreInterface + */ + protected $storeModel; + /** * @var \Magento\Store\Model\CreateStore */ @@ -30,6 +35,10 @@ protected function setUp(): void { $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->storeModel = $this->objectManager->get( + \Magento\Store\Model\Store::class + ); + $this->createStoreModel = $this->objectManager->get( \Magento\Store\Model\CreateStore::class ); @@ -49,7 +58,8 @@ public function testExecute($data) $defaultGroupId = $this->websiteModel->getDefaultGroupId(); $data['group_id'] = $defaultGroupId; - $store = $this->createStoreModel->execute($data); + $store = $this->storeModel->setData($data); + $store = $this->createStoreModel->execute($store); $this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $store); $this->assertNotNull($store->getId(), 'Store was not saved correctly'); $this->assertEquals($data['code'], $store->getCode()); From 5e87d6eef486a20c019075a7b424fcb073ff60a4 Mon Sep 17 00:00:00 2001 From: Alexander Kras'ko <0m3r.mail@gmail.com> Date: Fri, 8 Jan 2021 11:44:08 +0200 Subject: [PATCH 10/10] Fixed PHPdoc param name typo --- app/code/Magento/Store/Model/CreateStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Store/Model/CreateStore.php b/app/code/Magento/Store/Model/CreateStore.php index c17845aa5d176..f17f32715d817 100644 --- a/app/code/Magento/Store/Model/CreateStore.php +++ b/app/code/Magento/Store/Model/CreateStore.php @@ -32,7 +32,7 @@ public function __construct( /** * - * @param \Magento\Store\Api\Data\StoreInterface $data + * @param \Magento\Store\Api\Data\StoreInterface $storeModel * @return \Magento\Store\Api\Data\StoreInterface */ public function execute(StoreInterface $storeModel): StoreInterface