View | Details | Raw Unified | Return to bug 12787
Collapse All | Expand All

(-)a/t/db_dependent/Circulation_issuingrules.t (-24 / +1 lines)
Lines 2-8 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
4
5
use Test::More tests => 7;
5
use Test::More tests => 5;
6
use Test::MockModule;
6
use Test::MockModule;
7
use DBI;
7
use DBI;
8
use DateTime;
8
use DateTime;
Lines 20-37 my $issuelength = 10; Link Here
20
my $renewalperiod = 5;
20
my $renewalperiod = 5;
21
my $lengthunit = 'days';
21
my $lengthunit = 'days';
22
22
23
my $expected = {
24
    issuelength => $issuelength,
25
    renewalperiod => $renewalperiod,
26
    lengthunit => $lengthunit
27
};
28
29
my $default = {
30
    issuelength => 0,
31
    renewalperiod => 0,
32
    lengthunit => 'days'
33
};
34
35
my $loanlength;
23
my $loanlength;
36
my $mock_undef = [
24
my $mock_undef = [
37
    []
25
    []
Lines 46-62 my $categorycode = 'B'; Link Here
46
my $itemtype = 'MX';
34
my $itemtype = 'MX';
47
my $branchcode = 'FPL';
35
my $branchcode = 'FPL';
48
36
49
#=== GetLoanLength
50
$dbh->{mock_add_resultset} = $mock_loan_length;
51
$loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
52
is_deeply($loanlength, $expected, 'first matches');
53
54
$dbh->{mock_add_resultset} = $mock_undef;
55
$loanlength = C4::Circulation::GetLoanLength($categorycode, $itemtype, $branchcode);
56
is_deeply($loanlength, $default, 'none matches');
57
58
#=== CalcDateDue
59
60
#Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
37
#Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
61
t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
38
t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
62
t::lib::Mocks::mock_preference('useDaysMode', 'Days');
39
t::lib::Mocks::mock_preference('useDaysMode', 'Days');
(-)a/t/db_dependent/Koha_template_plugin_Branches.t (-57 lines)
Lines 1-57 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2013-2014 Equinox Software, Inc.
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, see <http://www.gnu.org/licenses>.
18
19
use Modern::Perl;
20
21
use Test::More tests => 7;
22
23
use C4::Context;
24
use Koha::Database;
25
26
use t::lib::TestBuilder;
27
28
BEGIN {
29
    use_ok('Koha::Template::Plugin::Branches');
30
}
31
32
my $schema = Koha::Database->schema;
33
$schema->storage->txn_begin;
34
my $builder = t::lib::TestBuilder->new;
35
my $library = $builder->build({
36
    source => 'Branch',
37
});
38
39
my $plugin = Koha::Template::Plugin::Branches->new();
40
ok($plugin, "initialized Branches plugin");
41
42
my $name = $plugin->GetName($library->{branchcode});
43
is($name, $library->{branchname}, 'retrieved expected name for library');
44
45
$name = $plugin->GetName('__ANY__');
46
is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
47
48
$name = $plugin->GetName(undef);
49
is($name, '', 'received empty string as name of NULL/undefined library code');
50
51
$library = $plugin->GetLoggedInBranchcode();
52
is($library, '', 'no active library if there is no active user session');
53
54
C4::Context->_new_userenv('DUMMY_SESSION_ID');
55
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
56
$library = $plugin->GetLoggedInBranchcode();
57
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
(-)a/t/db_dependent/Reports/Guided.t (-63 / +241 lines)
Lines 1-4 Link Here
1
# Copyright 2012 Catalyst IT Ltd.
1
# Copyright 2012 Catalyst IT Ltd.
2
# Copyright 2015 Koha Development team
2
#
3
#
3
# This file is part of Koha.
4
# This file is part of Koha.
4
#
5
#
Lines 15-89 Link Here
15
# You should have received a copy of the GNU General Public License
16
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
18
use strict;
19
use Modern::Perl;
19
use warnings;
20
20
21
use Test::More tests => 19;                      # last test to print
21
use Test::More tests => 7;
22
use Test::Warn;
23
24
use t::lib::TestBuilder;
25
use C4::Context;
26
use Koha::Database;
22
27
23
use_ok('C4::Reports::Guided');
28
use_ok('C4::Reports::Guided');
29
can_ok(
30
    'C4::Reports::Guided',
31
    qw(save_report delete_report execute_query)
32
);
33
34
my $schema = Koha::Database->new->schema;
35
$schema->storage->txn_begin;
36
my $builder = t::lib::TestBuilder->new;
24
37
25
# This is the query I found that triggered bug 8594.
38
subtest 'strip_limit' => sub {
26
my $sql = "SELECT aqorders.ordernumber, biblio.title, biblio.biblionumber, items.homebranch,
39
    # This is the query I found that triggered bug 8594.
27
    aqorders.entrydate, aqorders.datereceived,
40
    my $sql = "SELECT aqorders.ordernumber, biblio.title, biblio.biblionumber, items.homebranch,
28
    (SELECT DATE(datetime) FROM statistics
41
        aqorders.entrydate, aqorders.datereceived,
29
        WHERE itemnumber=items.itemnumber AND
30
            (type='return' OR type='issue') LIMIT 1)
31
    AS shelvedate,
32
    DATEDIFF(COALESCE(
33
        (SELECT DATE(datetime) FROM statistics
42
        (SELECT DATE(datetime) FROM statistics
34
            WHERE itemnumber=items.itemnumber AND
43
            WHERE itemnumber=items.itemnumber AND
35
            (type='return' OR type='issue') LIMIT 1),
44
                (type='return' OR type='issue') LIMIT 1)
36
    aqorders.datereceived), aqorders.entrydate) AS totaldays
45
        AS shelvedate,
37
FROM aqorders
46
        DATEDIFF(COALESCE(
38
LEFT JOIN biblio USING (biblionumber)
47
            (SELECT DATE(datetime) FROM statistics
39
LEFT JOIN items ON (items.biblionumber = biblio.biblionumber
48
                WHERE itemnumber=items.itemnumber AND
40
    AND dateaccessioned=aqorders.datereceived)
49
                (type='return' OR type='issue') LIMIT 1),
41
WHERE (entrydate >= '2011-01-01' AND (datereceived < '2011-02-01' OR datereceived IS NULL))
50
        aqorders.datereceived), aqorders.entrydate) AS totaldays
42
    AND items.homebranch LIKE 'INFO'
51
    FROM aqorders
43
ORDER BY title";
52
    LEFT JOIN biblio USING (biblionumber)
44
53
    LEFT JOIN items ON (items.biblionumber = biblio.biblionumber
45
my ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
54
        AND dateaccessioned=aqorders.datereceived)
46
is($res_sql, $sql, "Not breaking subqueries");
55
    WHERE (entrydate >= '2011-01-01' AND (datereceived < '2011-02-01' OR datereceived IS NULL))
47
is($res_lim1, 0, "Returns correct default offset");
56
        AND items.homebranch LIKE 'INFO'
48
is($res_lim2, undef, "Returns correct default LIMIT");
57
    ORDER BY title";
49
58
50
# Now the same thing, but we want it to remove the LIMIT from the end
59
    my ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
51
60
    is($res_sql, $sql, "Not breaking subqueries");
52
my $test_sql = $res_sql . " LIMIT 242";
61
    is($res_lim1, 0, "Returns correct default offset");
53
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
62
    is($res_lim2, undef, "Returns correct default LIMIT");
54
# The replacement drops a ' ' where the limit was
63
55
is(trim($res_sql), $sql, "Correctly removes only final LIMIT");
64
    # Now the same thing, but we want it to remove the LIMIT from the end
56
is($res_lim1, 0, "Returns correct default offset");
65
57
is($res_lim2, 242, "Returns correct extracted LIMIT");
66
    my $test_sql = $res_sql . " LIMIT 242";
58
67
    ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
59
$test_sql = $res_sql . " LIMIT 13,242";
68
    # The replacement drops a ' ' where the limit was
60
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
69
    is(trim($res_sql), $sql, "Correctly removes only final LIMIT");
61
# The replacement drops a ' ' where the limit was
70
    is($res_lim1, 0, "Returns correct default offset");
62
is(trim($res_sql), $sql, "Correctly removes only final LIMIT (with offset)");
71
    is($res_lim2, 242, "Returns correct extracted LIMIT");
63
is($res_lim1, 13, "Returns correct extracted offset");
72
64
is($res_lim2, 242, "Returns correct extracted LIMIT");
73
    $test_sql = $res_sql . " LIMIT 13,242";
65
74
    ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
66
# After here is the simpler case, where there isn't a WHERE clause to worry
75
    # The replacement drops a ' ' where the limit was
67
# about.
76
    is(trim($res_sql), $sql, "Correctly removes only final LIMIT (with offset)");
68
77
    is($res_lim1, 13, "Returns correct extracted offset");
69
# First case with nothing to change
78
    is($res_lim2, 242, "Returns correct extracted LIMIT");
70
$sql = "SELECT * FROM items";
79
71
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
80
    # After here is the simpler case, where there isn't a WHERE clause to worry
72
is($res_sql, $sql, "Not breaking simple queries");
81
    # about.
73
is($res_lim1, 0, "Returns correct default offset");
82
74
is($res_lim2, undef, "Returns correct default LIMIT");
83
    # First case with nothing to change
75
84
    $sql = "SELECT * FROM items";
76
$test_sql = $sql . " LIMIT 242";
85
    ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
77
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
86
    is($res_sql, $sql, "Not breaking simple queries");
78
is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case");
87
    is($res_lim1, 0, "Returns correct default offset");
79
is($res_lim1, 0, "Returns correct default offset");
88
    is($res_lim2, undef, "Returns correct default LIMIT");
80
is($res_lim2, 242, "Returns correct extracted LIMIT");
89
81
90
    $test_sql = $sql . " LIMIT 242";
82
$test_sql = $sql . " LIMIT 13,242";
91
    ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
83
($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
92
    is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case");
84
is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case (with offset)");
93
    is($res_lim1, 0, "Returns correct default offset");
85
is($res_lim1, 13, "Returns correct extracted offset");
94
    is($res_lim2, 242, "Returns correct extracted LIMIT");
86
is($res_lim2, 242, "Returns correct extracted LIMIT");
95
96
    $test_sql = $sql . " LIMIT 13,242";
97
    ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
98
    is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case (with offset)");
99
    is($res_lim1, 13, "Returns correct extracted offset");
100
    is($res_lim2, 242, "Returns correct extracted LIMIT");
101
};
102
103
$_->delete for Koha::AuthorisedValues->search({ category => 'XXX' });
104
Koha::AuthorisedValue->new({category => 'LOC'})->store;
105
106
subtest 'GetReservedAuthorisedValues' => sub {
107
    plan tests => 1;
108
    # This one will catch new reserved words not added
109
    # to GetReservedAuthorisedValues
110
    my %test_authval = (
111
        'date' => 1,
112
        'branches' => 1,
113
        'itemtypes' => 1,
114
        'cn_source' => 1,
115
        'categorycode' => 1,
116
        'biblio_framework' => 1,
117
    );
118
119
    my $reserved_authorised_values = GetReservedAuthorisedValues();
120
    is_deeply(\%test_authval, $reserved_authorised_values,
121
                'GetReservedAuthorisedValues returns a fixed list');
122
};
123
124
subtest 'IsAuthorisedValueValid' => sub {
125
    plan tests => 8;
126
    ok( IsAuthorisedValueValid('LOC'),
127
        'User defined authorised value category is valid');
128
129
    ok( ! IsAuthorisedValueValid('XXX'),
130
        'Not defined authorised value category is invalid');
131
132
    # Loop through the reserved authorised values
133
    foreach my $authorised_value ( keys %{GetReservedAuthorisedValues()} ) {
134
        ok( IsAuthorisedValueValid($authorised_value),
135
            '\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value');
136
    }
137
};
138
139
subtest 'GetParametersFromSQL+ValidateSQLParameters' => sub  {
140
    plan tests => 3;
141
    my $test_query_1 = "
142
        SELECT date_due
143
        FROM old_issues
144
        WHERE YEAR(timestamp) = <<Year|custom_list>> AND
145
              branchcode = <<Branch|branches>> AND
146
              borrowernumber = <<Borrower>>
147
    ";
148
149
    my @test_parameters_with_custom_list = (
150
        { 'name' => 'Year', 'authval' => 'custom_list' },
151
        { 'name' => 'Branch', 'authval' => 'branches' },
152
        { 'name' => 'Borrower', 'authval' => undef }
153
    );
154
155
    is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list,
156
        'SQL params are correctly parsed');
157
158
    my @problematic_parameters = ();
159
    push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' };
160
    is_deeply( ValidateSQLParameters( $test_query_1 ),
161
               \@problematic_parameters,
162
               '\'custom_list\' not a valid category' );
163
164
    my $test_query_2 = "
165
        SELECT date_due
166
        FROM old_issues
167
        WHERE YEAR(timestamp) = <<Year|date>> AND
168
              branchcode = <<Branch|branches>> AND
169
              borrowernumber = <<Borrower|LOC>>
170
    ";
171
172
    is_deeply( ValidateSQLParameters( $test_query_2 ),
173
        [],
174
        'All parameters valid, empty problematic authvals list'
175
    );
176
};
177
178
subtest 'get_saved_reports' => sub {
179
    plan tests => 16;
180
    my $dbh = C4::Context->dbh;
181
    $dbh->do(q|DELETE FROM saved_sql|);
182
    $dbh->do(q|DELETE FROM saved_reports|);
183
184
    #Test save_report
185
    my $count = scalar @{ get_saved_reports() };
186
    is( $count, 0, "There is no report" );
187
188
    my @report_ids;
189
    foreach ( 1..3 ) {
190
        my $id = $builder->build({ source => 'Borrower' })->{ borrowernumber };
191
        push @report_ids, save_report({
192
            borrowernumber => $id,
193
            sql            => "SQL$id",
194
            name           => "Name$id",
195
            area           => "area$id",
196
            group          => "group$id",
197
            subgroup       => "subgroup$id",
198
            type           => "type$id",
199
            notes          => "note$id",
200
            cache_expiry   => "null",
201
            public         => "null"
202
        });
203
        $count++;
204
    }
205
    like( $report_ids[0], '/^\d+$/', "Save_report returns an id for first" );
206
    like( $report_ids[1], '/^\d+$/', "Save_report returns an id for second" );
207
    like( $report_ids[2], '/^\d+$/', "Save_report returns an id for third" );
208
209
    is( scalar @{ get_saved_reports() },
210
        $count, "$count reports have been added" );
211
212
    is( scalar @{ get_saved_reports( $report_ids[0] ) },
213
        1, "filter takes report id" );
214
215
    #Test delete_report
216
    is (delete_report(),undef, "Without id delete_report returns undef");
217
218
    is( delete_report( $report_ids[0] ), 1, "report 1 is deleted" );
219
    $count--;
220
221
    is( scalar @{ get_saved_reports() }, $count, "Report1 has been deleted" );
222
223
    is( delete_report( $report_ids[1], $report_ids[2] ), 2, "report 2 and 3 are deleted" );
224
    $count -= 2;
225
226
    is( scalar @{ get_saved_reports() },
227
        $count, "Report2 and report3 have been deleted" );
228
229
    my $sth = execute_query('SELECT COUNT(*) FROM systempreferences', 0, 10);
230
    my $results = $sth->fetchall_arrayref;
231
    is(scalar @$results, 1, 'running a query returned a result');
232
233
    my $version = C4::Context->preference('Version');
234
    $sth = execute_query(
235
        'SELECT value FROM systempreferences WHERE variable = ?',
236
        0,
237
        10,
238
        [ 'Version' ],
239
    );
240
    $results = $sth->fetchall_arrayref;
241
    is_deeply(
242
        $results,
243
        [ [ $version ] ],
244
        'running a query with a parameter returned the expected result'
245
    );
246
247
    # for next test, we want to let execute_query capture any SQL errors
248
    $dbh->{RaiseError} = 0;
249
    my $errors;
250
    warning_like { ($sth, $errors) = execute_query(
251
            'SELECT surname FRM borrowers',  # error in the query is intentional
252
            0, 10 ) }
253
            qr/^DBD::mysql::st execute failed: You have an error in your SQL syntax;/,
254
            "Wrong SQL syntax raises warning";
255
    ok(
256
        defined($errors) && exists($errors->{queryerr}),
257
        'attempting to run a report with an SQL syntax error returns error message (Bug 12214)'
258
    );
259
260
    is_deeply( get_report_areas(), [ 'CIRC', 'CAT', 'PAT', 'ACQ', 'ACC', 'SER' ],
261
        "get_report_areas returns the correct array of report areas");
262
};
263
264
$schema->storage->txn_rollback;
87
265
88
sub trim {
266
sub trim {
89
    my ($s) = @_;
267
    my ($s) = @_;
(-)a/t/db_dependent/ReportsGuided.t (-85 lines)
Lines 1-85 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 13;
6
7
use Koha::Database;
8
9
use_ok('C4::Reports::Guided');
10
11
my $schema = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
13
14
$_->delete for Koha::AuthorisedValues->search({ category => 'XXX' });
15
Koha::AuthorisedValue->new({category => 'LOC'})->store;
16
17
{   # GetReservedAuthorisedValues tests
18
    # This one will catch new reserved words not added
19
    # to GetReservedAuthorisedValues
20
    my %test_authval = (
21
        'date' => 1,
22
        'branches' => 1,
23
        'itemtypes' => 1,
24
        'cn_source' => 1,
25
        'categorycode' => 1,
26
        'biblio_framework' => 1,
27
    );
28
29
    my $reserved_authorised_values = GetReservedAuthorisedValues();
30
    is_deeply(\%test_authval, $reserved_authorised_values,
31
                'GetReservedAuthorisedValues returns a fixed list');
32
}
33
34
{
35
    ok( IsAuthorisedValueValid('LOC'),
36
        'User defined authorised value category is valid');
37
38
    ok( ! IsAuthorisedValueValid('XXX'),
39
        'Not defined authorised value category is invalid');
40
41
    # Loop through the reserved authorised values
42
    foreach my $authorised_value ( keys %{GetReservedAuthorisedValues()} ) {
43
        ok( IsAuthorisedValueValid($authorised_value),
44
            '\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value');
45
    }
46
}
47
48
{   # GetParametersFromSQL tests
49
50
    my $test_query_1 = "
51
        SELECT date_due
52
        FROM old_issues
53
        WHERE YEAR(timestamp) = <<Year|custom_list>> AND
54
              branchcode = <<Branch|branches>> AND
55
              borrowernumber = <<Borrower>>
56
    ";
57
58
    my @test_parameters_with_custom_list = (
59
        { 'name' => 'Year', 'authval' => 'custom_list' },
60
        { 'name' => 'Branch', 'authval' => 'branches' },
61
        { 'name' => 'Borrower', 'authval' => undef }
62
    );
63
64
    is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list,
65
        'SQL params are correctly parsed');
66
67
    # ValidateSQLParameters tests
68
    my @problematic_parameters = ();
69
    push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' };
70
    is_deeply( ValidateSQLParameters( $test_query_1 ),
71
               \@problematic_parameters,
72
               '\'custom_list\' not a valid category' );
73
74
    my $test_query_2 = "
75
        SELECT date_due
76
        FROM old_issues
77
        WHERE YEAR(timestamp) = <<Year|date>> AND
78
              branchcode = <<Branch|branches>> AND
79
              borrowernumber = <<Borrower|LOC>>
80
    ";
81
82
    is_deeply( ValidateSQLParameters( $test_query_2 ),
83
        [],
84
        'All parameters valid, empty problematic authvals list');
85
}
(-)a/t/db_dependent/Reports_Guided.t (-124 lines)
Lines 1-124 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 18;
21
use Test::Warn;
22
use t::lib::TestBuilder;
23
24
use C4::Context;
25
use Koha::Database;
26
27
BEGIN {
28
    use_ok('C4::Reports::Guided');
29
}
30
can_ok(
31
    'C4::Reports::Guided',
32
    qw(save_report delete_report execute_query)
33
);
34
35
my $schema = Koha::Database->new->schema;
36
$schema->storage->txn_begin;
37
my $builder = t::lib::TestBuilder->new;
38
39
my $dbh = C4::Context->dbh;
40
$dbh->do(q|DELETE FROM saved_sql|);
41
$dbh->do(q|DELETE FROM saved_reports|);
42
43
#Start tests
44
45
#Test save_report
46
my $count = scalar @{ get_saved_reports() };
47
is( $count, 0, "There is no report" );
48
49
my @report_ids;
50
foreach ( 1..3 ) {
51
    my $id = $builder->build({ source => 'Borrower' })->{ borrowernumber };
52
    push @report_ids, save_report({
53
        borrowernumber => $id,
54
        sql            => "SQL$id",
55
        name           => "Name$id",
56
        area           => "area$id",
57
        group          => "group$id",
58
        subgroup       => "subgroup$id",
59
        type           => "type$id",
60
        notes          => "note$id",
61
        cache_expiry   => "null",
62
        public         => "null"
63
    });
64
    $count++;
65
}
66
like( $report_ids[0], '/^\d+$/', "Save_report returns an id for first" );
67
like( $report_ids[1], '/^\d+$/', "Save_report returns an id for second" );
68
like( $report_ids[2], '/^\d+$/', "Save_report returns an id for third" );
69
70
is( scalar @{ get_saved_reports() },
71
    $count, "$count reports have been added" );
72
73
is( scalar @{ get_saved_reports( $report_ids[0] ) },
74
    1, "filter takes report id" );
75
76
#Test delete_report
77
is (delete_report(),undef, "Without id delete_report returns undef");
78
79
is( delete_report( $report_ids[0] ), 1, "report 1 is deleted" );
80
$count--;
81
82
is( scalar @{ get_saved_reports() }, $count, "Report1 has been deleted" );
83
84
is( delete_report( $report_ids[1], $report_ids[2] ), 2, "report 2 and 3 are deleted" );
85
$count -= 2;
86
87
is( scalar @{ get_saved_reports() },
88
    $count, "Report2 and report3 have been deleted" );
89
90
my $sth = execute_query('SELECT COUNT(*) FROM systempreferences', 0, 10);
91
my $results = $sth->fetchall_arrayref;
92
is(scalar @$results, 1, 'running a query returned a result');
93
94
my $version = C4::Context->preference('Version');
95
$sth = execute_query(
96
    'SELECT value FROM systempreferences WHERE variable = ?',
97
    0,
98
    10,
99
    [ 'Version' ],
100
);
101
$results = $sth->fetchall_arrayref;
102
is_deeply(
103
    $results,
104
    [ [ $version ] ],
105
    'running a query with a parameter returned the expected result'
106
);
107
108
# for next test, we want to let execute_query capture any SQL errors
109
$dbh->{RaiseError} = 0;
110
my $errors;
111
warning_like { ($sth, $errors) = execute_query(
112
        'SELECT surname FRM borrowers',  # error in the query is intentional
113
        0, 10 ) }
114
        qr/^DBD::mysql::st execute failed: You have an error in your SQL syntax;/,
115
        "Wrong SQL syntax raises warning";
116
ok(
117
    defined($errors) && exists($errors->{queryerr}),
118
    'attempting to run a report with an SQL syntax error returns error message (Bug 12214)'
119
);
120
121
is_deeply( get_report_areas(), [ 'CIRC', 'CAT', 'PAT', 'ACQ', 'ACC', 'SER' ],
122
    "get_report_areas returns the correct array of report areas");
123
124
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Template/Plugin/Branches.t (-4 / +24 lines)
Lines 1-9 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
2
3
use Test::More tests => 5;
3
use Test::More tests => 11;
4
4
5
use C4::Context;
5
use C4::Context;
6
use Koha::Library;
6
use Koha::Library;
7
use Koha::Libraries;
7
use Koha::Template::Plugin::Branches;
8
use Koha::Template::Plugin::Branches;
8
9
9
my $dbh = C4::Context->dbh;
10
my $dbh = C4::Context->dbh;
Lines 17-32 for my $i ( 1 .. 5 ) { Link Here
17
        branchname     => "test_br_$i",
18
        branchname     => "test_br_$i",
18
}
19
}
19
    )->store;
20
    )->store;
