PluginProbe
Packeta / 2.3.2
Packeta v2.3.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Integration / Module / Checkout / CheckoutStorageTest.php

CheckoutStorageTest.php in Packeta 2.3.2, at tests/Integration/Module/Checkout/CheckoutStorageTest.php

244 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Tests\Integration\Module\Checkout;
6
7 use Packetery\Module\Checkout\CheckoutStorage;
8 use Packetery\Module\Framework\WcAdapter;
9 use Packetery\Module\Framework\WpAdapter;
10 use Packetery\Module\Order\Attribute as Attr;
11 use Packetery\Nette\Http\Request;
12 use Packetery\Nette\Http\UrlScript;
13 use ReflectionClass;
14 use Tests\Integration\AbstractIntegrationTestCase;
15
16 class CheckoutStorageTest extends AbstractIntegrationTestCase {
17 /**
18 * Allows setting raw POST payload (including non-array) to exercise branches when POST is not an array.
19 *
20 * @param Request $request
21 * @param mixed $post
22 */
23 private static function setRequestRawPost( Request $request, mixed $post ): void {
24 $reflection = new ReflectionClass( $request );
25 $property = $reflection->getProperty( 'post' );
26 $property->setAccessible( true );
27 $property->setValue( $request, $post );
28 }
29
30 private static function buildRequest( array $post = [] ): Request {
31 return new Request( new UrlScript( '/', '/' ), $post );
32 }
33
34 /**
35 * Builds request and sets raw POST value (can be scalar or object).
36 *
37 * @param mixed $post
38 *
39 * @return Request
40 */
41 private static function buildRequestWithRawPost( mixed $post ): Request {
42 $request = new Request( new UrlScript( '/', '/' ) );
43 self::setRequestRawPost( $request, $post );
44
45 return $request;
46 }
47
48 private function invokeTransientName( CheckoutStorage $storage ): string {
49 $reflection = new ReflectionClass( $storage );
50 $method = $reflection->getMethod( 'getTransientNamePacketaCheckoutData' );
51 $method->setAccessible( true );
52
53 return (string) $method->invoke( $storage );
54 }
55
56 public function testTransientStorage(): void {
57 /** @var CheckoutStorage $checkoutStorage */
58 $checkoutStorage = $this->container->getByType( CheckoutStorage::class );
59
60 $dummyData = [
61 'dummyKey' => [ 'dummyDataKey' => 'dummyDataValue' ],
62 ];
63
64 $checkoutStorage->setTransient( $dummyData );
65 $this->assertSame( $dummyData, $checkoutStorage->getFromTransient() );
66
67 $checkoutStorage->deleteTransient();
68 $this->assertFalse( $checkoutStorage->getFromTransient() );
69 }
70
71 public static function enrichmentProvider(): array {
72 $ratePp = 'dummyRate1';
73 $rateHd = 'dummyRate2';
74 $rateCar = 'dummyRate3';
75 $rateCarrierOnly = 'dummyRate4';
76
77 return [
78 'pickup point' => [
79 'data' => [
80 $ratePp => [
81 Attr::POINT_ID => 'PP123',
82 Attr::POINT_NAME => 'Point Name',
83 Attr::POINT_CITY => 'City',
84 Attr::POINT_ZIP => '12345',
85 Attr::POINT_STREET => 'Street 1',
86 Attr::POINT_PLACE => 'Business XYZ',
87 Attr::CARRIER_ID => 'carrier_1',
88 Attr::POINT_URL => 'dummyUrl',
89 ],
90 ],
91 'rateId' => $ratePp,
92 'attributes' => Attr::$pickupPointAttributes,
93 'additionalKeys' => [ Attr::CARRIER_ID ],
94 ],
95 'home delivery' => [
96 'data' => [
97 $rateHd => [
98 Attr::ADDRESS_IS_VALIDATED => '1',
99 Attr::ADDRESS_HOUSE_NUMBER => '10',
100 Attr::ADDRESS_STREET => 'Home Street',
101 Attr::ADDRESS_CITY => 'Praha',
102 Attr::ADDRESS_POST_CODE => '19000',
103 Attr::ADDRESS_COUNTY => 'Praha',
104 Attr::ADDRESS_COUNTRY => 'CZ',
105 Attr::ADDRESS_LATITUDE => '50.087',
106 Attr::ADDRESS_LONGITUDE => '14.421',
107 ],
108 ],
109 'rateId' => $rateHd,
110 'attributes' => Attr::$homeDeliveryAttributes,
111 'additionalKeys' => [],
112 ],
113 'car delivery' => [
114 'data' => [
115 $rateCar => [
116 Attr::CAR_DELIVERY_ID => 'cd_123',
117 Attr::ADDRESS_STREET => 'Car Street',
118 Attr::ADDRESS_HOUSE_NUMBER => '1',
119 Attr::ADDRESS_CITY => 'City',
120 Attr::ADDRESS_POST_CODE => '19000',
121 Attr::ADDRESS_COUNTRY => 'CZ',
122 Attr::EXPECTED_DELIVERY_FROM => '2025-01-01',
123 Attr::EXPECTED_DELIVERY_TO => '2025-01-02',
124 ],
125 ],
126 'rateId' => $rateCar,
127 'attributes' => Attr::$carDeliveryAttributes,
128 'additionalKeys' => [],
129 ],
130 'hd carrier id only' => [
131 'data' => [
132 $rateCarrierOnly => [
133 Attr::CARRIER_ID => 'dummyCarrier',
134 ],
135 ],
136 'rateId' => $rateCarrierOnly,
137 'attributes' => [ [ 'name' => Attr::CARRIER_ID ] ],
138 'additionalKeys' => [],
139 ],
140 ];
141 }
142
143 /**
144 * @dataProvider enrichmentProvider
145 */
146 public function testEnrichmentFromTransientCoversAllVariants( array $data, string $rateId, array $attributes, array $additionalKeys ): void {
147 $storage = new CheckoutStorage( self::buildRequest(), new WpAdapter(), new WcAdapter() );
148 $storage->setTransient( $data );
149
150 $updatedData = $storage->getPostDataIncludingStoredData( $rateId );
151 foreach ( $attributes as $attributeProperties ) {
152 $name = $attributeProperties['name'];
153 $this->assertArrayHasKey( $name, $updatedData );
154 $this->assertSame( $data[ $rateId ][ $name ], $updatedData[ $name ] );
155 }
156 foreach ( $additionalKeys as $additionalKey ) {
157 $this->assertArrayHasKey( $additionalKey, $updatedData );
158 $this->assertSame( $data[ $rateId ][ $additionalKey ], $updatedData[ $additionalKey ] );
159 }
160 }
161
162 public static function passthroughProvider(): array {
163 return [
164 'saved null' => [
165 'savedData' => null,
166 'postData' => [ 'a' => 'b' ],
167 'chosenRate' => 'rateA',
168 ],
169 'saved array without chosen' => [
170 'savedData' => [ 'other' => [] ],
171 'postData' => [ 'x' => 'y' ],
172 'chosenRate' => 'rateB',
173 ],
174 'saved chosen not array' => [
175 'savedData' => [ 'rateC' => 'unexpectedString' ],
176 'postData' => [ 'z' => 'w' ],
177 'chosenRate' => 'rateC',
178 ],
179 ];
180 }
181
182 /**
183 * @dataProvider passthroughProvider
184 */
185 public function testPassthroughReturnsPostDataWhenSavedDataInvalid( $savedData, array $postData, string $chosenRate ): void {
186 $checkoutStorage = new CheckoutStorage( self::buildRequest( $postData ), new WpAdapter(), new WcAdapter() );
187 if ( $savedData !== null ) {
188 $checkoutStorage->setTransient( $savedData );
189 }
190 $result = $checkoutStorage->getPostDataIncludingStoredData( $chosenRate );
191 $this->assertSame( $postData, $result );
192 }
193
194 public static function invalidCombinationProvider(): array {
195 return [
196 'empty post array and saved array' => [
197 'request' => self::buildRequest(),
198 'setEmptySaved' => true,
199 'chosenRate' => 'nonexistentRate',
200 ],
201 'non array post and no saved' => [
202 'request' => self::buildRequestWithRawPost( 'dummyString' ),
203 'setEmptySaved' => false,
204 'chosenRate' => 'rateX',
205 ],
206 ];
207 }
208
209 /**
210 * @dataProvider invalidCombinationProvider
211 */
212 public function testInvalidCombinationLogsWarningAndReturnsEmpty( Request $request, bool $setEmptySaved, string $chosenRate ): void {
213 $checkoutStorage = new CheckoutStorage( $request, new WpAdapter(), new WcAdapter() );
214
215 if ( $setEmptySaved ) {
216 $checkoutStorage->setTransient( [] );
217 }
218
219 $updatedData = $checkoutStorage->getPostDataIncludingStoredData( $chosenRate, 123 );
220 $this->assertSame( [], $updatedData, 'Expected empty array on invalid combination.' );
221 }
222
223 public function testGuestVsLoggedInTransientKeys(): void {
224 wp_logout();
225
226 $checkoutStorage = new CheckoutStorage( self::buildRequest(), new WpAdapter(), new WcAdapter() );
227 $guestKey = $this->invokeTransientName( $checkoutStorage );
228
229 $userEmail = 'test@example.com';
230 $wpUserId = wp_create_user( 'testuser', 'testpassword', $userEmail );
231 if ( is_wp_error( $wpUserId ) ) {
232 $wpUser = get_user_by( 'email', $userEmail );
233 $wpUserId = $wpUser->ID;
234 }
235 wp_set_current_user( $wpUserId );
236 wp_set_auth_cookie( $wpUserId );
237
238 $loggedKey = $this->invokeTransientName( $checkoutStorage );
239 $this->assertNotSame( $guestKey, $loggedKey, 'Transient keys must differ for guest vs logged-in.' );
240
241 wp_logout();
242 }
243 }
244