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