From f90a6201fa294844ac9a06daf3187bf0ae9c8224 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 17 Apr 2019 19:07:58 +0300 Subject: [PATCH] graphQl-309: checkout agreements support config --- .../DataProvider/CheckoutAgreements.php | 15 ++- .../Api/CheckoutAgreementsListTest.php | 91 +++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CheckoutAgreementsGraphQl/Model/Resolver/DataProvider/CheckoutAgreements.php b/app/code/Magento/CheckoutAgreementsGraphQl/Model/Resolver/DataProvider/CheckoutAgreements.php index c235d348375c5..3dab845627261 100644 --- a/app/code/Magento/CheckoutAgreementsGraphQl/Model/Resolver/DataProvider/CheckoutAgreements.php +++ b/app/code/Magento/CheckoutAgreementsGraphQl/Model/Resolver/DataProvider/CheckoutAgreements.php @@ -10,6 +10,8 @@ use Magento\CheckoutAgreements\Api\Data\AgreementInterface; use Magento\CheckoutAgreements\Model\Agreement; use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; /** @@ -27,16 +29,24 @@ class CheckoutAgreements */ private $storeManager; + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + /** * @param CollectionFactory $agreementCollectionFactory * @param StoreManagerInterface $storeManager + * @param ScopeConfigInterface $scopeConfig */ public function __construct( CollectionFactory $agreementCollectionFactory, - StoreManagerInterface $storeManager + StoreManagerInterface $storeManager, + ScopeConfigInterface $scopeConfig ) { $this->agreementCollectionFactory = $agreementCollectionFactory; $this->storeManager = $storeManager; + $this->scopeConfig = $scopeConfig; } /** @@ -46,6 +56,9 @@ public function __construct( */ public function getData(): array { + if (!$this->scopeConfig->isSetFlag('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE)) { + return []; + } $agreementsCollection = $this->agreementCollectionFactory->create(); $agreementsCollection->addStoreFilter($this->storeManager->getStore()->getId()); // TODO: store should be get from query context $agreementsCollection->addFieldToFilter('is_active', 1); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CheckoutAgreements/Api/CheckoutAgreementsListTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CheckoutAgreements/Api/CheckoutAgreementsListTest.php index 38498fb016f3c..17fa58be72fa2 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CheckoutAgreements/Api/CheckoutAgreementsListTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CheckoutAgreements/Api/CheckoutAgreementsListTest.php @@ -11,22 +11,37 @@ use Magento\CheckoutAgreements\Model\Agreement as AgreementModel; use Magento\CheckoutAgreements\Model\AgreementFactory; use Magento\CheckoutAgreements\Model\ResourceModel\Agreement; +use Magento\Config\Model\ResourceModel\Config; +use Magento\Framework\App\Config\ReinitableConfigInterface; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\ObjectManagerInterface; +use Magento\Store\Api\Data\StoreInterface; +use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQlAbstract; class CheckoutAgreementsListTest extends GraphQlAbstract { + private $agreementsXmlConfigPath = 'checkout/options/enable_agreements'; + /** * @var ObjectManagerInterface */ private $objectManager; + /** + * @var Config + */ + private $config; + protected function setUp() { parent::setUp(); + $this->objectManager = Bootstrap::getObjectManager(); + $this->config = $this->objectManager->get(Config::class); + $this->saveAgreementConfig(1); } /** @@ -106,6 +121,34 @@ public function testGetAgreementNotSet() $this->assertCount(0, $agreements); } + /** + * @magentoApiDataFixture Magento/CheckoutAgreements/_files/agreement_active_with_html_content.php + * @magentoApiDataFixture Magento/CheckoutAgreements/_files/agreement_inactive_with_text_content.php + * @magentoApiDataFixture Magento/Store/_files/second_store.php + */ + public function testDisabledAgreements() + { + $secondStoreCode = 'fixture_second_store'; + $agreementsName = 'Checkout Agreement (active)'; + + $query = $this->getQuery(); + $this->assignAgreementsToStore($secondStoreCode, $agreementsName); + + /** @var StoreManagerInterface $storeManager */ + $storeManager = $this->objectManager->get(StoreManagerInterface::class); + $store = $storeManager->getStore($secondStoreCode); + $this->saveAgreementConfig(0, $store); + + $headerMap['Store'] = $secondStoreCode; + $response = $this->graphQlQuery($query, [], '', $headerMap); + + $this->assertArrayHasKey('checkoutAgreements', $response); + $agreements = $response['checkoutAgreements']; + $this->assertCount(0, $agreements); + + $this->deleteAgreementConfig($store); + } + /** * @return string */ @@ -145,4 +188,52 @@ private function assignAgreementsToStore(string $storeCode, string $agreementsNa $agreements->setData('stores', [$store->getId()]); $agreementsResource->save($agreements); } + + protected function tearDown() + { + parent::tearDown(); + + $this->deleteAgreementConfig(); + } + + /** + * @param int $value + * @param StoreInterface $store + */ + private function saveAgreementConfig(int $value, ?StoreInterface $store = null): void + { + $scopeId = $store ? $store->getId() : 0; + $scope = $store ? ScopeInterface::SCOPE_STORE : ScopeConfigInterface::SCOPE_TYPE_DEFAULT; + $this->config->saveConfig( + $this->agreementsXmlConfigPath, + $value, + $scope, + $scopeId + ); + + $this->reinitConfig(); + } + + /** + * @param StoreInterface $store + */ + private function deleteAgreementConfig(?StoreInterface $store = null): void + { + $scopeId = $store ? $store->getId() : 0; + $scope = $store ? ScopeInterface::SCOPE_STORE : ScopeConfigInterface::SCOPE_TYPE_DEFAULT; + $this->config->deleteConfig( + $this->agreementsXmlConfigPath, + $scope, + $scopeId + ); + + $this->reinitConfig(); + } + + private function reinitConfig(): void + { + /** @var ReinitableConfigInterface $config */ + $config = $this->objectManager->get(ReinitableConfigInterface::class); + $config->reinit(); + } }