2016-11-26 20:45:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WC_Tests_Log_Handler_DB
|
|
|
|
* @package WooCommerce\Tests\Log
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-26 20:45:36 +00:00
|
|
|
*/
|
2016-11-27 18:37:20 +00:00
|
|
|
class WC_Tests_Log_Handler_DB extends WC_Unit_Test_Case {
|
2018-10-11 18:25:12 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->handler = new WC_Log_Handler_DB( array( 'threshold' => 'debug' ) );
|
|
|
|
}
|
|
|
|
|
2016-11-26 20:45:36 +00:00
|
|
|
/**
|
|
|
|
* Test handle writes to database correctly.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-26 20:45:36 +00:00
|
|
|
*/
|
|
|
|
public function test_handle() {
|
|
|
|
global $wpdb;
|
|
|
|
|
2018-10-11 18:10:46 +00:00
|
|
|
$time = time();
|
|
|
|
$context = array(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
'a',
|
|
|
|
'b',
|
|
|
|
'key' => 'value',
|
|
|
|
);
|
2016-11-26 20:45:36 +00:00
|
|
|
|
2018-10-11 18:25:12 +00:00
|
|
|
$this->handler->handle( $time, 'debug', 'msg_debug', array( 'source' => 'source_debug' ) );
|
|
|
|
$this->handler->handle( $time, 'info', 'msg_info', array( 'source' => 'source_info' ) );
|
|
|
|
$this->handler->handle( $time, 'notice', 'msg_notice', array( 'source' => 'source_notice' ) );
|
|
|
|
$this->handler->handle( $time, 'warning', 'msg_warning', array( 'source' => 'source_warning' ) );
|
|
|
|
$this->handler->handle( $time, 'error', 'msg_error', array( 'source' => 'source_error' ) );
|
|
|
|
$this->handler->handle( $time, 'critical', 'msg_critical', array( 'source' => 'source_critical' ) );
|
|
|
|
$this->handler->handle( $time, 'alert', 'msg_alert', array( 'source' => 'source_alert' ) );
|
|
|
|
$this->handler->handle( $time, 'emergency', 'msg_emergency', array( 'source' => 'source_emergency' ) );
|
2016-11-26 20:45:36 +00:00
|
|
|
|
2018-10-11 18:25:12 +00:00
|
|
|
$this->handler->handle( $time, 'debug', 'context_test', $context );
|
2016-12-11 17:59:12 +00:00
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source, context FROM {$wpdb->prefix}woocommerce_log", ARRAY_A );
|
2016-11-26 20:45:36 +00:00
|
|
|
|
|
|
|
$expected_ts = date( 'Y-m-d H:i:s', $time );
|
2018-10-11 18:10:46 +00:00
|
|
|
$expected = array(
|
2016-11-26 20:45:36 +00:00
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'debug' ),
|
|
|
|
'message' => 'msg_debug',
|
|
|
|
'source' => 'source_debug',
|
|
|
|
'context' => serialize( array( 'source' => 'source_debug' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'info' ),
|
|
|
|
'message' => 'msg_info',
|
|
|
|
'source' => 'source_info',
|
|
|
|
'context' => serialize( array( 'source' => 'source_info' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'notice' ),
|
|
|
|
'message' => 'msg_notice',
|
|
|
|
'source' => 'source_notice',
|
|
|
|
'context' => serialize( array( 'source' => 'source_notice' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'warning' ),
|
|
|
|
'message' => 'msg_warning',
|
|
|
|
'source' => 'source_warning',
|
|
|
|
'context' => serialize( array( 'source' => 'source_warning' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'error' ),
|
|
|
|
'message' => 'msg_error',
|
|
|
|
'source' => 'source_error',
|
|
|
|
'context' => serialize( array( 'source' => 'source_error' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'critical' ),
|
|
|
|
'message' => 'msg_critical',
|
|
|
|
'source' => 'source_critical',
|
|
|
|
'context' => serialize( array( 'source' => 'source_critical' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'alert' ),
|
|
|
|
'message' => 'msg_alert',
|
|
|
|
'source' => 'source_alert',
|
|
|
|
'context' => serialize( array( 'source' => 'source_alert' ) ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'emergency' ),
|
|
|
|
'message' => 'msg_emergency',
|
|
|
|
'source' => 'source_emergency',
|
|
|
|
'context' => serialize( array( 'source' => 'source_emergency' ) ),
|
2016-12-11 17:59:12 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'timestamp' => $expected_ts,
|
2018-10-11 18:10:46 +00:00
|
|
|
'level' => WC_Log_Levels::get_level_severity( 'debug' ),
|
|
|
|
'message' => 'context_test',
|
|
|
|
'source' => pathinfo( __FILE__, PATHINFO_FILENAME ),
|
|
|
|
'context' => serialize( $context ),
|
2016-11-26 20:45:36 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( $log_entries, $expected );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test flush.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-26 20:45:36 +00:00
|
|
|
*/
|
|
|
|
public function test_flush() {
|
|
|
|
global $wpdb;
|
|
|
|
|
2018-10-11 18:25:12 +00:00
|
|
|
$time = time();
|
2016-11-26 20:45:36 +00:00
|
|
|
|
2018-10-11 18:25:12 +00:00
|
|
|
$this->handler->handle( $time, 'debug', '', array() );
|
2016-11-26 20:45:36 +00:00
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source FROM {$wpdb->prefix}woocommerce_log" );
|
2016-11-26 20:45:36 +00:00
|
|
|
$this->assertCount( 1, $log_entries );
|
|
|
|
|
|
|
|
WC_Log_Handler_DB::flush();
|
|
|
|
|
2016-12-21 19:15:19 +00:00
|
|
|
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source FROM {$wpdb->prefix}woocommerce_log" );
|
2016-11-26 20:45:36 +00:00
|
|
|
$this->assertCount( 0, $log_entries );
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:25:12 +00:00
|
|
|
public function test_delete_logs_before_timestamp() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$time = time();
|
|
|
|
$this->handler->handle( $time, 'debug', '', array() );
|
|
|
|
$this->handler->handle( $time - 10, 'debug', '', array() );
|
|
|
|
|
|
|
|
$this->handler->delete_logs_before_timestamp( $time );
|
|
|
|
|
|
|
|
$log_count = $wpdb->get_var( "SELECT count(*) FROM {$wpdb->prefix}woocommerce_log" );
|
|
|
|
$this->assertEquals( 1, $log_count );
|
|
|
|
}
|
|
|
|
|
2016-11-26 20:45:36 +00:00
|
|
|
}
|