Bug 13141: Add ability for biblio_framework to be a dropdown in Guided Reports
[koha.git] / t / db_dependent / ReportsGuided.t
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         'biblio_framework' => 1,
49     );
50
51     my $reserved_authorised_values = GetReservedAuthorisedValues();
52     is_deeply(\%test_authval, $reserved_authorised_values,
53                 'GetReservedAuthorisedValues returns a fixed list');
54 }
55
56 SKIP: {
57
58     skip "DBD::Mock is too old", 7
59         unless $DBD::Mock::VERSION >= 1.45;
60
61     ok( IsAuthorisedValueValid('LOC'),
62         'User defined authorised value category is valid');
63
64     ok( ! IsAuthorisedValueValid('XXX'),
65         'Not defined authorised value category is invalid');
66
67     # Loop through the reserved authorised values
68     foreach my $authorised_value ( keys %{GetReservedAuthorisedValues()} ) {
69         ok( IsAuthorisedValueValid($authorised_value),
70             '\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value');
71     }
72 }
73
74 {   # GetParametersFromSQL tests
75
76     my $test_query_1 = "
77         SELECT date_due
78         FROM old_issues
79         WHERE YEAR(timestamp) = <<Year|custom_list>> AND
80               branchcode = <<Branch|branches>> AND
81               borrowernumber = <<Borrower>>
82     ";
83
84     my @test_parameters_with_custom_list = (
85         { 'name' => 'Year', 'authval' => 'custom_list' },
86         { 'name' => 'Branch', 'authval' => 'branches' },
87         { 'name' => 'Borrower', 'authval' => undef }
88     );
89
90     is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list,
91         'SQL params are correctly parsed');
92
93     # ValidateSQLParameters tests
94     my @problematic_parameters = ();
95     push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' };
96     is_deeply( ValidateSQLParameters( $test_query_1 ),
97                \@problematic_parameters,
98                '\'custom_list\' not a valid category' );
99
100     my $test_query_2 = "
101         SELECT date_due
102         FROM old_issues
103         WHERE YEAR(timestamp) = <<Year|date>> AND
104               branchcode = <<Branch|branches>> AND
105               borrowernumber = <<Borrower|LOC>>
106     ";
107
108     is_deeply( ValidateSQLParameters( $test_query_2 ),
109         [],
110         'All parameters valid, empty problematic authvals list');
111 }