1919use Ymir \Plugin \Tests \Mock \ContentDeliveryNetworkPageCacheClientInterfaceMockTrait ;
2020use Ymir \Plugin \Tests \Mock \EventManagerMockTrait ;
2121use Ymir \Plugin \Tests \Mock \FunctionMockTrait ;
22+ use Ymir \Plugin \Tests \Mock \WPPostMockTrait ;
2223use Ymir \Plugin \Tests \Mock \WPTermMockTrait ;
2324use Ymir \Plugin \Tests \Unit \TestCase ;
2425
@@ -27,6 +28,7 @@ class WooCommerceSubscriberTest extends TestCase
2728 use ContentDeliveryNetworkPageCacheClientInterfaceMockTrait;
2829 use EventManagerMockTrait;
2930 use FunctionMockTrait;
31+ use WPPostMockTrait;
3032 use WPTermMockTrait;
3133
3234 public function testChangeLogDirectoryWhenLogDirectoryIsAStringThatDoesntStartWithThePublicCloudStorageProtocol ()
@@ -48,6 +50,55 @@ public function testChangeLogDirectoryWhenLogDirectoryIsntAString()
4850 $ this ->assertSame ($ logDirectory , $ this ->createSubscriber ()->changeLogDirectory ($ logDirectory ));
4951 }
5052
53+ public function testClearCacheOnProductSave ()
54+ {
55+ $ wp_is_post_autosave = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wp_is_post_autosave ' );
56+ $ wp_is_post_autosave ->expects ($ this ->once ())
57+ ->with (123 )
58+ ->willReturn (false );
59+
60+ $ wp_is_post_revision = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wp_is_post_revision ' );
61+ $ wp_is_post_revision ->expects ($ this ->once ())
62+ ->with (123 )
63+ ->willReturn (false );
64+
65+ $ function_exists = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'function_exists ' );
66+ $ function_exists ->expects ($ this ->once ())
67+ ->with ('wc_get_page_permalink ' )
68+ ->willReturn (true );
69+
70+ $ get_permalink = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'get_permalink ' );
71+ $ get_permalink ->expects ($ this ->once ())
72+ ->with (123 )
73+ ->willReturn ('https://foo.com/product/bar/ ' );
74+
75+ $ wc_get_page_permalink = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wc_get_page_permalink ' );
76+ $ wc_get_page_permalink ->expects ($ this ->once ())
77+ ->with ('shop ' )
78+ ->willReturn ('https://foo.com/shop/ ' );
79+
80+ $ get_the_terms = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'get_the_terms ' );
81+ $ get_the_terms ->expects ($ this ->exactly (2 ))
82+ ->willReturn ([]);
83+
84+ $ pageCacheClient = $ this ->getContentDeliveryNetworkPageCacheClientInterfaceMock ();
85+ $ pageCacheClient ->expects ($ this ->once ())
86+ ->method ('clearUrls ' )
87+ ->with ($ this ->callback (function ($ urls ) {
88+ $ this ->assertSame ([
89+ 'https://foo.com/product/bar/ ' ,
90+ 'https://foo.com/shop/* ' ,
91+ ], $ urls ->all ());
92+
93+ return true ;
94+ }));
95+
96+ $ post = $ this ->getWPPostMock ();
97+
98+ $ this ->createSubscriber ('https://foo.com ' , '' , false , ['invalidation_enabled ' => true ], $ pageCacheClient )
99+ ->clearCacheOnProductSave (123 , $ post , true );
100+ }
101+
51102 public function testClearCacheOnProductUpdateWhenClearAllOnPostUpdateIsDisabled ()
52103 {
53104 $ function_exists = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'function_exists ' );
@@ -120,6 +171,60 @@ public function testClearCacheOnProductUpdateWhenInvalidationIsDisabled()
120171 $ this ->createSubscriber ('https://foo.com ' , '' , false , ['invalidation_enabled ' => false ], $ pageCacheClient )->clearCacheOnProductUpdate (123 );
121172 }
122173
174+ public function testClearCacheOnProductVariationSave ()
175+ {
176+ $ wp_is_post_autosave = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wp_is_post_autosave ' );
177+ $ wp_is_post_autosave ->expects ($ this ->once ())
178+ ->with (456 )
179+ ->willReturn (false );
180+
181+ $ wp_is_post_revision = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wp_is_post_revision ' );
182+ $ wp_is_post_revision ->expects ($ this ->once ())
183+ ->with (456 )
184+ ->willReturn (false );
185+
186+ $ wp_get_post_parent_id = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wp_get_post_parent_id ' );
187+ $ wp_get_post_parent_id ->expects ($ this ->once ())
188+ ->with (456 )
189+ ->willReturn (123 );
190+
191+ $ function_exists = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'function_exists ' );
192+ $ function_exists ->expects ($ this ->once ())
193+ ->with ('wc_get_page_permalink ' )
194+ ->willReturn (true );
195+
196+ $ get_permalink = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'get_permalink ' );
197+ $ get_permalink ->expects ($ this ->once ())
198+ ->with (123 )
199+ ->willReturn ('https://foo.com/product/bar/ ' );
200+
201+ $ wc_get_page_permalink = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'wc_get_page_permalink ' );
202+ $ wc_get_page_permalink ->expects ($ this ->once ())
203+ ->with ('shop ' )
204+ ->willReturn ('https://foo.com/shop/ ' );
205+
206+ $ get_the_terms = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'get_the_terms ' );
207+ $ get_the_terms ->expects ($ this ->exactly (2 ))
208+ ->willReturn ([]);
209+
210+ $ pageCacheClient = $ this ->getContentDeliveryNetworkPageCacheClientInterfaceMock ();
211+ $ pageCacheClient ->expects ($ this ->once ())
212+ ->method ('clearUrls ' )
213+ ->with ($ this ->callback (function ($ urls ) {
214+ $ this ->assertSame ([
215+ 'https://foo.com/product/bar/ ' ,
216+ 'https://foo.com/shop/* ' ,
217+ ], $ urls ->all ());
218+
219+ return true ;
220+ }));
221+
222+ $ post = $ this ->getWPPostMock ();
223+
224+ $ this ->createSubscriber ('https://foo.com ' , '' , false , ['invalidation_enabled ' => true ], $ pageCacheClient )
225+ ->clearCacheOnProductVariationSave (456 , $ post , true );
226+ }
227+
123228 public function testClearCacheOnProductVariationUpdate ()
124229 {
125230 $ function_exists = $ this ->getFunctionMock ($ this ->getNamespace (WooCommerceSubscriber::class), 'function_exists ' );
@@ -312,6 +417,8 @@ public function testGetSubscribedEvents()
312417 $ subscribedEvents = [
313418 'transient_woocommerce_blocks_asset_api_script_data ' => 'fixAssetUrlPathsInCachedScriptData ' ,
314419 'transient_woocommerce_blocks_asset_api_script_data_ssl ' => 'fixAssetUrlPathsInCachedScriptData ' ,
420+ 'save_post_product ' => ['clearCacheOnProductSave ' , 20 , 3 ],
421+ 'save_post_product_variation ' => ['clearCacheOnProductVariationSave ' , 20 , 3 ],
315422 'woocommerce_csv_importer_check_import_file_path ' => 'disableCheckImportFilePath ' ,
316423 'woocommerce_log_directory ' => 'changeLogDirectory ' ,
317424 'woocommerce_product_csv_importer_check_import_file_path ' => 'disableCheckImportFilePath ' ,
0 commit comments