|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the Syndication_Logger class. |
| 4 | + * |
| 5 | + * @package Automattic\Syndication\Tests |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Syndication\Tests; |
| 9 | + |
| 10 | +use Yoast\WPTestUtils\WPIntegration\TestCase as WPIntegrationTestCase; |
| 11 | +use Syndication_Logger; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class LoggerTest |
| 15 | + * |
| 16 | + * @covers Syndication_Logger |
| 17 | + */ |
| 18 | +class LoggerTest extends WPIntegrationTestCase { |
| 19 | + |
| 20 | + /** |
| 21 | + * Test that logging to a post without existing logs works correctly. |
| 22 | + * |
| 23 | + * This tests the fix for the "Array to string conversion" error that occurred |
| 24 | + * when get_post_meta was called with the third parameter as true, returning |
| 25 | + * an empty string instead of an empty array when no meta existed. |
| 26 | + * |
| 27 | + * Uses log_post_error because log_post_info requires debug_level='info'. |
| 28 | + * |
| 29 | + * @covers Syndication_Logger::log_post_error |
| 30 | + */ |
| 31 | + public function test_log_to_post_without_existing_logs(): void { |
| 32 | + // Create a site post. |
| 33 | + $site_id = $this->factory()->post->create( |
| 34 | + array( |
| 35 | + 'post_type' => 'syn_site', |
| 36 | + 'post_status' => 'publish', |
| 37 | + ) |
| 38 | + ); |
| 39 | + |
| 40 | + // Log should work without throwing an error. |
| 41 | + Syndication_Logger::log_post_error( |
| 42 | + $site_id, |
| 43 | + 'test_status', |
| 44 | + 'Test message', |
| 45 | + null, |
| 46 | + array() |
| 47 | + ); |
| 48 | + |
| 49 | + // Verify the log was stored. |
| 50 | + $log = get_post_meta( $site_id, 'syn_log', true ); |
| 51 | + $this->assertIsArray( $log ); |
| 52 | + $this->assertNotEmpty( $log ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test that logging to a post with existing logs appends correctly. |
| 57 | + * |
| 58 | + * @covers Syndication_Logger::log_post_error |
| 59 | + */ |
| 60 | + public function test_log_to_post_with_existing_logs(): void { |
| 61 | + $site_id = $this->factory()->post->create( |
| 62 | + array( |
| 63 | + 'post_type' => 'syn_site', |
| 64 | + 'post_status' => 'publish', |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + // Create initial log entry. |
| 69 | + Syndication_Logger::log_post_error( |
| 70 | + $site_id, |
| 71 | + 'first_status', |
| 72 | + 'First message', |
| 73 | + null, |
| 74 | + array() |
| 75 | + ); |
| 76 | + |
| 77 | + // Add second log entry. |
| 78 | + Syndication_Logger::log_post_error( |
| 79 | + $site_id, |
| 80 | + 'second_status', |
| 81 | + 'Second message', |
| 82 | + null, |
| 83 | + array() |
| 84 | + ); |
| 85 | + |
| 86 | + // Verify both logs exist. |
| 87 | + $log = get_post_meta( $site_id, 'syn_log', true ); |
| 88 | + $this->assertIsArray( $log ); |
| 89 | + $this->assertCount( 2, $log ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that error logging works correctly. |
| 94 | + * |
| 95 | + * @covers Syndication_Logger::log_post_error |
| 96 | + */ |
| 97 | + public function test_log_post_error(): void { |
| 98 | + $site_id = $this->factory()->post->create( |
| 99 | + array( |
| 100 | + 'post_type' => 'syn_site', |
| 101 | + 'post_status' => 'publish', |
| 102 | + ) |
| 103 | + ); |
| 104 | + |
| 105 | + Syndication_Logger::log_post_error( |
| 106 | + $site_id, |
| 107 | + 'error_status', |
| 108 | + 'Error message', |
| 109 | + null, |
| 110 | + array() |
| 111 | + ); |
| 112 | + |
| 113 | + $log = get_post_meta( $site_id, 'syn_log', true ); |
| 114 | + $this->assertIsArray( $log ); |
| 115 | + $this->assertNotEmpty( $log ); |
| 116 | + |
| 117 | + // Error count should be tracked. |
| 118 | + $errors = get_post_meta( $site_id, 'syn_log_errors', true ); |
| 119 | + $this->assertEquals( 1, (int) $errors ); |
| 120 | + } |
| 121 | +} |
0 commit comments