Bug 17047: SQL reports management with Mana-KB
[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 use_ok('Koha::Subscription');
36
37 subtest 'Koha::Subscription->biblio' => sub {
38     plan tests => 1;
39
40     my $biblio = Koha::Biblio->new()->store();
41     my $subscription = Koha::Subscription->new({
42         biblionumber => $biblio->biblionumber,
43     })->store();
44
45     my $b = $subscription->biblio;
46     is($b->biblionumber, $biblio->biblionumber, 'Koha::Subscription->biblio returns the correct biblio');
47 };
48
49 subtest 'Notifications on new issues - add_subscriber|remove_subscriber|subscribers' => sub {
50     plan tests => 5;
51     my $subscriber_1 = $builder->build_object( { class => 'Koha::Patrons' });
52     my $subscriber_2 = $builder->build_object( { class => 'Koha::Patrons' });
53
54     my $subscription = Koha::Subscription->new({
55         biblionumber => Koha::Biblio->new->store->biblionumber,
56     })->store();
57
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');
61
62     $subscription->add_subscriber( $subscriber_1 );
63     $subscription->add_subscriber( $subscriber_2 );
64
65     $subscribers = $subscription->subscribers;
66     is( $subscribers->count, 2, '->subscribers should return 2 if there are 2 subscribers' );
67
68     $subscription->remove_subscriber( $subscriber_1 );
69
70     $subscribers = $subscription->subscribers;
71     is( $subscribers->count, 1, '->remove_subscriber should have remove the subscriber' );
72
73     $subscription->remove_subscriber( $subscriber_1 ); # We do not explode if the patron is not a subscriber
74
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' );
77
78 };
79
80 subtest 'Koha::Subscription->vendor' => sub {
81     plan tests => 2;
82     my $vendor = $builder->build( { source => 'Aqbookseller' } );
83     my $subscription = $builder->build(
84         {
85             source => 'Subscription',
86             value  => { aqbooksellerid => $vendor->{id} }
87         }
88     );
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' );
92 };
93
94 subtest 'Koha::Subscription->frequency' => sub {
95     plan tests => 2;
96     my $frequency = $builder->build_object( { class => 'Koha::Subscription::Frequencies' } );
97     my $subscription = $builder->build_object(
98         {
99             class  => 'Koha::Subscriptions',
100             value  => { periodicity => $frequency->id }
101         }
102     );
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' );
105 };
106
107 my $nb_of_subs = Koha::Subscriptions->search->count;
108 my $biblio_1   = $builder->build( { source => 'Biblio' } );
109 my $bi_1       = $builder->build(
110     {
111         source => 'Biblioitem',
112         value  => {
113             biblionumber => $biblio_1->{biblionumber}
114         }
115     }
116 );
117 my $sub_freq_1 = $builder->build( { source => 'SubscriptionFrequency' } );
118 my $sub_np_1   = $builder->build( { source => 'SubscriptionNumberpattern' } );
119 my $sub_1      = $builder->build(
120     {
121         source => 'Subscription',
122         value  => {
123             biblionumber  => $biblio_1->{biblionumber},
124             periodicity   => $sub_freq_1->{id},
125             numberpattern => $sub_np_1->{id}
126         }
127     }
128 );
129
130 is(
131     Koha::Subscriptions->search->count,
132     $nb_of_subs + 1,
133     'The subscription should have been added'
134 );
135 is(
136     $sub_1->{biblionumber},
137     $biblio_1->{biblionumber},
138     'The link between sub and biblio is well done'
139 );
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},
143     $sub_np_1->{id},
144     'The link between sub and sub_numberpattern is well done' );
145
146 my $ref = {
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}
177 };
178
179 is_deeply( Koha::Subscription->get_sharable_info( $sub_1->{subscriptionid} ),
180     $ref, "get_sharable_info function is ok" );
181
182 $schema->storage->txn_rollback;
183
184 1;