Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[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 => 5;
21
22 use t::lib::TestBuilder;
23
24 use C4::Acquisition qw( NewBasket );
25 use C4::Biblio qw( AddBiblio );
26 use C4::Budgets qw( AddBudgetPeriod AddBudget );
27 use C4::Serials qw( NewSubscription SearchSubscriptions );
28
29 use Koha::Acquisition::Booksellers;
30 use Koha::Database;
31 use Koha::DateUtils qw( dt_from_string output_pref );
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 => 6;
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', b => 'A subtitle' ),
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     is( $subscriptions[0]->{subtitle},
118         'A subtitle',
119         'subscription search results include subtitle (bug 30204)'
120     );
121
122     my $id_subscription2 = NewSubscription(
123         undef,        'BRANCH2',     $vendor->id,          undef,
124         $id_budget,   $biblionumber, '2013-01-01',         undef,
125         undef,        undef,         undef,                undef,
126         undef,        undef,         undef,                undef,
127         undef,        1,             "subscription notes", undef,
128         '2013-01-01', undef,         undef,                undef,
129         'CALL DEF',   0,             "intnotes",           0,
130         undef,        undef,         0,                    undef,
131         '2013-07-31', 0
132     );
133
134     # Re-fetch vendor
135     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
136     my $subscriptions = $vendor->subscriptions;
137     is( $subscriptions->count, 2, 'Vendor has two subscriptions' );
138     while (my $subscription = $subscriptions->next ) {
139         is( ref($subscription), 'Koha::Subscription', 'Type is correct' );
140     }
141
142     $schema->storage->txn_rollback();
143 };
144
145 subtest '->contacts() tests' => sub {
146
147     plan tests => 3;
148
149     $schema->storage->txn_begin();
150
151     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
152
153     is( $vendor->contacts->count, 0, 'Vendor has no contacts' );
154
155     # Add two contacts
156     my $contact_1 = $builder->build_object(
157         {   class => 'Koha::Acquisition::Bookseller::Contacts',
158             value => { booksellerid => $vendor->id }
159         }
160     );
161     my $contact_2 = $builder->build_object(
162         {   class => 'Koha::Acquisition::Bookseller::Contacts',
163             value => { booksellerid => $vendor->id }
164         }
165     );
166
167     # Re-fetch vendor
168     $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
169     my $contacts = $vendor->contacts;
170     is( $contacts->count, 2, 'Vendor has two contacts' );
171     is( ref($contacts), 'Koha::Acquisition::Bookseller::Contacts', 'Type is correct' );
172
173     $schema->storage->txn_rollback();
174 };
175
176 subtest 'aliases' => sub {
177
178     plan tests => 3;
179
180     $schema->storage->txn_begin();
181
182     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
183
184     is( $vendor->aliases->count, 0, 'Vendor has no aliases' );
185
186     $vendor->aliases( [ { alias => 'alias 1' }, { alias => 'alias 2' } ] );
187
188     $vendor = $vendor->get_from_storage;
189     my $aliases = $vendor->aliases;
190     is( $aliases->count, 2 );
191     is( ref($aliases), 'Koha::Acquisition::Bookseller::Aliases', 'Type is correct' );
192
193     $schema->storage->txn_rollback();
194 };
195
196 subtest 'interfaces' => sub {
197
198     plan tests => 11;
199
200     $schema->storage->txn_begin();
201
202     my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
203
204     is( $vendor->interfaces->count, 0, 'Vendor has no interfaces' );
205
206     $vendor->interfaces( [ { name => 'first interface' }, { name => 'second interface', login => 'one_login' } ] );
207
208     $vendor = $vendor->get_from_storage;
209     my $interfaces = $vendor->interfaces;
210     is( $interfaces->count, 2, '2 interfaces stored' );
211     is( ref($interfaces), 'Koha::Acquisition::Bookseller::Interfaces', 'Type is correct' );
212
213     $vendor->interfaces( [ { name => 'first interface', login => 'one_login', password => 'oneP@sswOrd' } ] );
214     $vendor     = $vendor->get_from_storage;
215     $interfaces = $vendor->interfaces;
216     is( $interfaces->count, 1, '1 interface stored' );
217     my $interface = $interfaces->next;
218     is( $interface->name,  'first interface', 'name correctly saved' );
219     is( $interface->login, 'one_login',       'login correctly saved' );
220     is( $interface->uri,   undef,             'no value is stored as NULL' );
221     isnt( $interface->password, 'oneP@sswOrd', 'Password is not stored in plain text' );
222     isnt( $interface->password, '',            'Password is not removed' );
223     isnt( $interface->password, undef,         'Password is not set to NULL' );
224     is( $interface->plain_text_password, 'oneP@sswOrd', 'Password can be retrieved using ->plain_text_password' );
225
226     $schema->storage->txn_rollback();
227 };