Bug 12748 - Add test for step 7 and 8 and rename findSerialByStatus
[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 qw ( -utf8 );
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 => 49;
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 $notes = 'notes';
69 my $internalnotes = 'intnotes';
70 my $subscriptionid = NewSubscription(
71     undef,      "",     undef, undef, $budget_id, $biblionumber,
72     '2013-01-01', $frequency_id, undef, undef,  undef,
73     undef,      undef,  undef, undef, undef, undef,
74     1,          $notes,undef, '2013-01-01', undef, $pattern_id,
75     undef,       undef,  0,    $internalnotes,  0,
76     undef, undef, 0,          undef,         '2013-12-31', 0
77 );
78
79 my $subscriptioninformation = GetSubscription( $subscriptionid );
80
81 is( $subscriptioninformation->{notes}, $notes, 'NewSubscription should set notes' );
82 is( $subscriptioninformation->{internalnotes}, $internalnotes, 'NewSubscription should set internalnotes' );
83
84 my $subscription_history = C4::Serials::GetSubscriptionHistoryFromSubscriptionId($subscriptionid);
85 is( $subscription_history->{opacnote}, '', 'NewSubscription should not set subscriptionhistory opacnotes' );
86 is( $subscription_history->{librariannote}, '', 'NewSubscription should not set subscriptionhistory librariannotes' );
87
88 my @subscriptions = SearchSubscriptions({string => $subscriptioninformation->{bibliotitle}, orderby => 'title' });
89 isa_ok( \@subscriptions, 'ARRAY' );
90
91 @subscriptions = SearchSubscriptions({ issn => $subscriptioninformation->{issn}, orderby => 'title' });
92 isa_ok( \@subscriptions, 'ARRAY' );
93
94 @subscriptions = SearchSubscriptions({ ean => $subscriptioninformation->{ean}, orderby => 'title' });
95 isa_ok( \@subscriptions, 'ARRAY' );
96
97 @subscriptions = SearchSubscriptions({ biblionumber => $subscriptioninformation->{bibnum}, orderby => 'title' });
98 isa_ok( \@subscriptions, 'ARRAY' );
99
100 my $frequency = GetSubscriptionFrequency($subscriptioninformation->{periodicity});
101 my $old_frequency;
102 if (not $frequency->{unit}) {
103     $old_frequency = $frequency->{id};
104     $frequency->{unit} = "month";
105     $frequency->{unitsperissue} = 1;
106     $frequency->{issuesperunit} = 1;
107     $frequency->{description} = "Frequency created by t/db_dependant/Serials.t";
108     $subscriptioninformation->{periodicity} = AddSubscriptionFrequency($frequency);
109
110     ModSubscription( @$subscriptioninformation{qw(
111         librarian branchcode aqbooksellerid cost aqbudgetid startdate
112         periodicity firstacquidate irregularity numberpattern locale
113         numberlength weeklength monthlength lastvalue1 innerloop1 lastvalue2
114         innerloop2 lastvalue3 innerloop3 status biblionumber callnumber notes
115         letter manualhistory internalnotes serialsadditems staffdisplaycount
116         opacdisplaycount graceperiod location enddate subscriptionid
117         skip_serialseq
118     )} );
119 }
120 my $expirationdate = GetExpirationDate($subscriptionid) ;
121 ok( $expirationdate, "expiration date is not NULL" );
122
123 ok(C4::Serials::GetSubscriptionHistoryFromSubscriptionId($subscriptionid), 'test getting history from sub-scription');
124
125 my ($serials_count, @serials) = GetSerials($subscriptionid);
126 ok($serials_count > 0, 'Subscription has at least one serial');
127 my $serial = $serials[0];
128
129 ok(C4::Serials::GetSerialStatusFromSerialId($serial->{serialid}), 'test getting Serial Status From Serial Id');
130
131 isa_ok(C4::Serials::GetSerialInformation($serial->{serialid}), 'HASH', 'test getting Serial Information');
132
133 # Delete created frequency
134 if ($old_frequency) {
135     my $freq_to_delete = $subscriptioninformation->{periodicity};
136     $subscriptioninformation->{periodicity} = $old_frequency;
137
138     ModSubscription( @$subscriptioninformation{qw(
139         librarian branchcode aqbooksellerid cost aqbudgetid startdate
140         periodicity firstacquidate irregularity numberpattern locale
141         numberlength weeklength monthlength lastvalue1 innerloop1 lastvalue2
142         innerloop2 lastvalue3 innerloop3 status biblionumber callnumber notes
143         letter manualhistory internalnotes serialsadditems staffdisplaycount
144         opacdisplaycount graceperiod location enddate subscriptionid
145         skip_serialseq
146     )} );
147
148     DelSubscriptionFrequency($freq_to_delete);
149 }
150
151
152 # Test calling subs without parameters
153 is(C4::Serials::AddItem2Serial(), undef, 'test adding item to serial');
154 is(C4::Serials::UpdateClaimdateIssues(), undef, 'test updating claim date');
155 is(C4::Serials::GetFullSubscription(), undef, 'test getting full subscription');
156 is(C4::Serials::PrepareSerialsData(), undef, 'test preparing serial data');
157 is(C4::Serials::GetSubscriptionsFromBiblionumber(), undef, 'test getting subscriptions form biblio number');
158
159 is(C4::Serials::GetSerials(), undef, 'test getting serials when you enter nothing');
160 is(C4::Serials::GetSerials2(), undef, 'test getting serials when you enter nothing');
161
162 is(C4::Serials::GetLatestSerials(), undef, 'test getting lastest serials');
163
164 is(C4::Serials::GetDistributedTo(), undef, 'test getting distributed when nothing is entered');
165
166 is(C4::Serials::GetNextSeq(), undef, 'test getting next seq when you enter nothing');
167
168 is(C4::Serials::GetSeq(), undef, 'test getting seq when you enter nothing');
169
170 is(C4::Serials::CountSubscriptionFromBiblionumber(), undef, 'test counting subscription when nothing is entered');
171
172 is(C4::Serials::ModSubscriptionHistory(), undef, 'test modding subscription history');
173
174 is(C4::Serials::ModSerialStatus(),undef, 'test modding serials');
175
176 is(C4::Serials::findSerialsByStatus(), 0, 'test finding serial by status with no parameters');
177
178 is(C4::Serials::NewIssue(), undef, 'test getting 0 when nothing is entered');
179
180 is(C4::Serials::HasSubscriptionStrictlyExpired(), undef, 'test if the subscriptions has expired');
181 is(C4::Serials::HasSubscriptionExpired(), undef, 'test if the subscriptions has expired');
182
183 is(C4::Serials::GetLateOrMissingIssues(), undef, 'test getting last or missing issues');
184
185 is(C4::Serials::updateClaim(),undef, 'test updating claim');
186
187 is(C4::Serials::getsupplierbyserialid(),undef, 'test getting supplier idea');
188
189 is(C4::Serials::check_routing(), undef, 'test checking route');
190
191 is(C4::Serials::addroutingmember(),undef, 'test adding route member');
192
193
194 # Unit tests for statuses management (Bug 11689)
195 $subscriptionid = NewSubscription(
196     undef,      "",     undef, undef, $budget_id, $biblionumber,
197     '2013-01-01', $frequency_id, undef, undef,  undef,
198     undef,      undef,  undef, undef, undef, undef,
199     1,          $notes,undef, '2013-01-01', undef, $pattern_id,
200     undef,       undef,  0,    $internalnotes,  0,
201     undef, undef, 0,          undef,         '2013-12-31', 0
202 );
203 my $total_issues;
204 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
205 is( $total_issues, 1, "NewSubscription created a first serial" );
206 is( @serials, 1, "GetSerials returns the serial" );
207 my $subscription = C4::Serials::GetSubscription($subscriptionid);
208 my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
209 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
210 my $publisheddate = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
211 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
212 my $nextpublisheddate = C4::Serials::GetNextDate($subscription, $publisheddate, 1);
213 my @statuses = qw( 2 2 3 3 3 3 3 4 4 41 42 43 44 5 );
214 # Add 14 serials
215 my $counter = 0;
216 for my $status ( @statuses ) {
217     my $serialseq = "No.".$counter;
218     my ( $expected_serial ) = GetSerials2( $subscriptionid, [1] );
219     C4::Serials::ModSerialStatus( $expected_serial->{serialid}, $serialseq, $publisheddate, $publisheddate, $publisheddate, $statuses[$counter], 'an useless note' );
220     $counter++;
221 }
222 # 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
223 my @serialsByStatus = C4::Serials::findSerialsByStatus(2,$subscriptionid);
224 is(@serialsByStatus,2,"findSerialByStatus returns all serials with chosen status");
225 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid );
226 is( $total_issues, @statuses + 1, "GetSerials returns total_issues" );
227 my @arrived_missing = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? $_ : () } @serials;
228 my @others = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? () : $_ } @serials;
229 is( @arrived_missing, 5, "GetSerials returns 5 arrived/missing by default" );
230 is( @others, 6, "GetSerials returns all serials not arrived and not missing" );
231
232 ( $total_issues, @serials ) = C4::Serials::GetSerials( $subscriptionid, 10 );
233 is( $total_issues, @statuses + 1, "GetSerials returns total_issues" );
234 @arrived_missing = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? $_ : () } @serials;
235 @others = map { my $status = $_->{status}; ( grep { /^$status$/ } qw( 2 4 41 42 43 44 5 ) ) ? () : $_ } @serials;
236 is( @arrived_missing, 9, "GetSerials returns all arrived/missing if count given" );
237 is( @others, 6, "GetSerials returns all serials not arrived and not missing if count given" );
238
239 $subscription = C4::Serials::GetSubscription($subscriptionid); # Retrieve the updated subscription
240
241 my @serialseqs;
242 for my $am ( @arrived_missing ) {
243     if ( grep {/^$am->{status}$/} qw( 4 41 42 43 44 ) ) {
244         push @serialseqs, $am->{serialseq}
245     } elsif ( grep {/^$am->{status}$/} qw( 5 ) ) {
246         push @serialseqs, 'not issued ' . $am->{serialseq};
247     }
248 }
249 is( $subscription->{missinglist}, join('; ', @serialseqs), "subscription missinglist is updated after ModSerialStatus" );
250
251 my ( $expected_serial ) = GetSerials2( $subscriptionid, [1] );
252 #Find serialid for serial with status Expected
253 my $serialexpected = (C4::Serials::findSerialsByStatus(1,$subscriptionid))[0];
254 #delete serial with status Expected
255 C4::Serials::ModSerialStatus( $serialexpected->{serialid},$serialexpected->{serialseq},$publisheddate,$publisheddate, $publisheddate,'1','an useless note');
256 my @serialsByStatus = C4::Serials::findSerialsByStatus(1,$subscriptionid);
257 is (@serialsByStatus,1,"ModSerialStatus delete corectly serial expected and create another if not exist");
258 # add 1 serial with status=Expected 1
259 C4::Serials::ModSerialStatus( $expected_serial->{serialid}, 'NO.20', $publisheddate, $publisheddate, $publisheddate, '1', 'an useless note' );
260 #Now we have two serials it have status expected
261 #put status delete for last serial
262 C4::Serials::ModSerialStatus( $serialexpected->{serialid},$serialexpected->{serialseq},$publisheddate,$publisheddate, $publisheddate,'1','an useless note');
263 #try if create or note another serial with status is expected
264 @serialsByStatus = C4::Serials::findSerialsByStatus(1,$subscriptionid);
265 is(@serialsByStatus,1,"ModSerialStatus delete corectly serial expected and not create another if exist");
266
267 $dbh->rollback;