Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Koha / IssuingRules / guess_article_requestable_itemtypes.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 1;
5
6 use t::lib::Mocks;
7 use t::lib::TestBuilder;
8 use Koha::Database;
9 use Koha::CirculationRules;
10 use Koha::Caches;
11
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;
16
17 subtest 'guess_article_requestable_itemtypes' => sub {
18     plan tests => 13;
19
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(
30         {
31             branchcode   => $library->branchcode,
32             categorycode => undef,
33             itemtype     => $itype1->itemtype,
34             rule_name    => 'article_requests',
35             rule_value   => 'bib_only',
36         },
37     );
38     my $rule2 = Koha::CirculationRules->set_rule(
39         {
40             branchcode   => undef,
41             categorycode => $catg1->categorycode,
42             itemtype     => $itype2->itemtype,
43             rule_name    => 'article_requests',
44             rule_value   => 'yes',
45         },
46     );
47
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' );
56
57     # Change the rules
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' );
68
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' );
73
74     $cache->clear_from_cache( Koha::CirculationRules::GUESSED_ITEMTYPES_KEY );
75 };
76
77 $schema->storage->txn_rollback;