'debug' ) ); $handler->handle( time(), 'info', 'this is a message', array( 'source' => 'unit-tests', '_legacy' => true ) ); $this->assertStringMatchesFormat( '%d-%d-%d @ %d:%d:%d - %s', $this->read_content( 'unit-tests' ) ); $this->assertStringEndsWith( ' - this is a message' . PHP_EOL, $this->read_content( 'unit-tests' ) ); } /** * Test clear(). * * @since 3.0.0 */ public function test_clear() { $handler = new WC_Log_Handler_File(); $log_name = '_test_clear'; $handler->handle( time(), 'debug', 'debug', array( 'source' => $log_name ) ); $handler->clear( $log_name ); $this->assertEquals( '', $this->read_content( $log_name ) ); } /** * Test remove(). * * @since 3.0.0 */ public function test_remove() { $handler = new WC_Log_Handler_File(); $log_name = '_test_remove'; $handler->handle( time(), 'debug', 'debug', array( 'source' => $log_name ) ); $handler->remove( $log_name ); $this->assertFileNotExists( WC_Log_Handler_File::get_log_file_path( $log_name ) ); } /** * Test handle writes to default file correctly. * * @since 3.0.0 */ public function test_writes_file() { $handler = new WC_Log_Handler_File(); $time = time(); $handler->handle( $time, 'debug', 'debug', array() ); $handler->handle( $time, 'info', 'info', array() ); $handler->handle( $time, 'notice', 'notice', array() ); $handler->handle( $time, 'warning', 'warning', array() ); $handler->handle( $time, 'error', 'error', array() ); $handler->handle( $time, 'critical', 'critical', array() ); $handler->handle( $time, 'alert', 'alert', array() ); $handler->handle( $time, 'emergency', 'emergency', array() ); $log_content = $this->read_content( 'log' ); $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content ); } /** * Test 'source' context determines log file. * * @since 3.0.0 */ public function test_log_file_source() { $handler = new WC_Log_Handler_File(); $time = time(); $context_source = array( 'source' => 'unit-tests' ); $handler->handle( $time, 'debug', 'debug', $context_source ); $handler->handle( $time, 'info', 'info', $context_source ); $handler->handle( $time, 'notice', 'notice', $context_source ); $handler->handle( $time, 'warning', 'warning', $context_source ); $handler->handle( $time, 'error', 'error', $context_source ); $handler->handle( $time, 'critical', 'critical', $context_source ); $handler->handle( $time, 'alert', 'alert', $context_source ); $handler->handle( $time, 'emergency', 'emergency', $context_source ); $log_content = $this->read_content( 'unit-tests' ); $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content ); } /** * Test multiple handlers don't conflict on log writing. * * @since 3.0.0 */ public function test_multiple_handlers() { $handler_a = new WC_Log_Handler_File(); $handler_b = new WC_Log_Handler_File(); $time = time(); $context_source = array( 'source' => 'unit-tests' ); // Different loggers should not conflict. $handler_a->handle( $time, 'debug', 'debug', $context_source ); $handler_b->handle( $time, 'info', 'info', $context_source ); $handler_a->handle( $time, 'notice', 'notice', $context_source ); $handler_b->handle( $time, 'warning', 'warning', $context_source ); $handler_a->handle( $time, 'error', 'error', $context_source ); $handler_b->handle( $time, 'critical', 'critical', $context_source ); $handler_a->handle( $time, 'alert', 'alert', $context_source ); $handler_b->handle( $time, 'emergency', 'emergency', $context_source ); $log_content = $this->read_content( 'unit-tests' ); $this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content ); } /** * Test log_rotate() * * Ensure logs are rotated correctly when limit is surpassed. * * @since 3.0.0 */ public function test_log_rotate() { // Handler with log size limit of 5mb $handler = new WC_Log_Handler_File( 5 * 1024 * 1024 ); $time = time(); $log_name = '_test_log_rotate'; $base_log_file = WC_Log_Handler_File::get_log_file_path( $log_name ); // Create log file larger than 5mb to ensure log is rotated $handle = fopen( $base_log_file, 'w' ); fseek( $handle, 5 * 1024 * 1024 ); fwrite( $handle, '_base_log_file_contents_' ); fclose( $handle ); // Write some files to ensure they've rotated correctly for ( $i = 0; $i < 10; $i++ ) { file_put_contents( WC_Log_Handler_File::get_log_file_path( $log_name . ".{$i}" ), $i ); } $context_source = array( 'source' => $log_name ); $handler->handle( $time, 'emergency', 'emergency', $context_source ); $this->assertFileExists( WC_Log_Handler_File::get_log_file_path( $log_name ) ); // Ensure the handled log is correct $this->assertStringEndsWith( 'EMERGENCY emergency' . PHP_EOL, $this->read_content( $log_name ) ); // Ensure other logs have rotated correctly $this->assertEquals( '_base_log_file_contents_', trim( $this->read_content( $log_name . '.0' ) ) ); for ( $i = 1; $i < 10; $i++ ) { $this->assertEquals( $i - 1, $this->read_content( $log_name . ".{$i}" ) ); } } /** * Test get_log_file_path(). * * @since 3.0.0 */ public function test_get_log_file_path() { $log_dir = trailingslashit( WC_LOG_DIR ); $date_suffix = date( 'Y-m-d', current_time( 'timestamp', true ) ); $hash_name = sanitize_file_name( wp_hash( 'unit-tests' ) ); $this->assertEquals( $log_dir . 'unit-tests-' . $date_suffix . '-' . $hash_name . '.log', WC_Log_Handler_File::get_log_file_path( 'unit-tests' ) ); } }