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