77
88namespace Magento \Quote \Model ;
99
10+ use Magento \Customer \Api \CustomerRepositoryInterface ;
1011use Magento \Framework \Api \FilterBuilder ;
1112use Magento \Framework \Api \SearchCriteria ;
1213use Magento \Framework \Api \SearchCriteriaBuilder ;
@@ -85,6 +86,21 @@ class QuoteRepositoryTest extends TestCase
8586 */
8687 private $ quote ;
8788
89+ /**
90+ * @var \Magento\Quote\Model\QuoteFactory
91+ */
92+ private $ quoteFactorys ;
93+
94+ /**
95+ * @var \Magento\Store\Model\Store
96+ */
97+ private $ store ;
98+
99+ /**
100+ * @var CustomerRepositoryInterface
101+ */
102+ private $ customerRepository ;
103+
88104 /**
89105 * @inheritdoc
90106 */
@@ -101,6 +117,9 @@ protected function setUp(): void
101117 $ this ->addressFactory = $ this ->objectManager ->get (AddressInterfaceFactory::class);
102118 $ this ->quoteFactory = $ this ->objectManager ->get (CartInterfaceFactory::class);
103119 $ this ->itemFactory = $ this ->objectManager ->get (CartItemInterfaceFactory::class);
120+ $ this ->quoteFactorys = $ this ->objectManager ->get (\Magento \Quote \Model \QuoteFactory::class);
121+ $ this ->store = $ this ->objectManager ->get (\Magento \Store \Model \Store::class);
122+ $ this ->customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
104123 }
105124
106125 /**
@@ -278,4 +297,88 @@ private function performAssertions(CartSearchResultsInterface $searchResult): vo
278297 $ this ->assertEquals ($ expectedExtensionAttributes ['lastname ' ], $ testAttribute ->getLastName ());
279298 $ this ->assertEquals ($ expectedExtensionAttributes ['email ' ], $ testAttribute ->getEmail ());
280299 }
300+
301+ /**
302+ * @magentoDataFixture Magento/Customer/_files/customer.php
303+ * @magentoDataFixture Magento/Checkout/_files/quote_with_simple_product.php
304+ * @magentoDbIsolation disabled
305+ * @return void
306+ * @throws \Exception
307+ */
308+ public function testDeleteAllQuotesOnStoreViewDeletion (): void
309+ {
310+ $ storeData = [
311+ [
312+ 'code ' => 'store1 ' ,
313+ 'website_id ' => 1 ,
314+ 'group_id ' => 1 ,
315+ 'name ' => 'Store 1 ' ,
316+ 'sort_order ' => 0 ,
317+ 'is_active ' => 1 ,
318+ ],
319+ [
320+ 'code ' => 'store2 ' ,
321+ 'website_id ' => 1 ,
322+ 'group_id ' => 1 ,
323+ 'name ' => 'Store 2 ' ,
324+ 'sort_order ' => 1 ,
325+ 'is_active ' => 1 ,
326+ ],
327+ ];
328+
329+ foreach ($ storeData as $ storeInfo ) {
330+ $ this ->objectManager ->create (\Magento \Store \Model \Store::class)
331+ ->setData ($ storeInfo )
332+ ->save ();
333+ }
334+
335+ // Fetching the store id
336+ $ firstStoreId = $ this ->store ->load ('store1 ' )->getId ();
337+ $ secondStoreId = $ this ->store ->load ('store2 ' )->getId ();
338+
339+ // Create a quote for guest user with store id 2
340+ $ quote = $ this ->quoteFactorys ->create ();
341+ $ quote ->setStoreId ($ firstStoreId );
342+ $ quote ->save ();
343+
344+ // Assert that quote is created successfully.
345+ $ this ->assertNotNull ($ quote ->getId ());
346+
347+ // Create a quote for guest user with store id 3
348+ $ secondQuote = $ this ->quoteFactorys ->create ();
349+ $ secondQuote ->setStoreId ($ secondStoreId );
350+ $ secondQuote ->save ();
351+
352+ // Assert that second quote is created successfully.
353+ $ this ->assertNotNull ($ secondQuote ->getId ());
354+
355+ // load customer by id
356+ $ customer = $ this ->customerRepository ->getById (1 );
357+
358+ // Create a quote for customer with store id 3
359+ $ thirdQuote = $ this ->quoteFactorys ->create ();
360+ $ thirdQuote ->setStoreId ($ secondStoreId );
361+ $ thirdQuote ->setCustomer ($ customer );
362+ $ thirdQuote ->save ();
363+
364+ // Loading the second store from the data fixture
365+ $ this ->store ->load ('store2 ' , 'code ' );
366+ /** @var \Magento\TestFramework\Helper\Bootstrap $registry */
367+ $ registry = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
368+ \Magento \Framework \Registry::class
369+ );
370+ $ registry ->unregister ('isSecureArea ' );
371+ $ registry ->register ('isSecureArea ' , true );
372+
373+ // Deleting the second store.
374+ $ this ->store ->delete ();
375+
376+ // asserting that quote associated with guest user is also deleted when store is deleted
377+ $ afterDeletionQuote = $ this ->quoteFactorys ->create ()->load ($ secondQuote ->getId ());
378+ $ this ->assertNull ($ afterDeletionQuote ->getId ());
379+
380+ // asserting that quote associated with customer is also deleted when store is deleted
381+ $ afterDeletionQuote = $ this ->quoteFactorys ->create ()->load ($ thirdQuote ->getId ());
382+ $ this ->assertNull ($ afterDeletionQuote ->getId ());
383+ }
281384}
0 commit comments