Skip to content

Commit

Permalink
graphQl-309: checkout agreements support config
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Boyko committed Apr 17, 2019
1 parent 6a8c89b commit f90a620
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}
}

0 comments on commit f90a620

Please sign in to comment.