|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
|
| 5 |
use Test::More tests => 12; |
| 6 |
use Test::MockModule; |
| 7 |
use DBD::Mock; |
| 8 |
|
| 9 |
use_ok('C4::Reports::Guided'); |
| 10 |
|
| 11 |
my $context = new Test::MockModule('C4::Context'); |
| 12 |
my $koha = new Test::MockModule('C4::Koha'); |
| 13 |
|
| 14 |
$context->mock( |
| 15 |
'_new_dbh', |
| 16 |
sub { |
| 17 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
| 18 |
|| die "Cannot create handle: $DBI::errstr\n"; |
| 19 |
return $dbh; |
| 20 |
} |
| 21 |
); |
| 22 |
|
| 23 |
|
| 24 |
sub MockedIsAuthorisedValueCategory { |
| 25 |
my $authorised_value = shift; |
| 26 |
|
| 27 |
if ( $authorised_value eq 'LOC' ) { |
| 28 |
return 1; |
| 29 |
} else { |
| 30 |
return 0; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$koha->mock( |
| 35 |
'IsAuthorisedValueCategory', |
| 36 |
\&MockedIsAuthorisedValueCategory |
| 37 |
); |
| 38 |
|
| 39 |
{ # GetReservedAuthorisedValues tests |
| 40 |
# This one will catch new reserved words not added |
| 41 |
# to GetReservedAuthorisedValues |
| 42 |
my %test_authval = ( |
| 43 |
'date' => 1, |
| 44 |
'branches' => 1, |
| 45 |
'itemtypes' => 1, |
| 46 |
'cn_source' => 1, |
| 47 |
'categorycode' => 1 |
| 48 |
); |
| 49 |
|
| 50 |
my $reserved_authorised_values = GetReservedAuthorisedValues(); |
| 51 |
is_deeply(\%test_authval, $reserved_authorised_values, |
| 52 |
'GetReservedAuthorisedValues returns a fixed list'); |
| 53 |
} |
| 54 |
|
| 55 |
SKIP: { |
| 56 |
|
| 57 |
skip "DBD::Mock is too old", 7 |
| 58 |
unless $DBD::Mock::VERSION >= 1.45; |
| 59 |
|
| 60 |
ok( IsAuthorisedValueValid('LOC'), |
| 61 |
'User defined authorised value category is valid'); |
| 62 |
|
| 63 |
ok( ! IsAuthorisedValueValid('XXX'), |
| 64 |
'Not defined authorised value category is invalid'); |
| 65 |
|
| 66 |
# Loop through the reserved authorised values |
| 67 |
foreach my $authorised_value ( keys GetReservedAuthorisedValues() ) { |
| 68 |
ok( IsAuthorisedValueValid($authorised_value), |
| 69 |
'\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value'); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
{ # GetParametersFromSQL tests |
| 74 |
|
| 75 |
my $test_query_1 = " |
| 76 |
SELECT date_due |
| 77 |
FROM old_issues |
| 78 |
WHERE YEAR(timestamp) = <<Year|custom_list>> AND |
| 79 |
branchcode = <<Branch|branches>> AND |
| 80 |
borrowernumber = <<Borrower>> |
| 81 |
"; |
| 82 |
|
| 83 |
my @test_parameters_with_custom_list = ( |
| 84 |
{ 'name' => 'Year', 'authval' => 'custom_list' }, |
| 85 |
{ 'name' => 'Branch', 'authval' => 'branches' }, |
| 86 |
{ 'name' => 'Borrower', 'authval' => undef } |
| 87 |
); |
| 88 |
|
| 89 |
is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list, |
| 90 |
'SQL params are correctly parsed'); |
| 91 |
|
| 92 |
# ValidateSQLParameters tests |
| 93 |
my @problematic_parameters = (); |
| 94 |
push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' }; |
| 95 |
is_deeply( ValidateSQLParameters( $test_query_1 ), |
| 96 |
\@problematic_parameters, |
| 97 |
'\'custom_list\' not a valid category' ); |
| 98 |
|
| 99 |
my $test_query_2 = " |
| 100 |
SELECT date_due |
| 101 |
FROM old_issues |
| 102 |
WHERE YEAR(timestamp) = <<Year|date>> AND |
| 103 |
branchcode = <<Branch|branches>> AND |
| 104 |
borrowernumber = <<Borrower|LOC>> |
| 105 |
"; |
| 106 |
|
| 107 |
is_deeply( ValidateSQLParameters( $test_query_2 ), |
| 108 |
[], |
| 109 |
'All parameters valid, empty problematic authvals list'); |
| 110 |
} |