|
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 => 7; |
| 23 |
use Test::NoWarnings; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
use t::lib::Mocks; |
| 27 |
|
| 28 |
BEGIN { |
| 29 |
use_ok('Koha::CSV::Patrons'); |
| 30 |
} |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
| 33 |
my $builder = t::lib::TestBuilder->new; |
| 34 |
|
| 35 |
subtest 'new() tests' => sub { |
| 36 |
plan tests => 2; |
| 37 |
|
| 38 |
my $csv = Koha::CSV::Patrons->new(); |
| 39 |
isa_ok( $csv, 'Koha::CSV::Patrons', 'Object created' ); |
| 40 |
|
| 41 |
$csv = Koha::CSV::Patrons->new( { fields => [qw( cardnumber surname firstname )] } ); |
| 42 |
is_deeply( $csv->fields(), [qw( cardnumber surname firstname )], 'Fields set correctly' ); |
| 43 |
}; |
| 44 |
|
| 45 |
subtest 'fields() tests' => sub { |
| 46 |
plan tests => 2; |
| 47 |
|
| 48 |
my $csv = Koha::CSV::Patrons->new(); |
| 49 |
is_deeply( $csv->fields(), [], 'Empty fields by default' ); |
| 50 |
|
| 51 |
$csv->fields( [qw( email phone )] ); |
| 52 |
is_deeply( $csv->fields(), [qw( email phone )], 'Fields updated' ); |
| 53 |
}; |
| 54 |
|
| 55 |
subtest 'format_patron() tests' => sub { |
| 56 |
plan tests => 4; |
| 57 |
|
| 58 |
$schema->storage->txn_begin; |
| 59 |
|
| 60 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 61 |
|
| 62 |
my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } ); |
| 63 |
my $patron = $builder->build_object( |
| 64 |
{ |
| 65 |
class => 'Koha::Patrons', |
| 66 |
value => { |
| 67 |
categorycode => $category->categorycode, |
| 68 |
surname => 'Test', |
| 69 |
firstname => 'Patron', |
| 70 |
email => 'test@example.com', |
| 71 |
} |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
my $csv = |
| 76 |
Koha::CSV::Patrons->new( { fields => [qw( cardnumber surname firstname email description category_type )] } ); |
| 77 |
|
| 78 |
my $row = $csv->format_patron($patron); |
| 79 |
|
| 80 |
isa_ok( $row, 'ARRAY', 'format_patron returns arrayref' ); |
| 81 |
is( scalar @$row, 6, 'Correct number of fields' ); |
| 82 |
is( $row->[1], 'Test', 'Surname field correct' ); |
| 83 |
is( $row->[2], 'Patron', 'Firstname field correct' ); |
| 84 |
|
| 85 |
$schema->storage->txn_rollback; |
| 86 |
}; |
| 87 |
|
| 88 |
subtest 'print_headers() tests' => sub { |
| 89 |
plan tests => 2; |
| 90 |
|
| 91 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 92 |
|
| 93 |
my $csv = Koha::CSV::Patrons->new( { fields => [qw( cardnumber surname firstname )] } ); |
| 94 |
|
| 95 |
my $output = ''; |
| 96 |
open my $fh, '>', \$output or die "Cannot open string as file: $!"; |
| 97 |
|
| 98 |
my $status = $csv->print_headers($fh); |
| 99 |
ok( $status, 'print_headers returns true' ); |
| 100 |
|
| 101 |
close $fh; |
| 102 |
|
| 103 |
is( $output, "cardnumber,surname,firstname\n", 'Headers printed correctly' ); |
| 104 |
}; |
| 105 |
|
| 106 |
subtest 'print_patron() tests' => sub { |
| 107 |
plan tests => 2; |
| 108 |
|
| 109 |
$schema->storage->txn_begin; |
| 110 |
|
| 111 |
t::lib::Mocks::mock_preference( 'CSVDelimiter', ',' ); |
| 112 |
|
| 113 |
my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } ); |
| 114 |
my $patron = $builder->build_object( |
| 115 |
{ |
| 116 |
class => 'Koha::Patrons', |
| 117 |
value => { |
| 118 |
categorycode => $category->categorycode, |
| 119 |
surname => 'Smith', |
| 120 |
firstname => 'John', |
| 121 |
} |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
my $csv = Koha::CSV::Patrons->new( { fields => [qw( surname firstname )] } ); |
| 126 |
|
| 127 |
my $output = ''; |
| 128 |
open my $fh, '>', \$output or die "Cannot open string as file: $!"; |
| 129 |
|
| 130 |
my $status = $csv->print_patron( $fh, $patron ); |
| 131 |
ok( $status, 'print_patron returns true' ); |
| 132 |
|
| 133 |
close $fh; |
| 134 |
|
| 135 |
like( $output, qr/Smith,John/, 'Patron data printed correctly' ); |
| 136 |
|
| 137 |
$schema->storage->txn_rollback; |
| 138 |
}; |