Bug 13264: Follow up: in opac_utf8.t insert also delete of biblio
[koha.git] / t / db_dependent / Serials.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use Modern::Perl;
7 use YAML;
8
9 use CGI;
10 use C4::Serials;
11 use C4::Serials::Frequency;
12 use C4::Serials::Numberpattern;
13 use C4::Debug;
14 use C4::Bookseller;
15 use C4::Biblio;
16 use C4::Budgets;
17 use Koha::DateUtils;
18 use Test::More tests => 44;
19
20 BEGIN {
21     use_ok('C4::Serials');
22 }
23
24 my $dbh = C4::Context->dbh;
25
26 # Start transaction
27 $dbh->{AutoCommit} = 0;
28 $dbh->{RaiseError} = 1;
29
30 my $booksellerid = C4::Bookseller::AddBookseller(
31     {
32         name => "my vendor",
33         address1 => "bookseller's address",
34         phone => "0123456",
35         active => 1
36     }
37 );
38
39 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
40
41 my $budgetid;
42 my $bpid = AddBudgetPeriod({
43     budget_period_startdate   => '01-01-2015',
44     budget_period_enddate     => '31-12-2015',
45     budget_period_description => "budget desc"
46 });
47
48 my $budget_id = AddBudget({
49     budget_code        => "ABCD",
50     budget_amount      => "123.132",
51     budget_name        => "Périodiques",
52     budget_notes       => "This is a note",
53     budget_period_id   => $bpid
54 });
55
56 my $frequency_id = AddSubscriptionFrequency({ description => "Test frequency 1" });
57 my $pattern_id = AddSubscriptionNumberpattern({
58     label => 'Test numberpattern 1',
59     numberingmethod => '{X}',
60     label1 => q{},
61     add1 => 1,
62     every1 => 1,
63     every1 => 1,
64     numbering1 => 1,
65     whenmorethan1 => 1,
66 });
67
68 my $subscriptionid = NewSubscription(
69     undef,      "",     undef, undef, $budget_id, $biblionumber,
70     '2013-01-01', $frequency_id, undef, undef,  undef,
71     undef,      undef,  undef, undef, undef, undef,
72     1,          "notes",undef, '2013-01-01', undef, $pattern_id,
73     undef,       undef,  0,    "intnotes",  0,
74     undef, undef, 0,          undef,         '2013-12-31', 0
75 );
76
77 my $subscriptioninformation = GetSubscription( $subscriptionid );
78
79 my @subscriptions = GetSubscriptions( $$subscriptioninformation{bibliotitle} );
80 isa_ok( \@subscriptions, 'ARRAY' );
81
82 @subscriptions = GetSubscriptions( undef, $$subscriptioninformation{issn} );
83 isa_ok( \@subscriptions, 'ARRAY' );
84
85 @subscriptions = GetSubscriptions( undef, undef, $$subscriptioninformation{ean} );
86 isa_ok( \@subscriptions, 'ARRAY' );
87
88 @subscriptions = GetSubscriptions( undef, undef, undef, $$subscriptioninformation{bibnum} );
89 isa_ok( \@subscriptions, 'ARRAY' );
90
91 my $frequency = GetSubscriptionFrequency($subscriptioninformation->{periodicity});
92 my $old_frequency;
93 if (not $frequency->{unit}) {
94     $old_frequency = $frequency->{id};
95     $frequency->{unit} = "month";
96     $frequency->{unitsperissue} = 1;
97     $frequency->{issuesperunit} = 1;
98     $frequency->{description} = "Frequency created by t/db_dependant/Serials.t";
99     $subscriptioninformation->{periodicity} = AddSubscriptionFrequency($frequency);
100
101     ModSubscription( @$subscriptioninformation{qw(
102         librarian branchcode aqbooksellerid cost aqbudgetid startdate
103         periodicity firstacquidate irregularity numberpattern locale
104         numberlength weeklength monthlength lastvalue1 innerloop1 lastvalue2
105         innerloop2 lastvalue3 innerloop3 status biblionumber callnumber notes
106         letter manualhistory internalnotes serialsadditems staffdisplaycount
107         opacdisplaycount graceperiod location enddate subscriptionid
108         skip_serialseq
109     )} );
110 }
111 my $expirationdate = GetExpirationDate($subscriptionid) ;
112 ok( $expirationdate, "expiration date is not NULL" );
113
114 is(C4::Serials::GetLateIssues(), undef, 'test getting late issues');
115
116 ok(C4::Serials::GetSubscriptionHistoryFromSubscriptionId($subscriptionid), 'test getting history from sub-scription');
117
118 my ($serials_count, @serials) = GetSerials($subscriptionid);
119 ok($serials_count > 0, 'Subscription has at least one serial');
120 my $serial = $serials[0];
121
122 ok(C4::Serials::GetSerialStatusFromSerialId($serial->{serialid}), 'test getting Serial Status From Serial Id');
123
124 isa_ok(C4::Serials::GetSerialInformation($serial->{serialid}), 'HASH', 'test getting Serial Information');
125
126 # Delete created frequency
127 if ($old_frequency) {
128     my $freq_to_delete = $subscriptioninformation->{periodicity};
129     $subscriptioninformation->{periodicity} = $old_frequency;
130
131     ModSubscription( @$subscriptioninformation{qw(
132         librarian branchcode aqbooksellerid cost aqbudgetid startdate
133         periodicity firstacquidate irregularity numberpattern locale
134         numberlength weeklength monthlength lastvalue1 innerloop1 lastvalue2
135         innerloop2 lastvalue3 innerloop3 status biblionumber callnumber notes
136         letter manualhistory internalnotes serialsadditems staffdisplaycount
137         opacdisplaycount graceperiod location enddate subscriptionid
138         skip_serialseq
139     )} );
140
141     DelSubscriptionFrequency($freq_to_delete);
142 }
143
144
145 # Test calling subs without parameters
146 is(C4::Serials::AddItem2Serial(), undef, 'test adding item to serial');
147 is(C4::Serials::UpdateClaimdateIssues(), undef, 'test updating claim date');
148 is(C4::Serials::GetFullSubscription(), undef, 'test getting full subscription');
149 is(C4::Serials::PrepareSerialsData(), undef, 'test preparing serial data');
150 is(C4::Serials::GetSubscriptionsFromBiblionumber(), undef, 'test getting subscriptions form biblio number');
151
152 is(C4::Serials::GetSerials(), undef, 'test getting serials when you enter nothing');
153 is(C4::Serials::GetSerials2(), undef, 'test getting serials when you enter nothing');
154
155 is(C4::Serials::GetLatestSerials(), undef, 'test getting lastest serials');
156
157 is(C4::Serials::GetDistributedTo(), undef, 'test getting distributed when nothing is entered');
158
159 is(C4::Serials::GetNextSeq(), undef, 'test getting next seq when you enter nothing');
160
161 is(C4::Serials::GetSeq(), undef, 'test getting seq when you enter nothing');
162
163 is(C4::Serials::CountSubscriptionFromBiblionumber(), undef, 'test counting subscription when nothing is entered');
164
165 is(C4::Serials::ModSubscriptionHistory(), undef, 'test modding subscription history');
166
167 is(C4::Serials::ModSerialStatus(),undef, 'test modding serials');
168
169 is(C4::Serials::NewIssue(), undef, 'test getting 0 when nothing is entered');
170
171 is(C4::Serials::ItemizeSerials(),undef, 'test getting nothing when nothing is entered');
172
173 is(C4::Serials::HasSubscriptionStrictlyExpired(), undef, 'test if the subscriptions has expired');
174 is(C4::Serials::HasSubscriptionExpired(), undef, 'test if the subscriptions has expired');
175
176 is(C4::Serials::GetLateOrMissingIssues(), undef, 'test getting last or missing issues');
177
178 is(C4::Serials::removeMissingIssue(), undef, 'test removing a missing issue');
179
180 is(C4::Serials::updateClaim(),undef, 'test updating claim');
181
182 is(C4::Serials::getsupplierbyserialid(),undef, 'test getting supplier idea');
183
184 is(C4::Serials::check_routing(), undef, 'test checking route');
185
186 is(C4::Serials::addroutingmember(),undef, 'test adding route member');
187
188
189 # Unit tests for statuses management (Bug 11689)
190 $subscriptionid = NewSubscription(
191     undef,      "",     undef, undef, $budget_id, $biblionumber,
192     '2013-01-01', $frequency_id, undef, undef,  undef,
193     undef,      undef,  undef, undef, undef, undef,
194     1,          "notes",undef, '2013-01-01', undef, $pattern_id,
195     undef,       undef,  0,    "intnotes",  0,
196     undef, undef, 0,          undef,         '2013-12-31', 0
197 );
198 my $total_issues;
199 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
200 is( $total_issues, 1, "NewSubscription created a first serial" );
201 is( @serials, 1, "GetSerials returns the serial" );
202 my $subscription = C4::Serials::GetSubscription($subscriptionid);
203 my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
204 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
205 my $publisheddate = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
206 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
207 my $nextpublisheddate = C4::Serials::GetNextDate($subscription, $publisheddate, 1);
208 my @statuses = qw( 2 2 3 3 3 3 3 4 4 41 42 43 44 5 );
209 # Add 14 serials
210 my $counter = 0;
211 for my $status ( @statuses ) {
212     my $serialseq = "No.".$counter;
213     my ( $expected_serial ) = GetSerials2( $subscriptionid, 1 );
214     C4::Serials::ModSerialStatus( $expected_serial->{serialid}, $serialseq, $publisheddate, $publisheddate, $statuses[$counter], 'an useless note' );
215     $counter++;
216 }
217 # Here we have 15 serials with statuses : 2*2 + 5*3 + 2*4 + 1*41 + 1*42 + 1*43 + 1*44 + 1*5 + 1*1
218 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
219 is( $total_issues, @statuses + 1, "GetSerials returns total_issues" );
220 my @arrived_missing = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? $_ : () } @serials;
221 my @others = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? () : $_ } @serials;
222 is( @arrived_missing, 5, "GetSerials returns 5 arrived/missing by default" );
223 is( @others, 6, "GetSerials returns all serials not arrived and not missing" );
224
225 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid, 10 );
226 is( $total_issues, @statuses + 1, "GetSerials returns total_issues" );
227 @arrived_missing = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? $_ : () } @serials;
228 @others = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? () : $_ } @serials;
229 is( @arrived_missing, 9, "GetSerials returns all arrived/missing if count given" );
230 is( @others, 6, "GetSerials returns all serials not arrived and not missing if count given" );
231
232 $subscription = C4::Serials::GetSubscription($subscriptionid); # Retrieve the updated subscription
233
234 my @serialseqs;
235 for my $am ( @arrived_missing ) {
236     if ( grep {/^$am->{status}$/} qw( 4 41 42 43 44 ) ) {
237         push @serialseqs, $am->{serialseq}
238     } elsif ( grep {/^$am->{status}$/} qw( 5 ) ) {
239         push @serialseqs, 'not issued ' . $am->{serialseq};
240     }
241 }
242 is( $subscription->{missinglist}, join('; ', @serialseqs), "subscription missinglist is updated after ModSerialStatus" );
243
244 $dbh->rollback;