Bug 17530: (QA follow-up) Replace our variable by cached value
[koha.git] / t / db_dependent / Koha / IssuingRules / guess_article_requestable_itemtypes.t
blob55529181f3414e51cdfe245920ca1936b52c5cbf
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 1;
6 use t::lib::Mocks;
7 use t::lib::TestBuilder;
8 use Koha::Database;
9 use Koha::IssuingRules;
10 use Koha::Caches;
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 {
18 plan tests => 12;
20 t::lib::Mocks::mock_preference('ArticleRequests', 1);
21 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
22 Koha::IssuingRules->delete;
23 my $itype1 = $builder->build_object({ class => 'Koha::ItemTypes' });
24 my $itype2 = $builder->build_object({ class => 'Koha::ItemTypes' });
25 my $catg1 = $builder->build_object({ class => 'Koha::Patron::Categories' });
26 my $catg2 = $builder->build_object({ class => 'Koha::Patron::Categories' });
27 my $rule1 = $builder->build_object({
28 class => 'Koha::IssuingRules',
29 value => {
30 branchcode => 'MPL', # no worries: no FK
31 categorycode => '*',
32 itemtype => $itype1->itemtype,
33 article_requests => 'bib_only',
35 });
36 my $rule2 = $builder->build_object({
37 class => 'Koha::IssuingRules',
38 value => {
39 branchcode => '*',
40 categorycode => $catg1->categorycode,
41 itemtype => $itype2->itemtype,
42 article_requests => 'yes',
44 });
46 my $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
47 is( $res->{'*'}, undef, 'Item type * seems not permitted' );
48 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
49 is( $res->{$itype2->itemtype}, 1, 'Item type 2 seems permitted' );
50 $res = Koha::IssuingRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
51 is( $res->{'*'}, undef, 'Item type * seems not permitted' );
52 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
53 is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
55 # Change the rules
56 $rule2->itemtype('*')->store;
57 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
58 $res = Koha::IssuingRules->guess_article_requestable_itemtypes;
59 is( $res->{'*'}, 1, 'Item type * seems permitted' );
60 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
61 is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
62 $res = Koha::IssuingRules->guess_article_requestable_itemtypes({ categorycode => $catg2->categorycode });
63 is( $res->{'*'}, undef, 'Item type * seems not permitted' );
64 is( $res->{$itype1->itemtype}, 1, 'Item type 1 seems permitted' );
65 is( $res->{$itype2->itemtype}, undef, 'Item type 2 seems not permitted' );
67 $cache->clear_from_cache( Koha::IssuingRules::GUESSED_ITEMTYPES_KEY );
70 $schema->storage->txn_rollback;