3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 5;
22 use t::lib::TestBuilder;
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 );
29 use Koha::Acquisition::Booksellers;
31 use Koha::DateUtils qw( dt_from_string output_pref );
33 my $schema = Koha::Database->schema();
34 my $builder = t::lib::TestBuilder->new;
36 subtest '->baskets() tests' => sub {
40 $schema->storage->txn_begin();
42 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
44 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
46 is( $vendor->baskets->count, 0, 'Vendor has no 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' );
53 $vendor = Koha::Acquisition::Booksellers->find( $vendor->id );
54 is( $vendor->baskets->count, 2, 'Vendor has two baskets' );
56 $schema->storage->txn_rollback();
59 subtest '->subscriptions() tests' => sub {
63 $schema->storage->txn_begin();
65 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
66 is( $vendor->subscriptions->count, 0, 'Vendor has no subscriptions' );
68 my $dt_today = dt_from_string;
69 my $today = output_pref(
70 { dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 } );
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 } );
78 my $budgetperiod = C4::Budgets::AddBudgetPeriod(
79 { budget_period_startdate => $daysago5,
80 budget_period_enddate => $today,
81 budget_period_description => "budget desc"
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
92 my $bib = MARC::Record->new();
94 MARC::Field->new( '245', ' ', ' ', a => 'Journal of ethnology', b => 'A subtitle' ),
95 MARC::Field->new( '500', ' ', ' ', a => 'bib notes' ),
97 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $bib, '' );
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,
112 my @subscriptions = SearchSubscriptions( { biblionumber => $biblionumber } );
113 is( $subscriptions[0]->{publicnotes},
114 'subscription notes',
115 'subscription search results include public notes (bug 10689)'
117 is( $subscriptions[0]->{subtitle},
119 'subscription search results include subtitle (bug 30204)'
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,
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' );
142 $schema->storage->txn_rollback();
145 subtest '->contacts() tests' => sub {
149 $schema->storage->txn_begin();
151 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
153 is( $vendor->contacts->count, 0, 'Vendor has no contacts' );
156 my $contact_1 = $builder->build_object(
157 { class => 'Koha::Acquisition::Bookseller::Contacts',
158 value => { booksellerid => $vendor->id }
161 my $contact_2 = $builder->build_object(
162 { class => 'Koha::Acquisition::Bookseller::Contacts',
163 value => { booksellerid => $vendor->id }
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' );
173 $schema->storage->txn_rollback();
176 subtest 'aliases' => sub {
180 $schema->storage->txn_begin();
182 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
184 is( $vendor->aliases->count, 0, 'Vendor has no aliases' );
186 $vendor->aliases( [ { alias => 'alias 1' }, { alias => 'alias 2' } ] );
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' );
193 $schema->storage->txn_rollback();
196 subtest 'interfaces' => sub {
200 $schema->storage->txn_begin();
202 my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } );
204 is( $vendor->interfaces->count, 0, 'Vendor has no interfaces' );
206 $vendor->interfaces( [ { name => 'first interface' }, { name => 'second interface', login => 'one_login' } ] );
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' );
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' );
226 $schema->storage->txn_rollback();