Bug 18152: Add tests
[koha.git] / t / Charset.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 17;
21 use Encode qw( is_utf8 );
22
23 use MARC::Record;
24
25 use t::lib::Mocks;
26
27 use utf8;
28 use open ':std', ':encoding(utf8)';
29
30 BEGIN {
31     use_ok('C4::Charset');
32 }
33
34 my $string;
35 ok(!defined(NormalizeString($string,undef,1)),'Uninitialized string case 1 normalizes to uninitialized string.');
36
37 $string = 'Sample';
38 ok(defined(NormalizeString($string,undef,0)), 'Initialized string case 1 normalizes to some string.');
39 ok(defined(NormalizeString($string,undef,1)), 'Initialized string case 2 normalizes to some string.');
40 ok(defined(NormalizeString($string,1,0)),     'Initialized string case 3 normalizes to some string.');
41 ok(defined(NormalizeString($string,1,1)),     'Initialized string case 4 normalizes to some string.');
42
43 my $octets = "abc";
44 ok(IsStringUTF8ish($octets), "verify octets are valid UTF-8 (ASCII)");
45
46 $octets = "flamb\xc3\xa9";
47 ok(!Encode::is_utf8($octets), "verify that string does not have Perl UTF-8 flag on");
48 ok(IsStringUTF8ish($octets), "verify octets are valid UTF-8 (LATIN SMALL LETTER E WITH ACUTE)");
49 ok(!Encode::is_utf8($octets), "verify that IsStringUTF8ish does not magically turn Perl UTF-8 flag on");
50
51 $octets = "a\xc2" . "c";
52 ok(!IsStringUTF8ish($octets), "verify octets are not valid UTF-8");
53
54 ok( !SetUTF8Flag(), 'SetUTF8Flag returns undef if no record passed' );
55
56 my $record = MARC::Record->new();
57 ok( !SetUTF8Flag($record), 'SetUTF8Flag returns undef if the record has no subfields' );
58 # Add some fields/subfields
59 $record->append_fields(
60     MARC::Field->new('100', ' ', ' ', a => 'Julio Cortazar'),
61     MARC::Field->new('245', ' ', ' ', a => 'Rayuela'),
62 );
63 # Verify our data serves its purpose
64 ok( !Encode::is_utf8($record->subfield('100','a')) &&
65     !Encode::is_utf8($record->subfield('245','a')),
66     'Verify that the subfields are NOT set the UTF-8 flag yet' );
67
68 SetUTF8Flag($record);
69
70 ok( Encode::is_utf8($record->subfield('100','a')) &&
71     Encode::is_utf8($record->subfield('245','a')),
72     'SetUTF8Flag sets the UTF-8 flag to all subfields' );
73
74 is( nsb_clean("\98Le\9c Moyen Âge"), "Le Moyen Âge", "nsb_clean removes \98 and \9c" );
75
76 subtest 'SetMarcUnicodeFlag' => sub {
77     plan tests => 2;
78     # TODO This should be done in MARC::Record
79     my $leader                  = '012345678X0             ';
80     my $expected_marc21_leader  = '012345678a0             '; # position 9 of leader must be 'a'
81     my $expected_unimarc_leader = '012345678X0             '; # position 9 of leader must not be changed
82     # Note that position 9 of leader should be blank for UNIMARC, but as it is not related to encoding
83     # we do not want to change it
84
85     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
86     my $marc21_record = MARC::Record->new;
87     $marc21_record->leader($leader);
88     SetMarcUnicodeFlag( $marc21_record, C4::Context->preference('marcflavour') );
89     is( $marc21_record->leader, $expected_marc21_leader, 'Leader 9 for MARC21 mush be "a"' );
90
91     t::lib::Mocks::mock_preference( 'marcflavour',             'UNIMARC' );
92     t::lib::Mocks::mock_preference( 'UNIMARCField100Language', 'fre' );
93     my $unimarc_record = MARC::Record->new;
94     $unimarc_record->leader($leader);
95     SetMarcUnicodeFlag( $unimarc_record, C4::Context->preference('marcflavour') );
96     is( $unimarc_record->leader, $expected_unimarc_leader, 'Leader 9 for UNIMARC must be blank' );
97 };
98
99 1;