Skip to content

Commit 53ae779

Browse files
chore: add unit test
1 parent f4d9fab commit 53ae779

File tree

3 files changed

+519
-18
lines changed

3 files changed

+519
-18
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,15 @@ private function tail_file( $lines = 50 ) {
490490
}
491491
$line = fgets( $handle );
492492
if ( false !== $line ) {
493-
$text[ $lines - $line_counter - 1 ] = $line;
493+
$text[] = $line;
494494
}
495495
if ( $beginning ) {
496496
break;
497497
}
498498
}
499499
fclose( $handle );
500-
501-
return array_reverse( $text );
500+
501+
return $text;
502502
} catch ( Throwable $e ) {
503503
return new WP_Error( 'error_reading_log_file', $e->getMessage() );
504504
}
@@ -529,7 +529,7 @@ function ( $line ) {
529529
}
530530
);
531531

532-
return array_slice( $lines, -$lines_count );
532+
return array_reverse( array_slice( $lines, -$lines_count ) );
533533
}
534534

535535
/**
@@ -846,7 +846,7 @@ public function try_accumulate_error_message( $message ) {
846846
return;
847847
}
848848

849-
if ( 200 >= count( $this->error_messages_accumulator ) ) {
849+
if ( 200 <= count( $this->error_messages_accumulator ) ) {
850850
return;
851851
}
852852

tests/test-image-import.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,26 @@ public function test_image_import_url() {
2323
$try_save_featured_image->setAccessible( true );
2424

2525
// Check that NON-IMAGE URL returns invalid
26-
$import_errors = array();
2726
$import_info = array();
28-
$arguments = array( 'a random string', 0, 'Post Title', &$import_errors, &$import_info, array() );
27+
$arguments = array( 'a random string', 0, 'Post Title', $import_info, array() );
2928
$response = $try_save_featured_image->invokeArgs( $feedzy, $arguments );
3029

3130
$this->assertFalse( $response );
3231

33-
$this->assertTrue( count( $import_errors ) > 0 );
34-
$this->assertEquals( 'Invalid Featured Image URL: a random string', $import_errors[0] );
35-
32+
// Check that error was logged for invalid URL
33+
$logger = Feedzy_Rss_Feeds_Log::get_instance();
34+
$recent_logs = $logger->get_recent_logs( 5, 'ERROR' );
35+
$this->assertNotEmpty( $recent_logs );
36+
37+
// Find the error log for invalid image URL
38+
$found_error = false;
39+
foreach ( $recent_logs as $log ) {
40+
if ( strpos( $log['message'], 'Invalid image URL' ) !== false && strpos( $log['message'], 'a random string' ) !== false ) {
41+
$found_error = true;
42+
break;
43+
}
44+
}
45+
$this->assertTrue( $found_error, 'Expected error log for invalid image URL not found' );
3646

3747
// For the next test, we will use a valid URL, but the image does not exist. We will check that the error is logged and is the expected one.
3848
add_filter( 'themeisle_log_event', function( $product, $message, $type, $file, $line ) {
@@ -41,23 +51,19 @@ public function test_image_import_url() {
4151
}
4252
}, 10, 5 );
4353

44-
$import_errors = array();
4554
$import_info = array();
46-
$arguments = array( 'https://example.com/path_to_image/image.jpeg', 0, 'Post Title', &$import_errors, &$import_info, array() );
55+
$arguments = array( 'https://example.com/path_to_image/image.jpeg', 0, 'Post Title', $import_info, array() );
4756
$response = $try_save_featured_image->invokeArgs( $feedzy, $arguments );
4857

49-
// expected response is false because the image does not exist, but the URL is valid so no $import_errors should be set.
58+
// expected response is false because the image does not exist, but the URL is valid so no errors should be logged for URL validation
5059
$this->assertFalse( $response );
51-
$this->assertTrue( empty( $import_errors ) );
5260

53-
$import_errors = array();
5461
$import_info = array();
55-
$arguments = array( 'https://example.com/path_to_image/image w space in name.jpeg', 0, 'Post Title', &$import_errors, &$import_info, array() );
62+
$arguments = array( 'https://example.com/path_to_image/image w space in name.jpeg', 0, 'Post Title', $import_info, array() );
5663
$response = $try_save_featured_image->invokeArgs( $feedzy, $arguments );
5764

58-
// expected response is false because the image does not exist, but the URL is valid so no $import_errors should be set.
65+
// expected response is false because the image does not exist, but the URL is valid so no errors should be logged for URL validation
5966
$this->assertFalse( $response );
60-
$this->assertTrue( empty( $import_errors ) );
6167
}
6268

6369
public function test_import_image_special_characters() {

0 commit comments

Comments
 (0)