1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2021 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
8
8
namespace Magento \GraphQl \GraphQlCache \CacheIdFactorProviders \Customer ;
9
9
10
+ use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
10
11
use Magento \Customer \Api \CustomerRepositoryInterface ;
11
12
use Magento \Customer \Model \Group ;
13
+ use Magento \Customer \Test \Fixture \Customer as CustomerFixture ;
12
14
use Magento \Framework \Exception \AuthenticationException ;
15
+ use Magento \Framework \Exception \EmailNotConfirmedException ;
13
16
use Magento \GraphQlCache \Model \CacheId \CacheIdCalculator ;
14
17
use Magento \Integration \Api \CustomerTokenServiceInterface ;
18
+ use Magento \TestFramework \Fixture \DataFixture ;
19
+ use Magento \TestFramework \Fixture \DataFixtureStorage ;
20
+ use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
15
21
use Magento \TestFramework \Helper \Bootstrap ;
16
22
use Magento \TestFramework \TestCase \GraphQlAbstract ;
17
23
@@ -35,57 +41,70 @@ class CustomerGroupProviderTest extends GraphQlAbstract
35
41
*/
36
42
private $ customerRepository ;
37
43
44
+ /**
45
+ * @var DataFixtureStorage
46
+ */
47
+ private $ fixtures ;
48
+
38
49
protected function setUp (): void
39
50
{
40
- $ objectManager = Bootstrap::getObjectManager ();
41
- $ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface ::class);
42
- $ this ->customerGroup = $ objectManager ->get (Group ::class);
43
- $ this ->customerRepository = $ objectManager ->get (CustomerRepositoryInterface ::class);
51
+ $ this -> customerTokenService = Bootstrap::getObjectManager ()-> get (CustomerTokenServiceInterface::class );
52
+ $ this ->customerGroup = Bootstrap:: getObjectManager () ->get (Group ::class);
53
+ $ this ->customerRepository = Bootstrap:: getObjectManager () ->get (CustomerRepositoryInterface ::class);
54
+ $ this ->fixtures = Bootstrap:: getObjectManager () ->get (DataFixtureStorageManager ::class)-> getStorage ( );
44
55
}
45
56
46
57
/**
47
58
* Tests that cache id header changes based on customer group and remains consistent for same customer group
48
- *
49
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
50
- * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
51
59
*/
52
- public function testCacheIdHeaderWithCustomerGroup ()
60
+ #[
61
+ DataFixture(ProductFixture::class, as: 'product ' ),
62
+ DataFixture(CustomerFixture::class, as: 'customer ' ),
63
+ ]
64
+ public function testCacheIdHeaderWithCustomerGroup (): void
53
65
{
54
- $ sku = ' simple_product ' ;
66
+ $ customerEmail = $ this -> fixtures -> get ( ' customer ' )-> getEmail () ;
55
67
$ query = <<<QUERY
56
- {
57
- products(filter: {sku: {eq: " {$ sku }"}})
58
- {
59
- items {
60
- id
61
- name
62
- sku
63
- description {
64
- html
68
+ {
69
+ products(filter: {sku: {eq: " {$ this ->fixtures ->get ('product ' )->getSku ()}"}})
70
+ {
71
+ items {
72
+ id
73
+ name
74
+ sku
75
+ description {
76
+ html
77
+ }
65
78
}
66
79
}
67
80
}
68
- }
69
- QUERY ;
70
- $ response = $ this ->graphQlQueryWithResponseHeaders ($ query , [], '' , $ this ->getHeaderMap ());
81
+ QUERY ;
82
+ $ response = $ this ->graphQlQueryWithResponseHeaders (
83
+ $ query ,
84
+ [],
85
+ '' ,
86
+ $ this ->getHeaderMap ($ customerEmail )
87
+ );
71
88
$ this ->assertArrayHasKey (CacheIdCalculator::CACHE_ID_HEADER , $ response ['headers ' ]);
72
89
$ cacheId = $ response ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ];
73
90
$ this ->assertTrue ((boolean )preg_match ('/^[0-9a-f]{64}$/i ' , $ cacheId ));
74
- $ groupCode = 'Retailer ' ;
75
- $ customer =
$ this ->
customerRepository ->
get (
'[email protected] ' );
76
- $ customerGroupId = $ this ->customerGroup ->load ($ groupCode , 'customer_group_code ' )->getId ();
91
+ $ customer = $ this ->customerRepository ->get ($ customerEmail );
92
+ $ customerGroupId = $ this ->customerGroup ->load ('Retailer ' , 'customer_group_code ' )->getId ();
77
93
// change the customer group of this customer from the default group
78
94
$ customer ->setGroupId ($ customerGroupId );
79
95
$ this ->customerRepository ->save ($ customer );
80
96
$ responseAfterCustomerGroupChange = $ this ->graphQlQueryWithResponseHeaders (
81
97
$ query ,
82
98
[],
83
99
'' ,
84
- $ this ->getHeaderMap ()
100
+ $ this ->getHeaderMap ($ customerEmail )
101
+ );
102
+ $ this ->assertArrayHasKey (
103
+ CacheIdCalculator::CACHE_ID_HEADER ,
104
+ $ responseAfterCustomerGroupChange ['headers ' ]
85
105
);
86
- $ this ->assertArrayHasKey (CacheIdCalculator::CACHE_ID_HEADER , $ responseAfterCustomerGroupChange ['headers ' ]);
87
106
$ cacheIdCustomerGroupChange = $ responseAfterCustomerGroupChange ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ];
88
- // Verify that the the cache id generated is a 64 character long
107
+ // Verify that the cache id generated is a 64 character long
89
108
$ this ->assertTrue ((boolean )preg_match ('/^[0-9a-f]{64}$/i ' , $ cacheId ));
90
109
// check that the cache ids generated before and after customer group changes are not equal
91
110
$ this ->assertNotEquals ($ cacheId , $ cacheIdCustomerGroupChange );
@@ -96,28 +115,28 @@ public function testCacheIdHeaderWithCustomerGroup()
96
115
$ query ,
97
116
[],
98
117
'' ,
99
- $ this ->getHeaderMap ()
118
+ $ this ->getHeaderMap ($ customerEmail )
100
119
);
101
- $ cacheIdDefaultCustomerGroup = $ responseDefaultCustomerGroup ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ];
102
120
//Verify that the cache id is same as original $cacheId
103
- $ this ->assertEquals ($ cacheIdDefaultCustomerGroup , $ cacheId );
121
+ $ this ->assertEquals (
122
+ $ responseDefaultCustomerGroup ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ],
123
+ $ cacheId
124
+ );
104
125
}
105
126
106
127
/**
107
128
* Authentication header map
108
129
*
109
130
* @param string $username
110
- * @param string $password
111
- *
112
131
* @return array
113
132
*
114
133
* @throws AuthenticationException
134
+ * @throws EmailNotConfirmedException
115
135
*/
116
- private function getHeaderMap (
string $ username = ' [email protected] ' , string $ password = ' password ' ):
array
136
+ private function getHeaderMap (string $ username ): array
117
137
{
118
- $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
138
+ $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , ' password ' );
119
139
120
140
return ['Authorization ' => 'Bearer ' . $ customerToken ];
121
141
}
122
-
123
142
}
0 commit comments