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