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',
|
2016-11-19 21:29:43 +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 ) {
|
2016-12-19 20:28:55 +00:00
|
|
|
$file_path = WC_Log_Handler_File::get_log_file_path( $file );
|
2016-11-19 18:29:41 +00:00
|
|
|
if ( file_exists( $file_path ) && is_writable( $file_path ) ) {
|
|
|
|
unlink( $file_path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function read_content( $handle ) {
|
2016-12-19 20:28:55 +00:00
|
|
|
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' ) );
|
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
$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() {
|
2016-12-11 11:02:40 +00:00
|
|
|
$handler = new WC_Log_Handler_File();
|
2016-11-19 21:29:43 +00:00
|
|
|
$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() {
|
2016-12-11 11:02:40 +00:00
|
|
|
$handler = new WC_Log_Handler_File();
|
2016-11-19 21:29:43 +00:00
|
|
|
$log_name = '_test_remove';
|
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->remove( $log_name );
|
2016-12-19 20:28:55 +00:00
|
|
|
$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() {
|
2016-12-11 11:02:40 +00:00
|
|
|
$handler = new WC_Log_Handler_File();
|
2016-11-19 18:29:41 +00:00
|
|
|
$time = time();
|
|
|
|
|
2016-11-20 13:32:46 +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() {
|
2016-12-11 11:02:40 +00:00
|
|
|
$handler = new WC_Log_Handler_File();
|
2016-11-19 18:29:41 +00:00
|
|
|
$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
|
|
|
|
2016-11-27 18:53:15 +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
|
2016-11-27 18:53:15 +00:00
|
|
|
*/
|
|
|
|
public function test_multiple_handlers() {
|
2016-12-11 11:02:40 +00:00
|
|
|
$handler_a = new WC_Log_Handler_File();
|
|
|
|
$handler_b = new WC_Log_Handler_File();
|
2016-11-27 18:53:15 +00:00
|
|
|
$time = time();
|
2016-12-21 19:15:19 +00:00
|
|
|
$context_source = array( 'source' => 'unit-tests' );
|
2016-11-27 18:53:15 +00:00
|
|
|
|
|
|
|
// 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 );
|
2016-11-27 18:53:15 +00:00
|
|
|
|
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
|
2016-11-19 21:29:43 +00:00
|
|
|
/**
|
|
|
|
* Test log_rotate()
|
|
|
|
*
|
2016-12-11 11:02:40 +00:00
|
|
|
* Ensure logs are rotated correctly when limit is surpassed.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-19 21:29:43 +00:00
|
|
|
*/
|
|
|
|
public function test_log_rotate() {
|
2016-12-11 11:02:40 +00:00
|
|
|
|
|
|
|
// Handler with log size limit of 5mb
|
|
|
|
$handler = new WC_Log_Handler_File( 5 * 1024 * 1024 );
|
2016-11-19 21:29:43 +00:00
|
|
|
$time = time();
|
|
|
|
$log_name = '_test_log_rotate';
|
2016-12-19 20:28:55 +00:00
|
|
|
$base_log_file = WC_Log_Handler_File::get_log_file_path( $log_name );
|
2016-11-19 21:29:43 +00:00
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
// Create log file larger than 5mb to ensure log is rotated
|
2016-11-19 21:29:43 +00:00
|
|
|
$handle = fopen( $base_log_file, 'w' );
|
2016-12-11 11:02:40 +00:00
|
|
|
fseek( $handle, 5 * 1024 * 1024 );
|
2016-11-19 21:29:43 +00:00
|
|
|
fwrite( $handle, '_base_log_file_contents_' );
|
|
|
|
fclose( $handle );
|
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
// Write some files to ensure they've rotated correctly
|
2016-11-19 21:29:43 +00:00
|
|
|
for ( $i = 0; $i < 10; $i++ ) {
|
2016-12-19 20:28:55 +00:00
|
|
|
file_put_contents( WC_Log_Handler_File::get_log_file_path( $log_name . ".{$i}" ), $i );
|
2016-11-19 21:29:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
$context_source = array( 'source' => $log_name );
|
|
|
|
$handler->handle( $time, 'emergency', 'emergency', $context_source );
|
2016-11-19 21:29:43 +00:00
|
|
|
|
2016-12-19 20:28:55 +00:00
|
|
|
$this->assertFileExists( WC_Log_Handler_File::get_log_file_path( $log_name ) );
|
2016-12-11 11:02:40 +00:00
|
|
|
|
|
|
|
// Ensure the handled log is correct
|
2016-11-19 21:29:43 +00:00
|
|
|
$this->assertStringEndsWith( 'EMERGENCY emergency' . PHP_EOL, $this->read_content( $log_name ) );
|
|
|
|
|
2016-12-11 11:02:40 +00:00
|
|
|
// Ensure other logs have rotated correctly
|
2016-11-19 21:29:43 +00:00
|
|
|
$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}" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 20:28:55 +00:00
|
|
|
/**
|
|
|
|
* Test get_log_file_path().
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-19 20:28:55 +00:00
|
|
|
*/
|
|
|
|
public function test_get_log_file_path() {
|
|
|
|
$log_dir = trailingslashit( WC_LOG_DIR );
|
|
|
|
$hash_name = sanitize_file_name( wp_hash( 'unit-tests' ) );
|
|
|
|
$this->assertEquals( $log_dir . 'unit-tests-' . $hash_name . '.log', WC_Log_Handler_File::get_log_file_path( 'unit-tests' ) );
|
|
|
|
}
|
|
|
|
|
2016-11-19 18:29:41 +00:00
|
|
|
}
|