@@ -214,6 +214,62 @@ public function test_perflab_maybe_set_object_cache_dropin_with_conflict() {
214214 $ this ->assertSame ( $ dummy_file_content , $ wp_filesystem ->get_contents ( WP_CONTENT_DIR . '/object-cache.php ' ) );
215215 }
216216
217+ public function test_perflab_maybe_set_object_cache_dropin_with_older_version () {
218+ global $ wp_filesystem ;
219+
220+ $ this ->set_up_mock_filesystem ();
221+
222+ $ latest_file_content = file_get_contents ( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php ' );
223+ $ older_file_content = preg_replace ( '/define\( \'PERFLAB_OBJECT_CACHE_DROPIN_VERSION \', (\d+) \)\;/ ' , "define( 'PERFLAB_OBJECT_CACHE_DROPIN_VERSION', 1 ); " , $ latest_file_content );
224+ $ wp_filesystem ->put_contents ( WP_CONTENT_DIR . '/object-cache.php ' , $ older_file_content );
225+
226+ // Simulate PL constant is set to the value from the older file.
227+ add_filter (
228+ 'perflab_object_cache_dropin_version ' ,
229+ static function () {
230+ return 1 ;
231+ }
232+ );
233+
234+ // Ensure older object-cache.php drop-in is present.
235+ $ this ->assertTrue ( $ wp_filesystem ->exists ( WP_CONTENT_DIR . '/object-cache.php ' ) );
236+ $ this ->assertSame ( $ older_file_content , $ wp_filesystem ->get_contents ( WP_CONTENT_DIR . '/object-cache.php ' ) );
237+
238+ // Run function to place drop-in and ensure it overrides the existing drop-in with the latest version.
239+ perflab_maybe_set_object_cache_dropin ();
240+ $ this ->assertTrue ( $ wp_filesystem ->exists ( WP_CONTENT_DIR . '/object-cache.php ' ) );
241+ $ this ->assertSame ( $ latest_file_content , $ wp_filesystem ->get_contents ( WP_CONTENT_DIR . '/object-cache.php ' ) );
242+ }
243+
244+ public function test_perflab_maybe_set_object_cache_dropin_with_latest_version () {
245+ global $ wp_filesystem ;
246+
247+ $ this ->set_up_mock_filesystem ();
248+
249+ $ latest_file_content = file_get_contents ( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php ' );
250+ $ wp_filesystem ->put_contents ( WP_CONTENT_DIR . '/object-cache.php ' , $ latest_file_content );
251+
252+ // Simulate PL constant is set to the value from the current file.
253+ $ this ->assertTrue ( (bool ) preg_match ( '/define\( \'PERFLAB_OBJECT_CACHE_DROPIN_VERSION \', (\d+) \)\;/ ' , $ latest_file_content , $ matches ) );
254+ $ latest_version = (int ) $ matches [1 ];
255+ add_filter (
256+ 'perflab_object_cache_dropin_version ' ,
257+ static function () use ( $ latest_version ) {
258+ return $ latest_version ;
259+ }
260+ );
261+
262+ // Ensure latest object-cache.php drop-in is present.
263+ $ this ->assertTrue ( $ wp_filesystem ->exists ( WP_CONTENT_DIR . '/object-cache.php ' ) );
264+ $ this ->assertSame ( $ latest_file_content , $ wp_filesystem ->get_contents ( WP_CONTENT_DIR . '/object-cache.php ' ) );
265+
266+ // Run function to place drop-in and ensure it doesn't attempt to replace the file.
267+ perflab_maybe_set_object_cache_dropin ();
268+ $ this ->assertTrue ( $ wp_filesystem ->exists ( WP_CONTENT_DIR . '/object-cache.php ' ) );
269+ $ this ->assertSame ( $ latest_file_content , $ wp_filesystem ->get_contents ( WP_CONTENT_DIR . '/object-cache.php ' ) );
270+ $ this ->assertFalse ( get_transient ( 'perflab_set_object_cache_dropin ' ) );
271+ }
272+
217273 public function test_perflab_object_cache_dropin_may_be_disabled_via_filter () {
218274 global $ wp_filesystem ;
219275
@@ -231,6 +287,20 @@ public function test_perflab_object_cache_dropin_may_be_disabled_via_filter() {
231287 $ this ->assertFalse ( $ wp_filesystem ->exists ( WP_CONTENT_DIR . '/object-cache.php ' ) );
232288 }
233289
290+ public function test_perflab_object_cache_dropin_version_matches_latest () {
291+ $ file_content = file_get_contents ( PERFLAB_PLUGIN_DIR_PATH . 'includes/server-timing/object-cache.copy.php ' );
292+
293+ // Get the version from the file header and the constant.
294+ $ this ->assertTrue ( (bool ) preg_match ( '/^ \* Version: (\d+)$/m ' , $ file_content , $ matches ) );
295+ $ file_header_version = (int ) $ matches [1 ];
296+ $ this ->assertTrue ( (bool ) preg_match ( '/define\( \'PERFLAB_OBJECT_CACHE_DROPIN_VERSION \', (\d+) \)\;/ ' , $ file_content , $ matches ) );
297+ $ file_constant_version = (int ) $ matches [1 ];
298+
299+ // Assert the versions are in sync.
300+ $ this ->assertSame ( PERFLAB_OBJECT_CACHE_DROPIN_LATEST_VERSION , $ file_header_version );
301+ $ this ->assertSame ( PERFLAB_OBJECT_CACHE_DROPIN_LATEST_VERSION , $ file_constant_version );
302+ }
303+
234304 private function set_up_mock_filesystem () {
235305 global $ wp_filesystem ;
236306
0 commit comments