|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2025 Koha Development team |
| 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 => 2; |
| 23 |
use Test::NoWarnings; |
| 24 |
use Test::Warn; |
| 25 |
|
| 26 |
use C4::SIP::ILS::Item; |
| 27 |
use C4::Context; |
| 28 |
use Koha::Database; |
| 29 |
use t::lib::TestBuilder; |
| 30 |
use t::lib::Mocks; |
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema(); |
| 33 |
my $builder = t::lib::TestBuilder->new(); |
| 34 |
|
| 35 |
subtest 'C4::SIP::ILS::Item->new() tests' => sub { |
| 36 |
|
| 37 |
plan tests => 7; |
| 38 |
|
| 39 |
$schema->storage->txn_begin; |
| 40 |
|
| 41 |
# Create test data |
| 42 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 43 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 44 |
my $biblio = $builder->build_sample_biblio(); |
| 45 |
my $item = $builder->build_sample_item( |
| 46 |
{ |
| 47 |
biblionumber => $biblio->biblionumber, |
| 48 |
homebranch => $library->branchcode, |
| 49 |
holdingbranch => $library->branchcode, |
| 50 |
barcode => 'TEST_BARCODE_123' |
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
subtest 'Valid barcode' => sub { |
| 55 |
|
| 56 |
plan tests => 3; |
| 57 |
|
| 58 |
my $sip_item = C4::SIP::ILS::Item->new('TEST_BARCODE_123'); |
| 59 |
ok( defined $sip_item, 'Item created successfully with valid barcode' ); |
| 60 |
is( $sip_item->{id}, 'TEST_BARCODE_123', 'Item ID matches barcode' ); |
| 61 |
isa_ok( $sip_item->{_object}, 'Koha::Item', 'Item object is correct type' ); |
| 62 |
}; |
| 63 |
|
| 64 |
subtest 'Nonexistent barcode' => sub { |
| 65 |
|
| 66 |
plan tests => 2; |
| 67 |
|
| 68 |
warnings_like { |
| 69 |
my $sip_item = C4::SIP::ILS::Item->new('NONEXISTENT_BARCODE'); |
| 70 |
ok( !defined $sip_item, 'Item not created for nonexistent barcode' ); |
| 71 |
} |
| 72 |
[qr/No item 'NONEXISTENT_BARCODE'/], 'Expected warning for nonexistent barcode'; |
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'Undefined item_id (barcodedecode returns undef)' => sub { |
| 76 |
|
| 77 |
plan tests => 2; |
| 78 |
|
| 79 |
# barcodedecode(undef) returns undef - should generate no warnings |
| 80 |
warnings_are { |
| 81 |
my $sip_item = C4::SIP::ILS::Item->new(undef); |
| 82 |
ok( !defined $sip_item, 'Item not created for undefined item_id' ); |
| 83 |
} |
| 84 |
[], 'No warnings when barcodedecode returns undef'; |
| 85 |
}; |
| 86 |
|
| 87 |
subtest 'Empty item_id (barcodedecode returns empty)' => sub { |
| 88 |
|
| 89 |
plan tests => 2; |
| 90 |
|
| 91 |
# barcodedecode('') returns '' after trimming - should generate no warnings |
| 92 |
warnings_are { |
| 93 |
my $sip_item = C4::SIP::ILS::Item->new(''); |
| 94 |
ok( !defined $sip_item, 'Item not created for empty item_id' ); |
| 95 |
} |
| 96 |
[], 'No warnings when barcodedecode returns empty string'; |
| 97 |
}; |
| 98 |
|
| 99 |
subtest 'Whitespace-only item_id (barcodedecode trims to empty)' => sub { |
| 100 |
|
| 101 |
plan tests => 6; |
| 102 |
|
| 103 |
# barcodedecode trims whitespace, may result in empty string |
| 104 |
warnings_are { |
| 105 |
my $sip_item = C4::SIP::ILS::Item->new(' '); |
| 106 |
ok( !defined $sip_item, 'Item not created for whitespace-only item_id' ); |
| 107 |
} |
| 108 |
[], 'No warnings for spaces-only item_id'; |
| 109 |
|
| 110 |
warnings_are { |
| 111 |
my $sip_item = C4::SIP::ILS::Item->new("\t"); |
| 112 |
ok( !defined $sip_item, 'Item not created for tab-only item_id' ); |
| 113 |
} |
| 114 |
[], 'No warnings for tab-only item_id'; |
| 115 |
|
| 116 |
warnings_are { |
| 117 |
my $sip_item = C4::SIP::ILS::Item->new("\n"); |
| 118 |
ok( !defined $sip_item, 'Item not created for newline-only item_id' ); |
| 119 |
} |
| 120 |
[], 'No warnings for newline-only item_id'; |
| 121 |
}; |
| 122 |
|
| 123 |
subtest 'barcodedecode edge cases' => sub { |
| 124 |
|
| 125 |
plan tests => 4; |
| 126 |
|
| 127 |
# Test cases where barcodedecode might return unexpected values |
| 128 |
warnings_are { |
| 129 |
my $sip_item = C4::SIP::ILS::Item->new(" \t \n "); |
| 130 |
ok( !defined $sip_item, 'Item not created for mixed whitespace' ); |
| 131 |
} |
| 132 |
[], 'No warnings for mixed whitespace that trims to empty'; |
| 133 |
|
| 134 |
# Zero is a valid barcode that should generate expected warning |
| 135 |
warnings_like { |
| 136 |
my $sip_item = C4::SIP::ILS::Item->new(0); |
| 137 |
ok( !defined $sip_item, 'Item not created for zero item_id' ); |
| 138 |
} |
| 139 |
[qr/No item '0'/], 'Expected warning for zero item_id (valid barcode)'; |
| 140 |
}; |
| 141 |
|
| 142 |
subtest 'Barcode decoding functionality' => sub { |
| 143 |
|
| 144 |
plan tests => 2; |
| 145 |
|
| 146 |
# Test that barcodedecode is working properly with valid input |
| 147 |
my $sip_item = C4::SIP::ILS::Item->new('TEST_BARCODE_123'); |
| 148 |
ok( defined $sip_item, 'Item found with barcode that needs decoding' ); |
| 149 |
is( $sip_item->{id}, 'TEST_BARCODE_123', 'Barcode properly decoded and stored' ); |
| 150 |
}; |
| 151 |
|
| 152 |
$schema->storage->txn_rollback; |
| 153 |
}; |