1010use PHPUnit \Framework \TestCase ;
1111use Magento \Framework \ObjectManagerInterface ;
1212use Magento \Sales \Api \CreditmemoRepositoryInterface ;
13+ use Magento \Sales \Api \Data \CreditmemoInterface ;
14+ use Magento \Sales \Api \Data \OrderInterface ;
1315use Magento \TestFramework \Helper \Bootstrap ;
1416use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
1517use Magento \Store \Test \Fixture \Website as WebsiteFixture ;
1618use Magento \Store \Test \Fixture \Group as StoreGroupFixture ;
1719use Magento \Store \Test \Fixture \Store as StoreFixture ;
20+ use Magento \Store \Api \Data \StoreInterface ;
1821use Magento \Customer \Test \Fixture \Customer as CustomerFixture ;
19- use Magento \Quote \Test \Fixture \CustomerCart as CustomerCartFixture ;
2022use Magento \Checkout \Test \Fixture \SetBillingAddress as SetBillingAddressFixture ;
2123use Magento \Checkout \Test \Fixture \SetShippingAddress as SetShippingAddressFixture ;
2224use Magento \Checkout \Test \Fixture \SetDeliveryMethod as SetDeliveryMethodFixture ;
2325use Magento \Checkout \Test \Fixture \SetPaymentMethod as SetPaymentMethodFixture ;
2426use Magento \Checkout \Test \Fixture \PlaceOrder as PlaceOrderFixture ;
2527use Magento \Sales \Test \Fixture \Invoice as InvoiceFixture ;
2628use Magento \Sales \Test \Fixture \Creditmemo as CreditmemoFixture ;
27- use Magento \Quote \Test \Fixture \AddProductToCart as AddProductToCartFixture ;
2829use Magento \TestFramework \Fixture \DataFixture ;
2930use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
3031
3132/**
3233 * Integration test for complete workflow:
3334 * Create website, store, and store view with numeric names -> Place orders -> Create credit memo -> Verify grid display
3435 *
35- * @magentoDbIsolation enabled
36+ * @magentoDbIsolation disabled
3637 * @magentoAppIsolation enabled
3738 * @magentoConfigFixture default/general/country/allow US
3839 * @magentoConfigFixture default/general/country/default US
@@ -52,13 +53,6 @@ class StoreWithNumericNameCreditmemoWorkflowTest extends TestCase
5253 */
5354 private $ creditmemoRepository ;
5455
55- /**
56- * Test data constants
57- */
58- private const WEBSITE_NAME = '123test Website ' ;
59- private const STORE_GROUP_NAME = '123test Store Group ' ;
60- private const STORE_NAME = '123test Store View ' ;
61-
6256 /**
6357 * @inheritdoc
6458 */
@@ -91,56 +85,85 @@ protected function setUp(): void
9185 ],
9286 'test_store '
9387 ),
94- DataFixture(ProductFixture::class, ['sku ' => 'simple ' , 'price ' => 10 ], 'product ' ),
95- DataFixture(CustomerFixture::class, ['email ' => 'customer@123test.com ' ], 'customer ' ),
9688 DataFixture(
97- CustomerCartFixture ::class,
98- ['customer_id ' => '$customer.id$ ' , 'store_id ' => ' $test_store .id$ ' ],
99- 'cart '
89+ ProductFixture ::class,
90+ ['sku ' => 'simple ' , 'price ' => 10 , ' website_ids ' => [ 1 , ' $test_website .id$ '] ],
91+ 'product '
10092 ),
101- DataFixture(SetBillingAddressFixture::class, ['cart_id ' => '$cart.id$ ' ]),
102- DataFixture(SetShippingAddressFixture::class, ['cart_id ' => '$cart.id$ ' ]),
10393 DataFixture(
104- AddProductToCartFixture::class,
105- ['cart_id ' => '$cart.id$ ' , 'product_id ' => '$product.id$ ' , 'qty ' => 2 ]
94+ CustomerFixture::class,
95+ ['email ' => 'customer@123test.com ' , 'website_id ' => '$test_website.id$ ' ],
96+ 'customer '
10697 ),
107- DataFixture(
108- SetDeliveryMethodFixture::class,
109- ['cart_id ' => '$cart.id$ ' ]
110- ),
111- DataFixture(SetPaymentMethodFixture::class, ['cart_id ' => '$cart.id$ ' ]),
112- DataFixture(PlaceOrderFixture::class, ['cart_id ' => '$cart.id$ ' ], 'order ' ),
113- DataFixture(InvoiceFixture::class, ['order_id ' => '$order.id$ ' ], 'invoice ' ),
114- DataFixture(
115- CreditmemoFixture::class,
116- ['order_id ' => '$order.id$ ' , 'items ' => [['qty ' => 1 , 'product_id ' => '$product.id$ ' ]]],
117- 'creditmemo '
118- )
11998 ]
12099 public function testCompleteWorkflowWithNumericStoreNames (): void
121100 {
122- // Step 1: Get fixtures
101+ // Step 1: Get basic fixtures
123102 $ fixtures = DataFixtureStorageManager::getStorage ();
103+ /** @var StoreInterface $store */
124104 $ store = $ fixtures ->get ('test_store ' );
125- $ order = $ fixtures ->get ('order ' );
126- $ creditmemo = $ fixtures ->get ('creditmemo ' );
105+ $ customer = $ fixtures ->get ('customer ' );
106+ $ product = $ fixtures ->get ('product ' );
107+
108+ // Step 2: Create cart manually with correct store ID (CustomerCartFixture doesn't support store_id)
109+ $ cartManagement = $ this ->objectManager ->get ('Magento\Quote\Api\CartManagementInterface ' );
110+ $ cartRepository = $ this ->objectManager ->get ('Magento\Quote\Api\CartRepositoryInterface ' );
111+
112+ $ cartId = $ cartManagement ->createEmptyCartForCustomer ($ customer ->getId ());
113+ $ cart = $ cartRepository ->get ($ cartId );
114+ $ cart ->setStoreId ($ store ->getId ());
115+ $ cartRepository ->save ($ cart );
116+
117+ // Add product to cart
118+ $ cart ->addProduct ($ product , 2 );
119+ $ cartRepository ->save ($ cart );
120+
121+ // Step 3: Use fixtures for checkout process
122+ $ billingAddressFixture = $ this ->objectManager ->create (SetBillingAddressFixture::class);
123+ $ billingAddressFixture ->apply (['cart_id ' => $ cart ->getId ()]);
124+
125+ $ shippingAddressFixture = $ this ->objectManager ->create (SetShippingAddressFixture::class);
126+ $ shippingAddressFixture ->apply (['cart_id ' => $ cart ->getId ()]);
127127
128- // Step 2: Verify credit memo grid displays records
129- $ this ->verifyCreditMemoGridDisplaysRecords ($ creditmemo , $ order );
128+ $ deliveryMethodFixture = $ this ->objectManager ->create (SetDeliveryMethodFixture::class);
129+ $ deliveryMethodFixture ->apply (
130+ ['cart_id ' => $ cart ->getId (), 'carrier_code ' => 'flatrate ' , 'method_code ' => 'flatrate ' ]
131+ );
130132
131- // Step 3: Verify store name rendering in grid context (UI validation proves DB layer works)
132- $ this ->verifyStoreNameRenderingInGrid ($ creditmemo , $ store );
133+ $ paymentMethodFixture = $ this ->objectManager ->create (SetPaymentMethodFixture::class);
134+ $ paymentMethodFixture ->apply (['cart_id ' => $ cart ->getId ()]);
135+
136+ $ placeOrderFixture = $ this ->objectManager ->create (PlaceOrderFixture::class);
137+ $ order = $ placeOrderFixture ->apply (['cart_id ' => $ cart ->getId ()]);
138+
139+ $ invoiceFixture = $ this ->objectManager ->create (InvoiceFixture::class);
140+ $ invoiceFixture ->apply (['order_id ' => $ order ->getId ()]);
141+
142+ $ creditmemoFixture = $ this ->objectManager ->create (CreditmemoFixture::class);
143+ $ creditmemo = $ creditmemoFixture ->apply ([
144+ 'order_id ' => $ order ->getId (),
145+ 'items ' => [['qty ' => 1 , 'product_id ' => $ product ->getId ()]]
146+ ]);
147+
148+ $ this ->assertEquals ($ store ->getId (), $ creditmemo ->getStoreId (), 'Credit memo should be in test store ' );
149+
150+ // Step 4: Verify credit memo displays in grid page
151+ $ this ->verifyCreditMemoGridDisplaysRecords ($ creditmemo , $ order , $ store );
133152 }
134153
135154 /**
136155 * Verify credit memo grid displays records correctly
137156 *
138157 * @param CreditmemoInterface $creditmemo
139158 * @param OrderInterface $order
159+ * @param StoreInterface $store
140160 * @return void
141161 */
142- private function verifyCreditMemoGridDisplaysRecords ($ creditmemo , $ order ): void
143- {
162+ private function verifyCreditMemoGridDisplaysRecords (
163+ CreditmemoInterface $ creditmemo ,
164+ OrderInterface $ order ,
165+ StoreInterface $ store
166+ ): void {
144167 // Test credit memo retrieval by order ID
145168 $ creditmemoByOrder = $ this ->getCreditmemosByFilter ('order_id ' , $ order ->getId ());
146169 $ this ->assertCount (1 , $ creditmemoByOrder );
@@ -159,6 +182,39 @@ private function verifyCreditMemoGridDisplaysRecords($creditmemo, $order): void
159182 $ foundCreditmemoById = reset ($ creditmemoById );
160183 $ this ->assertEquals ($ creditmemo ->getId (), $ foundCreditmemoById ->getId ());
161184 $ this ->assertEquals ($ order ->getStoreId (), $ foundCreditmemoById ->getStoreId ());
185+
186+ // Explicitly verify the credit memo is created in "123test Store View"
187+ $ this ->assertEquals (
188+ $ store ->getId (),
189+ $ creditmemo ->getStoreId (),
190+ 'Credit memo should be created in the test store '
191+ );
192+ $ this ->assertEquals (
193+ '123test Store View ' ,
194+ $ store ->getName (),
195+ 'Test store should have numeric name '
196+ );
197+
198+ // Verify credit memo is visible when filtering by test store
199+ $ creditmemosByTestStore = $ this ->getCreditmemosByFilter ('store_id ' , $ store ->getId ());
200+ $ this ->assertGreaterThan (
201+ 0 ,
202+ count ($ creditmemosByTestStore ),
203+ 'Credit memo should be visible when filtering by test store '
204+ );
205+
206+ // Verify our specific credit memo is in the store-filtered results
207+ $ foundInStoreFilter = false ;
208+ foreach ($ creditmemosByTestStore as $ cm ) {
209+ if ($ cm ->getId () === $ creditmemo ->getId ()) {
210+ $ foundInStoreFilter = true ;
211+ break ;
212+ }
213+ }
214+ $ this ->assertTrue (
215+ $ foundInStoreFilter ,
216+ 'Credit memo should be found when filtering grid by "123test Store View" '
217+ );
162218 }
163219
164220 /**
@@ -168,82 +224,12 @@ private function verifyCreditMemoGridDisplaysRecords($creditmemo, $order): void
168224 * @param mixed $value
169225 * @return array
170226 */
171- private function getCreditmemosByFilter (string $ field , $ value ): array
227+ private function getCreditmemosByFilter (string $ field , mixed $ value ): array
172228 {
173229 $ searchCriteria = $ this ->objectManager ->get ('Magento\Framework\Api\SearchCriteriaBuilder ' )
174230 ->addFilter ($ field , $ value )
175231 ->create ();
176232
177233 return $ this ->creditmemoRepository ->getList ($ searchCriteria )->getItems ();
178234 }
179-
180- /**
181- * Verify store name rendering for credit memo
182- * Tests how store names with numeric prefixes are displayed
183- *
184- * @param CreditmemoInterface $creditmemo
185- * @param StoreInterface $store
186- * @return void
187- */
188- private function verifyStoreNameRenderingInGrid ($ creditmemo , $ store ): void
189- {
190- // Test store name rendering using the store renderer that would be used in grids
191- $ storeRenderer = $ this ->objectManager ->create ('Magento\Backend\Block\Widget\Grid\Column\Renderer\Store ' );
192-
193- // Create a mock grid column for the renderer
194- $ mockColumn = $ this ->objectManager ->create ('Magento\Framework\DataObject ' );
195- $ mockColumn ->setData ([
196- 'index ' => 'store_id ' ,
197- 'type ' => 'store ' ,
198- 'skipEmptyStoresLabel ' => false ,
199- 'skipAllStoresLabel ' => false
200- ]);
201-
202- $ storeRenderer ->setColumn ($ mockColumn );
203-
204- // Create a mock row data object representing a grid row
205- $ mockRow = $ this ->objectManager ->create ('Magento\Framework\DataObject ' );
206- $ mockRow ->setData ([
207- 'store_id ' => $ store ->getId (),
208- 'entity_id ' => $ creditmemo ->getId ()
209- ]);
210-
211- // Test rendering of store name
212- $ renderedOutput = $ storeRenderer ->render ($ mockRow );
213-
214- // Verify that the store name is properly rendered and includes our numeric store name
215- $ this ->assertIsString ($ renderedOutput );
216- $ this ->assertNotEmpty ($ renderedOutput );
217-
218- // The rendered output should contain the store hierarchy
219- $ this ->assertStringContainsString (self ::WEBSITE_NAME , $ renderedOutput ); // '123test Website'
220- $ this ->assertStringContainsString (self ::STORE_GROUP_NAME , $ renderedOutput ); // '123test Store Group'
221- $ this ->assertStringContainsString (self ::STORE_NAME , $ renderedOutput ); // '123test Store View'
222-
223- // Verify that numeric prefixes are properly handled (not truncated or misinterpreted)
224- $ this ->assertStringContainsString ('123test ' , $ renderedOutput );
225-
226- // Verify store hierarchy rendering with numeric names
227- $ lines = explode ('<br/> ' , $ renderedOutput );
228- $ this ->assertGreaterThan (0 , count ($ lines ));
229-
230- // Basic structure validation
231- $ flattenedOutput = strip_tags ($ renderedOutput );
232- $ this ->assertNotEmpty (trim ($ flattenedOutput ));
233-
234- // Test alternative rendering scenario - what happens with just store ID array
235- $ mockRowWithArray = $ this ->objectManager ->create ('Magento\Framework\DataObject ' );
236- $ mockRowWithArray ->setData ([
237- 'store_id ' => [$ store ->getId ()], // Array format
238- 'entity_id ' => $ creditmemo ->getId ()
239- ]);
240-
241- $ arrayRenderedOutput = $ storeRenderer ->render ($ mockRowWithArray );
242- $ this ->assertStringContainsString ('123test ' , $ arrayRenderedOutput );
243-
244- // Verify grid display integrity - no XSS or formatting issues with numeric store names
245- $ this ->assertStringNotContainsString ('<script ' , strtolower ($ renderedOutput ));
246- $ cleanOutput = strip_tags ($ renderedOutput );
247- $ this ->assertStringNotContainsString ('< ' , $ cleanOutput );
248- }
249235}
0 commit comments