7
7
8
8
namespace Magento \GraphQl \Customer ;
9
9
10
+ use Exception ;
11
+ use Magento \Customer \Api \AccountManagementInterface ;
10
12
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
13
use Magento \Customer \Model \CustomerAuthUpdate ;
12
14
use Magento \Customer \Model \CustomerRegistry ;
15
+ use Magento \Framework \ObjectManagerInterface ;
16
+ use Magento \Integration \Api \AdminTokenServiceInterface ;
13
17
use Magento \Integration \Api \CustomerTokenServiceInterface ;
14
18
use Magento \TestFramework \Helper \Bootstrap ;
19
+ use Magento \TestFramework \Bootstrap as TestBootstrap ;
15
20
use Magento \TestFramework \TestCase \GraphQlAbstract ;
16
21
22
+ /**
23
+ * GraphQl tests for @see \Magento\CustomerGraphQl\Model\Customer\GetCustomer.
24
+ */
17
25
class GetCustomerTest extends GraphQlAbstract
18
26
{
19
27
/**
@@ -36,14 +44,23 @@ class GetCustomerTest extends GraphQlAbstract
36
44
*/
37
45
private $ customerRepository ;
38
46
47
+ /**
48
+ * @var ObjectManagerInterface
49
+ */
50
+ private $ objectManager ;
51
+
52
+ /**
53
+ * @inheridoc
54
+ */
39
55
protected function setUp (): void
40
56
{
41
57
parent ::setUp ();
42
58
43
- $ this ->customerTokenService = Bootstrap::getObjectManager ()->get (CustomerTokenServiceInterface::class);
44
- $ this ->customerRegistry = Bootstrap::getObjectManager ()->get (CustomerRegistry::class);
45
- $ this ->customerAuthUpdate = Bootstrap::getObjectManager ()->get (CustomerAuthUpdate::class);
46
- $ this ->customerRepository = Bootstrap::getObjectManager ()->get (CustomerRepositoryInterface::class);
59
+ $ this ->objectManager = Bootstrap::getObjectManager ();
60
+ $ this ->customerTokenService = $ this ->objectManager ->get (CustomerTokenServiceInterface::class);
61
+ $ this ->customerRegistry = $ this ->objectManager ->get (CustomerRegistry::class);
62
+ $ this ->customerAuthUpdate = $ this ->objectManager ->get (CustomerAuthUpdate::class);
63
+ $ this ->customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
47
64
}
48
65
49
66
/**
@@ -71,18 +88,19 @@ public function testGetCustomer()
71
88
$ this ->getCustomerAuthHeaders ($ currentEmail , $ currentPassword )
72
89
);
73
90
74
- $ this ->assertEquals ( null , $ response ['customer ' ]['id ' ]);
91
+ $ this ->assertNull ( $ response ['customer ' ]['id ' ]);
75
92
$ this ->assertEquals ('John ' , $ response ['customer ' ]['firstname ' ]);
76
93
$ this ->assertEquals ('Smith ' , $ response ['customer ' ]['lastname ' ]);
77
94
$ this ->assertEquals ($ currentEmail , $ response ['customer ' ]['email ' ]);
78
95
}
79
96
80
97
/**
81
- * @expectedException \Exception
82
- * @expectedExceptionMessage The current customer isn't authorized.
83
98
*/
84
99
public function testGetCustomerIfUserIsNotAuthorized ()
85
100
{
101
+ $ this ->expectException (Exception::class);
102
+ $ this ->expectExceptionMessage ('The current customer isn \'t authorized. ' );
103
+
86
104
$ query = <<<QUERY
87
105
query {
88
106
customer {
@@ -95,17 +113,49 @@ public function testGetCustomerIfUserIsNotAuthorized()
95
113
$ this ->graphQlQuery ($ query );
96
114
}
97
115
116
+ /**
117
+ * @magentoApiDataFixture Magento/User/_files/user_with_role.php
118
+ * @return void
119
+ */
120
+ public function testGetCustomerIfUserHasWrongType (): void
121
+ {
122
+ /** @var $adminTokenService AdminTokenServiceInterface */
123
+ $ adminTokenService = $ this ->objectManager ->get (AdminTokenServiceInterface::class);
124
+ $ adminToken = $ adminTokenService ->createAdminAccessToken ('adminUser ' , TestBootstrap::ADMIN_PASSWORD );
125
+
126
+ $ this ->expectException (Exception::class);
127
+ $ this ->expectExceptionMessage ('The current customer isn \'t authorized. ' );
128
+
129
+ $ query = <<<QUERY
130
+ query {
131
+ customer {
132
+ firstname
133
+ lastname
134
+ email
135
+ }
136
+ }
137
+ QUERY ;
138
+ $ this ->graphQlQuery (
139
+ $ query ,
140
+ [],
141
+ '' ,
142
+ ['Authorization ' => 'Bearer ' . $ adminToken ]
143
+ );
144
+ }
145
+
98
146
/**
99
147
* @magentoApiDataFixture Magento/Customer/_files/customer.php
100
- * @expectedException \Exception
101
- * @expectedExceptionMessage The account is locked.
102
148
*/
103
149
public function testGetCustomerIfAccountIsLocked ()
104
150
{
105
- $ this ->lockCustomer (1 );
106
-
107
151
$ currentEmail = 'customer@example.com ' ;
108
152
$ currentPassword = 'password ' ;
153
+ $ customer = $ this ->customerRepository ->get ($ currentEmail );
154
+
155
+ $ this ->lockCustomer ((int )$ customer ->getId ());
156
+
157
+ $ this ->expectException (Exception::class);
158
+ $ this ->expectExceptionMessage ('The account is locked. ' );
109
159
110
160
$ query = <<<QUERY
111
161
query {
@@ -125,18 +175,19 @@ public function testGetCustomerIfAccountIsLocked()
125
175
}
126
176
127
177
/**
128
- * @magentoApiDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
178
+ * @magentoConfigFixture customer/create_account/confirm 1
129
179
* @magentoApiDataFixture Magento/Customer/_files/customer.php
130
- * @expectedExceptionMessage This account isn't confirmed. Verify and try again.
180
+ *
131
181
*/
132
182
public function testAccountIsNotConfirmed ()
133
183
{
184
+ $ this ->expectExceptionMessage ("This account isn't confirmed. Verify and try again. " );
134
185
$ customerEmail = 'customer@example.com ' ;
135
186
$ currentPassword = 'password ' ;
187
+ $ customer = $ this ->customerRepository ->get ($ customerEmail );
136
188
$ headersMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
137
- $ customer = $ this ->customerRepository ->getById (1 )->setConfirmation (
138
- \Magento \Customer \Api \AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
139
- );
189
+ $ customer = $ this ->customerRepository ->getById ((int )$ customer ->getId ())
190
+ ->setConfirmation (AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED );
140
191
$ this ->customerRepository ->save ($ customer );
141
192
$ query = <<<QUERY
142
193
query {
@@ -158,6 +209,7 @@ public function testAccountIsNotConfirmed()
158
209
private function getCustomerAuthHeaders (string $ email , string $ password ): array
159
210
{
160
211
$ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ email , $ password );
212
+
161
213
return ['Authorization ' => 'Bearer ' . $ customerToken ];
162
214
}
163
215
0 commit comments