Bug 21474: Add the Koha::Subscription->frequency method
[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 => 4;
23
24 use Koha::Subscriptions;
25 use Koha::Biblio;
26
27 use t::lib::TestBuilder;
28
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
31
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'Koha::Subscription->biblio' => sub {
35     plan tests => 1;
36
37     my $biblio = Koha::Biblio->new()->store();
38     my $subscription = Koha::Subscription->new({
39         biblionumber => $biblio->biblionumber,
40     })->store();
41
42     my $b = $subscription->biblio;
43     is($b->biblionumber, $biblio->biblionumber, 'Koha::Subscription->biblio returns the correct biblio');
44 };
45
46 subtest 'Notifications on new issues - add_subscriber|remove_subscriber|subscribers' => sub {
47     plan tests => 5;
48     my $subscriber_1 = $builder->build_object( { class => 'Koha::Patrons' });
49     my $subscriber_2 = $builder->build_object( { class => 'Koha::Patrons' });
50
51     my $subscription = Koha::Subscription->new({
52         biblionumber => Koha::Biblio->new->store->biblionumber,
53     })->store();
54
55     my $subscribers = $subscription->subscribers;
56     is( $subscribers->count, 0, '->subscribers should return 0 if there are no subscribers');
57     is( ref($subscribers), 'Koha::Patrons', '->subscribers should return a Koha::Patrons object');
58
59     $subscription->add_subscriber( $subscriber_1 );
60     $subscription->add_subscriber( $subscriber_2 );
61
62     $subscribers = $subscription->subscribers;
63     is( $subscribers->count, 2, '->subscribers should return 2 if there are 2 subscribers' );
64
65     $subscription->remove_subscriber( $subscriber_1 );
66
67     $subscribers = $subscription->subscribers;
68     is( $subscribers->count, 1, '->remove_subscriber should have remove the subscriber' );
69
70     $subscription->remove_subscriber( $subscriber_1 ); # We do not explode if the patron is not a subscriber
71
72     my $is_subscriber = $subscribers->find( $subscriber_2->borrowernumber );
73     ok( $is_subscriber, 'This structure is used in the code and should work as expected' );
74
75 };
76
77 subtest 'Koha::Subscription->vendor' => sub {
78     plan tests => 2;
79     my $vendor = $builder->build( { source => 'Aqbookseller' } );
80     my $subscription = $builder->build(
81         {
82             source => 'Subscription',
83             value  => { aqbooksellerid => $vendor->{id} }
84         }
85     );
86     my $object = Koha::Subscriptions->find( $subscription->{subscriptionid} );
87     is( ref($object->vendor), 'Koha::Acquisition::Bookseller', 'Koha::Subscription->vendor should return a Koha::Acquisition::Bookseller' );
88     is( $object->vendor->id, $subscription->{aqbooksellerid}, 'Koha::Subscription->vendor should return the correct vendor' );
89 };
90
91 subtest 'Koha::Subscription->frequency' => sub {
92     plan tests => 2;
93     my $frequency = $builder->build_object( { class => 'Koha::Subscription::Frequencies' } );
94     my $subscription = $builder->build_object(
95         {
96             class  => 'Koha::Subscriptions',
97             value  => { periodicity => $frequency->id }
98         }
99     );
100     is( ref($subscription->frequency), 'Koha::Subscription::Frequency', 'Koha::Subscription->frequency should return a Koha::Subscription::Frequency' );
101     is( $subscription->frequency->id, $frequency->id, 'Koha::Subscription->frequency should return the correct frequency' );
102 };
103
104 $schema->storage->txn_rollback;
105
106 1;