Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.3-develop' into magento/graphq…
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Jun 19, 2019
2 parents 4788d35 + ee6bc37 commit 87296c1
Show file tree
Hide file tree
Showing 31 changed files with 1,871 additions and 208 deletions.
6 changes: 3 additions & 3 deletions app/code/Magento/CatalogUrlRewriteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

interface ProductInterface {
url_key: String @doc(description: "The part of the URL that identifies the product")
url_path: String @doc(description: "The part of the URL that precedes the url_key")
url_path: String @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")
url_rewrites: [UrlRewrite] @doc(description: "URL rewrites list") @resolver(class: "Magento\\UrlRewriteGraphQl\\Model\\Resolver\\UrlRewrite")
}

input ProductFilterInput {
url_key: FilterTypeInput @doc(description: "The part of the URL that identifies the product")
url_path: FilterTypeInput @doc(description: "The part of the URL that precedes the url_key")
url_path: FilterTypeInput @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")
}

input ProductSortInput {
url_key: SortEnum @doc(description: "The part of the URL that identifies the product")
url_path: SortEnum @doc(description: "The part of the URL that precedes the url_key")
url_path: SortEnum @deprecated(reason: "Use product's `canonical_url` or url rewrites instead")
}

enum UrlRewriteEntityTypeEnum @doc(description: "This enumeration defines the entity type.") {
Expand Down
19 changes: 19 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
'value' => $cartTotals->getSubtotalWithDiscount(), 'currency' => $currency
],
'applied_taxes' => $this->getAppliedTaxes($cartTotals, $currency),
'discount' => $this->getDiscount($cartTotals, $currency),
'model' => $quote
];
}
Expand Down Expand Up @@ -84,4 +85,22 @@ private function getAppliedTaxes(Total $total, string $currency): array
}
return $appliedTaxesData;
}

