Skip to content

Commit 54ad1ab

Browse files
committed
ACP2E-3253: GraphQL cart itemsV2 pagination is not working correctly
1 parent 71432ae commit 54ad1ab

File tree

3 files changed

+160
-5
lines changed

3 files changed

+160
-5
lines changed

app/code/Magento/QuoteGraphQl/Model/CartItem/GetPaginatedCartItems.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public function __construct(
2828
*
2929
* @param Quote $cart
3030
* @param int $pageSize
31-
* @param int $offset
31+
* @param int $currentPage
3232
* @param string $orderBy
3333
* @param string $order
3434
* @return array
3535
*/
36-
public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy, string $order): array
36+
public function execute(Quote $cart, int $pageSize, int $currentPage, string $orderBy, string $order): array
3737
{
3838
if (!$cart->getId()) {
3939
return [
@@ -46,7 +46,7 @@ public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy
4646
->addFieldToFilter('parent_item_id', ['null' => true])
4747
->addFieldToFilter('quote_id', $cart->getId())
4848
->setOrder($orderBy, $order)
49-
->setCurPage($offset)
49+
->setCurPage($currentPage)
5050
->setPageSize($pageSize)
5151
->setQuote($cart);
5252

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemsPaginated.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4949

5050
$pageSize = $args['pageSize'];
5151
$currentPage = $args['currentPage'];
52-
$offset = ($currentPage - 1) * $pageSize;
5352
$order = CartItemsPaginated::SORT_ORDER;
5453
$orderBy = CartItemsPaginated::SORT_ORDER_BY;
5554

@@ -59,7 +58,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5958
}
6059

6160
$allVisibleItems = $cart->getAllVisibleItems();
62-
$paginatedCartItems = $this->pagination->execute($cart, $pageSize, (int) $offset, $orderBy, $order);
61+
$paginatedCartItems = $this->pagination->execute($cart, $pageSize, (int) $currentPage, $orderBy, $order);
6362

6463
$cartItems = [];
6564
/** @var CartItemInterface $cartItem */
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote\Customer;
9+
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\Customer\Test\Fixture\Customer;
12+
use Magento\Framework\Exception\AuthenticationException;
13+
use Magento\Framework\Exception\EmailNotConfirmedException;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Integration\Api\CustomerTokenServiceInterface;
16+
use Magento\Quote\Test\Fixture\AddProductToCart;
17+
use Magento\Quote\Test\Fixture\CustomerCart;
18+
use Magento\Quote\Test\Fixture\QuoteIdMask as QuoteMaskFixture;
19+
use Magento\TestFramework\Fixture\DataFixture;
20+
use Magento\TestFramework\Fixture\DataFixtureStorage;
21+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
22+
use Magento\TestFramework\Helper\Bootstrap;
23+
use Magento\TestFramework\TestCase\GraphQlAbstract;
24+
25+
class GetCartPaginatedItemsTest extends GraphQlAbstract
26+
{
27+
/**
28+
* @var ObjectManagerInterface
29+
*/
30+
private $objectManager;
31+
32+
/**
33+
* @var CustomerTokenServiceInterface
34+
*/
35+
private $customerTokenService;
36+
37+
/**
38+
* @var DataFixtureStorage
39+
*/
40+
private $fixtures;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
$this->objectManager = Bootstrap::getObjectManager();
49+
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
50+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
51+
}
52+
53+
/**
54+
* @dataProvider paginatedDataProvider
55+
* @throws \Exception
56+
*/
57+
#[
58+
DataFixture(ProductFixture::class, ['sku' => 'p1'], as: 'product1'),
59+
DataFixture(ProductFixture::class, ['sku' => 'p2'], as: 'product2'),
60+
DataFixture(ProductFixture::class, ['sku' => 'p3'], as: 'product3'),
61+
DataFixture(ProductFixture::class, ['sku' => 'p4'], as: 'product4'),
62+
DataFixture(ProductFixture::class, ['sku' => 'p5'], as: 'product5'),
63+
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
64+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'),
65+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product1.id$', 'qty' => 1]),
66+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product2.id$', 'qty' => 1]),
67+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product3.id$', 'qty' => 1]),
68+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product4.id$', 'qty' => 1]),
69+
DataFixture(AddProductToCart::class, ['cart_id' => '$cart.id$', 'product_id' => '$product5.id$', 'qty' => 1]),
70+
DataFixture(QuoteMaskFixture::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
71+
]
72+
public function testGetCartPaginatedItems(array $expectedSkus, int $pageSize, int $currentPage, int $totalCount)
73+
{
74+
$customer = $this->fixtures->get('customer');
75+
$maskedQuoteId = $this->fixtures->get('quoteIdMask')->getMaskedId();
76+
$query = $this->getQuery($maskedQuoteId, $pageSize, $currentPage);
77+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap($customer->getEmail()));
78+
79+
$this->assertArrayNotHasKey('errors', $response);
80+
$this->assertEquals($totalCount, $response['cart']['itemsV2']['total_count']);
81+
$this->assertEquals($pageSize, $response['cart']['itemsV2']['page_info']['page_size']);
82+
$this->assertEquals($currentPage, $response['cart']['itemsV2']['page_info']['current_page']);
83+
$actualSkus = [];
84+
foreach ($response['cart']['itemsV2']['items'] as $item) {
85+
$actualSkus[] = $item['product']['sku'];
86+
}
87+
$this->assertEquals($expectedSkus, $actualSkus);
88+
}
89+
90+
/**
91+
* @param string $maskedQuoteId
92+
* @param int $pageSize
93+
* @param int $currentPage
94+
* @return string
95+
*/
96+
private function getQuery(string $maskedQuoteId, int $pageSize, int $currentPage): string
97+
{
98+
return <<<QUERY
99+
{
100+
cart(cart_id: "{$maskedQuoteId}") {
101+
email
102+
itemsV2(pageSize: {$pageSize} currentPage: {$currentPage}) {
103+
total_count
104+
page_info {
105+
page_size
106+
current_page
107+
total_pages
108+
}
109+
items {
110+
id
111+
product {
112+
name
113+
sku
114+
}
115+
quantity
116+
}
117+
}
118+
}
119+
}
120+
QUERY;
121+
}
122+
123+
/**
124+
* @param string $username
125+
* @param string $password
126+
* @return string[]
127+
* @throws AuthenticationException
128+
* @throws EmailNotConfirmedException
129+
*/
130+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
131+
{
132+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
133+
return ['Authorization' => 'Bearer ' . $customerToken];
134+
}
135+
136+
/**
137+
* @return array
138+
*/
139+
public function paginatedDataProvider(): array
140+
{
141+
return [
142+
[
143+
['p1', 'p2', 'p3'],
144+
3,
145+
1,
146+
5
147+
],
148+
[
149+
['p4', 'p5'],
150+
3,
151+
2,
152+
5
153+
],
154+
];
155+
}
156+
}

0 commit comments

Comments
 (0)