Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

graphQl-702 Added test coverage for Customer Downloadable Products #779

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Downloadable;

use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test retrieving of customer downloadable products.
*/
class CustomerDownloadableProductsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
* @magentoApiDataFixture Magento/Downloadable/_files/customer_order_with_downloadable_product.php
*/
public function testCustomerDownloadableProducts()
{
$query = $this->getQuery();
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('items', $response['customerDownloadableProducts']);
self::assertCount(1, $response['customerDownloadableProducts']['items']);

self::assertArrayHasKey('date', $response['customerDownloadableProducts']['items'][0]);
self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['date']);

self::assertArrayHasKey('download_url', $response['customerDownloadableProducts']['items'][0]);
self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['download_url']);

self::assertArrayHasKey('order_increment_id', $response['customerDownloadableProducts']['items'][0]);
self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['order_increment_id']);

self::assertArrayHasKey('remaining_downloads', $response['customerDownloadableProducts']['items'][0]);
self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['remaining_downloads']);

self::assertArrayHasKey('status', $response['customerDownloadableProducts']['items'][0]);
self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['status']);
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
* @magentoApiDataFixture Magento/Downloadable/_files/customer_order_with_downloadable_product.php
*
* @expectedException \Exception
* @expectedExceptionMessage The current customer isn't authorized.
*/
public function testGuestCannotAccessDownloadableProducts()
{
$this->graphQlQuery($this->getQuery());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
*/
public function testCustomerHasNoOrders()
{
$query = $this->getQuery();
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());

self::assertArrayHasKey('items', $response['customerDownloadableProducts']);
self::assertCount(0, $response['customerDownloadableProducts']['items']);
}

/**
* @return string
*/
private function getQuery(): string
{
return <<<QUERY
{
customerDownloadableProducts {
items {
date
download_url
order_increment_id
remaining_downloads
status
}
}
}
QUERY;
}

/**
* @param string $username
* @param string $password
* @return array
* @throws \Magento\Framework\Exception\AuthenticationException
*/
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Quote\Api\Data\AddressInterface;

$billingAddress = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Sales\Model\Order\Address::class,
[
'data' => [
AddressInterface::KEY_TELEPHONE => 3468676,
AddressInterface::KEY_POSTCODE => 75477,
AddressInterface::KEY_COUNTRY_ID => 'US',
AddressInterface::KEY_CITY => 'CityM',
AddressInterface::KEY_COMPANY => 'CompanyName',
AddressInterface::KEY_STREET => 'Green str, 67',
AddressInterface::KEY_LASTNAME => 'Smith',
AddressInterface::KEY_FIRSTNAME => 'John',
AddressInterface::KEY_REGION_ID => 1,
]
]
);
$billingAddress->setAddressType('billing');

$payment = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Sales\Model\Order\Payment::class
);
$payment->setMethod('checkmo');

/** @var \Magento\Sales\Model\Order\Item $orderItem */
$orderItem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Sales\Model\Order\Item::class
);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->getById(1);
$link = $product->getExtensionAttributes()->getDownloadableProductLinks()[0];

$orderItem->setProductId(1)
->setProductType(\Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE)
->setProductOptions(['links' => [$link->getId()]])
->setBasePrice(100)
->setQtyOrdered(1);

$order = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Sales\Model\Order::class);
$order->setCustomerEmail('[email protected]')
->addItem($orderItem)
->setIncrementId('100000001')
->setCustomerId(1)
->setStoreId(1)
->setEmailSent(1)
->setBillingAddress($billingAddress)
->setPayment($payment);
$order->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

require __DIR__ . '/../../../Magento/Sales/_files/default_rollback.php';