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

(-)a/t/db_dependent/Koha/Plugins/Overdues.t (-30 / +68 lines)
Lines 16-23 Link Here
16
16
17
use Modern::Perl;
17
use Modern::Perl;
18
18
19
use Test::NoWarnings;
19
use Test::More tests => 8;
20
use Test::More tests => 6;
20
use Test::Warn;
21
21
22
use C4::Overdues qw(CalcFine);
22
use C4::Overdues qw(CalcFine);
23
23
Lines 34-69 BEGIN { Link Here
34
    use_ok('C4::Overdues');
34
    use_ok('C4::Overdues');
35
    use_ok('Koha::Plugins');
35
    use_ok('Koha::Plugins');
36
    use_ok('Koha::Plugins::Handler');
36
    use_ok('Koha::Plugins::Handler');
37
    use_ok('Koha::Plugin::1_CalcFineEmpty');
38
    use_ok('Koha::Plugin::2_CalcFineNotEmpty');
39
    use_ok('Koha::Plugin::3_CalcFineBadValue');
37
    use_ok('Koha::Plugin::Test');
40
    use_ok('Koha::Plugin::Test');
38
}
41
}
39
42
40
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
43
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
41
44
42
my $builder = t::lib::TestBuilder->new;
45
my $builder = t::lib::TestBuilder->new;
43
46
my $schema  = Koha::Database->new->schema;
44
my $branch = $builder->build(
45
    {
46
        source => 'Branch',
47
    }
48
);
49
50
my $category = $builder->build(
51
    {
52
        source => 'Category',
53
    }
54
);
55
56
my $item = $builder->build_sample_item;
57
47
58
subtest 'overwrite_calc_fine hook tests' => sub {
48
subtest 'overwrite_calc_fine hook tests' => sub {
59
    plan tests => 6;
60
49
61
    my $schema = Koha::Database->new->schema;
50
    plan tests => 13;
51
62
    $schema->storage->txn_begin;
52
    $schema->storage->txn_begin;
63
53
64
    my $plugins = Koha::Plugins->new;
54
    my $library  = $builder->build_object( { class => 'Koha::Libraries', } );
65
    $plugins->InstallPlugins;
55
    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', } );
66
    Koha::Plugin::Test->new->enable;
56
    my $item     = $builder->build_sample_item;
57
58
    Koha::Plugins->new->InstallPlugins();
59
    my $test_plugin = Koha::Plugin::Test->new->disable();
67
60
68
    Koha::CirculationRules->set_rules(
61
    Koha::CirculationRules->set_rules(
69
        {
62
        {
Lines 93-111 subtest 'overwrite_calc_fine hook tests' => sub { Link Here
93
        day   => 30,
86
        day   => 30,
94
    );
87
    );
95
88
96
    my ( $amount, $units, $chargable_units ) = CalcFine( $item->{itemnumber}, undef, undef, $due_date, $end_date );
89
    my ( $amount, $units, $chargable_units ) =
97
90
        CalcFine( $item->unblessed, $category->categorycode, $library->branchcode, $due_date, $end_date );
98
    is( $amount,          87, 'Amount is calculated correctly with custom function' );
99
    is( $units,           29, 'Units are calculated correctly with custom function' );
100
    is( $chargable_units, 27, 'Chargable units are calculated correctly with custom function' );
101
102
    ( $amount, $units, $chargable_units ) =
103
        CalcFine( $item->{itemnumber}, $category->{categorycode}, $branch->{branchcode}, $due_date, $end_date );
104
91
105
    is( $amount,          58, 'Amount is calculated correctly with original function' );
92
    is( $amount,          58, 'Amount is calculated correctly with original function' );
106
    is( $units,           29, 'Units are calculated correctly with original function' );
93
    is( $units,           29, 'Units are calculated correctly with original function' );
107
    is( $chargable_units, 29, 'Chargable units are calculated correctly with original function' );
94
    is( $chargable_units, 29, 'Chargable units are calculated correctly with original function' );
108
95
109
    Koha::Plugins->RemovePlugins;
96
    # initialize undef value plugin
97
    my $empty_value_plugin = Koha::Plugin::1_CalcFineEmpty->new->enable();
98
    ( $amount, $units, $chargable_units ) =
99
        CalcFine( $item->unblessed, $category->categorycode, $library->branchcode, $due_date, $end_date );
100
101
    is( $amount,          58, 'Amount is calculated correctly with original function, undef plugin skipped' );
102
    is( $units,           29, 'Units are calculated correctly with original function, undef plugin skipped' );
103
    is( $chargable_units, 29, 'Chargable units are calculated correctly with original function, undef plugin skipped' );
104
105
    # initialize valid value plugin
106
    my $not_empty_value_plugin = Koha::Plugin::2_CalcFineNotEmpty->new->enable();
107
108
    ( $amount, $units, $chargable_units ) = CalcFine( $item->unblessed, undef, undef, $due_date, $end_date );
109
110
    is( $amount,          1, 'Amount is calculated correctly with custom function' );
111
    is( $units,           2, 'Units are calculated correctly with custom function' );
112
    is( $chargable_units, 3, 'Chargable units are calculated correctly with custom function' );
113
114
    # initialize bad value plugin
115
    my $bad_value_plugin = Koha::Plugin::3_CalcFineBadValue->new->enable();
116
    $not_empty_value_plugin->disable();
117
118
    ( $amount, $units, $chargable_units ) = CalcFine( $item->unblessed, undef, undef, $due_date, $end_date );
119
120
    is( $amount, 'a', 'Amount is calculated correctly with custom function, bad value returned anyway' );
121
    is( $units,  'b', 'Units are calculated correctly with custom function, bad value returned anyway' );
122
    is(
123
        $chargable_units, undef,
124
        'Chargable units are calculated correctly with custom function, bad value returned anyway'
125
    );
126
127
    Koha::Plugin::Test->new->enable();
128
129
    $empty_value_plugin->disable();
130
    $bad_value_plugin->disable();
131
132
    my $itemnumber   = $item->itemnumber;
133
    my $branchcode   = $library->branchcode;
134
    my $categorycode = $category->categorycode;
135
136
    warnings_like { CalcFine( $item->unblessed, $category->categorycode, $library->branchcode, $due_date, $end_date ); }
137
    [
138
        qr/itemnumber:$itemnumber/,
139
        qr/branchcode:$branchcode/,
140
        qr/categorycode:$categorycode/,
141
        qr/due_date_type:DateTime/,
142
        qr/end_date_type:DateTime/
143
    ],
144
        'Parameters are correct';
145
146
    Koha::Plugins->RemovePlugins();
147
110
    $schema->storage->txn_rollback;
148
    $schema->storage->txn_rollback;
111
};
149
};
(-)a/t/lib/plugins/Koha/Plugin/1_CalcFineEmpty.pm (+43 lines)
Line 0 Link Here
1
package Koha::Plugin::1_CalcFineEmpty;
2
3
use Modern::Perl;
4
5
use base qw(Koha::Plugins::Base);
6
7
our $VERSION  = "0.0.1";
8
our $metadata = {
9
    name            => 'Calc Fine Plugin',
10
    author          => 'Kyle M Hall',
11
    description     => 'Test plugin',
12
    date_authored   => '2013-01-14',
13
    date_updated    => '2013-01-14',
14
    minimum_version => '3.11',
15
    maximum_version => undef,
16
    version         => $VERSION,
17
    namespace       => 'calc_fine_empty',
18
};
19
20
=head1 Methods
21
22
=head2 new
23
24
=cut
25
26
sub new {
27
    my ( $class, $args ) = @_;
28
    $args->{'metadata'} = $metadata;
29
    my $self = $class->SUPER::new($args);
30
    return $self;
31
}
32
33
=head2 overwrite_calc_fine
34
35
=cut
36
37
sub overwrite_calc_fine {
38
    my ( $self, $params ) = @_;
39
40
    return;
41
}
42
43
1;
(-)a/t/lib/plugins/Koha/Plugin/2_CalcFineNotEmpty.pm (+43 lines)
Line 0 Link Here
1
package Koha::Plugin::2_CalcFineNotEmpty;
2
3
use Modern::Perl;
4
5
use base qw(Koha::Plugins::Base);
6
7
our $VERSION  = "0.0.1";
8
our $metadata = {
9
    name            => 'Calc Fine Not Empty Plugin',
10
    author          => 'Kyle M Hall',
11
    description     => 'Test plugin',
12
    date_authored   => '2013-01-14',
13
    date_updated    => '2013-01-14',
14
    minimum_version => '3.11',
15
    maximum_version => undef,
16
    version         => $VERSION,
17
    namespace       => 'calc_fine_not_empty',
18
};
19
20
=head1 Methods
21
22
=head2 new
23
24
=cut
25
26
sub new {
27
    my ( $class, $args ) = @_;
28
    $args->{'metadata'} = $metadata;
29
    my $self = $class->SUPER::new($args);
30
    return $self;
31
}
32
33
=head2 overwrite_calc_fine
34
35
=cut
36
37
sub overwrite_calc_fine {
38
    my ( $self, $params ) = @_;
39
40
    return [ 1, 2, 3 ];
41
}
42
43
1;
(-)a/t/lib/plugins/Koha/Plugin/3_CalcFineBadValue.pm (+43 lines)
Line 0 Link Here
1
package Koha::Plugin::3_CalcFineBadValue;
2
3
use Modern::Perl;
4
5
use base qw(Koha::Plugins::Base);
6
7
our $VERSION  = "0.0.1";
8
our $metadata = {
9
    name            => 'Calc Fine Plugin',
10
    author          => 'Kyle M Hall',
11
    description     => 'Test plugin',
12
    date_authored   => '2013-01-14',
13
    date_updated    => '2013-01-14',
14
    minimum_version => '3.11',
15
    maximum_version => undef,
16
    version         => $VERSION,
17
    namespace       => 'calc_fine_bad_value',
18
};
19
20
=head1 Methods
21
22
=head2 new
23
24
=cut
25
26
sub new {
27
    my ( $class, $args ) = @_;
28
    $args->{'metadata'} = $metadata;
29
    my $self = $class->SUPER::new($args);
30
    return $self;
31
}
32
33
=head2 overwrite_calc_fine
34
35
=cut
36
37
sub overwrite_calc_fine {
38
    my ( $self, $params ) = @_;
39
40
    return [ "a", "b" ];
41
}
42
43
1;
(-)a/t/lib/plugins/Koha/Plugin/Test.pm (-5 / +6 lines)
Lines 451-461 sub auth_client_get_user { Link Here
451
sub overwrite_calc_fine {
451
sub overwrite_calc_fine {
452
    my ( $self, $params ) = @_;
452
    my ( $self, $params ) = @_;
453
453
454
    my $days = $params->{end_date}->delta_days( $params->{due_date} )->delta_days;
454
    warn "itemnumber:" . $params->{itemnumber};
455
    warn "branchcode:" . $params->{branchcode};
456
    warn "categorycode:" . $params->{categorycode};
457
    warn "due_date_type:" . ref( $params->{due_date} );
458
    warn "end_date_type:" . ref( $params->{end_date} );
455
459
456
    return $params->{categorycode} && $params->{branchcode}
460
    return [ 1, 2, 3 ];
457
        ? undef
458
        : [ 3 * $days, $days, $days - 2 ];
459
}
461
}
460
462
461
sub _private_sub {
463
sub _private_sub {
462
- 

Return to bug 39405