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