Skip to content

Commit

Permalink
graphQl-309: checkout agreements support multistore
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Boyko committed Apr 17, 2019
1 parent f17893d commit 6a8c89b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* CMS page field resolver, used for GraphQL request processing
* Checkout Agreements resolver, used for GraphQL request processing
*/
class CheckoutAgreements implements ResolverInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,36 @@

namespace Magento\CheckoutAgreementsGraphQl\Model\Resolver\DataProvider;

use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
use Magento\CheckoutAgreements\Api\Data\AgreementInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\CheckoutAgreements\Model\Agreement;
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Checkout Agreements data provider
*/
class CheckoutAgreements
{
/**
* @var SearchCriteriaBuilder
* @var CollectionFactory
*/
private $searchCriteriaBuilder;
private $agreementCollectionFactory;

/**
* @var CheckoutAgreementsListInterface
* @var StoreManagerInterface
*/
private $checkoutAgreementsList;
private $storeManager;

/**
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param CollectionFactory $agreementCollectionFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
CheckoutAgreementsListInterface $checkoutAgreementsList,
SearchCriteriaBuilder $searchCriteriaBuilder
CollectionFactory $agreementCollectionFactory,
StoreManagerInterface $storeManager
) {
$this->checkoutAgreementsList = $checkoutAgreementsList;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->agreementCollectionFactory = $agreementCollectionFactory;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -45,12 +46,13 @@ public function __construct(
*/
public function getData(): array
{
$this->searchCriteriaBuilder->addFilter(AgreementInterface::IS_ACTIVE, true);
$searchCriteria = $this->searchCriteriaBuilder->create();
$checkoutAgreements = $this->checkoutAgreementsList->getList($searchCriteria);
$agreementsCollection = $this->agreementCollectionFactory->create();
$agreementsCollection->addStoreFilter($this->storeManager->getStore()->getId()); // TODO: store should be get from query context
$agreementsCollection->addFieldToFilter('is_active', 1);

$checkoutAgreementData = [];
foreach ($checkoutAgreements as $checkoutAgreement) {
/** @var Agreement $checkoutAgreement */
foreach ($agreementsCollection->getItems() as $checkoutAgreement) {
$checkoutAgreementData[] = [
AgreementInterface::AGREEMENT_ID => $checkoutAgreement->getAgreementId(),
AgreementInterface::CONTENT => $checkoutAgreement->getContent(),
Expand Down
6 changes: 1 addition & 5 deletions app/code/Magento/CheckoutAgreementsGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CheckoutAgreementsGraphQl">
<sequence>
<module name="Magento_GraphQl"/>
</sequence>
</module>
<module name="Magento_CheckoutAgreementsGraphQl" />
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See COPYING.txt for license details.

type Query {
checkoutAgreements: [CheckoutAgreement] @resolver(class: "Magento\\CheckoutAgreementsGraphQl\\Model\\Resolver\\CheckoutAgreements") @doc(description: "The Checkout Agreements query returns information about a Checkout Agreements")
checkoutAgreements: [CheckoutAgreement] @resolver(class: "Magento\\CheckoutAgreementsGraphQl\\Model\\Resolver\\CheckoutAgreements") @doc(description: "The Checkout Agreements information")
}

type CheckoutAgreement @doc(description: "Defines all Checkout Agreement information") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,111 @@

namespace Magento\GraphQl\CheckoutAgreements\Api;

use Magento\CheckoutAgreements\Api\Data\AgreementInterface;
use Magento\CheckoutAgreements\Model\Agreement as AgreementModel;
use Magento\CheckoutAgreements\Model\AgreementFactory;
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class CheckoutAgreementsListTest extends GraphQlAbstract
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

protected function setUp()
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
}

/**
* @magentoApiDataFixture Magento/CheckoutAgreements/_files/agreement_active_with_html_content.php
* @magentoApiDataFixture Magento/CheckoutAgreements/_files/agreement_inactive_with_text_content.php
*/
public function testGetActiveAgreement()
{
$query =
$query = $this->getQuery();

$response = $this->graphQlQuery($query);

$this->assertArrayHasKey('checkoutAgreements', $response);
$agreements = $response['checkoutAgreements'];
$this->assertCount(1, $agreements);
$this->assertEquals('Checkout Agreement (active)', $agreements[0]['name']);
$this->assertEquals('Checkout agreement content: <b>HTML</b>', $agreements[0]['content']);
$this->assertEquals('200px', $agreements[0]['content_height']);
$this->assertEquals('Checkout agreement checkbox text.', $agreements[0]['checkbox_text']);
$this->assertEquals(true, $agreements[0]['is_html']);
}

/**
* @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 testGetActiveAgreementOnSecondStore()
{
$secondStoreCode = 'fixture_second_store';
$agreementsName = 'Checkout Agreement (active)';

$query = $this->getQuery();
$this->assignAgreementsToStore($secondStoreCode, $agreementsName);

$headerMap['Store'] = $secondStoreCode;
$response = $this->graphQlQuery($query, [], '', $headerMap);

$this->assertArrayHasKey('checkoutAgreements', $response);
$agreements = $response['checkoutAgreements'];
$this->assertCount(1, $agreements);
$this->assertEquals($agreementsName, $agreements[0]['name']);
$this->assertEquals('Checkout agreement content: <b>HTML</b>', $agreements[0]['content']);
$this->assertEquals('200px', $agreements[0]['content_height']);
$this->assertEquals('Checkout agreement checkbox text.', $agreements[0]['checkbox_text']);
$this->assertEquals(true, $agreements[0]['is_html']);
}

/**
* @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 testGetActiveAgreementFromSecondStoreOnDefaultStore()
{
$secondStoreCode = 'fixture_second_store';
$agreementsName = 'Checkout Agreement (active)';

$query = $this->getQuery();
$this->assignAgreementsToStore($secondStoreCode, $agreementsName);

$response = $this->graphQlQuery($query);

$this->assertArrayHasKey('checkoutAgreements', $response);
$agreements = $response['checkoutAgreements'];
$this->assertCount(0, $agreements);
}

public function testGetAgreementNotSet()
{
$query = $this->getQuery();

$response = $this->graphQlQuery($query);

$this->assertArrayHasKey('checkoutAgreements', $response);
$agreements = $response['checkoutAgreements'];
$this->assertCount(0, $agreements);
}

/**
* @return string
*/
private function getQuery(): string
{
return
<<<QUERY
{
checkoutAgreements {
Expand All @@ -30,15 +124,25 @@ public function testGetActiveAgreement()
}
}
QUERY;
}

$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('checkoutAgreements', $response);
$agreements = $response['checkoutAgreements'];
$this->assertEquals(1, count($agreements));
$this->assertEquals('Checkout Agreement (active)', $agreements[0]['name']);
$this->assertEquals('Checkout agreement content: <b>HTML</b>', $agreements[0]['content']);
$this->assertEquals('200px', $agreements[0]['content_height']);
$this->assertEquals('Checkout agreement checkbox text.', $agreements[0]['checkbox_text']);
$this->assertEquals(true, $agreements[0]['is_html']);
/**
* @param string $storeCode
* @param string $agreementsName
* @return void
*/
private function assignAgreementsToStore(string $storeCode, string $agreementsName): void
{
$agreementsFactory = $this->objectManager->get(AgreementFactory::class);
/** @var Agreement $agreementsResource */
$agreementsResource = $this->objectManager->get(Agreement::class);
/** @var StoreManagerInterface $storeManager */
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
$store = $storeManager->getStore($storeCode);
/** @var AgreementModel $agreements */
$agreements = $agreementsFactory->create();
$agreementsResource->load($agreements, $agreementsName, AgreementInterface::NAME);
$agreements->setData('stores', [$store->getId()]);
$agreementsResource->save($agreements);
}
}

0 comments on commit 6a8c89b

Please sign in to comment.