Bug 14364: Allow automatically canceled expired waiting holds to fill the next hold
[koha.git] / t / Ediorder.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use FindBin qw( $Bin );
5
6 use Test::More tests => 13;
7 use t::lib::Mocks;
8
9 BEGIN { use_ok('Koha::Edifact::Order') }
10 t::lib::Mocks::mock_preference('EdifactLSQ', 'location');
11
12 # The following tests are for internal methods but they could
13 # error spectacularly so best
14 # Check that quoting is done correctly
15 #
16 my $processed_text =
17   Koha::Edifact::Order::encode_text(q{string containing ?,',:,+});
18
19 cmp_ok(
20     $processed_text, 'eq',
21     q{string containing ??,?',?:,?+},
22     'Outgoing text correctly quoted'
23 );
24
25 # extend above test to test chunking in imd_segment
26 #
27 my $code           = '010';
28 my $data_to_encode = $processed_text;
29
30 my @segs = Koha::Edifact::Order::imd_segment( $code, $data_to_encode );
31
32 my $testseg = "IMD+L+010+:::$processed_text";
33 $testseg .= q{'};    # add segment terminator
34
35 cmp_ok( $segs[0], 'eq', $testseg, 'IMD segment correctly formed' );
36
37 $data_to_encode = 'A' x 35;
38 $data_to_encode .= 'B' x 35;
39 $data_to_encode .= 'C' x 10;
40
41 @segs = Koha::Edifact::Order::imd_segment( $code, $data_to_encode );
42
43 cmp_ok(
44     $segs[0],
45     'eq',
46 q{IMD+L+010+:::AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'},
47     'IMD segment correctly chunked'
48 );
49 cmp_ok( $segs[1], 'eq', q{IMD+L+010+:::CCCCCCCCCC'},
50     'IMD segment correctly split across segments' );
51
52 $data_to_encode .= '??';
53
54 # this used to cause an infinite loop
55 @segs = Koha::Edifact::Order::imd_segment( $code, $data_to_encode );
56 cmp_ok( $segs[1], 'eq', q{IMD+L+010+:::CCCCCCCCCC??'},
57     'IMD segment deals with quoted character at end' );
58
59 # special case for text ending in apostrophe e.g. nuthin'
60 $data_to_encode .= q{?'};
61 @segs = Koha::Edifact::Order::imd_segment( $code, $data_to_encode );
62 cmp_ok(
63     $segs[1], 'eq',
64     q{IMD+L+010+:::CCCCCCCCCC???''},
65     'IMD segment deals with quoted apostrophe at end'
66 );
67
68 $data_to_encode =~ s/\?'$//;
69 @segs = Koha::Edifact::Order::imd_segment( $code, $data_to_encode );
70 cmp_ok( $segs[1], 'eq', q{IMD+L+010+:::CCCCCCCCCC??'},
71     'IMD segment deals with apostrophe preceded by quoted ?  at end' );
72
73 my $isbn = '3540556753';
74 my $ean  = '9783540556756';
75
76 my $seg = Koha::Edifact::Order::additional_product_id($isbn);
77 cmp_ok( $seg, 'eq', q{PIA+5+3540556753:IB'},
78     'isbn correctly encoded in PIA segment' );
79
80 $seg = Koha::Edifact::Order::additional_product_id($ean);
81 cmp_ok( $seg, 'eq', q{PIA+5+9783540556756:EN'},
82     'ean correctly encoded in PIA segment' );
83
84 my $orderfields = { budget_code => 'BUDGET', };
85 my @items       = (
86     {
87         itype          => 'TYPE',
88         location       => 'LOCATION',
89         itemcallnumber => 'CALL',
90         branchcode     => 'BRANCH',
91     },
92     {
93         itype          => 'TYPE',
94         location       => 'LOCATION',
95         itemcallnumber => 'CALL',
96         branchcode     => 'BRANCH',
97     }
98 );
99
100 my @gsegs = Koha::Edifact::Order::gir_segments(
101     {
102         ol_fields => $orderfields,
103         items     => \@items
104     }
105 );
106 cmp_ok(
107     $gsegs[0], 'eq',
108     q{GIR+001+BUDGET:LFN+BRANCH:LLO+TYPE:LST+LOCATION:LSQ+CALL:LSM},
109     'Single Gir field OK'
110 );
111
112 $orderfields->{servicing_instruction} = 'S_I';
113 @gsegs = Koha::Edifact::Order::gir_segments(
114     {
115         ol_fields => $orderfields,
116         items     => \@items
117     }
118 );
119 cmp_ok(
120     $gsegs[2], 'eq',
121     q{GIR+002+BUDGET:LFN+BRANCH:LLO+TYPE:LST+LOCATION:LSQ+CALL:LSM},
122     'First part of split Gir field OK'
123 );
124
125 cmp_ok( $gsegs[3], 'eq', q{GIR+002+S_I:LVT},
126     'Second part of split GIR field OK' );