Bug 26922: Regression tests
[koha.git] / t / db_dependent / Koha / Subscription.t
blob6765640d3810d4053813652d6784eadc5ecdc24e
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 10;
24 use Koha::Database;
25 use Koha::Subscription;
26 use Koha::Subscriptions;
27 use Koha::Biblio;
28 use Koha::Biblios;
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 {
40 plan tests => 1;
42 my $biblio = Koha::Biblio->new()->store();
43 my $subscription = Koha::Subscription->new({
44 biblionumber => $biblio->biblionumber,
45 })->store();
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 {
52 plan tests => 5;
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,
58 })->store();
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 {
83 plan tests => 2;
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 {
97 plan tests => 2;
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_sample_biblio;
111 my $bi_1 = $biblio_1->biblioitem;
112 my $sub_freq_1 = $builder->build( { source => 'SubscriptionFrequency' } );
113 my $sub_np_1 = $builder->build( { source => 'SubscriptionNumberpattern' } );
114 my $sub_1 = $builder->build(
116 source => 'Subscription',
117 value => {
118 biblionumber => $biblio_1->biblionumber,
119 periodicity => $sub_freq_1->{id},
120 numberpattern => $sub_np_1->{id}
126 Koha::Subscriptions->search->count,
127 $nb_of_subs + 1,
128 'The subscription should have been added'
131 $sub_1->{biblionumber},
132 $biblio_1->biblionumber,
133 'The link between sub and biblio is well done'
135 is( $sub_1->{periodicity}, $sub_freq_1->{id},
136 'The link between sub and sub_freq is well done' );
137 is( $sub_1->{numberpattern},
138 $sub_np_1->{id},
139 'The link between sub and sub_numberpattern is well done' );
141 my $ref = {
142 'title' => $biblio_1->title,
143 'sfdescription' => $sub_freq_1->{description},
144 'unit' => $sub_freq_1->{unit},
145 'unitsperissue' => $sub_freq_1->{unitsperissue},
146 'issuesperunit' => $sub_freq_1->{issuesperunit},
147 'label' => $sub_np_1->{label},
148 'sndescription' => $sub_np_1->{description},
149 'numberingmethod' => $sub_np_1->{numberingmethod},
150 'label' => $sub_np_1->{label},
151 'label1' => $sub_np_1->{label1},
152 'add1' => $sub_np_1->{add1},
153 'every1' => $sub_np_1->{every1},
154 'whenmorethan1' => $sub_np_1->{whenmorethan1},
155 'setto1' => $sub_np_1->{setto1},
156 'numbering1' => $sub_np_1->{numbering1},
157 'label2' => $sub_np_1->{label2},
158 'add2' => $sub_np_1->{add2},
159 'every2' => $sub_np_1->{every2},
160 'whenmorethan2' => $sub_np_1->{whenmorethan2},
161 'setto2' => $sub_np_1->{setto2},
162 'numbering2' => $sub_np_1->{numbering2},
163 'label3' => $sub_np_1->{label3},
164 'add3' => $sub_np_1->{add3},
165 'every3' => $sub_np_1->{every3},
166 'whenmorethan3' => $sub_np_1->{whenmorethan3},
167 'setto3' => $sub_np_1->{setto3},
168 'numbering3' => $sub_np_1->{numbering3},
169 'issn' => $bi_1->issn,
170 'ean' => $bi_1->ean,
171 'publishercode' => $bi_1->publishercode,
174 is_deeply( Koha::Subscription->get_sharable_info( $sub_1->{subscriptionid} ),
175 $ref, "get_sharable_info function is ok" );
177 $schema->storage->txn_rollback;