Skip to content

Commit

Permalink
428 - Test coverage: GetAvailableShippingMethodsTest
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'origin/2.3-develop' into 428-getAvailableShippingMethodsTest
  • Loading branch information
atwixfirster committed Mar 14, 2019
2 parents 10a2202 + 6d8e725 commit 4ed4d59
Show file tree
Hide file tree
Showing 24 changed files with 1,022 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ public function getFields(array $context = []): array
$searchCriteria = $this->searchCriteriaBuilder->create();
$groups = $this->groupRepository->getList($searchCriteria)->getItems();
$priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
$ctx = isset($context['websiteId']) ? ['websiteId' => $context['websiteId']] : [];
foreach ($groups as $group) {
$ctx['customerGroupId'] = $group->getId();
$groupPriceKey = $this->fieldNameResolver->getFieldName(
$priceAttribute,
['customerGroupId' => $group->getId(), 'websiteId' => $context['websiteId']]
$ctx
);
$allAttributes[$groupPriceKey] = [
'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_FLOAT),
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Elasticsearch6/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<type name="Magento\Search\Model\Search\PageSizeProvider">
<arguments>
<argument name="pageSizeBySearchEngine" xsi:type="array">
<item name="elasticsearch6" xsi:type="number">2147483647</item>
<item name="elasticsearch6" xsi:type="number">10000</item>
</argument>
</arguments>
</type>
Expand Down
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function execute(string $cartHash, ?int $customerId): Quote
);
}

if (false === (bool)$cart->getIsActive()) {
throw new GraphQlNoSuchEntityException(
__('Current user does not have an active cart.')
);
}

$cartCustomerId = (int)$cart->getCustomerId();

/* Guest cart, allow operations */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ protected function _prepareForm()
$this->_addAttributesToForm($attributes, $fieldset);

$this->_form->addFieldNameSuffix('order[account]');

$formValues = $this->extractValuesFromAttributes($attributes);
$this->_form->setValues($formValues);
$this->_form->setValues($this->extractValuesFromAttributes($attributes));

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminChangeCustomerGroupInNewOrder">
<annotations>
<title value="Customer account group cannot be selected while creating a new customer in order"/>
<stories value="MC-15290: Customer account group cannot be selected while creating a new customer in order"/>
<description value="Customer account group cannot be selected while creating a new customer in order"/>
<severity value="MAJOR"/>
<testCaseId value="MC-15290"/>
<useCaseId value="MC-15289"/>
<group value="sales"/>
</annotations>

<before>
<actionGroup ref="LoginAsAdmin" stepKey="login"/>
</before>
<after>
<actionGroup ref="logout" stepKey="logout"/>
</after>

<actionGroup ref="navigateToNewOrderPageNewCustomerSingleStore" stepKey="openNewOrder"/>
<selectOption selector="{{AdminOrderFormAccountSection.group}}" userInput="Retailer" stepKey="selectCustomerGroup"/>
<waitForPageLoad stepKey="waitForPageLoad"/>
<grabValueFrom selector="{{AdminOrderFormAccountSection.group}}" stepKey="grabGroupValue"/>
<assertEquals stepKey="assertValueIsStillSelected">
<actualResult type="variable">$grabGroupValue</actualResult>
<expectedResult type="string">3</expectedResult>
</assertEquals>
</test>
</tests>
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
</arguments>
<amOnPage url="{{AdminSystemStorePage.url}}" stepKey="amOnTheStorePage"/>
<click selector="{{AdminStoresGridSection.resetButton}}" stepKey="clickOnResetButton"/>
<waitForPageLoad stepKey="waitForPageLoadAfterResetButtonClicked" time="10"/>
<fillField userInput="{{websiteName}}" selector="{{AdminStoresGridSection.websiteFilterTextField}}" stepKey="fillSearchWebsiteField"/>
<click selector="{{AdminStoresGridSection.searchButton}}" stepKey="clickSearchButton" />
<waitForPageLoad stepKey="waitForPageLoadAfterSearch" time="10"/>
<see userInput="{{websiteName}}" selector="{{AdminStoresGridSection.websiteNameInFirstRow}}" stepKey="verifyThatCorrectWebsiteFound"/>
<click selector="{{AdminStoresGridSection.websiteNameInFirstRow}}" stepKey="clickEditExistingWebsite"/>

