From b87d9be073a1be785bf3b8f3d2bb9de702b47218 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Sat, 30 Aug 2025 11:49:15 -0300 Subject: [PATCH] Bug 39190: Regression tests for polymorphic object creation This patch adds comprehensive tests to verify that Koha::File::Transports correctly returns polymorphic objects based on the transport field value. The tests cover: - build_object() polymorphic creation - search() with polymorphic resultset iteration - find() with polymorphic object return To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/File/Transports.t => FAIL: Tests fail - objects return base Koha::File::Transport instead of polymorphic Koha::File::Transport::SFTP/FTP classes Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/File/Transports.t | 153 ++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100755 t/db_dependent/Koha/File/Transports.t diff --git a/t/db_dependent/Koha/File/Transports.t b/t/db_dependent/Koha/File/Transports.t new file mode 100755 index 00000000000..21fb5f9cc08 --- /dev/null +++ b/t/db_dependent/Koha/File/Transports.t @@ -0,0 +1,153 @@ +#!/usr/bin/perl + +# 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 => 4; +use Test::NoWarnings; + +use Koha::Database; +use Koha::File::Transports; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'Polymorphic object creation' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + # Test SFTP transport polymorphism + my $sftp_transport = $builder->build_object( + { + class => 'Koha::File::Transports', + value => { + transport => 'sftp', + name => 'Test SFTP', + host => 'test.example.com', + } + } + ); + + is( + ref($sftp_transport), 'Koha::File::Transport::SFTP', + 'SFTP transport should be polymorphic Koha::File::Transport::SFTP object' + ); + + can_ok( $sftp_transport, '_write_key_file' ); + + # Test FTP transport polymorphism + my $ftp_transport = $builder->build_object( + { + class => 'Koha::File::Transports', + value => { + transport => 'ftp', + name => 'Test FTP', + host => 'ftp.example.com', + } + } + ); + + is( + ref($ftp_transport), 'Koha::File::Transport::FTP', + 'FTP transport should be polymorphic Koha::File::Transport::FTP object' + ); + + can_ok( $ftp_transport, 'connect' ); + + $schema->storage->txn_rollback; +}; + +subtest 'search() tests' => sub { + plan tests => 3; + + $schema->storage->txn_begin; + + # Create test transports + my $sftp_transport = $builder->build_object( + { + class => 'Koha::File::Transports', + value => { transport => 'sftp', name => 'Test SFTP' } + } + ); + my $ftp_transport = $builder->build_object( + { + class => 'Koha::File::Transports', + value => { transport => 'ftp', name => 'Test FTP' } + } + ); + + # Search for both transports + my $transports = Koha::File::Transports->search( { id => { -in => [ $sftp_transport->id, $ftp_transport->id ] } } ); + + is( + ref($transports), 'Koha::File::Transports', + 'search() should return Koha::File::Transports object' + ); + + # Check each object type from resultset + my @objects = $transports->as_list; + my %refs = map { $_->transport => ref($_) } @objects; + + is( + $refs{sftp}, 'Koha::File::Transport::SFTP', + 'SFTP object from search should be polymorphic' + ); + is( + $refs{ftp}, 'Koha::File::Transport::FTP', + 'FTP object from search should be polymorphic' + ); + + $schema->storage->txn_rollback; +}; + +subtest 'find() tests' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + # Create test transports + my $sftp_transport = $builder->build_object( + { + class => 'Koha::File::Transports', + value => { transport => 'sftp', name => 'Test SFTP' } + } + ); + my $ftp_transport = $builder->build_object( + { + class => 'Koha::File::Transports', + value => { transport => 'ftp', name => 'Test FTP' } + } + ); + + # Test find() returns correct polymorphic types + my $found_sftp = Koha::File::Transports->find( $sftp_transport->id ); + my $found_ftp = Koha::File::Transports->find( $ftp_transport->id ); + + is( + ref($found_sftp), 'Koha::File::Transport::SFTP', + 'find() should return polymorphic SFTP object' + ); + is( + ref($found_ftp), 'Koha::File::Transport::FTP', + 'find() should return polymorphic FTP object' + ); + + $schema->storage->txn_rollback; +}; -- 2.51.0