Bug 19130: (followup) Add t/db_dependent/Koha/Acquisition/Booksellers.t
[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
48     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
49
50     is( $vendor->baskets, 0, 'Vendor has no baskets' );
51
52     # Add two baskets
53     my $basket_1_id = C4::Acquisition::NewBasket( $vendor->id, 'authorizedby1', 'basketname1' );
54     my $basket_2_id = C4::Acquisition::NewBasket( $vendor->id, 'authorizedby2', 'basketname2' );
55
56     # Re-fetch vendor
57     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
58     is( $vendor->baskets, 2, 'Vendor has two baskets' );
59
60     $schema->storage->txn_rollback();
61 };
62
63 subtest '->subscriptions() tests' => sub {
64
65     plan tests => 5;
66
67     $schema->storage->txn_begin();
68
69     # Delete existing data
70     $schema->resultset('Aqorder')->delete();
71     $schema->resultset('Aqbasket')->delete();
72     Koha::Acquisition::Booksellers->delete();
73     $schema->resultset('Subscription')->delete();
74
75     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
76     is( $vendor->subscriptions->count, 0, 'Vendor has no subscriptions' );
77
78     my $dt_today = dt_from_string;
79     my $today    = output_pref(
80         { dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
81
82     my $dt_today1 = dt_from_string;
83     my $dur5 = DateTime::Duration->new( days => -5 );
84     $dt_today1->add_duration($dur5);
85     my $daysago5 = output_pref(
86         { dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
87
88     my $budgetperiod = C4::Budgets::AddBudgetPeriod(
89         {   budget_period_startdate   => $daysago5,
90             budget_period_enddate     => $today,
91             budget_period_description => "budget desc"
92         }
93     );
94     my $id_budget = AddBudget(
95         {   budget_code      => "CODE",
96             budget_amount    => "123.132",
97             budget_name      => "Budgetname",
98             budget_notes     => "This is a note",
99             budget_period_id => $budgetperiod
100         }
101     );
102     my $bib = MARC::Record->new();
103     $bib->append_fields(
104         MARC::Field->new( '245', ' ', ' ', a => 'Journal of ethnology' ),
105         MARC::Field->new( '500', ' ', ' ', a => 'bib notes' ),
106     );
107     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $bib, '' );
108
109     # Add two subscriptions
110     my $subscription_1_id = NewSubscription(
111         undef,        'BRANCH2',     $vendor->id,          undef,
112         $id_budget,   $biblionumber, '01-01-2013',         undef,
113         undef,        undef,         undef,                undef,
114         undef,        undef,         undef,                undef,
115         undef,        1,             "subscription notes", undef,
116         '01-01-2013', undef,         undef,                undef,
117         'CALL ABC',   0,             "intnotes",           0,
118         undef,        undef,         0,                    undef,
119         '2013-11-30', 0
120     );
121
122     my @subscriptions = SearchSubscriptions( { biblionumber => $biblionumber } );
123     is( $subscriptions[0]->{publicnotes},
124         'subscription notes',
125         'subscription search results include public notes (bug 10689)'
126     );
127
128     my $id_subscription2 = NewSubscription(
129         undef,        'BRANCH2',     $vendor->id,          undef,
130         $id_budget,   $biblionumber, '01-01-2013',         undef,
131         undef,        undef,         undef,                undef,
132         undef,        undef,         undef,                undef,
133         undef,        1,             "subscription notes", undef,
134         '01-01-2013', undef,         undef,                undef,
135         'CALL DEF',   0,             "intnotes",           0,
136         undef,        undef,         0,                    undef,
137         '2013-07-31', 0
138     );
139
140     # Re-fetch vendor
141     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
142     is( $vendor->subscriptions->count, 2, 'Vendor has two subscriptions' );
143     foreach my $subscription ( $vendor->subscriptions ) {
144         is( ref($subscription), 'Koha::Subscription', 'Type is correct' );
145     }
146
147     $schema->storage->txn_rollback();
148 };
149
150 subtest '->contacts() tests' => sub {
151
152     plan tests => 4;
153
154     $schema->storage->txn_begin();
155
156     # Delete existing data
157     $schema->resultset('Aqorder')->delete();
158     $schema->resultset('Aqbasket')->delete();
159     Koha::Acquisition::Booksellers->delete();
160     $schema->resultset('Subscription')->delete();
161
162     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
163
164     is( $vendor->contacts->count, 0, 'Vendor has no contacts' );
165
166     # Add two contacts
167     my $contact_1 = $builder->build_object(
168         {   class => 'Koha::Acquisition::Bookseller::Contacts',
169             value => { booksellerid => $vendor->id }
170         }
171     );
172     my $contact_2 = $builder->build_object(
173         {   class => 'Koha::Acquisition::Bookseller::Contacts',
174             value => { booksellerid => $vendor->id }
175         }
176     );
177
178     # Re-fetch vendor
179     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
180     is( $vendor->contacts->count, 2, 'Vendor has two contacts' );
181     foreach my $contact ( $vendor->contacts ) {
182         is( ref($contact), 'Koha::Acquisition::Bookseller::Contact', 'Type is correct' );
183     }
184
185     $schema->storage->txn_rollback();
186 };