@@ -462,13 +462,38 @@ public function test_should_clean_logs() {
462462 // Create a log file
463463 Feedzy_Rss_Feeds_Log::error ( 'Test error ' );
464464 $ this ->assertTrue ( $ this ->logger ->log_file_exists () );
465-
465+
466+ $ file_path = $ this ->logger ->get_log_file_path ();
467+
468+ // Simulate an old log file by setting its modification time far in the past
469+ $ this ->assertTrue ( file_exists ( $ file_path ), 'Log file should exist before aging it ' );
470+ $ old_time = time () - ( defined ( 'YEAR_IN_SECONDS ' ) ? YEAR_IN_SECONDS : 365 * 24 * 60 * 60 );
471+ $ this ->assertTrue ( touch ( $ file_path , $ old_time ), 'Should be able to update mtime to an old timestamp ' );
472+ clearstatcache ( true , $ file_path );
473+
474+ // Old file should be cleaned
475+ $ this ->logger ->should_clean_logs ();
476+ $ this ->assertFalse ( $ this ->logger ->log_file_exists (), 'Old log file should be cleaned (deleted) ' );
477+
478+ // Create a fresh small log again
479+ Feedzy_Rss_Feeds_Log::error ( 'Recent small error ' );
480+ $ this ->assertTrue ( $ this ->logger ->log_file_exists (), 'Recent log file should exist ' );
481+
466482 // File is small and recent, should not be cleaned
467483 $ this ->logger ->should_clean_logs ();
468- $ this ->assertTrue ( $ this ->logger ->log_file_exists () );
469-
470- // Manually modify file time to be old (would require file manipulation)
471- // This is a simplified test - in practice you'd need to mock filemtime()
484+ $ this ->assertTrue ( $ this ->logger ->log_file_exists (), 'Recent small log should not be cleaned ' );
485+
486+ // Enlarge the log to exceed the max size and ensure it gets cleaned
487+ $ fp = fopen ( $ file_path , 'r+ ' );
488+ $ this ->assertNotFalse ( $ fp , 'Should be able to open log file for resizing ' );
489+ $ target_size = Feedzy_Rss_Feeds_Log::DEFAULT_MAX_FILE_SIZE + 1 ;
490+ $ this ->assertTrue ( ftruncate ( $ fp , $ target_size ), 'Should be able to enlarge log file ' );
491+ fclose ( $ fp );
492+ clearstatcache ( true , $ file_path );
493+ $ this ->assertGreaterThan ( Feedzy_Rss_Feeds_Log::DEFAULT_MAX_FILE_SIZE , filesize ( $ file_path ), 'Log size should exceed max size ' );
494+
495+ $ this ->logger ->should_clean_logs ();
496+ $ this ->assertFalse ( $ this ->logger ->log_file_exists (), 'Oversized log file should be cleaned (deleted) ' );
472497 }
473498
474499 /**
0 commit comments