/**
* Returns information about an applied discount
*
* @param Total $total
* @param string $currency
* @return array|null
*/
private function getDiscount(Total $total, string $currency)
{
if ($total->getDiscountAmount() === 0) {
return null;
}
return [
'label' => explode(', ', $total->getDiscountDescription()),
'amount' => ['value' => $total->getDiscountAmount(), 'currency' => $currency]
];
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type CartPrices {
grand_total: Money
subtotal_including_tax: Money
subtotal_excluding_tax: Money
discount: CartDiscount
subtotal_with_discount_excluding_tax: Money
applied_taxes: [CartTaxItem]
}
Expand All @@ -155,6 +156,11 @@ type CartTaxItem {
label: String!
}

type CartDiscount {
amount: Money!
label: [String!]!
}

type SetPaymentMethodOnCartOutput {
cart: Cart!
}
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/RelatedProductGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See COPYING.txt for license details.

interface ProductInterface {
related_products: [ProductInterface] @doc(description: "RelatedProduct") @resolver(class: "Magento\\RelatedProductGraphQl\\Model\\Resolver\\RelatedProducts")
upsell_products: [ProductInterface] @doc(description: "RelatedProduct") @resolver(class: "Magento\\RelatedProductGraphQl\\Model\\Resolver\\UpSellProducts")
crosssell_products: [ProductInterface] @doc(description: "RelatedProduct") @resolver(class: "Magento\\RelatedProductGraphQl\\Model\\Resolver\\CrossSellProducts")
}
related_products: [ProductInterface] @doc(description: "Related Products") @resolver(class: "Magento\\RelatedProductGraphQl\\Model\\Resolver\\RelatedProducts")
upsell_products: [ProductInterface] @doc(description: "Upsell Products") @resolver(class: "Magento\\RelatedProductGraphQl\\Model\\Resolver\\UpSellProducts")
crosssell_products: [ProductInterface] @doc(description: "Crosssell Products") @resolver(class: "Magento\\RelatedProductGraphQl\\Model\\Resolver\\CrossSellProducts")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test cases for product media gallery data retrieval.
*/
class MediaGalleryTest extends GraphQlAbstract
{
/**
Expand Down Expand Up @@ -45,19 +48,48 @@ public function testProductSmallImageUrlWithExistingImage()
self::assertTrue($this->checkImageExists($response['products']['items'][0]['small_image']['url']));
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_with_image.php
*/
public function testProductMediaGalleryEntries()
{
$this->markTestSkipped('https://github.com/magento/graphql-ce/issues/738');
$productSku = 'simple';
$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
name
sku
media_gallery_entries {
id
file
types
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertArrayHasKey('file', $response['products']['items'][0]['media_gallery_entries'][0]);
self::assertContains('magento_image.jpg', $response['products']['items'][0]['media_gallery_entries'][0]['url']);
}

/**
* @param string $url
* @return bool
*/
private function checkImageExists(string $url): bool
{
// phpcs:disable Magento2.Functions.DiscouragedFunction
$connection = curl_init($url);
curl_setopt($connection, CURLOPT_HEADER, true);
curl_setopt($connection, CURLOPT_NOBODY, true);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
curl_exec($connection);
$responseStatus = curl_getinfo($connection, CURLINFO_HTTP_CODE);

// phpcs:enable Magento2.Functions.DiscouragedFunction
return $responseStatus === 200 ? true : false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
namespace Magento\GraphQl\Customer;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\CustomerAuthUpdate;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
Expand All @@ -34,11 +38,23 @@ class ChangeCustomerPasswordTest extends GraphQlAbstract
*/
private $customerRegistry;

/**
* @var CustomerAuthUpdate
*/
private $customerAuthUpdate;

/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;

protected function setUp()
{
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
$this->accountManagement = Bootstrap::getObjectManager()->get(AccountManagementInterface::class);
$this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
$this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
}

/**
Expand All @@ -47,19 +63,19 @@ protected function setUp()
public function testChangePassword()
{
$customerEmail = '[email protected]';
$oldCustomerPassword = 'password';
$newCustomerPassword = 'anotherPassword1';
$currentPassword = 'password';
$newPassword = 'anotherPassword1';

$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
$query = $this->getQuery($currentPassword, $newPassword);
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);

$response = $this->graphQlMutation($query, [], '', $headerMap);
$this->assertEquals($customerEmail, $response['changeCustomerPassword']['email']);

try {
// registry contains the old password hash so needs to be reset
$this->customerRegistry->removeByEmail($customerEmail);
$this->accountManagement->authenticate($customerEmail, $newCustomerPassword);
$this->accountManagement->authenticate($customerEmail, $newPassword);
} catch (LocalizedException $e) {
$this->fail('Password was not changed: ' . $e->getMessage());
}
Expand All @@ -71,7 +87,7 @@ public function testChangePassword()
*/
public function testChangePasswordIfUserIsNotAuthorizedTest()
{
$query = $this->getChangePassQuery('currentpassword', 'newpassword');
$query = $this->getQuery('currentpassword', 'newpassword');
$this->graphQlMutation($query);
}

Expand All @@ -81,11 +97,11 @@ public function testChangePasswordIfUserIsNotAuthorizedTest()
public function testChangeWeakPassword()
{
$customerEmail = '[email protected]';
$oldCustomerPassword = 'password';
$newCustomerPassword = 'weakpass';
$currentPassword = 'password';
$newPassword = 'weakpass';

$query = $this->getChangePassQuery($oldCustomerPassword, $newCustomerPassword);
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
$query = $this->getQuery($currentPassword, $newPassword);
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);

$this->expectException(\Exception::class);
$this->expectExceptionMessageRegExp('/Minimum of different classes of characters in password is.*/');
Expand All @@ -101,17 +117,123 @@ public function testChangeWeakPassword()
public function testChangePasswordIfPasswordIsInvalid()
{
$customerEmail = '[email protected]';
$oldCustomerPassword = 'password';
$newCustomerPassword = 'anotherPassword1';
$incorrectPassword = 'password-incorrect';
$currentPassword = 'password';
$newPassword = 'anotherPassword1';
$incorrectCurrentPassword = 'password-incorrect';

$query = $this->getQuery($incorrectCurrentPassword, $newPassword);

$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
$this->graphQlMutation($query, [], '', $headerMap);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage Specify the "currentPassword" value.
*/
public function testChangePasswordIfCurrentPasswordIsEmpty()
{
$customerEmail = '[email protected]';
$currentPassword = 'password';
$newPassword = 'anotherPassword1';
$incorrectCurrentPassword = '';

$query = $this->getQuery($incorrectCurrentPassword, $newPassword);

$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
$this->graphQlMutation($query, [], '', $headerMap);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage Specify the "newPassword" value.
*/
public function testChangePasswordIfNewPasswordIsEmpty()
{
$customerEmail = '[email protected]';
$currentPassword = 'password';
$incorrectNewPassword = '';

$query = $this->getChangePassQuery($incorrectPassword, $newCustomerPassword);
$query = $this->getQuery($currentPassword, $incorrectNewPassword);

$headerMap = $this->getCustomerAuthHeaders($customerEmail, $oldCustomerPassword);
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
$this->graphQlMutation($query, [], '', $headerMap);
}

private function getChangePassQuery($currentPassword, $newPassword)
/**
* @magentoApiDataFixture Magento/GraphQl/Customer/_files/enable_customer_account_confirmation.php
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage This account isn't confirmed. Verify and try again.
*/
public function testChangePasswordIfAccountIsNotConfirmed()
{
$customerEmail = '[email protected]';
$currentPassword = 'password';
$newPassword = 'anotherPassword1';

/* get header map before setting the customer unconfirmed */
$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);

$this->setCustomerConfirmation(1);
$query = $this->getQuery($currentPassword, $newPassword);

$this->graphQlMutation($query, [], '', $headerMap);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage The account is locked.
*/
public function testChangePasswordIfCustomerIsLocked()
{
$customerEmail = '[email protected]';
$currentPassword = 'password';
$newPassword = 'anotherPassword1';

$this->lockCustomer(1);
$query = $this->getQuery($currentPassword, $newPassword);

$headerMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
$this->graphQlMutation($query, [], '', $headerMap);
}

/**
* @param int $customerId
*
* @return void
* @throws NoSuchEntityException
*/
private function lockCustomer(int $customerId): void
{
$customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
$customerSecure->setLockExpires('2030-12-31 00:00:00');
$this->customerAuthUpdate->saveAuth($customerId);
}

/**
* @param int $customerId
*
* @return void
* @throws LocalizedException
*/
private function setCustomerConfirmation(int $customerId): void
{
$customer = $this->customerRepository->getById($customerId);
$customer->setConfirmation('d5a21f15bd4cc21bd1b21ef6d9989a38');
$this->customerRepository->save($customer);
}

/**
* @param $currentPassword
* @param $newPassword
*
* @return string
*/
private function getQuery($currentPassword, $newPassword)
{
$query = <<<QUERY
mutation {
Expand All @@ -133,7 +255,9 @@ private function getChangePassQuery($currentPassword, $newPassword)
/**
* @param string $email
* @param string $password
*
* @return array
* @throws AuthenticationException
*/
private function getCustomerAuthHeaders(string $email, string $password): array
{
Expand Down
Loading

0 comments on commit 87296c1

Please sign in to comment.