]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/t/14-OpenILS-Utils.t
LP#1796945 Reporter cloning and creation fixes
[working/Evergreen.git] / Open-ILS / src / perlmods / t / 14-OpenILS-Utils.t
1 #!perl
2
3 # FIXME: unlike the rest of the test cases here, we're /not/ enabling
4 # taint checks. The version of DateTime::TimeZone that ships with
5 # Ubuntu 14.04 LTS (Trusty) has a bug where attempting to get the
6 # local time zone can fail (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=737265).
7 #
8 # It's arguable whether taint checking should be enabled at all in
9 # the test suite. On the one hand, it is recommended practice for
10 # all code that accepts external input; on the other hand, a typical
11 # Evergreen installation doesn't run anything setuid/setgid that
12 # would automatically trigger taint-checking. Ideally we would
13 # eat our Wheaties, but we may be looking at consuming an entire
14 # truckload to verify that everything would continue to work if
15 # we turn it on across the board.
16
17 use Test::More tests => 47;
18 use Test::Warn;
19 use DateTime::TimeZone;
20 use DateTime::Format::ISO8601;
21 use utf8;
22
23 use_ok( 'OpenILS::Utils::Configure' );
24 use_ok( 'OpenILS::Utils::Cronscript' );
25 use_ok( 'OpenILS::Utils::CStoreEditor' );
26 use_ok( 'OpenILS::Utils::Fieldmapper' );
27 use_ok( 'OpenILS::Utils::ISBN' );
28 use_ok( 'OpenILS::Utils::Lockfile' );
29 use_ok( 'OpenILS::Utils::MFHDParser' );
30 use_ok( 'OpenILS::Utils::MFHD' );
31 use_ok( 'OpenILS::Utils::ModsParser' );
32 use_ok( 'OpenILS::Utils::Normalize' );
33 use_ok( 'OpenILS::Utils::OfflineStore' );
34 use_ok( 'OpenILS::Utils::Penalty' );
35 use_ok( 'OpenILS::Utils::PermitHold' );
36 use_ok( 'OpenILS::Utils::RemoteAccount' );
37 use_ok( 'OpenILS::Utils::ZClient' );
38 use_ok( 'OpenILS::Utils::EDIReader' );
39 use_ok( 'OpenILS::Utils::HTTPClient' );
40 use_ok( 'OpenILS::Utils::DateTime' );
41
42 # LP 800269 - Test MFHD holdings for records that only contain a caption field
43 my $co_marc = MARC::Record->new();
44 $co_marc->append_fields(
45     MARC::Field->new('853','','',
46         '8' => '1',
47         'a' => 'v.',
48         'b' => '[no.]',
49     )
50 );
51 my $co_mfhd = MFHD->new($co_marc);
52
53 my @comp_holdings;
54 warning_like {
55     @comp_holdings = $co_mfhd->get_compressed_holdings($co_mfhd->field('853'));
56 } [ qr/Cannot compress without pattern data, returning original holdings/ ],
57     "warning when attempting to compress holdings without a pattern";
58
59 is(@comp_holdings, 0, "Compressed holdings for an MFHD record that only has a caption");
60
61 my @decomp_holdings = $co_mfhd->get_decompressed_holdings($co_mfhd->field('853'));
62 is(@decomp_holdings, 0, "Decompressed holdings for an MFHD record that only has a caption");
63
64 my $apostring = OpenILS::Utils::Normalize::naco_normalize("it's time");
65 is($apostring, "its time", "naco_normalize: strip apostrophes");
66
67 my $apos = OpenILS::Utils::Normalize::search_normalize("it's time");
68 is($apos, "it s time", "search_normalize: replace apostrophes with space");
69
70 my $raw_marcxml = <<RAWMARC;
71 <?xml version="1.0" encoding="utf-8"?>
72 <record>
73   <leader>01614nmm a22003975u 4500</leader>
74   <controlfield tag="001">978-0-387-35767-6</controlfield>
75   <controlfield tag="003">Springer</controlfield>
76   <controlfield tag="005">20071022150035.8</controlfield>
77   <controlfield tag="007">cr nn 008mamaa</controlfield>
78   <controlfield tag="008">071022s2008    xx         j        eng d</controlfield>
79   <datafield tag="020" ind1=" " ind2=" ">
80     <subfield code="a">9780387685748</subfield>
81   </datafield>
82   <datafield tag="100" ind1="1" ind2=" ">
83     <subfield code="a">Neteler, Markus.</subfield>
84   </datafield>
85   <datafield tag="245" ind1="1" ind2="0">
86     <subfield code="a">Open Source GIS</subfield>
87     <subfield code="h">[electronic resource] :</subfield>
88     <subfield code="b">A GRASS GIS Approach /</subfield>
89     <subfield code="c">edited by Markus Neteler, Helena Mitasova.</subfield>
90   </datafield>
91   <datafield tag="250" ind1=" " ind2=" ">
92     <subfield code="a">Third Edition.</subfield>
93   </datafield>
94   <datafield tag="260" ind1=" " ind2=" ">
95     <subfield code="a">Boston, MA :</subfield>
96     <subfield code="b">Springer Science+Business Media, LLC,</subfield>
97     <subfield code="c">2008.</subfield>
98   </datafield>
99 </record>
100 RAWMARC
101 my $exp_xml = '<record><leader>01614nmm a22003975u 4500</leader><controlfield tag="001">978-0-387-35767-6</controlfield><controlfield tag="003">Springer</controlfield><controlfield tag="005">20071022150035.8</controlfield><controlfield tag="007">cr nn 008mamaa</controlfield><controlfield tag="008">071022s2008    xx         j        eng d</controlfield><datafield tag="020" ind1=" " ind2=" "><subfield code="a">9780387685748</subfield></datafield><datafield tag="100" ind1="1" ind2=" "><subfield code="a">Neteler, Markus.</subfield></datafield><datafield tag="245" ind1="1" ind2="0"><subfield code="a">Open Source GIS</subfield><subfield code="h">[electronic resource] :</subfield><subfield code="b">A GRASS GIS Approach /</subfield><subfield code="c">edited by Markus Neteler, Helena Mitasova.</subfield></datafield><datafield tag="250" ind1=" " ind2=" "><subfield code="a">Third Edition.</subfield></datafield><datafield tag="260" ind1=" " ind2=" "><subfield code="a">Boston, MA :</subfield><subfield code="b">Springer Science+Business Media, LLC,</subfield><subfield code="c">2008.</subfield></datafield></record>';
102 my $clean_xml = OpenILS::Utils::Normalize::clean_marc($raw_marcxml);
103 is($clean_xml, $exp_xml, "clean_marc: header and space normalization");
104
105 is(OpenILS::Application::AppUtils->entityize('èöçÇÈÀ'), '&#xE8;&#xF6;&#xE7;&#xC7;&#xC8;&#xC0;', 'entityize: diacritics NFC');
106 is(OpenILS::Application::AppUtils->entityize('èöçÇÈÀ', 'D'), 'e&#x300;o&#x308;c&#x327;C&#x327;E&#x300;A&#x300;', 'entityize: diacritics NFD');
107 is(OpenILS::Utils::Normalize::clean_marc('èöçÇÈÀ'), '&#xE8;&#xF6;&#xE7;&#xC7;&#xC8;&#xC0;', 'clean_marc: diacritics');
108
109 my $edi_invoice = "UNA:+.? 'UNB+UNOC:3+1556150:31B+123EVER:31B+120926:1621+4'UNH+11+INVOIC:D:96A:UN'BGM+380+5TST084026+9'DTM+137:20120924:102'RFF+ON:24'NAD+BY+123EVER 0001::91'NAD+SU+1691503::31B'CUX+2:USD:4'LIN+1++9780446360272'QTY+47:5'MOA+146:4.5:USD:10'MOA+203:14.65'PRI+AAF:2.93:DI:NTP'RFF+LI:24/102'LIN+2++9780446357197'QTY+47:8'MOA+146:6.5:USD:10'MOA+203:33.84'PRI+AAF:4.23:DI:NTP'RFF+LI:24/100'UNS+S'MOA+86:66.18'ALC+C++++DL'MOA+8:2'ALC+C++++CA'MOA+131:12.3'ALC+C++++TX'MOA+8:3.39'UNT+28+11'UNH+12+INVOIC:D:96A:UN'BGM+380+5TST084027+9'DTM+137:20120924:102'RFF+ON:26'NAD+BY+123EVER 0001::91'NAD+SU+1691503::31B'CUX+2:USD:4'LIN+1++9780446360272'QTY+47:1'MOA+146:4.5:USD:10'MOA+203:4.05'PRI+AAF:4.05:DI:NTP'RFF+LI:26/106'LIN+2++9780446350105'QTY+47:3'MOA+146:6.99:USD:10'MOA+203:14.67'PRI+AAF:4.89:DI:NTP'RFF+LI:26/105'UNS+S'MOA+86:25.03'ALC+C++++DL'MOA+8:2'ALC+C++++CA'MOA+131:3'ALC+C++++TX'MOA+8:1.31'UNT+28+12'UNZ+4+4'";
110
111 my $edi_msgs = OpenILS::Utils::EDIReader->new->read($edi_invoice);
112
113 is($edi_msgs->[0]->{message_type}, 'INVOIC', 'edi reader: message type');
114 is($edi_msgs->[0]->{purchase_order}, '24', 'edi reader: PO number');
115 is($edi_msgs->[1]->{invoice_ident}, '5TST084027', 'edi reader: invoice ident');
116 is(scalar(@{$edi_msgs->[1]->{lineitems}}), '2', 'edi reader: lineitem count');
117
118 is (OpenILS::Utils::DateTime::interval_to_seconds('1 second'), 1);
119 is (OpenILS::Utils::DateTime::interval_to_seconds('1 minute'), 60);
120 is (OpenILS::Utils::DateTime::interval_to_seconds('1 hour'), 3600);
121 is (OpenILS::Utils::DateTime::interval_to_seconds('1 day'), 86400);
122 is (OpenILS::Utils::DateTime::interval_to_seconds('1 week'), 604800);
123 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month'), 2628000);
124
125 # With context, no DST change, with timezone
126 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
127     DateTime::Format::ISO8601->new->parse_datetime('2017-02-04T23:59:59-04')->set_time_zone("America/New_York")), 2419200);
128
129 # With context, with DST change, with timezone
130 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
131     DateTime::Format::ISO8601->new->parse_datetime('2017-02-14T23:59:59-04')->set_time_zone("America/New_York")), 2415600);
132
133 # With context, no DST change, no time zone
134 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
135     DateTime::Format::ISO8601->new->parse_datetime('2017-02-04T23:59:59-04')), 2419200);
136
137 # With context, with DST change, no time zone (so, not DST-aware)
138 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
139     DateTime::Format::ISO8601->new->parse_datetime('2017-02-14T23:59:59-04')), 2419200);
140
141 is (OpenILS::Utils::DateTime::interval_to_seconds('1 year'), 31536000);
142 is (OpenILS::Utils::DateTime::interval_to_seconds('1 year 1 second'), 31536001);
143
144 sub get_offset {
145     # get current timezone offset for future use
146     my $offset = DateTime::TimeZone::offset_as_string(
147                     DateTime::TimeZone->new( name => 'local' )->offset_for_datetime(
148                         DateTime::Format::ISO8601->new()->parse_datetime('2018-09-17')
149                     )
150                 );
151     $offset =~ s/^(.\d\d)(\d\d)+/$1:$2/;
152     return $offset;
153 }
154
155 is (OpenILS::Utils::DateTime::clean_ISO8601('20180917'), '2018-09-17T00:00:00', 'plain date converted to ISO8601 timestamp');
156 is (OpenILS::Utils::DateTime::clean_ISO8601('I am not a date'), 'I am not a date', 'non-date simply returned as is');
157 my $offset = get_offset();
158 is (OpenILS::Utils::DateTime::clean_ISO8601('20180917 08:31:15'), "2018-09-17T08:31:15$offset", 'time zone added to date/time');
159
160 # force timezone to specific value to avoid a spurious
161 # pass if this test happens to be run in UTC
162 $ENV{TZ} = 'EST';
163 is (OpenILS::Utils::DateTime::clean_ISO8601('2018-09-17T17:31:15Z'), "2018-09-17T17:31:15+00:00", 'interpret Z in timestamp correctly');