-
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.
[Wishlist] Remove name from WishlistOutput #920
- Loading branch information
Showing
3 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlistsResolver.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,67 @@ | ||
<?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\ResourceModel\Wishlist\CollectionFactory; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
|
||
/** | ||
* Fetches the Wishlists data according to the GraphQL schema | ||
*/ | ||
class CustomerWishlistsResolver implements ResolverInterface | ||
{ | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $_wishlistCollectionFactory; | ||
|
||
/** | ||
* @param CollectionFactory $wishlistCollectionFactory | ||
*/ | ||
public function __construct(CollectionFactory $wishlistCollectionFactory) | ||
{ | ||
$this->_wishlistCollectionFactory = $wishlistCollectionFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
$customerId = $context->getUserId(); | ||
|
||
/* Guest checking */ | ||
if (!$customerId && 0 === $customerId) { | ||
throw new GraphQlAuthorizationException(__('The current user cannot perform operations on wishlist')); | ||
} | ||
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId($customerId); | ||
$wishlists = $collection->getItems(); | ||
$wishlistsData = []; | ||
if (0 === count($wishlists)) { | ||
return $wishlistsData; | ||
} | ||
|
||
foreach ($wishlists as $wishlist) { | ||
$wishlistsData [] = [ | ||
'sharing_code' => $wishlist->getSharingCode(), | ||
'updated_at' => $wishlist->getUpdatedAt(), | ||
'items_count' => $wishlist->getItemsCount(), | ||
'model' => $wishlist, | ||
]; | ||
} | ||
return $wishlistsData; | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistsTest.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,110 @@ | ||
<?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 CustomerWishlistsTest 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 testGetCustomerWishlists(): void | ||
{ | ||
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */ | ||
$collection = $this->_wishlistCollectionFactory->create()->filterByCustomerId(1); | ||
|
||
/** @var Item $wishlistItem */ | ||
$wishlistItem = $collection->getFirstItem(); | ||
$query = | ||
<<<QUERY | ||
{ | ||
customer | ||
{ | ||
wishlists { | ||
items_count | ||
sharing_code | ||
updated_at | ||
items { | ||
product { | ||
sku | ||
} | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
|
||
$response = $this->graphQlQuery( | ||
$query, | ||
[], | ||
'', | ||
$this->getCustomerAuthHeaders('customer@example.com', 'password') | ||
); | ||
|
||
$this->assertEquals($wishlistItem->getItemsCount(), $response['wishlists'][0]['items_count']); | ||
$this->assertEquals($wishlistItem->getSharingCode(), $response['wishlists'][0]['sharing_code']); | ||
$this->assertEquals($wishlistItem->getUpdatedAt(), $response['wishlists'][0]['updated_at']); | ||
$this->assertEquals('simple', $response['wishlists'][0]['items'][0]['product']['sku']); | ||
|
||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage The current customer isn't authorized. | ||
*/ | ||
public function testGetGuestWishlist() | ||
{ | ||
$query = | ||
<<<QUERY | ||
{ | ||
customer | ||
{ | ||
wishlists { | ||
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]; | ||
} | ||
} |