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 use_ok('Koha::Subscription');
37 subtest 'Koha::Subscription->biblio' => sub {
40 my $biblio = Koha::Biblio->new()->store();
41 my $subscription = Koha::Subscription->new({
42 biblionumber => $biblio->biblionumber,
45 my $b = $subscription->biblio;
46 is($b->biblionumber, $biblio->biblionumber, 'Koha::Subscription->biblio returns the correct biblio');
49 subtest 'Notifications on new issues - add_subscriber|remove_subscriber|subscribers' => sub {
51 my $subscriber_1 = $builder->build_object( { class => 'Koha::Patrons' });
52 my $subscriber_2 = $builder->build_object( { class => 'Koha::Patrons' });
54 my $subscription = Koha::Subscription->new({
55 biblionumber => Koha::Biblio->new->store->biblionumber,
58 my $subscribers = $subscription->subscribers;
59 is( $subscribers->count, 0, '->subscribers should return 0 if there are no subscribers');
60 is( ref($subscribers), 'Koha::Patrons', '->subscribers should return a Koha::Patrons object');
62 $subscription->add_subscriber( $subscriber_1 );
63 $subscription->add_subscriber( $subscriber_2 );
65 $subscribers = $subscription->subscribers;
66 is( $subscribers->count, 2, '->subscribers should return 2 if there are 2 subscribers' );
68 $subscription->remove_subscriber( $subscriber_1 );
70 $subscribers = $subscription->subscribers;
71 is( $subscribers->count, 1, '->remove_subscriber should have remove the subscriber' );
73 $subscription->remove_subscriber( $subscriber_1 ); # We do not explode if the patron is not a subscriber
75 my $is_subscriber = $subscribers->find( $subscriber_2->borrowernumber );
76 ok( $is_subscriber, 'This structure is used in the code and should work as expected' );
80 subtest 'Koha::Subscription->vendor' => sub {
82 my $vendor = $builder->build( { source => 'Aqbookseller' } );
83 my $subscription = $builder->build(
85 source => 'Subscription',
86 value => { aqbooksellerid => $vendor->{id} }
89 my $object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
90 is( ref($object->vendor), 'Koha::Acquisition::Bookseller', 'Koha::Subscription->vendor should return a Koha::Acquisition::Bookseller' );
91 is( $object->vendor->id, $subscription->{aqbooksellerid}, 'Koha::Subscription->vendor should return the correct vendor' );
94 subtest 'Koha::Subscription->frequency' => sub {
96 my $frequency = $builder->build_object( { class => 'Koha::Subscription::Frequencies' } );
97 my $subscription = $builder->build_object(
99 class => 'Koha::Subscriptions',
100 value => { periodicity => $frequency->id }
103 is( ref($subscription->frequency), 'Koha::Subscription::Frequency', 'Koha::Subscription->frequency should return a Koha::Subscription::Frequency' );
104 is( $subscription->frequency->id, $frequency->id, 'Koha::Subscription->frequency should return the correct frequency' );
107 my $nb_of_subs = Koha::Subscriptions->search->count;
108 my $biblio_1 = $builder->build( { source => 'Biblio' } );
109 my $bi_1 = $builder->build(
111 source => 'Biblioitem',
113 biblionumber => $biblio_1->{biblionumber}
117 my $sub_freq_1 = $builder->build( { source => 'SubscriptionFrequency' } );
118 my $sub_np_1 = $builder->build( { source => 'SubscriptionNumberpattern' } );
119 my $sub_1 = $builder->build(
121 source => 'Subscription',
123 biblionumber => $biblio_1->{biblionumber},
124 periodicity => $sub_freq_1->{id},
125 numberpattern => $sub_np_1->{id}
131 Koha::Subscriptions->search->count,
133 'The subscription should have been added'
136 $sub_1->{biblionumber},
137 $biblio_1->{biblionumber},
138 'The link between sub and biblio is well done'
140 is( $sub_1->{periodicity}, $sub_freq_1->{id},
141 'The link between sub and sub_freq is well done' );
142 is( $sub_1->{numberpattern},
144 'The link between sub and sub_numberpattern is well done' );
147 'title' => $biblio_1->{title},
148 'sfdescription' => $sub_freq_1->{description},
149 'unit' => $sub_freq_1->{unit},
150 'unitsperissue' => $sub_freq_1->{unitsperissue},
151 'issuesperunit' => $sub_freq_1->{issuesperunit},
152 'label' => $sub_np_1->{label},
153 'sndescription' => $sub_np_1->{description},
154 'numberingmethod' => $sub_np_1->{numberingmethod},
155 'label' => $sub_np_1->{label},
156 'label1' => $sub_np_1->{label1},
157 'add1' => $sub_np_1->{add1},
158 'every1' => $sub_np_1->{every1},
159 'whenmorethan1' => $sub_np_1->{whenmorethan1},
160 'setto1' => $sub_np_1->{setto1},
161 'numbering1' => $sub_np_1->{numbering1},
162 'label2' => $sub_np_1->{label2},
163 'add2' => $sub_np_1->{add2},
164 'every2' => $sub_np_1->{every2},
165 'whenmorethan2' => $sub_np_1->{whenmorethan2},
166 'setto2' => $sub_np_1->{setto2},
167 'numbering2' => $sub_np_1->{numbering2},
168 'label3' => $sub_np_1->{label3},
169 'add3' => $sub_np_1->{add3},
170 'every3' => $sub_np_1->{every3},
171 'whenmorethan3' => $sub_np_1->{whenmorethan3},
172 'setto3' => $sub_np_1->{setto3},
173 'numbering3' => $sub_np_1->{numbering3},
174 'issn' => $bi_1->{issn},
175 'ean' => $bi_1->{ean},
176 'publishercode' => $bi_1->{publishercode}
179 is_deeply( Koha::Subscription->get_sharable_info( $sub_1->{subscriptionid} ),
180 $ref, "get_sharable_info function is ok" );
182 $schema->storage->txn_rollback;