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

(-)a/Koha/IssuingRules.pm (-3 / +6 lines)
Lines 21-31 package Koha::IssuingRules; Link Here
21
use Modern::Perl;
21
use Modern::Perl;
22
22
23
use Koha::Database;
23
use Koha::Database;
24
use Koha::Caches;
24
25
25
use Koha::IssuingRule;
26
use Koha::IssuingRule;
26
27
27
use base qw(Koha::Objects);
28
use base qw(Koha::Objects);
28
29
30
use constant GUESSED_ITEMTYPES_KEY => 'Koha_IssuingRules_last_guess';
31
29
=head1 NAME
32
=head1 NAME
30
33
31
Koha::IssuingRules - Koha IssuingRule Object set class
34
Koha::IssuingRules - Koha IssuingRule Object set class
Lines 157-169 sub article_requestable_rules { Link Here
157
160
158
=cut
161
=cut
159
162
160
our $last_article_requestable_guesses; # used during Plack life time
161
162
sub guess_article_requestable_itemtypes {
163
sub guess_article_requestable_itemtypes {
163
    my ( $class_or_self, $params ) = @_;
164
    my ( $class_or_self, $params ) = @_;
164
    my $category = $params->{categorycode};
165
    my $category = $params->{categorycode};
165
    return {} if !C4::Context->preference('ArticleRequests');
166
    return {} if !C4::Context->preference('ArticleRequests');
166
167
168
    my $cache = Koha::Caches->get_instance;
169
    my $last_article_requestable_guesses = $cache->get_from_cache(GUESSED_ITEMTYPES_KEY);
167
    my $key = $category || '*';
170
    my $key = $category || '*';
168
    return $last_article_requestable_guesses->{$key}
171
    return $last_article_requestable_guesses->{$key}
169
        if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
172
        if $last_article_requestable_guesses && exists $last_article_requestable_guesses->{$key};
Lines 176-182 sub guess_article_requestable_itemtypes { Link Here
176
    foreach my $rule ( $rules->as_list ) {
179
    foreach my $rule ( $rules->as_list ) {
177
        $res->{ $rule->itemtype } = 1;
180
        $res->{ $rule->itemtype } = 1;
178
    }
181
    }
179
    $last_article_requestable_guesses->{$key} = $res;
182
    $cache->set_in_cache(GUESSED_ITEMTYPES_KEY, $res);
180
    return $res;
183
    return $res;
181
}
184
}
182
185
(-)a/admin/smart-rules.pl (+4 lines)
Lines 33-38 use Koha::RefundLostItemFeeRule; Link Here
33
use Koha::RefundLostItemFeeRules;
33
use Koha::RefundLostItemFeeRules;
34
use Koha::Libraries;
34
use Koha::Libraries;
35
use Koha::Patron::Categories;
35
use Koha::Patron::Categories;
36
use Koha::Caches;
36
37
37
my $input = CGI->new;
38
my $input = CGI->new;
38
my $dbh = C4::Context->dbh;
39
my $dbh = C4::Context->dbh;
Lines 64-69 $branch = '*' if $branch eq 'NO_LIBRARY_SET'; Link Here
64
my $op = $input->param('op') || q{};
65
my $op = $input->param('op') || q{};
65
my $language = C4::Languages::getlanguage();
66
my $language = C4::Languages::getlanguage();
66
67
68
my $cache = Koha::Caches->get_instance;
69
$cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
70
67
if ($op eq 'delete') {
71
if ($op eq 'delete') {
68
    my $itemtype     = $input->param('itemtype');
72
    my $itemtype     = $input->param('itemtype');
69
    my $categorycode = $input->param('categorycode');
73
    my $categorycode = $input->param('categorycode');
(-)a/t/db_dependent/ArticleRequests.t (-3 / +5 lines)
Lines 30-35 use Koha::Notice::Messages; Link Here
30
use Koha::Patron;
30
use Koha::Patron;
31
use Koha::Library::Group;
31
use Koha::Library::Group;
32
use Koha::IssuingRules;
32
use Koha::IssuingRules;
33
use Koha::Caches;
33
34
34
BEGIN {
35
BEGIN {
35
    use_ok('Koha::ArticleRequest');
36
    use_ok('Koha::ArticleRequest');
Lines 40-45 BEGIN { Link Here
40
my $schema = Koha::Database->new()->schema();
41
my $schema = Koha::Database->new()->schema();
41
$schema->storage->txn_begin();
42
$schema->storage->txn_begin();
42
my $builder = t::lib::TestBuilder->new;
43
my $builder = t::lib::TestBuilder->new;
44
our $cache = Koha::Caches->get_instance;
43
45
44
my $dbh = C4::Context->dbh;
46
my $dbh = C4::Context->dbh;
45
$dbh->{RaiseError} = 1;
47
$dbh->{RaiseError} = 1;
Lines 223-233 subtest 'may_article_request' => sub { Link Here
223
225
224
    # mocking
226
    # mocking
225
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
227
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
226
    $Koha::IssuingRules::last_article_requestable_guesses = {
228
    $cache->set_in_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY, {
227
        '*'  => { 'CR' => 1 },
229
        '*'  => { 'CR' => 1 },
228
        'S'  => { '*'  => 1 },
230
        'S'  => { '*'  => 1 },
229
        'PT' => { 'BK' => 1 },
231
        'PT' => { 'BK' => 1 },
230
    };
232
    });
231
233
232
    # tests for class method call
234
    # tests for class method call
233
    is( Koha::Biblio->may_article_request({ itemtype => 'CR' }), 1, 'SER/* should be true' );
235
    is( Koha::Biblio->may_article_request({ itemtype => 'CR' }), 1, 'SER/* should be true' );
Lines 243-249 subtest 'may_article_request' => sub { Link Here
243
    is( $biblio->may_article_request({ categorycode => 'PT' }), 1, 'BK/PT true' );
245
    is( $biblio->may_article_request({ categorycode => 'PT' }), 1, 'BK/PT true' );
244
246
245
    # Cleanup
247
    # Cleanup
246
    $Koha::IssuingRules::last_article_requestable_guesses = undef;
248
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
247
};
249
};
248
250
249
$schema->storage->txn_rollback();
251
$schema->storage->txn_rollback();
(-)a/t/db_dependent/Koha/IssuingRules/guess_article_requestable_itemtypes.t (-3 / +5 lines)
Lines 7-21 use t::lib::Mocks; Link Here
7
use t::lib::TestBuilder;
7
use t::lib::TestBuilder;
8
use Koha::Database;
8
use Koha::Database;
9
use Koha::IssuingRules;
9
use Koha::IssuingRules;
10
use Koha::Caches;
10
11
11
my $schema = Koha::Database->new->schema;
12
my $schema = Koha::Database->new->schema;
12
$schema->storage->txn_begin;
13
$schema->storage->txn_begin;
13
our $builder = t::lib::TestBuilder->new;
14
our $builder = t::lib::TestBuilder->new;
15
our $cache = Koha::Caches->get_instance;
14
16
15
subtest 'guess_article_requestable_itemtypes' => sub {
17
subtest 'guess_article_requestable_itemtypes' => sub {
16
    plan tests => 12;
18
    plan tests => 12;
17
19
18
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
20
    t::lib::Mocks::mock_preference('ArticleRequests', 1);
21
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
19
    Koha::IssuingRules->delete;
22
    Koha::IssuingRules->delete;
20
    my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
23
    my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
21
    my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
24
    my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
Lines 51-57 subtest 'guess_article_requestable_itemtypes' => sub { Link Here
51
54
52
    # Change the rules
55
    # Change the rules
53
    $rule2->itemtype('*')->store;
56
    $rule2->itemtype('*')->store;
54
    $Koha::IssuingRules::last_article_requestable_guesses = {};
57
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
55
    $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
58
    $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
56
    is( $res->{'*'}, 1, 'Item type * seems permitted' );
59
    is( $res->{'*'}, 1, 'Item type * seems permitted' );
57
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
60
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
Lines 61-67 subtest 'guess_article_requestable_itemtypes' => sub { Link Here
61
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
64
    is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
62
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
65
    is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
63
66
64
    $Koha::IssuingRules::last_article_requestable_guesses = {};
67
    $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
65
};
68
};
66
69
67
$schema->storage->txn_rollback;
70
$schema->storage->txn_rollback;
68
- 

Return to bug 17530