3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test
::More tests
=> 10;
25 use Koha
::Subscription
;
26 use Koha
::Subscriptions
;
30 use t
::lib
::TestBuilder
;
32 my $schema = Koha
::Database
->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t
::lib
::TestBuilder
->new;
37 use_ok
('Koha::Subscription');
39 subtest
'Koha::Subscription->biblio' => sub {
42 my $biblio = Koha
::Biblio
->new()->store();
43 my $subscription = Koha
::Subscription
->new({
44 biblionumber
=> $biblio->biblionumber,
47 my $b = $subscription->biblio;
48 is
($b->biblionumber, $biblio->biblionumber, 'Koha::Subscription->biblio returns the correct biblio');
51 subtest
'Notifications on new issues - add_subscriber|remove_subscriber|subscribers' => sub {
53 my $subscriber_1 = $builder->build_object( { class => 'Koha::Patrons' });
54 my $subscriber_2 = $builder->build_object( { class => 'Koha::Patrons' });
56 my $subscription = Koha
::Subscription
->new({
57 biblionumber
=> Koha
::Biblio
->new->store->biblionumber,
60 my $subscribers = $subscription->subscribers;
61 is
( $subscribers->count, 0, '->subscribers should return 0 if there are no subscribers');
62 is
( ref($subscribers), 'Koha::Patrons', '->subscribers should return a Koha::Patrons object');
64 $subscription->add_subscriber( $subscriber_1 );
65 $subscription->add_subscriber( $subscriber_2 );
67 $subscribers = $subscription->subscribers;
68 is
( $subscribers->count, 2, '->subscribers should return 2 if there are 2 subscribers' );
70 $subscription->remove_subscriber( $subscriber_1 );
72 $subscribers = $subscription->subscribers;
73 is
( $subscribers->count, 1, '->remove_subscriber should have remove the subscriber' );
75 $subscription->remove_subscriber( $subscriber_1 ); # We do not explode if the patron is not a subscriber
77 my $is_subscriber = $subscribers->find( $subscriber_2->borrowernumber );
78 ok
( $is_subscriber, 'This structure is used in the code and should work as expected' );
82 subtest
'Koha::Subscription->vendor' => sub {
84 my $vendor = $builder->build( { source
=> 'Aqbookseller' } );
85 my $subscription = $builder->build(
87 source
=> 'Subscription',
88 value
=> { aqbooksellerid
=> $vendor->{id
} }
91 my $object = Koha
::Subscriptions
->find( $subscription->{subscriptionid
} );
92 is
( ref($object->vendor), 'Koha::Acquisition::Bookseller', 'Koha::Subscription->vendor should return a Koha::Acquisition::Bookseller' );
93 is
( $object->vendor->id, $subscription->{aqbooksellerid
}, 'Koha::Subscription->vendor should return the correct vendor' );
96 subtest
'Koha::Subscription->frequency' => sub {
98 my $frequency = $builder->build_object( { class => 'Koha::Subscription::Frequencies' } );
99 my $subscription = $builder->build_object(
101 class => 'Koha::Subscriptions',
102 value
=> { periodicity
=> $frequency->id }
105 is
( ref($subscription->frequency), 'Koha::Subscription::Frequency', 'Koha::Subscription->frequency should return a Koha::Subscription::Frequency' );
106 is
( $subscription->frequency->id, $frequency->id, 'Koha::Subscription->frequency should return the correct frequency' );
109 my $nb_of_subs = Koha
::Subscriptions
->search->count;
110 my $biblio_1 = $builder->build( { source
=> 'Biblio' } );
111 my $bi_1 = $builder->build(
113 source
=> 'Biblioitem',
115 biblionumber
=> $biblio_1->{biblionumber
}
119 my $sub_freq_1 = $builder->build( { source
=> 'SubscriptionFrequency' } );
120 my $sub_np_1 = $builder->build( { source
=> 'SubscriptionNumberpattern' } );
121 my $sub_1 = $builder->build(
123 source
=> 'Subscription',
125 biblionumber
=> $biblio_1->{biblionumber
},
126 periodicity
=> $sub_freq_1->{id
},
127 numberpattern
=> $sub_np_1->{id
}
133 Koha
::Subscriptions
->search->count,
135 'The subscription should have been added'
138 $sub_1->{biblionumber
},
139 $biblio_1->{biblionumber
},
140 'The link between sub and biblio is well done'
142 is
( $sub_1->{periodicity
}, $sub_freq_1->{id
},
143 'The link between sub and sub_freq is well done' );
144 is
( $sub_1->{numberpattern
},
146 'The link between sub and sub_numberpattern is well done' );
149 'title' => $biblio_1->{title
},
150 'sfdescription' => $sub_freq_1->{description
},
151 'unit' => $sub_freq_1->{unit
},
152 'unitsperissue' => $sub_freq_1->{unitsperissue
},
153 'issuesperunit' => $sub_freq_1->{issuesperunit
},
154 'label' => $sub_np_1->{label
},
155 'sndescription' => $sub_np_1->{description
},
156 'numberingmethod' => $sub_np_1->{numberingmethod
},
157 'label' => $sub_np_1->{label
},
158 'label1' => $sub_np_1->{label1
},
159 'add1' => $sub_np_1->{add1
},
160 'every1' => $sub_np_1->{every1
},
161 'whenmorethan1' => $sub_np_1->{whenmorethan1
},
162 'setto1' => $sub_np_1->{setto1
},
163 'numbering1' => $sub_np_1->{numbering1
},
164 'label2' => $sub_np_1->{label2
},
165 'add2' => $sub_np_1->{add2
},
166 'every2' => $sub_np_1->{every2
},
167 'whenmorethan2' => $sub_np_1->{whenmorethan2
},
168 'setto2' => $sub_np_1->{setto2
},
169 'numbering2' => $sub_np_1->{numbering2
},
170 'label3' => $sub_np_1->{label3
},
171 'add3' => $sub_np_1->{add3
},
172 'every3' => $sub_np_1->{every3
},
173 'whenmorethan3' => $sub_np_1->{whenmorethan3
},
174 'setto3' => $sub_np_1->{setto3
},
175 'numbering3' => $sub_np_1->{numbering3
},
176 'issn' => $bi_1->{issn
},
177 'ean' => $bi_1->{ean
},
178 'publishercode' => $bi_1->{publishercode
}
181 is_deeply
( Koha
::Subscription
->get_sharable_info( $sub_1->{subscriptionid
} ),
182 $ref, "get_sharable_info function is ok" );
184 $schema->storage->txn_rollback;