]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/t/14-OpenILS-Utils.t
LP1856906: remove unused OpenILS::Utils::ISBN perl module
[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 => 48;
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::Lockfile' );
28 use_ok( 'OpenILS::Utils::MFHDParser' );
29 use_ok( 'OpenILS::Utils::MFHD' );
30 use_ok( 'OpenILS::Utils::ModsParser' );
31 use_ok( 'OpenILS::Utils::Normalize' );
32 use_ok( 'OpenILS::Utils::OfflineStore' );
33 use_ok( 'OpenILS::Utils::Penalty' );
34 use_ok( 'OpenILS::Utils::PermitHold' );
35 use_ok( 'OpenILS::Utils::RemoteAccount' );
36 use_ok( 'OpenILS::Utils::ZClient' );
37 use_ok( 'OpenILS::Utils::EDIReader' );
38 use_ok( 'OpenILS::Utils::HTTPClient' );
39 use_ok( 'OpenILS::Utils::DateTime' );
40
41 # LP 800269 - Test MFHD holdings for records that only contain a caption field
42 my $co_marc = MARC::Record->new();
43 $co_marc->append_fields(
44     MARC::Field->new('853','','',
45         '8' => '1',
46         'a' => 'v.',
47         'b' => '[no.]',
48     )
49 );
50 my $co_mfhd = MFHD->new($co_marc);
51
52 my @comp_holdings;
53 warning_like {
54     @comp_holdings = $co_mfhd->get_compressed_holdings($co_mfhd->field('853'));
55 } [ qr/Cannot compress without pattern data, returning original holdings/ ],
56     "warning when attempting to compress holdings without a pattern";
57
58 is(@comp_holdings, 0, "Compressed holdings for an MFHD record that only has a caption");
59
60 my @decomp_holdings = $co_mfhd->get_decompressed_holdings($co_mfhd->field('853'));
61 is(@decomp_holdings, 0, "Decompressed holdings for an MFHD record that only has a caption");
62
63 my $apostring = OpenILS::Utils::Normalize::naco_normalize("it's time");
64 is($apostring, "its time", "naco_normalize: strip apostrophes");
65
66 my $apos = OpenILS::Utils::Normalize::search_normalize("it's time");
67 is($apos, "it s time", "search_normalize: replace apostrophes with space");
68
69 my $raw_marcxml = <<RAWMARC;
70 <?xml version="1.0" encoding="utf-8"?>
71 <record>
72   <leader>01614nmm a22003975u 4500</leader>
73   <controlfield tag="001">978-0-387-35767-6</controlfield>
74   <controlfield tag="003">Springer</controlfield>
75   <controlfield tag="005">20071022150035.8</controlfield>
76   <controlfield tag="007">cr nn 008mamaa</controlfield>
77   <controlfield tag="008">071022s2008    xx         j        eng d</controlfield>
78   <datafield tag="020" ind1=" " ind2=" ">
79     <subfield code="a">9780387685748</subfield>
80   </datafield>
81   <datafield tag="100" ind1="1" ind2=" ">
82     <subfield code="a">Neteler, Markus.</subfield>
83   </datafield>
84   <datafield tag="245" ind1="1" ind2="0">
85     <subfield code="a">Open Source GIS</subfield>
86     <subfield code="h">[electronic resource] :</subfield>
87     <subfield code="b">A GRASS GIS Approach /</subfield>
88     <subfield code="c">edited by Markus Neteler, Helena Mitasova.</subfield>
89   </datafield>
90   <datafield tag="250" ind1=" " ind2=" ">
91     <subfield code="a">Third Edition.</subfield>
92   </datafield>
93   <datafield tag="260" ind1=" " ind2=" ">
94     <subfield code="a">Boston, MA :</subfield>
95     <subfield code="b">Springer Science+Business Media, LLC,</subfield>
96     <subfield code="c">2008.</subfield>
97   </datafield>
98 </record>
99 RAWMARC
100 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>';
101 my $clean_xml = OpenILS::Utils::Normalize::clean_marc($raw_marcxml);
102 is($clean_xml, $exp_xml, "clean_marc: header and space normalization");
103
104 is(OpenILS::Application::AppUtils->entityize('èöçÇÈÀ'), '&#xE8;&#xF6;&#xE7;&#xC7;&#xC8;&#xC0;', 'entityize: diacritics NFC');
105 is(OpenILS::Application::AppUtils->entityize('èöçÇÈÀ', 'D'), 'e&#x300;o&#x308;c&#x327;C&#x327;E&#x300;A&#x300;', 'entityize: diacritics NFD');
106 is(OpenILS::Utils::Normalize::clean_marc('èöçÇÈÀ'), '&#xE8;&#xF6;&#xE7;&#xC7;&#xC8;&#xC0;', 'clean_marc: diacritics');
107
108 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'";
109
110 my $edi_msgs = OpenILS::Utils::EDIReader->new->read($edi_invoice);
111
112 is($edi_msgs->[0]->{message_type}, 'INVOIC', 'edi reader: message type');
113 is($edi_msgs->[0]->{purchase_order}, '24', 'edi reader: PO number');
114 is($edi_msgs->[1]->{invoice_ident}, '5TST084027', 'edi reader: invoice ident');
115 is(scalar(@{$edi_msgs->[1]->{lineitems}}), '2', 'edi reader: lineitem count');
116
117 is (OpenILS::Utils::DateTime::interval_to_seconds('1 second'), 1);
118 is (OpenILS::Utils::DateTime::interval_to_seconds('1 minute'), 60);
119 is (OpenILS::Utils::DateTime::interval_to_seconds('1 hour'), 3600);
120 is (OpenILS::Utils::DateTime::interval_to_seconds('1 day'), 86400);
121 is (OpenILS::Utils::DateTime::interval_to_seconds('1 week'), 604800);
122 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month'), 2628000);
123
124 # With context, no DST change, with timezone
125 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
126     DateTime::Format::ISO8601->new->parse_datetime('2017-02-04T23:59:59-04')->set_time_zone("America/New_York")), 2419200);
127
128 # With context, with DST change, with timezone
129 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
130     DateTime::Format::ISO8601->new->parse_datetime('2017-02-14T23:59:59-04')->set_time_zone("America/New_York")), 2415600);
131
132 # With context, no DST change, no time zone
133 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
134     DateTime::Format::ISO8601->new->parse_datetime('2017-02-04T23:59:59-04')), 2419200);
135
136 # With context, with DST change, no time zone (so, not DST-aware)
137 is (OpenILS::Utils::DateTime::interval_to_seconds('1 month',
138     DateTime::Format::ISO8601->new->parse_datetime('2017-02-14T23:59:59-04')), 2419200);
139
140 is (OpenILS::Utils::DateTime::interval_to_seconds('1 year'), 31536000);
141 is (OpenILS::Utils::DateTime::interval_to_seconds('1 year 1 second'), 31536001);
142 is (OpenILS::Utils::DateTime::interval_to_seconds('167:59:59'), 604799, 'correctly convert HHH:MM:SS intervals where hours longer than 2 digits');
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');