<waitForPageLoad stepKey="waitForPageLoadAfterWebsiteSelected" time="30"/>
<click selector="{{AdminStoresMainActionsSection.deleteButton}}" stepKey="clickDeleteWebsiteButtonOnEditStorePage"/>
<selectOption userInput="No" selector="{{AdminStoresDeleteWebsiteSection.createDbBackup}}" stepKey="setCreateDbBackupToNo"/>
<click selector="{{AdminStoresDeleteWebsiteSection.deleteButton}}" stepKey="clickDeleteButtonOnDeleteWebsitePage"/>
<see selector="{{AdminMessagesSection.successMessage}}" userInput="You deleted the website." stepKey="checkSuccessMessage"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<sections xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Page/etc/SectionObject.xsd">
<section name="AdminStoresDeleteWebsiteSection">
<element name="createDbBackup" type="select" selector="#store_create_backup"/>
<element name="deleteButton" type="button" selector="#delete" timeout="30"/>
<element name="deleteButton" type="button" selector="#delete" timeout="120"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\OfflineShipping;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for setting offline shipping methods on cart
*/
class SetOfflineShippingMethodsOnCartTest extends GraphQlAbstract
{
/**
* @var QuoteFactory
*/
private $quoteFactory;

/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var QuoteResource
*/
private $quoteResource;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/tablerates_weight.php
*
* @param string $carrierCode
* @param string $methodCode
* @param float $amount
* @param string $label
* @dataProvider offlineShippingMethodDataProvider
*/
public function testSetOfflineShippingMethod(string $carrierCode, string $methodCode, float $amount, string $label)
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load(
$quote,
'test_order_1',
'reserved_order_id'
);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
$shippingAddressId = (int)$quote->getShippingAddress()->getId();

$query = $this->getQuery(
$maskedQuoteId,
$shippingAddressId,
$carrierCode,
$methodCode
);

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

$addressesInformation = $response['setShippingMethodsOnCart']['cart']['shipping_addresses'];
self::assertEquals($addressesInformation[0]['selected_shipping_method']['carrier_code'], $carrierCode);
self::assertEquals($addressesInformation[0]['selected_shipping_method']['method_code'], $methodCode);
self::assertEquals($addressesInformation[0]['selected_shipping_method']['amount'], $amount);
self::assertEquals($addressesInformation[0]['selected_shipping_method']['label'], $label);
}

/**
* @return array
*/
public function offlineShippingMethodDataProvider()
{
return [
'flatrate_flatrate' => ['flatrate', 'flatrate', 10, 'Flat Rate - Fixed'],
'tablerate_bestway' => ['tablerate', 'bestway', 10, 'Best Way - Table Rate'],
'freeshipping_freeshipping' => ['freeshipping', 'freeshipping', 0, 'Free Shipping - Free'],
];
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
* @magentoApiDataFixture Magento/OfflineShipping/_files/enable_offline_shipping_methods.php
*/
public function testSetShippingMethodTwiceInOneRequest()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load(
$quote,
'test_order_1',
'reserved_order_id'
);
$shippingAddress = $quote->getShippingAddress();
$shippingAddressId = $shippingAddress->getId();
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());

$query = <<<QUERY
mutation {
setShippingMethodsOnCart(input: {
cart_id: "$maskedQuoteId"
shipping_methods: [
{
cart_address_id: $shippingAddressId
carrier_code: "flatrate"
method_code: "flatrate"
}
{
cart_address_id: $shippingAddressId
carrier_code: "freeshipping"
method_code: "freeshipping"
}
]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
label
amount
}
}
}
}
}
QUERY;
self::expectExceptionMessage('You cannot specify multiple shipping methods.');
$this->sendRequestWithToken($query);
}

/**
* Generates query for setting the specified shipping method on cart
*
* @param int $shippingAddressId
* @param string $maskedQuoteId
* @param string $carrierCode
* @param string $methodCode
* @return string
*/
private function getQuery(
string $maskedQuoteId,
int $shippingAddressId,
string $carrierCode,
string $methodCode
): string {
return <<<QUERY
mutation {
setShippingMethodsOnCart(input: {
cart_id: "$maskedQuoteId"
shipping_methods: [
{
cart_address_id: $shippingAddressId
carrier_code: "$carrierCode"
method_code: "$methodCode"
}
]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
label
amount
}
}
}
}
}
QUERY;
}

/**
* Sends a GraphQL request with using a bearer token
*
* @param string $query
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function sendRequestWithToken(string $query): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];

return $this->graphQlQuery($query, [], '', $headerMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,43 @@ public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int
}
QUERY;
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/products.php
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "wrong_cart_hash"
*/
public function testAddProductWithWrongCartHash()
{
$sku = 'simple';
$qty = 1;

$maskedQuoteId = 'wrong_cart_hash';

$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$maskedQuoteId}"
cartItems: [
{
data: {
qty: $qty
sku: "$sku"
}
}
]
}
) {
cart {
items {
qty
}
}
}
}
QUERY;

$this->graphQlQuery($query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ public function testGetNonExistentCart()
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage Current user does not have an active cart.
*/
public function testGetInactiveCart()
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id');
$quote->setCustomerId(1);
$quote->setIsActive(false);
$this->quoteResource->save($quote);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());

$query = $this->getCartQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @param string $maskedQuoteId
* @return string
Expand Down
Loading

0 comments on commit 4ed4d59

Please sign in to comment.