Bug 19855: Move getalert, addalert and delalert to Koha::Subscription
[koha.git] / t / db_dependent / Koha / Acquisition / Booksellers.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 3;
21
22 use t::lib::TestBuilder;
23
24 use C4::Acquisition;
25 use C4::Biblio;
26 use C4::Budgets;
27 use C4::Serials;
28
29 use Koha::Acquisition::Booksellers;
30 use Koha::Database;
31 use Koha::DateUtils;
32
33 my $schema  = Koha::Database->schema();
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest '->baskets() tests' => sub {
37
38     plan tests => 2;
39
40     $schema->storage->txn_begin();
41
42     # Delete existing data
43     $schema->resultset('Aqorder')->delete();
44     $schema->resultset('Aqbasket')->delete();
45     Koha::Acquisition::Booksellers->delete();
46     $schema->resultset('Subscription')->delete();
47     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
48
49     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
50
51     is( $vendor->baskets, 0, 'Vendor has no baskets' );
52
53     # Add two baskets
54     my $basket_1_id = C4::Acquisition::NewBasket( $vendor->id, $patron->borrowernumber, 'basketname1' );
55     my $basket_2_id = C4::Acquisition::NewBasket( $vendor->id, $patron->borrowernumber, 'basketname2' );
56
57     # Re-fetch vendor
58     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
59     is( $vendor->baskets, 2, 'Vendor has two baskets' );
60
61     $schema->storage->txn_rollback();
62 };
63
64 subtest '->subscriptions() tests' => sub {
65
66     plan tests => 5;
67
68     $schema->storage->txn_begin();
69
70     # Delete existing data
71     $schema->resultset('Aqorder')->delete();
72     $schema->resultset('Aqbasket')->delete();
73     Koha::Acquisition::Booksellers->delete();
74     $schema->resultset('Subscription')->delete();
75
76     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
77     is( $vendor->subscriptions->count, 0, 'Vendor has no subscriptions' );
78
79     my $dt_today = dt_from_string;
80     my $today    = output_pref(
81         { dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
82
83     my $dt_today1 = dt_from_string;
84     my $dur5 = DateTime::Duration->new( days => -5 );
85     $dt_today1->add_duration($dur5);
86     my $daysago5 = output_pref(
87         { dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
88
89     my $budgetperiod = C4::Budgets::AddBudgetPeriod(
90         {   budget_period_startdate   => $daysago5,
91             budget_period_enddate     => $today,
92             budget_period_description => "budget desc"
93         }
94     );
95     my $id_budget = AddBudget(
96         {   budget_code      => "CODE",
97             budget_amount    => "123.132",
98             budget_name      => "Budgetname",
99             budget_notes     => "This is a note",
100             budget_period_id => $budgetperiod
101         }
102     );
103     my $bib = MARC::Record->new();
104     $bib->append_fields(
105         MARC::Field->new( '245', ' ', ' ', a => 'Journal of ethnology' ),
106         MARC::Field->new( '500', ' ', ' ', a => 'bib notes' ),
107     );
108     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $bib, '' );
109
110     # Add two subscriptions
111     my $subscription_1_id = NewSubscription(
112         undef,        'BRANCH2',     $vendor->id,          undef,
113         $id_budget,   $biblionumber, '2013-01-01',         undef,
114         undef,        undef,         undef,                undef,
115         undef,        undef,         undef,                undef,
116         undef,        1,             "subscription notes", undef,
117         '2013-01-01', undef,         undef,                undef,
118         'CALL ABC',   0,             "intnotes",           0,
119         undef,        undef,         0,                    undef,
120         '2013-11-30', 0
121     );
122
123     my @subscriptions = SearchSubscriptions( { biblionumber => $biblionumber } );
124     is( $subscriptions[0]->{publicnotes},
125         'subscription notes',
126         'subscription search results include public notes (bug 10689)'
127     );
128
129     my $id_subscription2 = NewSubscription(
130         undef,        'BRANCH2',     $vendor->id,          undef,
131         $id_budget,   $biblionumber, '2013-01-01',         undef,
132         undef,        undef,         undef,                undef,
133         undef,        undef,         undef,                undef,
134         undef,        1,             "subscription notes", undef,
135         '2013-01-01', undef,         undef,                undef,
136         'CALL DEF',   0,             "intnotes",           0,
137         undef,        undef,         0,                    undef,
138         '2013-07-31', 0
139     );
140
141     # Re-fetch vendor
142     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
143     is( $vendor->subscriptions->count, 2, 'Vendor has two subscriptions' );
144     foreach my $subscription ( $vendor->subscriptions ) {
145         is( ref($subscription), 'Koha::Subscription', 'Type is correct' );
146     }
147
148     $schema->storage->txn_rollback();
149 };
150
151 subtest '->contacts() tests' => sub {
152
153     plan tests => 4;
154
155     $schema->storage->txn_begin();
156
157     # Delete existing data
158     $schema->resultset('Aqorder')->delete();
159     $schema->resultset('Aqbasket')->delete();
160     Koha::Acquisition::Booksellers->delete();
161     $schema->resultset('Subscription')->delete();
162
163     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
164
165     is( $vendor->contacts->count, 0, 'Vendor has no contacts' );
166
167     # Add two contacts
168     my $contact_1 = $builder->build_object(
169         {   class => 'Koha::Acquisition::Bookseller::Contacts',
170             value => { booksellerid => $vendor->id }
171         }
172     );
173     my $contact_2 = $builder->build_object(
174         {   class => 'Koha::Acquisition::Bookseller::Contacts',
175             value => { booksellerid => $vendor->id }
176         }
177     );
178
179     # Re-fetch vendor
180     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
181     is( $vendor->contacts->count, 2, 'Vendor has two contacts' );
182     foreach my $contact ( $vendor->contacts ) {
183         is( ref($contact), 'Koha::Acquisition::Bookseller::Contact', 'Type is correct' );
184     }
185
186     $schema->storage->txn_rollback();
187 };