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