Bug 18356: Extend GetNextDate.t, add GetFictiveIssueNumber.t (unit=year)
[koha.git] / t / db_dependent / Serials / GetNextDate.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 102;
5
6 use Koha::Database;
7 use C4::Serials;
8 use C4::Serials::Frequency;
9
10 my $schema  = Koha::Database->new->schema;
11 $schema->storage->txn_begin;
12 my $dbh = C4::Context->dbh;
13
14
15 # TEST CASE - 1 issue per day, no irregularities
16 my $frequency = {
17     description => "One issue per day",
18     unit => 'day',
19     issuesperunit => 1,
20     unitsperissue => 1,
21 };
22 my $id = AddSubscriptionFrequency($frequency);
23
24 my $subscription = {
25     periodicity => $id,
26     firstacquidate => '1970-01-01',
27     irregularity => '',
28     countissuesperunit => 1,
29 };
30 my $publisheddate = $subscription->{firstacquidate};
31
32 $publisheddate = GetNextDate($subscription, $publisheddate);
33 is($publisheddate, '1970-01-02');
34 $publisheddate = GetNextDate($subscription, $publisheddate);
35 is($publisheddate, '1970-01-03');
36 $publisheddate = GetNextDate($subscription, $publisheddate);
37 is($publisheddate, '1970-01-04');
38
39 # TEST CASE - 1 issue per day, irregularities
40 $subscription = {
41     periodicity => $id,
42     firstacquidate => '1970-01-01',
43     irregularity => '2;4',  # Skip the second and fourth issues
44     countissuesperunit => 1,
45 };
46 $publisheddate = $subscription->{firstacquidate};
47 $publisheddate = GetNextDate($subscription, $publisheddate);
48 is($publisheddate, '1970-01-03');
49 $publisheddate = GetNextDate($subscription, $publisheddate);
50 is($publisheddate, '1970-01-05');
51 $publisheddate = GetNextDate($subscription, $publisheddate);
52 is($publisheddate, '1970-01-06');
53
54 # TEST CASE - 2 issues per day, no irregularity
55 $id = AddSubscriptionFrequency({
56     description => "Two issues per day",
57     unit => 'day',
58     issuesperunit => 2,
59     unitsperissue => 1,
60 });
61 $subscription = {
62     periodicity => $id,
63     firstacquidate => '1970-01-01',
64     irregularity => '',
65     countissuesperunit => 1,
66 };
67 $publisheddate = $subscription->{firstacquidate};
68 $publisheddate = GetNextDate($subscription, $publisheddate);
69 is($publisheddate, '1970-01-01');
70 $publisheddate = GetNextDate($subscription, $publisheddate);
71 is($publisheddate, '1970-01-02');
72 $publisheddate = GetNextDate($subscription, $publisheddate);
73 is($publisheddate, '1970-01-02');
74 $publisheddate = GetNextDate($subscription, $publisheddate);
75 is($publisheddate, '1970-01-03');
76
77 # TEST CASE - 2 issues per day, irregularities
78 $subscription = {
79     periodicity => $id,
80     firstacquidate => '1970-01-01',
81     irregularity => '3;5;6',
82     countissuesperunit => 1,
83 };
84 $publisheddate = $subscription->{firstacquidate};
85 $publisheddate = GetNextDate($subscription, $publisheddate);
86 is($publisheddate, '1970-01-01');
87 $publisheddate = GetNextDate($subscription, $publisheddate);
88 is($publisheddate, '1970-01-02');
89 $publisheddate = GetNextDate($subscription, $publisheddate);
90 is($publisheddate, '1970-01-04');
91 $publisheddate = GetNextDate($subscription, $publisheddate);
92 is($publisheddate, '1970-01-04');
93 $publisheddate = GetNextDate($subscription, $publisheddate);
94 is($publisheddate, '1970-01-05');
95
96 # TEST CASE - 1 issue every 2 days, no irregularity
97 $id = AddSubscriptionFrequency({
98     description => "one issue every two days",
99     unit => 'day',
100     issuesperunit => 1,
101     unitsperissue => 2,
102 });
103 $subscription = {
104     periodicity => $id,
105     firstacquidate => '1970-01-01',
106     irregularity => '',
107     countissuesperunit => 1,
108 };
109 $publisheddate = $subscription->{firstacquidate};
110 $publisheddate = GetNextDate($subscription, $publisheddate);
111 is($publisheddate, '1970-01-03');
112 $publisheddate = GetNextDate($subscription, $publisheddate);
113 is($publisheddate, '1970-01-05');
114
115 # TEST CASE - 1 issue every 2 days, irregularities
116 $subscription = {
117     periodicity => $id,
118     firstacquidate => '1970-01-01',
119     irregularity => '3',
120     countissuesperunit => 1,
121 };
122 $publisheddate = $subscription->{firstacquidate};
123 $publisheddate = GetNextDate($subscription, $publisheddate);
124 is($publisheddate, '1970-01-03');
125 $publisheddate = GetNextDate($subscription, $publisheddate);
126 is($publisheddate, '1970-01-07');
127 $publisheddate = GetNextDate($subscription, $publisheddate);
128 is($publisheddate, '1970-01-09');
129
130 # TEST CASE - 1 issue per week, no irregularity
131 $id = AddSubscriptionFrequency({
132     description => "one issue per week",
133     unit => 'week',
134     issuesperunit => 1,
135     unitsperissue => 1,
136 });
137 $subscription = {
138     periodicity => $id,
139     firstacquidate => '1970-01-01',
140     irregularity => '',
141     countissuesperunit => 1,
142 };
143 $publisheddate = $subscription->{firstacquidate};
144 $publisheddate = GetNextDate($subscription, $publisheddate);
145 is($publisheddate, '1970-01-08');
146 $publisheddate = GetNextDate($subscription, $publisheddate);
147 is($publisheddate, '1970-01-15');
148
149 # TEST CASE - 1 issue per week, irregularities
150 $subscription = {
151     periodicity => $id,
152     firstacquidate => '1970-01-01',
153     irregularity => '3',
154     countissuesperunit => 1,
155 };
156 $publisheddate = $subscription->{firstacquidate};
157 $publisheddate = GetNextDate($subscription, $publisheddate);
158 is($publisheddate, '1970-01-08');
159 $publisheddate = GetNextDate($subscription, $publisheddate);
160 is($publisheddate, '1970-01-22');
161 $publisheddate = GetNextDate($subscription, $publisheddate);
162 is($publisheddate, '1970-01-29');
163
164 # TEST CASE - 1 issue every 2 weeks, no irregularity
165 $id = AddSubscriptionFrequency({
166     description => "one issue every 2 weeks",
167     unit => 'week',
168     issuesperunit => 1,
169     unitsperissue => 2,
170 });
171 $subscription = {
172     periodicity => $id,
173     firstacquidate => '1970-01-01',
174     irregularity => '',
175     countissuesperunit => 1,
176 };
177 $publisheddate = $subscription->{firstacquidate};
178 $publisheddate = GetNextDate($subscription, $publisheddate);
179 is($publisheddate, '1970-01-15');
180 $publisheddate = GetNextDate($subscription, $publisheddate);
181 is($publisheddate, '1970-01-29');
182 $publisheddate = GetNextDate($subscription, $publisheddate);
183 is($publisheddate, '1970-02-12');
184
185 # TEST CASE - 1 issue every 2 weeks, irregularities
186 $subscription = {
187     periodicity => $id,
188     firstacquidate => '1970-01-01',
189     irregularity => '3',
190     countissuesperunit => 1,
191 };
192 $publisheddate = $subscription->{firstacquidate};
193 $publisheddate = GetNextDate($subscription, $publisheddate);
194 is($publisheddate, '1970-01-15');
195 $publisheddate = GetNextDate($subscription, $publisheddate);
196 is($publisheddate, '1970-02-12');
197 $publisheddate = GetNextDate($subscription, $publisheddate);
198 is($publisheddate, '1970-02-26');
199
200 # TEST CASE - 2 issues per week, no irregularity
201 $id = AddSubscriptionFrequency({
202     description => "two issues per week",
203     unit => 'week',
204     issuesperunit => 2,
205     unitsperissue => 1,
206 });
207 $subscription = {
208     periodicity => $id,
209     firstacquidate => '1970-01-01',
210     irregularity => '',
211     countissuesperunit => 1,
212 };
213 $publisheddate = $subscription->{firstacquidate};
214 $publisheddate = GetNextDate($subscription, $publisheddate);
215 is($publisheddate, '1970-01-03');
216 # when more than 1 issue per week, date is automatically set to the same day of
217 # week as firstacquidate
218 $publisheddate = GetNextDate($subscription, $publisheddate);
219 is($publisheddate, '1970-01-08');
220 $publisheddate = GetNextDate($subscription, $publisheddate);
221 is($publisheddate, '1970-01-10');
222 $publisheddate = GetNextDate($subscription, $publisheddate);
223 is($publisheddate, '1970-01-15');
224
225 # TEST CASE - 2 issues per week, irregularities
226 $subscription = {
227     periodicity => $id,
228     firstacquidate => '1970-01-01',
229     irregularity => '3;5;6',
230     countissuesperunit => 1,
231 };
232 $publisheddate = $subscription->{firstacquidate};
233 $publisheddate = GetNextDate($subscription, $publisheddate);
234 is($publisheddate, '1970-01-03');
235 $publisheddate = GetNextDate($subscription, $publisheddate);
236 is($publisheddate, '1970-01-10');
237 $publisheddate = GetNextDate($subscription, $publisheddate);
238 is($publisheddate, '1970-01-22');
239 $publisheddate = GetNextDate($subscription, $publisheddate);
240 is($publisheddate, '1970-01-24');
241 $publisheddate = GetNextDate($subscription, $publisheddate);
242 is($publisheddate, '1970-01-29');
243
244 # TEST CASE - 6 issues per week, no irregularity
245 $id = AddSubscriptionFrequency({
246     description => "six issues per week",
247     unit => 'week',
248     issuesperunit => 6,
249     unitsperissue => 1,
250 });
251 $subscription = {
252     periodicity => $id,
253     firstacquidate => '1970-01-06',
254     irregularity => '',
255     countissuesperunit => 1,
256 };
257 $publisheddate = $subscription->{firstacquidate};
258 $publisheddate = GetNextDate($subscription, $publisheddate);
259 is($publisheddate, '1970-01-07');
260 $publisheddate = GetNextDate($subscription, $publisheddate);
261 is($publisheddate, '1970-01-08');
262 $publisheddate = GetNextDate($subscription, $publisheddate);
263 is($publisheddate, '1970-01-09');
264 $publisheddate = GetNextDate($subscription, $publisheddate);
265 is($publisheddate, '1970-01-10');
266 $publisheddate = GetNextDate($subscription, $publisheddate);
267 is($publisheddate, '1970-01-11');
268 $publisheddate = GetNextDate($subscription, $publisheddate);
269 is($publisheddate, '1970-01-13');
270
271 # TEST CASE - 6 issues per week, irregularities
272 $subscription = {
273     periodicity => $id,
274     firstacquidate => '1970-01-06',
275     irregularity => '3;5;6',
276     countissuesperunit => 1,
277 };
278 $publisheddate = $subscription->{firstacquidate};
279 $publisheddate = GetNextDate($subscription, $publisheddate);
280 is($publisheddate, '1970-01-07');
281 $publisheddate = GetNextDate($subscription, $publisheddate);
282 is($publisheddate, '1970-01-09');
283 $publisheddate = GetNextDate($subscription, $publisheddate);
284 is($publisheddate, '1970-01-13');
285 $publisheddate = GetNextDate($subscription, $publisheddate);
286 is($publisheddate, '1970-01-14');
287
288 # TEST CASE - 1 issue per month, no irregularity
289 $id = AddSubscriptionFrequency({
290     description => "1 issue per month",
291     unit => 'month',
292     issuesperunit => 1,
293     unitsperissue => 1,
294 });
295 $subscription = {
296     periodicity => $id,
297     firstacquidate => '1970-01-01',
298     irregularity => '',
299     countissuesperunit => 1,
300 };
301 $publisheddate = $subscription->{firstacquidate};
302 $publisheddate = GetNextDate($subscription, $publisheddate);
303 is($publisheddate, '1970-02-01');
304 $publisheddate = GetNextDate($subscription, $publisheddate);
305 is($publisheddate, '1970-03-01');
306 $publisheddate = GetNextDate($subscription, $publisheddate);
307 is($publisheddate, '1970-04-01');
308
309 # TEST CASE - 1 issue per month, irregularities
310 $subscription = {
311     periodicity => $id,
312     firstacquidate => '1970-01-01',
313     irregularity => '2;4',
314     countissuesperunit => 1,
315 };
316 $publisheddate = $subscription->{firstacquidate};
317 $publisheddate = GetNextDate($subscription, $publisheddate);
318 is($publisheddate, '1970-03-01');
319 $publisheddate = GetNextDate($subscription, $publisheddate);
320 is($publisheddate, '1970-05-01');
321 $publisheddate = GetNextDate($subscription, $publisheddate);
322 is($publisheddate, '1970-06-01');
323
324 # TEST CASE - 1 issue every 2 months, no irregularity
325 $id = AddSubscriptionFrequency({
326     description => "1 issue every 2 months",
327     unit => 'month',
328     issuesperunit => 1,
329     unitsperissue => 2,
330 });
331 $subscription = {
332     periodicity => $id,
333     firstacquidate => '1970-01-01',
334     irregularity => '',
335     countissuesperunit => 1,
336 };
337 $publisheddate = $subscription->{firstacquidate};
338 $publisheddate = GetNextDate($subscription, $publisheddate);
339 is($publisheddate, '1970-03-01');
340 $publisheddate = GetNextDate($subscription, $publisheddate);
341 is($publisheddate, '1970-05-01');
342 $publisheddate = GetNextDate($subscription, $publisheddate);
343 is($publisheddate, '1970-07-01');
344
345 # TEST CASE - 1 issue every 2 months, irregularities
346 $subscription = {
347     periodicity => $id,
348     firstacquidate => '1970-01-01',
349     irregularity => '2;3',
350     countissuesperunit => 1,
351 };
352 $publisheddate = $subscription->{firstacquidate};
353 $publisheddate = GetNextDate($subscription, $publisheddate);
354 is($publisheddate, '1970-07-01');
355 $publisheddate = GetNextDate($subscription, $publisheddate);
356 is($publisheddate, '1970-09-01');
357 $publisheddate = GetNextDate($subscription, $publisheddate);
358 is($publisheddate, '1970-11-01');
359
360 # TEST CASE - 2 issues per month, no irregularity
361 $id = AddSubscriptionFrequency({
362     description => "2 issues per month",
363     unit => 'month',
364     issuesperunit => 2,
365     unitsperissue => 1,
366 });
367 $subscription = {
368     periodicity => $id,
369     firstacquidate => '1970-01-01',
370     irregularity => '',
371     countissuesperunit => 1,
372 };
373 $publisheddate = $subscription->{firstacquidate};
374 $publisheddate = GetNextDate($subscription, $publisheddate);
375 is($publisheddate, '1970-01-16', 'January has 31 days');
376 $publisheddate = GetNextDate($subscription, $publisheddate);
377 is($publisheddate, '1970-02-01');
378 $publisheddate = GetNextDate($subscription, $publisheddate);
379 is($publisheddate, '1970-02-15', 'February has only 28 days');
380 $publisheddate = GetNextDate($subscription, $publisheddate);
381 is($publisheddate, '1970-03-01');
382
383 # TEST CASE - 2 issues per month, irregularities
384 $subscription = {
385     periodicity => $id,
386     firstacquidate => '1970-01-01',
387     irregularity => '3;5;6',
388     countissuesperunit => 1,
389 };
390 $publisheddate = $subscription->{firstacquidate};
391 $publisheddate = GetNextDate($subscription, $publisheddate);
392 is($publisheddate, '1970-01-16', 'January has 31 days');
393 $publisheddate = GetNextDate($subscription, $publisheddate);
394 is($publisheddate, '1970-02-15', 'February has only 28 days');
395 $publisheddate = GetNextDate($subscription, $publisheddate);
396 is($publisheddate, '1970-04-01');
397 $publisheddate = GetNextDate($subscription, $publisheddate);
398 is($publisheddate, '1970-04-16', 'April has 30 days');
399 $publisheddate = GetNextDate($subscription, $publisheddate);
400 is($publisheddate, '1970-05-01');
401
402 # TEST CASE - 1 issue per year, no irregularity
403 $id = AddSubscriptionFrequency({
404     description => "1 issue per year",
405     unit => 'year',
406     issuesperunit => 1,
407     unitsperissue => 1,
408 });
409 $subscription = {
410     periodicity => $id,
411     firstacquidate => '1970-01-01',
412     irregularity => '',
413     countissuesperunit => 1,
414 };
415 $publisheddate = $subscription->{firstacquidate};
416 $publisheddate = GetNextDate($subscription, $publisheddate);
417 is($publisheddate, '1971-01-01');
418 $publisheddate = GetNextDate($subscription, $publisheddate);
419 is($publisheddate, '1972-01-01');
420 $publisheddate = GetNextDate($subscription, $publisheddate);
421 is($publisheddate, '1973-01-01');
422
423 # TEST CASE - 1 issue per year, irregularities
424 $subscription = {
425     periodicity => $id,
426     firstacquidate => '1970-01-01',
427     irregularity => '2;4',
428     countissuesperunit => 1,
429 };
430 $publisheddate = $subscription->{firstacquidate};
431 $publisheddate = GetNextDate($subscription, $publisheddate);
432 is($publisheddate, '1972-01-01');
433 $publisheddate = GetNextDate($subscription, $publisheddate);
434 is($publisheddate, '1974-01-01');
435 $publisheddate = GetNextDate($subscription, $publisheddate);
436 is($publisheddate, '1975-01-01');
437
438 # TEST CASE - 1 issue every 2 years, no irregularity
439 $id = AddSubscriptionFrequency({
440     description => "1 issue every 2 years",
441     unit => 'year',
442     issuesperunit => 1,
443     unitsperissue => 2,
444 });
445 $subscription = {
446     periodicity => $id,
447     firstacquidate => '1970-01-01',
448     irregularity => '',
449     countissuesperunit => 1,
450 };
451 $publisheddate = $subscription->{firstacquidate};
452 $publisheddate = GetNextDate($subscription, $publisheddate);
453 is($publisheddate, '1972-01-01');
454 $publisheddate = GetNextDate($subscription, $publisheddate);
455 is($publisheddate, '1974-01-01');
456 $publisheddate = GetNextDate($subscription, $publisheddate);
457 is($publisheddate, '1976-01-01');
458
459 # TEST CASE - 1 issue every 2 years, irregularities
460 $subscription = {
461     periodicity => $id,
462     firstacquidate => '1970-01-01',
463     irregularity => '2;4',
464     countissuesperunit => 1,
465 };
466 $publisheddate = $subscription->{firstacquidate};
467 $publisheddate = GetNextDate($subscription, $publisheddate);
468 is($publisheddate, '1974-01-01');
469 $publisheddate = GetNextDate($subscription, $publisheddate);
470 is($publisheddate, '1978-01-01');
471 $publisheddate = GetNextDate($subscription, $publisheddate);
472 is($publisheddate, '1980-01-01');
473 # Move publisheddate to Feb 29 (leap year 1980)
474 $publisheddate = '1980-02-29';
475 $publisheddate = GetNextDate( $subscription, $publisheddate );
476 is( $publisheddate, '1982-02-28', 'Test +2 year from Feb 29' );
477
478 # TEST CASE - 2 issues per year, no irregularity
479 $id = AddSubscriptionFrequency({
480     description => "2 issues per year",
481     unit => 'year',
482     issuesperunit => 2,
483     unitsperissue => 1,
484 });
485 $subscription = {
486     periodicity => $id,
487     firstacquidate => '1970-01-01',
488     irregularity => '',
489     countissuesperunit => 1,
490 };
491 $publisheddate = $subscription->{firstacquidate};
492 $publisheddate = GetNextDate($subscription, $publisheddate);
493 is($publisheddate, '1970-07-02');
494 $publisheddate = GetNextDate($subscription, $publisheddate);
495 is($publisheddate, '1971-01-01');
496 $publisheddate = GetNextDate($subscription, $publisheddate);
497 is($publisheddate, '1971-07-02');
498 $publisheddate = GetNextDate($subscription, $publisheddate);
499 is($publisheddate, '1972-01-01');
500
501 # TEST CASE - 2 issues per year, irregularities
502 $subscription = {
503     periodicity => $id,
504     firstacquidate => '1970-01-01',
505     irregularity => '3;5;6',
506     countissuesperunit => 1,
507 };
508 $publisheddate = $subscription->{firstacquidate};
509 $publisheddate = GetNextDate($subscription, $publisheddate);
510 is($publisheddate, '1970-07-02');
511 $publisheddate = GetNextDate($subscription, $publisheddate);
512 is($publisheddate, '1971-07-02');
513 $publisheddate = GetNextDate($subscription, $publisheddate);
514 is($publisheddate, '1973-01-01');
515 $publisheddate = GetNextDate($subscription, $publisheddate);
516 is($publisheddate, '1973-07-02');
517 $publisheddate = GetNextDate($subscription, $publisheddate);
518 is($publisheddate, '1974-01-01');
519
520 # TEST CASE - 9 issues per year, dates spread throughout month
521 $id = AddSubscriptionFrequency({
522     description => "9 issues per year",
523     unit => 'year',
524     issuesperunit => 9,
525     unitsperissue => 1,
526 });
527 $subscription = {
528     periodicity => $id,
529     firstacquidate => '1970-08-10',
530     irregularity => '',
531     countissuesperunit => 1,
532 };
533 my @dates = ( $subscription->{firstacquidate} );
534 foreach(1..27) {
535     push @dates, GetNextDate( $subscription, $dates[-1] );
536 }
537 is( $dates[9],  '1971-08-10', 'Freq 9/yr, 1 year passed' );
538 is( $dates[18], '1972-08-10', 'Freq 9/yr, 2 years passed (leap year)' );
539 is( $dates[27], '1973-08-10', 'Freq 9/yr, 3 years passed' );
540 # Keep (first) position in cycle, but shift back 9 days
541 is( GetNextDate( $subscription, '1973-08-01' ), '1973-09-10', 'Back 9 days, without annual correction' );
542 # Set position to last in cycle, and shift back 9 days; annual correction
543 $subscription->{countissuesperunit} = 9;
544 is( GetNextDate( $subscription, '1973-08-01' ), '1973-09-15', 'Last in cycle, back 9 days, expect annual correction' );
545
546 # TEST CASE - Irregular
547 $id = AddSubscriptionFrequency({
548     description => "Irregular",
549     unit => undef,
550     issuesperunit => 1,
551     unitsperissue => 1,
552 });
553 $subscription = {
554     periodicity => $id,
555     firstacquidate => '1970-01-01',
556     irregularity => '',
557     countissuesperunit => 1,
558 };
559 $publisheddate = $subscription->{firstacquidate};
560 # GetNextDate always return undef if subscription is irregular
561 $publisheddate = GetNextDate($subscription, $publisheddate);
562 is($publisheddate, undef);
563
564 # GetNextDate returns undef if one of two first parameters is undef
565 $publisheddate = GetNextDate($subscription, undef);
566 is($publisheddate, undef);
567 $publisheddate = GetNextDate(undef, $subscription->{firstacquidate});
568 is($publisheddate, undef);
569 $publisheddate = GetNextDate(undef, undef);
570 is($publisheddate, undef);
571
572 $schema->storage->txn_rollback;