From 9c44df5fa1ace310ae2dcd4a5d24bc8d8a3a0ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Mon, 22 Sep 2025 15:56:07 -0300 Subject: [PATCH] Bug 40855: Add unit tests for Standard ILL backend edititem method This patch adds comprehensive unit tests for the edititem() method in the Standard ILL backend to establish baseline behavior before refactoring. The tests cover: - Form stage (initial load) - displays current attributes for editing - Commit stage (form submission) - processes updates with validation - Core field updates (title, author, year, etc.) - Custom field handling (null-delimited key/value pairs) - Attribute persistence after updates These tests demonstrate the current raw SQL implementation behavior and will ensure the upcoming refactoring maintains compatibility. Current behavior tested: - edititem() uses multi-stage workflow (init -> form -> commit) - Validation requires 'type' and 'branchcode' fields - Custom fields use null-delimited strings for multiple values - Method returns 'create' (not 'edititem') in commit stage - Raw SQL delete-all-and-recreate pattern for attribute updates Test plan: 1. Apply patch 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/ILL/Backend/Standard.t => SUCCESS: Tests pass! Current edititem behavior documented 3. Tests establish baseline for upcoming refactoring 4. Sign off :-D --- t/db_dependent/Koha/ILL/Backend/Standard.t | 115 +++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100755 t/db_dependent/Koha/ILL/Backend/Standard.t diff --git a/t/db_dependent/Koha/ILL/Backend/Standard.t b/t/db_dependent/Koha/ILL/Backend/Standard.t new file mode 100755 index 0000000000..652a3d21b9 --- /dev/null +++ b/t/db_dependent/Koha/ILL/Backend/Standard.t @@ -0,0 +1,115 @@ +#!/usr/bin/perl + +# Copyright 2025 Koha Development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 2; +use Test::MockModule; +use Test::NoWarnings; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Database; +use Koha::ILL::Backend::Standard; +use Koha::ILL::Requests; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'edititem() tests' => sub { + + plan tests => 12; + + $schema->storage->txn_begin; + + # Create a test library for validation + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + + # Create a test ILL request with NEW status (required for editing) + my $request = $builder->build_object( + { + class => 'Koha::ILL::Requests', + value => { status => 'NEW' } + } + ); + + # Add some initial attributes + $request->add_or_update_attributes( + { + title => 'Original Title', + author => 'Original Author', + isbn => '1234567890' + } + ); + + my $backend = Koha::ILL::Backend::Standard->new; + + # Test form stage (initial load) + my $form_params = { + request => $request, + other => { stage => 'init' } + }; + + my $form_result = $backend->edititem($form_params); + is( $form_result->{error}, 0, 'edititem form stage returns success' ); + is( $form_result->{method}, 'edititem', 'edititem form stage returns correct method' ); + is( $form_result->{stage}, 'form', 'edititem form stage returns form stage' ); + + # Test commit stage (form submission with all required fields) + my $commit_params = { + request => $request, + other => { + stage => 'form', + type => 'book', # Required field + branchcode => $library->branchcode, # Required field + title => 'Updated Title', + author => 'Updated Author', + year => '2023', # New attribute + custom_key => "custom1\0custom2", # Custom fields + custom_value => "value1\0value2" + } + }; + + my $commit_result = $backend->edititem($commit_params); + + # Check the result structure (method returns 'create' in commit stage) + is( $commit_result->{error}, 0, 'edititem commit returns success' ); + is( $commit_result->{method}, 'create', 'edititem commit returns create method' ); + is( $commit_result->{stage}, 'commit', 'edititem commit returns commit stage' ); + + # Refresh request to get updated attributes + $request->discard_changes; + + # Check attributes were updated correctly + my $title_attr = $request->extended_attributes->find( { type => 'title' } ); + my $author_attr = $request->extended_attributes->find( { type => 'author' } ); + my $year_attr = $request->extended_attributes->find( { type => 'year' } ); + my $custom1_attr = $request->extended_attributes->find( { type => 'custom1' } ); + my $custom2_attr = $request->extended_attributes->find( { type => 'custom2' } ); + + is( $title_attr->value, 'Updated Title', 'Title attribute updated' ); + is( $author_attr->value, 'Updated Author', 'Author attribute updated' ); + ok( $year_attr, 'New year attribute created' ); + is( $year_attr->value, '2023', 'Year attribute has correct value' ); + is( $custom1_attr->value, 'value1', 'Custom1 attribute created correctly' ); + is( $custom2_attr->value, 'value2', 'Custom2 attribute created correctly' ); + + $schema->storage->txn_rollback; +}; -- 2.51.0