|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2026 Theke Solutions |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 8; |
| 23 |
use Test::Warn; |
| 24 |
use Test::NoWarnings; |
| 25 |
|
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
use C4::Context; |
| 29 |
|
| 30 |
BEGIN { |
| 31 |
use_ok('Koha::CSV'); |
| 32 |
} |
| 33 |
|
| 34 |
subtest 'new() tests' => sub { |
| 35 |
plan tests => 5; |
| 36 |
|
| 37 |
my $csv = Koha::CSV->new(); |
| 38 |
isa_ok( $csv, 'Koha::CSV', 'Object created' ); |
| 39 |
is( $csv->binary, 1, 'binary defaults to 1' ); |
| 40 |
is( $csv->formula, 'empty', 'formula defaults to empty' ); |
| 41 |
is( $csv->always_quote, 0, 'always_quote defaults to 0' ); |
| 42 |
is( $csv->eol, "\n", 'eol defaults to newline' ); |
| 43 |
}; |
| 44 |
|
| 45 |
subtest 'new() with CSVDelimiter preference tests' => sub { |
| 46 |
plan tests => 3; |
| 47 |
|
| 48 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 49 |
my $csv = Koha::CSV->new(); |
| 50 |
is( $csv->sep_char, ',', 'Uses comma from preference' ); |
| 51 |
|
| 52 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ';' ); |
| 53 |
$csv = Koha::CSV->new(); |
| 54 |
is( $csv->sep_char, ';', 'Uses semicolon from preference' ); |
| 55 |
|
| 56 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', 'tabulation' ); |
| 57 |
$csv = Koha::CSV->new(); |
| 58 |
is( $csv->sep_char, "\t", 'Converts tabulation to tab character' ); |
| 59 |
}; |
| 60 |
|
| 61 |
subtest 'new() with overrides tests' => sub { |
| 62 |
plan tests => 3; |
| 63 |
|
| 64 |
my $csv = Koha::CSV->new( |
| 65 |
{ |
| 66 |
sep_char => '|', |
| 67 |
always_quote => 1, |
| 68 |
eol => "\r\n", |
| 69 |
} |
| 70 |
); |
| 71 |
|
| 72 |
is( $csv->sep_char, '|', 'sep_char overridden' ); |
| 73 |
is( $csv->always_quote, 1, 'always_quote overridden' ); |
| 74 |
is( $csv->eol, "\r\n", 'eol overridden' ); |
| 75 |
}; |
| 76 |
|
| 77 |
subtest 'add_row() tests' => sub { |
| 78 |
plan tests => 4; |
| 79 |
|
| 80 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 81 |
my $csv = Koha::CSV->new(); |
| 82 |
|
| 83 |
# Test with array |
| 84 |
my $line = $csv->add_row( 'Title', 'Author', 'Year' ); |
| 85 |
is( $line, "Title,Author,Year\n", 'add_row with array' ); |
| 86 |
|
| 87 |
# Test with arrayref |
| 88 |
$line = $csv->add_row( [ 'Book 1', 'Smith, John', '2024' ] ); |
| 89 |
is( $line, "\"Book 1\",\"Smith, John\",2024\n", 'add_row with arrayref and quoted field' ); |
| 90 |
|
| 91 |
# Test with special characters |
| 92 |
$line = $csv->add_row( [ 'Title with "quotes"', 'Normal', 'Value' ] ); |
| 93 |
is( $line, "\"Title with \"\"quotes\"\"\",Normal,Value\n", 'add_row with quotes escaped' ); |
| 94 |
|
| 95 |
# Test with empty fields |
| 96 |
$line = $csv->add_row( [ 'Field1', '', 'Field3' ] ); |
| 97 |
is( $line, "Field1,,Field3\n", 'add_row with empty field' ); |
| 98 |
}; |
| 99 |
|
| 100 |
subtest 'combine() and string() tests' => sub { |
| 101 |
plan tests => 3; |
| 102 |
|
| 103 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 104 |
my $csv = Koha::CSV->new(); |
| 105 |
|
| 106 |
my $status = $csv->combine( 'A', 'B', 'C' ); |
| 107 |
ok( $status, 'combine returns true on success' ); |
| 108 |
|
| 109 |
my $line = $csv->string(); |
| 110 |
is( $line, "A,B,C\n", 'string returns combined line' ); |
| 111 |
|
| 112 |
# Test with always_quote |
| 113 |
$csv = Koha::CSV->new( { always_quote => 1 } ); |
| 114 |
$csv->combine( 'A', 'B', 'C' ); |
| 115 |
$line = $csv->string(); |
| 116 |
is( $line, "\"A\",\"B\",\"C\"\n", 'always_quote wraps all fields' ); |
| 117 |
}; |
| 118 |
|
| 119 |
subtest 'print() tests' => sub { |
| 120 |
plan tests => 2; |
| 121 |
|
| 122 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 123 |
my $csv = Koha::CSV->new(); |
| 124 |
|
| 125 |
# Create temp file |
| 126 |
my $output = ''; |
| 127 |
open my $fh, '>', \$output or die "Cannot open string as file: $!"; |
| 128 |
|
| 129 |
my $status = $csv->print( $fh, [ 'Header1', 'Header2', 'Header3' ] ); |
| 130 |
ok( $status, 'print returns true on success' ); |
| 131 |
|
| 132 |
$csv->print( $fh, [ 'Value1', 'Value2', 'Value3' ] ); |
| 133 |
close $fh; |
| 134 |
|
| 135 |
is( |
| 136 |
$output, |
| 137 |
"Header1,Header2,Header3\nValue1,Value2,Value3\n", |
| 138 |
'print writes correct CSV to filehandle' |
| 139 |
); |
| 140 |
}; |