|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Test script to verify IP address logging in action_logs |
| 4 |
# This script tests that the IP address is properly stored in action_logs |
| 5 |
# based on the ActionLogsEnableIPLogging system preference |
| 6 |
|
| 7 |
use Modern::Perl; |
| 8 |
use Test::More tests => 10; |
| 9 |
|
| 10 |
use C4::Context; |
| 11 |
use C4::Log qw(logaction); |
| 12 |
use Koha::Database; |
| 13 |
use Koha::ActionLogs; |
| 14 |
use t::lib::TestBuilder; |
| 15 |
use t::lib::Mocks; |
| 16 |
|
| 17 |
my $schema = Koha::Database->new->schema; |
| 18 |
$schema->storage->txn_begin; |
| 19 |
|
| 20 |
my $builder = t::lib::TestBuilder->new; |
| 21 |
|
| 22 |
# Mock REMOTE_ADDR |
| 23 |
$ENV{REMOTE_ADDR} = '192.168.1.100'; |
| 24 |
|
| 25 |
# Clear any existing action logs for this test |
| 26 |
Koha::ActionLogs->search->delete; |
| 27 |
|
| 28 |
# Perform a test action log |
| 29 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 30 |
|
| 31 |
# Test 1: IP logging DISABLED (default) |
| 32 |
t::lib::Mocks::mock_preference( 'ActionLogsEnableIPLogging', 0 ); |
| 33 |
|
| 34 |
logaction( "TESTING", "ADD_DISABLED", $patron->borrowernumber, "Test IP logging disabled", "intranet" ); |
| 35 |
|
| 36 |
my $action_log_disabled = Koha::ActionLogs->search( { module => 'TESTING', action => 'ADD_DISABLED' } )->next; |
| 37 |
ok( $action_log_disabled, "Action log was created with IP logging disabled" ); |
| 38 |
is( $action_log_disabled->module, "TESTING", "Module is correct when disabled" ); |
| 39 |
is( $action_log_disabled->action, "ADD_DISABLED", "Action is correct when disabled" ); |
| 40 |
is( $action_log_disabled->info, "Test IP logging disabled", "Info is correct when disabled" ); |
| 41 |
is( $action_log_disabled->ip_address, undef, "IP address is NOT logged when disabled" ); |
| 42 |
|
| 43 |
# Test 2: IP logging ENABLED |
| 44 |
t::lib::Mocks::mock_preference( 'ActionLogsEnableIPLogging', 1 ); |
| 45 |
|
| 46 |
logaction( "TESTING", "ADD_ENABLED", $patron->borrowernumber, "Test IP logging enabled", "intranet" ); |
| 47 |
|
| 48 |
my $action_log_enabled = Koha::ActionLogs->search( { module => 'TESTING', action => 'ADD_ENABLED' } )->next; |
| 49 |
ok( $action_log_enabled, "Action log was created with IP logging enabled" ); |
| 50 |
is( $action_log_enabled->module, "TESTING", "Module is correct when enabled" ); |
| 51 |
is( $action_log_enabled->action, "ADD_ENABLED", "Action is correct when enabled" ); |
| 52 |
is( $action_log_enabled->info, "Test IP logging enabled", "Info is correct when enabled" ); |
| 53 |
is( $action_log_enabled->ip_address, "192.168.1.100", "IP address is logged when enabled" ); |
| 54 |
|
| 55 |
# Clean up |
| 56 |
$schema->storage->txn_rollback; |
| 57 |
|
| 58 |
done_testing(); |