|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2026 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 => 9; |
| 23 |
use Test::NoWarnings; |
| 24 |
|
| 25 |
use Koha::Availability::Result; |
| 26 |
|
| 27 |
subtest 'new() creates empty result' => sub { |
| 28 |
|
| 29 |
plan tests => 5; |
| 30 |
|
| 31 |
my $result = Koha::Availability::Result->new(); |
| 32 |
|
| 33 |
isa_ok( $result, 'Koha::Availability::Result', 'new() returns Result object' ); |
| 34 |
is( ref( $result->blockers ), 'HASH', 'blockers is a hashref' ); |
| 35 |
is( ref( $result->confirmations ), 'HASH', 'confirmations is a hashref' ); |
| 36 |
is( ref( $result->warnings ), 'HASH', 'warnings is a hashref' ); |
| 37 |
is( ref( $result->context ), 'HASH', 'context is a hashref' ); |
| 38 |
}; |
| 39 |
|
| 40 |
subtest 'add_blocker()' => sub { |
| 41 |
|
| 42 |
plan tests => 3; |
| 43 |
|
| 44 |
my $result = Koha::Availability::Result->new(); |
| 45 |
|
| 46 |
$result->add_blocker( test_blocker => 'value' ); |
| 47 |
|
| 48 |
is( $result->blockers->{test_blocker}, 'value', 'blocker added' ); |
| 49 |
is( keys %{ $result->blockers }, 1, 'one blocker present' ); |
| 50 |
isa_ok( $result->add_blocker( another => 1 ), 'Koha::Availability::Result', 'returns self for chaining' ); |
| 51 |
}; |
| 52 |
|
| 53 |
subtest 'add_confirmation()' => sub { |
| 54 |
|
| 55 |
plan tests => 3; |
| 56 |
|
| 57 |
my $result = Koha::Availability::Result->new(); |
| 58 |
|
| 59 |
$result->add_confirmation( test_confirm => 'value' ); |
| 60 |
|
| 61 |
is( $result->confirmations->{test_confirm}, 'value', 'confirmation added' ); |
| 62 |
is( keys %{ $result->confirmations }, 1, 'one confirmation present' ); |
| 63 |
isa_ok( $result->add_confirmation( another => 1 ), 'Koha::Availability::Result', 'returns self for chaining' ); |
| 64 |
}; |
| 65 |
|
| 66 |
subtest 'add_warning()' => sub { |
| 67 |
|
| 68 |
plan tests => 3; |
| 69 |
|
| 70 |
my $result = Koha::Availability::Result->new(); |
| 71 |
|
| 72 |
$result->add_warning( test_warning => 'value' ); |
| 73 |
|
| 74 |
is( $result->warnings->{test_warning}, 'value', 'warning added' ); |
| 75 |
is( keys %{ $result->warnings }, 1, 'one warning present' ); |
| 76 |
isa_ok( $result->add_warning( another => 1 ), 'Koha::Availability::Result', 'returns self for chaining' ); |
| 77 |
}; |
| 78 |
|
| 79 |
subtest 'set_context()' => sub { |
| 80 |
|
| 81 |
plan tests => 3; |
| 82 |
|
| 83 |
my $result = Koha::Availability::Result->new(); |
| 84 |
|
| 85 |
$result->set_context( item => 'test_item' ); |
| 86 |
|
| 87 |
is( $result->context->{item}, 'test_item', 'context value set' ); |
| 88 |
is( keys %{ $result->context }, 1, 'one context value present' ); |
| 89 |
isa_ok( |
| 90 |
$result->set_context( patron => 'test_patron' ), 'Koha::Availability::Result', |
| 91 |
'returns self for chaining' |
| 92 |
); |
| 93 |
}; |
| 94 |
|
| 95 |
subtest 'available()' => sub { |
| 96 |
|
| 97 |
plan tests => 2; |
| 98 |
|
| 99 |
my $result = Koha::Availability::Result->new(); |
| 100 |
|
| 101 |
ok( $result->available, 'available when no blockers' ); |
| 102 |
|
| 103 |
$result->add_blocker( test => 1 ); |
| 104 |
|
| 105 |
ok( !$result->available, 'not available when blockers present' ); |
| 106 |
}; |
| 107 |
|
| 108 |
subtest 'needs_confirmation()' => sub { |
| 109 |
|
| 110 |
plan tests => 2; |
| 111 |
|
| 112 |
my $result = Koha::Availability::Result->new(); |
| 113 |
|
| 114 |
ok( !$result->needs_confirmation, 'no confirmation needed when empty' ); |
| 115 |
|
| 116 |
$result->add_confirmation( test => 1 ); |
| 117 |
|
| 118 |
ok( $result->needs_confirmation, 'confirmation needed when confirmations present' ); |
| 119 |
}; |
| 120 |
|
| 121 |
subtest 'to_hashref()' => sub { |
| 122 |
|
| 123 |
plan tests => 6; |
| 124 |
|
| 125 |
my $result = Koha::Availability::Result->new(); |
| 126 |
|
| 127 |
$result->add_blocker( blocker1 => 'b1' ); |
| 128 |
$result->add_confirmation( confirm1 => 'c1' ); |
| 129 |
$result->add_warning( warning1 => 'w1' ); |
| 130 |
$result->set_context( item => 'test_item' ); |
| 131 |
$result->set_context( patron => 'test_patron' ); |
| 132 |
|
| 133 |
my $hashref = $result->to_hashref(); |
| 134 |
|
| 135 |
is( ref($hashref), 'HASH', 'returns hashref' ); |
| 136 |
is( $hashref->{blockers}->{blocker1}, 'b1', 'blockers included' ); |
| 137 |
is( $hashref->{confirms}->{confirm1}, 'c1', 'confirmations included as confirms' ); |
| 138 |
is( $hashref->{warnings}->{warning1}, 'w1', 'warnings included' ); |
| 139 |
is( $hashref->{item}, 'test_item', 'context item included at top level' ); |
| 140 |
is( $hashref->{patron}, 'test_patron', 'context patron included at top level' ); |
| 141 |
}; |