Bug 17047: add a dedicated page for Mana setup
[koha.git] / t / db_dependent / Koha / Subscription.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 10;
23
24 use Koha::Database;
25 use Koha::Subscription;
26 use Koha::Subscriptions;
27 use Koha::Biblio;
28 use Koha::Biblios;
29
30 use t::lib::TestBuilder;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder     = t::lib::TestBuilder->new;
36
37 use_ok('Koha::Subscription');
38
39 subtest 'Koha::Subscription->biblio' => sub {
40     plan tests => 1;
41
42     my $biblio = Koha::Biblio->new()->store();
43     my $subscription = Koha::Subscription->new({
44         biblionumber => $biblio->biblionumber,
45     })->store();
46
47     my $b = $subscription->biblio;
48     is($b->biblionumber, $biblio->biblionumber, 'Koha::Subscription->biblio returns the correct biblio');
49 };
50
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' });
55
56     my $subscription = Koha::Subscription->new({
57         biblionumber => Koha::Biblio->new->store->biblionumber,
58     })->store();
59
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');
63
64     $subscription->add_subscriber( $subscriber_1 );
65     $subscription->add_subscriber( $subscriber_2 );
66
67     $subscribers = $subscription->subscribers;
68     is( $subscribers->count, 2, '->subscribers should return 2 if there are 2 subscribers' );
69
70     $subscription->remove_subscriber( $subscriber_1 );
71
72     $subscribers = $subscription->subscribers;
73     is( $subscribers->count, 1, '->remove_subscriber should have remove the subscriber' );
74
75     $subscription->remove_subscriber( $subscriber_1 ); # We do not explode if the patron is not a subscriber
76
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' );
79
80 };
81
82 subtest 'Koha::Subscription->vendor' => sub {
83     plan tests => 2;
84     my $vendor = $builder->build( { source => 'Aqbookseller' } );
85     my $subscription = $builder->build(
86         {
87             source => 'Subscription',
88             value  => { aqbooksellerid => $vendor->{id} }
89         }
90     );
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' );
94 };
95
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(
100         {
101             class  => 'Koha::Subscriptions',
102             value  => { periodicity => $frequency->id }
103         }
104     );
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' );
107 };
108
109 my $nb_of_subs = Koha::Subscriptions->search->count;
110 my $biblio_1   = $builder->build( { source => 'Biblio' } );
111 my $bi_1       = $builder->build(
112     {
113         source => 'Biblioitem',
114         value  => {
115             biblionumber => $biblio_1->{biblionumber}
116         }
117     }
118 );
119 my $sub_freq_1 = $builder->build( { source => 'SubscriptionFrequency' } );
120 my $sub_np_1   = $builder->build( { source => 'SubscriptionNumberpattern' } );
121 my $sub_1      = $builder->build(
122     {
123         source => 'Subscription',
124         value  => {
125             biblionumber  => $biblio_1->{biblionumber},
126             periodicity   => $sub_freq_1->{id},
127             numberpattern => $sub_np_1->{id}
128         }
129     }
130 );
131
132 is(
133     Koha::Subscriptions->search->count,
134     $nb_of_subs + 1,
135     'The subscription should have been added'
136 );
137 is(
138     $sub_1->{biblionumber},
139     $biblio_1->{biblionumber},
140     'The link between sub and biblio is well done'
141 );
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},
145     $sub_np_1->{id},
146     'The link between sub and sub_numberpattern is well done' );
147
148 my $ref = {
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}
179 };
180
181 is_deeply( Koha::Subscription->get_sharable_info( $sub_1->{subscriptionid} ),
182     $ref, "get_sharable_info function is ok" );
183
184 $schema->storage->txn_rollback;
185
186 1;