Skip to content

Commit f90a620

Browse files
author
Vitaliy Boyko
committed
graphQl-309: checkout agreements support config
1 parent 6a8c89b commit f90a620

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

app/code/Magento/CheckoutAgreementsGraphQl/Model/Resolver/DataProvider/CheckoutAgreements.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\CheckoutAgreements\Api\Data\AgreementInterface;
1111
use Magento\CheckoutAgreements\Model\Agreement;
1212
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Store\Model\ScopeInterface;
1315
use Magento\Store\Model\StoreManagerInterface;
1416

1517
/**
@@ -27,16 +29,24 @@ class CheckoutAgreements
2729
*/
2830
private $storeManager;
2931

32+
/**
33+
* @var ScopeConfigInterface
34+
*/
35+
private $scopeConfig;
36+
3037
/**
3138
* @param CollectionFactory $agreementCollectionFactory
3239
* @param StoreManagerInterface $storeManager
40+
* @param ScopeConfigInterface $scopeConfig
3341
*/
3442
public function __construct(
3543
CollectionFactory $agreementCollectionFactory,
36-
StoreManagerInterface $storeManager
44+
StoreManagerInterface $storeManager,
45+
ScopeConfigInterface $scopeConfig
3746
) {
3847
$this->agreementCollectionFactory = $agreementCollectionFactory;
3948
$this->storeManager = $storeManager;
49+
$this->scopeConfig = $scopeConfig;
4050
}
4151

4252
/**
@@ -46,6 +56,9 @@ public function __construct(
4656
*/
4757
public function getData(): array
4858
{
59+
if (!$this->scopeConfig->isSetFlag('checkout/options/enable_agreements', ScopeInterface::SCOPE_STORE)) {
60+
return [];
61+
}
4962
$agreementsCollection = $this->agreementCollectionFactory->create();
5063
$agreementsCollection->addStoreFilter($this->storeManager->getStore()->getId()); // TODO: store should be get from query context
5164
$agreementsCollection->addFieldToFilter('is_active', 1);

dev/tests/api-functional/testsuite/Magento/GraphQl/CheckoutAgreements/Api/CheckoutAgreementsListTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,37 @@
1111
use Magento\CheckoutAgreements\Model\Agreement as AgreementModel;
1212
use Magento\CheckoutAgreements\Model\AgreementFactory;
1313
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement;
14+
use Magento\Config\Model\ResourceModel\Config;
15+
use Magento\Framework\App\Config\ReinitableConfigInterface;
16+
use Magento\Framework\App\Config\ScopeConfigInterface;
1417
use Magento\Framework\ObjectManagerInterface;
18+
use Magento\Store\Api\Data\StoreInterface;
19+
use Magento\Store\Model\ScopeInterface;
1520
use Magento\Store\Model\StoreManagerInterface;
1621
use Magento\TestFramework\Helper\Bootstrap;
1722
use Magento\TestFramework\TestCase\GraphQlAbstract;
1823

1924
class CheckoutAgreementsListTest extends GraphQlAbstract
2025
{
26+
private $agreementsXmlConfigPath = 'checkout/options/enable_agreements';
27+
2128
/**
2229
* @var ObjectManagerInterface
2330
*/
2431
private $objectManager;
2532

33+
/**
34+
* @var Config
35+
*/
36+
private $config;
37+
2638
protected function setUp()
2739
{
2840
parent::setUp();
41+
2942
$this->objectManager = Bootstrap::getObjectManager();
43+
$this->config = $this->objectManager->get(Config::class);
44+
$this->saveAgreementConfig(1);
3045
}
3146

3247
/**
@@ -106,6 +121,34 @@ public function testGetAgreementNotSet()
106121
$this->assertCount(0, $agreements);
107122
}
108123

124+
/**
125+
* @magentoApiDataFixture Magento/CheckoutAgreements/_files/agreement_active_with_html_content.php
126+
* @magentoApiDataFixture Magento/CheckoutAgreements/_files/agreement_inactive_with_text_content.php
127+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
128+
*/
129+
public function testDisabledAgreements()
130+
{
131+
$secondStoreCode = 'fixture_second_store';
132+
$agreementsName = 'Checkout Agreement (active)';
133+
134+
$query = $this->getQuery();
135+
$this->assignAgreementsToStore($secondStoreCode, $agreementsName);
136+
137+
/** @var StoreManagerInterface $storeManager */
138+
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
139+
$store = $storeManager->getStore($secondStoreCode);
140+
$this->saveAgreementConfig(0, $store);
141+
142+
$headerMap['Store'] = $secondStoreCode;
143+
$response = $this->graphQlQuery($query, [], '', $headerMap);
144+
145+
$this->assertArrayHasKey('checkoutAgreements', $response);
146+
$agreements = $response['checkoutAgreements'];
147+
$this->assertCount(0, $agreements);
148+
149+
$this->deleteAgreementConfig($store);
150+
}
151+
109152
/**
110153
* @return string
111154
*/
@@ -145,4 +188,52 @@ private function assignAgreementsToStore(string $storeCode, string $agreementsNa
145188
$agreements->setData('stores', [$store->getId()]);
146189
$agreementsResource->save($agreements);
147190
}
191+
192+
protected function tearDown()
193+
{
194+
parent::tearDown();
195+
196+
$this->deleteAgreementConfig();
197+
}
198+
199+
/**
200+
* @param int $value
201+
* @param StoreInterface $store
202+
*/
203+
private function saveAgreementConfig(int $value, ?StoreInterface $store = null): void
204+
{
205+
$scopeId = $store ? $store->getId() : 0;
206+
$scope = $store ? ScopeInterface::SCOPE_STORE : ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
207+
$this->config->saveConfig(
208+
$this->agreementsXmlConfigPath,
209+
$value,
210+
$scope,
211+
$scopeId
212+
);
213+
214+
$this->reinitConfig();
215+
}
216+
217+
/**
218+
* @param StoreInterface $store
219+
*/
220+
private function deleteAgreementConfig(?StoreInterface $store = null): void
221+
{
222+
$scopeId = $store ? $store->getId() : 0;
223+
$scope = $store ? ScopeInterface::SCOPE_STORE : ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
224+
$this->config->deleteConfig(
225+
$this->agreementsXmlConfigPath,
226+
$scope,
227+
$scopeId
228+
);
229+
230+
$this->reinitConfig();
231+
}
232+
233+
private function reinitConfig(): void
234+
{
235+
/** @var ReinitableConfigInterface $config */
236+
$config = $this->objectManager->get(ReinitableConfigInterface::class);
237+
$config->reinit();
238+
}
148239
}

0 commit comments

Comments
 (0)