Skip to content

Commit d6ff7b7

Browse files
authored
Merge pull request #7886 from magento-gl/spartans_2_4_6_graphql_backlog
Spartans 2 4 6 graphql backlog
2 parents fa3d6ec + 190994a commit d6ff7b7

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

app/code/Magento/SalesGraphQl/Model/Resolver/Orders.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function resolve(
6161
'created_at' => $order->getCreatedAt(),
6262
'grand_total' => $order->getGrandTotal(),
6363
'status' => $order->getStatus(),
64+
'model' => $order
6465
];
6566
}
6667
return ['items' => $items];
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Sales\CustomerOrders;
9+
10+
use Exception;
11+
use Magento\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
/**
16+
* Class OrdersTest
17+
*/
18+
class CustomerOrdersTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var CustomerTokenServiceInterface
22+
*/
23+
private $customerTokenService;
24+
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
29+
}
30+
31+
/**
32+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
33+
* @magentoApiDataFixture Magento/Sales/_files/orders_with_customer.php
34+
*/
35+
public function testOrdersQuery()
36+
{
37+
$query =
38+
<<<QUERY
39+
query {
40+
customer {
41+
orders(filter: {}) {
42+
items {
43+
number
44+
status
45+
created_at
46+
}
47+
}
48+
}
49+
}
50+
QUERY;
51+
52+
$currentEmail = 'customer@example.com';
53+
$currentPassword = 'password';
54+
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
55+
56+
$expectedData = [
57+
[
58+
'number' => '100000002',
59+
'status' => 'Processing',
60+
'created_at' => "2022-09-04 00:00:00"
61+
],
62+
[
63+
'number' => '100000004',
64+
'status' => 'Closed',
65+
'created_at' => "2022-09-05 00:00:00"
66+
],
67+
[
68+
'number' => '100000005',
69+
'status' => 'Complete',
70+
'created_at' => "2022-09-08 00:00:00"
71+
],
72+
[
73+
'number' => '100000006',
74+
'status' => 'Complete',
75+
'created_at' => "2022-09-09 00:00:00"
76+
]
77+
];
78+
79+
$actualData = $response['customer']['orders']['items'];
80+
foreach ($expectedData as $key => $data) {
81+
$this->assertEquals(
82+
$data['number'],
83+
$actualData[$key]['number'],
84+
"order_number is different than the expected for order - " . $data['number']
85+
);
86+
87+
$this->assertEquals(
88+
$data['created_at'],
89+
$actualData[$key]['created_at'],
90+
"created_at is different than the expected for order - " . $data['created_at']
91+
);
92+
}
93+
}
94+
95+
/**
96+
*/
97+
public function testCustomerOrdersQueryNotAuthorized()
98+
{
99+
$this->expectException(\Exception::class);
100+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
101+
102+
$query = <<<QUERY
103+
{
104+
customerOrders {
105+
items {
106+
increment_id
107+
grand_total
108+
}
109+
}
110+
}
111+
QUERY;
112+
$this->graphQlQuery($query);
113+
}
114+
115+
/**
116+
* @param string $email
117+
* @param string $password
118+
* @return array
119+
* @throws \Magento\Framework\Exception\AuthenticationException
120+
*/
121+
private function getCustomerAuthHeaders(string $email, string $password): array
122+
{
123+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
124+
return ['Authorization' => 'Bearer ' . $customerToken];
125+
}
126+
}

dev/tests/integration/testsuite/Magento/Sales/_files/orders_with_customer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
'base_grand_total' => 120.00,
3535
'store_id' => 1,
3636
'website_id' => 1,
37+
'created_at' => '2022-09-04'
3738
],
3839
[
3940
'increment_id' => '100000003',
@@ -45,6 +46,7 @@
4546
'total_paid' => 130.00,
4647
'store_id' => 0,
4748
'website_id' => 0,
49+
'created_at' => '2022-09-10'
4850
],
4951
[
5052
'increment_id' => '100000004',
@@ -55,6 +57,7 @@
5557
'subtotal' => 140.00,
5658
'store_id' => 1,
5759
'website_id' => 1,
60+
'created_at' => '2022-09-05'
5861
],
5962
[
6063
'increment_id' => '100000005',
@@ -66,6 +69,7 @@
6669
'total_paid' => 150.00,
6770
'store_id' => 1,
6871
'website_id' => 1,
72+
'created_at' => '2022-09-08'
6973
],
7074
[
7175
'increment_id' => '100000006',
@@ -77,6 +81,7 @@
7781
'total_paid' => 160.00,
7882
'store_id' => 1,
7983
'website_id' => 1,
84+
'created_at' => '2022-09-09'
8085
],
8186
];
8287

lib/internal/Magento/Framework/Api/SearchCriteria/CollectionProcessor/FilterProcessor.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ private function addFilterGroupToCollection(
8585
}
8686
}
8787

88+
$this->checkFromTo($fields, $conditions);
89+
8890
if ($fields) {
8991
$collection->addFieldToFilter($fields, $conditions);
9092
}
@@ -125,4 +127,26 @@ private function getFieldMapping($field)
125127
{
126128
return $this->fieldMapping[$field] ?? $field;
127129
}
130+
131+
/**
132+
* Check filtergoup for type from & to
133+
*
134+
* @param string[] $fields
135+
* @param array<string[]> $conditions
136+
* @return void
137+
*/
138+
private function checkFromTo(&$fields, &$conditions)
139+
{
140+
$_fields = array_unique($fields);
141+
$_conditions = [];
142+
foreach ($conditions as $condition) {
143+
$_conditions[array_key_first($condition)] = array_first($condition);
144+
}
145+
if ((count($_fields) == 1) && (count($_conditions) == 2)
146+
&& isset($_conditions['from']) && isset($_conditions['to'])
147+
) {
148+
$fields = $_fields;
149+
$conditions = [$_conditions];
150+
}
151+
}
128152
}

0 commit comments

Comments
 (0)