-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
212 additions
and
5 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\WishlistGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Wishlist\Model\WishlistFactory; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
|
||
/** | ||
* Fetches customer wishlist data | ||
*/ | ||
class CustomerWishlistResolver implements ResolverInterface | ||
{ | ||
/** | ||
* @var WishlistFactory | ||
*/ | ||
private $wishlistFactory; | ||
|
||
/** | ||
* @param WishlistFactory $wishlistFactory | ||
*/ | ||
public function __construct(WishlistFactory $wishlistFactory) | ||
{ | ||
$this->wishlistFactory = $wishlistFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
if (false === $context->getExtensionAttributes()->getIsCustomer()) { | ||
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); | ||
} | ||
$wishlist = $this->wishlistFactory->create()->loadByCustomerId($context->getUserId(), true); | ||
return [ | ||
'id' => (string) $wishlist->getId(), | ||
'sharing_code' => $wishlist->getSharingCode(), | ||
'updated_at' => $wishlist->getUpdatedAt(), | ||
'items_count' => $wishlist->getItemsCount(), | ||
'model' => $wishlist, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Wishlist; | ||
|
||
use Magento\Integration\Api\CustomerTokenServiceInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\Wishlist\Model\Item; | ||
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory; | ||
|
||
class CustomerWishlistTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var CustomerTokenServiceInterface | ||
*/ | ||
private $customerTokenService; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $wishlistCollectionFactory; | ||
|
||
protected function setUp() | ||
{ | ||
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); | ||
$this->wishlistCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist.php | ||
*/ | ||
public function testCustomerWishlist(): void | ||
{ | ||
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */ | ||
$collection = $this->wishlistCollectionFactory->create()->filterByCustomerId(1); | ||
|
||
/** @var Item $wishlistItem */ | ||
$wishlistItem = $collection->getFirstItem(); | ||
$query = | ||
<<<QUERY | ||
{ | ||
customer { | ||
wishlist { | ||
id | ||
items_count | ||
sharing_code | ||
updated_at | ||
items { | ||
product { | ||
sku | ||
} | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
|
||
$response = $this->graphQlQuery( | ||
$query, | ||
[], | ||
'', | ||
$this->getCustomerAuthHeaders('[email protected]', 'password') | ||
); | ||
$this->assertEquals((string)$wishlistItem->getId(), $response['customer']['wishlist']['id']); | ||
$this->assertEquals($wishlistItem->getItemsCount(), $response['customer']['wishlist']['items_count']); | ||
$this->assertEquals($wishlistItem->getSharingCode(), $response['customer']['wishlist']['sharing_code']); | ||
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['customer']['wishlist']['updated_at']); | ||
$this->assertEquals('simple', $response['customer']['wishlist']['items'][0]['product']['sku']); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Customer/_files/customer.php | ||
*/ | ||
public function testCustomerAlwaysHasWishlist(): void | ||
{ | ||
$query = | ||
<<<QUERY | ||
{ | ||
customer { | ||
wishlist { | ||
id | ||
} | ||
} | ||
} | ||
QUERY; | ||
|
||
$response = $this->graphQlQuery( | ||
$query, | ||
[], | ||
'', | ||
$this->getCustomerAuthHeaders('[email protected]', 'password') | ||
); | ||
|
||
$this->assertNotEmpty($response['customer']['wishlist']['id']); | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage The current customer isn't authorized. | ||
*/ | ||
public function testGuestCannotGetWishlist() | ||
{ | ||
$query = | ||
<<<QUERY | ||
{ | ||
customer { | ||
wishlist { | ||
items_count | ||
sharing_code | ||
updated_at | ||
} | ||
} | ||
} | ||
QUERY; | ||
$this->graphQlQuery($query); | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @param string $password | ||
* @return array | ||
* @throws \Magento\Framework\Exception\AuthenticationException | ||
*/ | ||
private function getCustomerAuthHeaders(string $email, string $password): array | ||
{ | ||
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); | ||
return ['Authorization' => 'Bearer ' . $customerToken]; | ||
} | ||
} |