20
21
}
21
}
22
my $library = Koha::Libraries->search->next->unblessed;
23
24
my $plugin = Koha::Template::Plugin::Branches->new();
25
ok($plugin, "initialized Branches plugin");
26
27
my $name = $plugin->GetName($library->{branchcode});
28
is($name, $library->{branchname}, 'retrieved expected name for library');
29
30
$name = $plugin->GetName('__ANY__');
31
is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
32
33
$name = $plugin->GetName(undef);
34
is($name, '', 'received empty string as name of NULL/undefined library code');
35
36
$library = $plugin->GetLoggedInBranchcode();
37
is($library, '', 'no active library if there is no active user session');
38
C4::Context->_new_userenv('DUMMY_SESSION_ID');
39
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
40
$library = $plugin->GetLoggedInBranchcode();
41
is($library, 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library');
22
42
23
my $branches = Koha::Template::Plugin::Branches->new->all;
43
my $branches = $plugin->all;
24
my $test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
44
my $test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
25
is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches' );
45
is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches' );
26
my $selected_branches = [ grep { $_->{selected} } @$branches ];
46
my $selected_branches = [ grep { $_->{selected} } @$branches ];
27
is( scalar( @$selected_branches ), 0, 'Plugin Branches should not select a branch if not needed' );
47
is( scalar( @$selected_branches ), 0, 'Plugin Branches should not select a branch if not needed' );
28
48
29
$branches = Koha::Template::Plugin::Branches->new->all({selected => 'test_br_3'});
49
$branches = $plugin->all({selected => 'test_br_3'});
30
$test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
50
$test_branches = [ grep { $_->{branchcode} =~ m|^test_br_| } @$branches ];
31
is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches if selected passed' );
51
is( scalar( @$test_branches ), 5, 'Plugin Branches should return the branches if selected passed' );
32
$selected_branches = [ grep { $_->{selected} } @$branches ];
52
$selected_branches = [ grep { $_->{selected} } @$branches ];

Return to bug 12787