|
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 <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 2; |
| 23 |
use Test::MockModule; |
| 24 |
use Test::NoWarnings; |
| 25 |
|
| 26 |
use t::lib::TestBuilder; |
| 27 |
use t::lib::Mocks; |
| 28 |
|
| 29 |
use Koha::Database; |
| 30 |
use Koha::ILL::Backend::Standard; |
| 31 |
use Koha::ILL::Requests; |
| 32 |
|
| 33 |
my $schema = Koha::Database->new->schema; |
| 34 |
my $builder = t::lib::TestBuilder->new; |
| 35 |
|
| 36 |
subtest 'edititem() tests' => sub { |
| 37 |
|
| 38 |
plan tests => 12; |
| 39 |
|
| 40 |
$schema->storage->txn_begin; |
| 41 |
|
| 42 |
# Create a test library for validation |
| 43 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 44 |
|
| 45 |
# Create a test ILL request with NEW status (required for editing) |
| 46 |
my $request = $builder->build_object( |
| 47 |
{ |
| 48 |
class => 'Koha::ILL::Requests', |
| 49 |
value => { status => 'NEW' } |
| 50 |
} |
| 51 |
); |
| 52 |
|
| 53 |
# Add some initial attributes |
| 54 |
$request->add_or_update_attributes( |
| 55 |
{ |
| 56 |
title => 'Original Title', |
| 57 |
author => 'Original Author', |
| 58 |
isbn => '1234567890' |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
my $backend = Koha::ILL::Backend::Standard->new; |
| 63 |
|
| 64 |
# Test form stage (initial load) |
| 65 |
my $form_params = { |
| 66 |
request => $request, |
| 67 |
other => { stage => 'init' } |
| 68 |
}; |
| 69 |
|
| 70 |
my $form_result = $backend->edititem($form_params); |
| 71 |
is( $form_result->{error}, 0, 'edititem form stage returns success' ); |
| 72 |
is( $form_result->{method}, 'edititem', 'edititem form stage returns correct method' ); |
| 73 |
is( $form_result->{stage}, 'form', 'edititem form stage returns form stage' ); |
| 74 |
|
| 75 |
# Test commit stage (form submission with all required fields) |
| 76 |
my $commit_params = { |
| 77 |
request => $request, |
| 78 |
other => { |
| 79 |
stage => 'form', |
| 80 |
type => 'book', # Required field |
| 81 |
branchcode => $library->branchcode, # Required field |
| 82 |
title => 'Updated Title', |
| 83 |
author => 'Updated Author', |
| 84 |
year => '2023', # New attribute |
| 85 |
custom_key => "custom1\0custom2", # Custom fields |
| 86 |
custom_value => "value1\0value2" |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
my $commit_result = $backend->edititem($commit_params); |
| 91 |
|
| 92 |
# Check the result structure (method returns 'create' in commit stage) |
| 93 |
is( $commit_result->{error}, 0, 'edititem commit returns success' ); |
| 94 |
is( $commit_result->{method}, 'create', 'edititem commit returns create method' ); |
| 95 |
is( $commit_result->{stage}, 'commit', 'edititem commit returns commit stage' ); |
| 96 |
|
| 97 |
# Refresh request to get updated attributes |
| 98 |
$request->discard_changes; |
| 99 |
|
| 100 |
# Check attributes were updated correctly |
| 101 |
my $title_attr = $request->extended_attributes->find( { type => 'title' } ); |
| 102 |
my $author_attr = $request->extended_attributes->find( { type => 'author' } ); |
| 103 |
my $year_attr = $request->extended_attributes->find( { type => 'year' } ); |
| 104 |
my $custom1_attr = $request->extended_attributes->find( { type => 'custom1' } ); |
| 105 |
my $custom2_attr = $request->extended_attributes->find( { type => 'custom2' } ); |
| 106 |
|
| 107 |
is( $title_attr->value, 'Updated Title', 'Title attribute updated' ); |
| 108 |
is( $author_attr->value, 'Updated Author', 'Author attribute updated' ); |
| 109 |
ok( $year_attr, 'New year attribute created' ); |
| 110 |
is( $year_attr->value, '2023', 'Year attribute has correct value' ); |
| 111 |
is( $custom1_attr->value, 'value1', 'Custom1 attribute created correctly' ); |
| 112 |
is( $custom2_attr->value, 'value2', 'Custom2 attribute created correctly' ); |
| 113 |
|
| 114 |
$schema->storage->txn_rollback; |
| 115 |
}; |