BUG8446, QA Followup: Minor Code Tidies
[koha.git] / t / db_dependent / ReportsGuided.t
blob200dd86bef221d81720534d6156335a2caa93075
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Test::More tests => 12;
6 use Test::MockModule;
7 use DBD::Mock;
9 use_ok('C4::Reports::Guided');
11 my $context = new Test::MockModule('C4::Context');
12 my $koha = new Test::MockModule('C4::Koha');
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;
24 sub MockedIsAuthorisedValueCategory {
25 my $authorised_value = shift;
27 if ( $authorised_value eq 'LOC' ) {
28 return 1;
29 } else {
30 return 0;
34 $koha->mock(
35 'IsAuthorisedValueCategory',
36 \&MockedIsAuthorisedValueCategory
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
50 my $reserved_authorised_values = GetReservedAuthorisedValues();
51 is_deeply(\%test_authval, $reserved_authorised_values,
52 'GetReservedAuthorisedValues returns a fixed list');
55 SKIP: {
57 skip "DBD::Mock is too old", 7
58 unless $DBD::Mock::VERSION >= 1.45;
60 ok( IsAuthorisedValueValid('LOC'),
61 'User defined authorised value category is valid');
63 ok( ! IsAuthorisedValueValid('XXX'),
64 'Not defined authorised value category is invalid');
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');
73 { # GetParametersFromSQL tests
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>>
83 my @test_parameters_with_custom_list = (
84 { 'name' => 'Year', 'authval' => 'custom_list' },
85 { 'name' => 'Branch', 'authval' => 'branches' },
86 { 'name' => 'Borrower', 'authval' => undef }
89 is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list,
90 'SQL params are correctly parsed');
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' );
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>>
107 is_deeply( ValidateSQLParameters( $test_query_2 ),
109 'All parameters valid, empty problematic authvals list');