woocommerce/tests/legacy/unit-tests/log/log-handler-file.php

214 lines
6.9 KiB
PHP
Raw Normal View History

2016-11-19 18:29:41 +00:00
<?php
/**
* Class WC_Tests_Log_Handler_File
* @package WooCommerce\Tests\Log
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-19 18:29:41 +00:00
*/
class WC_Tests_Log_Handler_File extends WC_Unit_Test_Case {
public function tearDown() {
$log_files = array(
'unit-tests',
'log',
2018-07-09 11:04:16 +00:00
'_test_clear',
'_test_remove',
'_test_log_rotate',
'_test_log_rotate.0',
'_test_log_rotate.1',
'_test_log_rotate.2',
'_test_log_rotate.3',
'_test_log_rotate.4',
'_test_log_rotate.5',
'_test_log_rotate.6',
'_test_log_rotate.7',
'_test_log_rotate.8',
'_test_log_rotate.9',
2016-11-19 18:29:41 +00:00
);
foreach ( $log_files as $file ) {
$file_path = WC_Log_Handler_File::get_log_file_path( $file );
2018-07-09 11:04:16 +00:00
if ( file_exists( $file_path ) && is_writable( $file_path ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
unlink( $file_path ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink
2016-11-19 18:29:41 +00:00
}
}
parent::tearDown();
}
public function read_content( $handle ) {
return file_get_contents( WC_Log_Handler_File::get_log_file_path( $handle ) );
2016-11-19 18:29:41 +00:00
}
/**
* Test _legacy format.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-19 18:29:41 +00:00
*/
public function test_legacy_format() {
$handler = new WC_Log_Handler_File( array( 'threshold' => 'debug' ) );
$handler->handle(
time(),
'info',
'this is a message',
array(
'source' => 'unit-tests',
'_legacy' => true,
)
);
2016-11-19 18:29:41 +00:00
$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().
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-19 18:29:41 +00:00
*/
public function test_clear() {
$handler = new WC_Log_Handler_File();
$log_name = '_test_clear';
2016-12-21 19:15:19 +00:00
$handler->handle( time(), 'debug', 'debug', array( 'source' => $log_name ) );
2016-11-19 18:29:41 +00:00
$handler->clear( $log_name );
$this->assertEquals( '', $this->read_content( $log_name ) );
}
/**
* Test remove().
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-19 18:29:41 +00:00
*/
public function test_remove() {
$handler = new WC_Log_Handler_File();
$log_name = '_test_remove';
2016-12-21 19:15:19 +00:00
$handler->handle( time(), 'debug', 'debug', array( 'source' => $log_name ) );
2018-07-09 11:01:23 +00:00
$handler->remove( wc_get_log_file_name( $log_name ) );
$this->assertFileNotExists( WC_Log_Handler_File::get_log_file_path( $log_name ) );
2016-11-19 18:29:41 +00:00
}
/**
* Test handle writes to default file correctly.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-19 18:29:41 +00:00
*/
public function test_writes_file() {
$handler = new WC_Log_Handler_File();
$time = time();
2016-11-19 18:29:41 +00:00
$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() );
2016-11-19 18:29:41 +00:00
$log_content = $this->read_content( 'log' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
/**
2016-12-21 19:15:19 +00:00
* Test 'source' context determines log file.
2016-11-19 18:29:41 +00:00
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-19 18:29:41 +00:00
*/
2016-12-21 19:15:19 +00:00
public function test_log_file_source() {
$handler = new WC_Log_Handler_File();
$time = time();
2016-12-21 19:15:19 +00:00
$context_source = array( 'source' => 'unit-tests' );
2016-11-19 18:29:41 +00:00
2016-12-21 19:15:19 +00:00
$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 );
2016-11-19 18:29:41 +00:00
$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.
*
2017-03-15 16:36:53 +00:00
* @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();
2016-12-21 19:15:19 +00:00
$context_source = array( 'source' => 'unit-tests' );
// Different loggers should not conflict.
2016-12-21 19:15:19 +00:00
$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' );
2016-11-19 18:29:41 +00:00
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
}
/**
* Test log_rotate()
*
* Ensure logs are rotated correctly when limit is surpassed.
*
2017-03-15 16:36:53 +00:00
* @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++ ) {
2018-07-09 11:04:16 +00:00
file_put_contents( WC_Log_Handler_File::get_log_file_path( $log_name . ".{$i}" ), $i ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_file_put_contents
}
2016-12-21 19:15:19 +00:00
$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().
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_log_file_path() {
$log_dir = trailingslashit( WC_LOG_DIR );
$date_suffix = date( 'Y-m-d', time() );
$hash_name = sanitize_file_name( wp_hash( 'unit-tests' ) );
2018-05-01 16:32:29 +00:00
$this->assertEquals( $log_dir . 'unit-tests-' . $date_suffix . '-' . $hash_name . '.log', WC_Log_Handler_File::get_log_file_path( 'unit-tests' ) );
}
2016-11-19 18:29:41 +00:00
}