4 use Test::More tests => 1;
7 use t::lib::TestBuilder;
9 use Koha::CirculationRules;
12 my $schema = Koha::Database->new->schema;
13 $schema->storage->txn_begin;
14 our $builder = t::lib::TestBuilder->new;
15 our $cache = Koha::Caches->get_instance;
17 subtest 'guess_article_requestable_itemtypes' => sub {
20 t::lib::Mocks::mock_preference('ArticleRequests', 1);
21 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'calc');
22 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
23 Koha::CirculationRules->delete;
24 my $library = $builder->build_object({ class => 'Koha::Libraries' });
25 my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
26 my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
27 my $catg1 = $builder->build_object({ class => 'Koha::Patron::Categories' });
28 my $catg2 = $builder->build_object({ class => 'Koha::Patron::Categories' });
29 my $rule1 = Koha::CirculationRules->set_rule(
31 branchcode => $library->branchcode,
32 categorycode => undef,
33 itemtype => $itype1->itemtype,
34 rule_name => 'article_requests',
35 rule_value => 'bib_only',
38 my $rule2 = Koha::CirculationRules->set_rule(
41 categorycode => $catg1->categorycode,
42 itemtype => $itype2->itemtype,
43 rule_name => 'article_requests',
48 my $res = Koha::CirculationRules->guess_article_requestable_itemtypes;
49 is( $res->{'*'}, undef, 'Item type * seems not permitted' );
50 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
51 is( $res->{$itype2->itemtype}, 1, 'Item type 2 seems permitted' );
52 $res = Koha::CirculationRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
53 is( $res->{'*'}, undef, 'Item type * seems not permitted' );
54 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
55 is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
58 $rule2->itemtype(undef)->store;
59 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
60 $res = Koha::CirculationRules->guess_article_requestable_itemtypes;
61 is( $res->{'*'}, 1, 'Item type * seems permitted' );
62 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
63 is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
64 $res = Koha::CirculationRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
65 is( $res->{'*'}, undef, 'Item type * seems not permitted' );
66 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
67 is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
69 # Finally test the overriding pref
70 t::lib::Mocks::mock_preference('ArticleRequestsLinkControl', 'always');
71 $res = Koha::CirculationRules->guess_article_requestable_itemtypes({});
72 is( $res->{'*'}, 1, 'Override algorithm with pref setting' );
74 $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
77 $schema->storage->txn_rollback;