Skip to content

Commit ddfc436

Browse files
chore: fix clean up test
1 parent f41c4e7 commit ddfc436

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

includes/admin/feedzy-rss-feeds-log.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -536,18 +536,21 @@ function ( $line ) {
536536
* Delete the log file if it is older than 14 days or if the size exceeds the maximum allowed size.
537537
*
538538
* @since 5.1.0
539-
* @return void
539+
* @return bool
540540
*/
541541
public function should_clean_logs() {
542-
if (
543-
! file_exists( $this->filepath ) ||
544-
self::DEFAULT_MAX_FILE_SIZE > $this->get_log_file_size() ||
545-
strtotime( '-14 days' ) < filemtime( $this->filepath )
546-
) {
547-
return;
542+
if ( ! file_exists( $this->filepath ) ) {
543+
return false;
548544
}
549-
550-
$this->delete_log_file();
545+
546+
$is_too_big = $this->get_log_file_size() > self::DEFAULT_MAX_FILE_SIZE;
547+
$is_too_old = filemtime( $this->filepath ) < strtotime( '-14 days' );
548+
549+
if ( $is_too_big || $is_too_old ) {
550+
return $this->delete_log_file();
551+
}
552+
553+
return false;
551554
}
552555

553556
/**
@@ -801,7 +804,7 @@ public function reset_log_stats() {
801804
* @since 5.1.0
802805
* @return string The full path to the log file.
803806
*/
804-
private function get_log_file_path() {
807+
public function get_log_file_path() {
805808
$upload_dir = wp_upload_dir();
806809
$log_dir = $upload_dir['basedir'] . '/feedzy-logs';
807810
return $log_dir . '/' . self::FILE_NAME . self::FILE_EXT;

tests/test-log.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)