From 29c263241d122b651e58d6c296eecb4a7e0aa648 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 4 Feb 2022 14:24:18 -0300 Subject: [PATCH] Bug 27424: (QA follow-up) Make Koha::SMTP::Server->store deal with defaults This patch makes the ->store method take care of setting the server as the only default one. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/SMTP/Server.t => SUCCESS: Tests pass! 3. The UI keeps working as it should. 4. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- Koha/SMTP/Server.pm | 24 +++++++++++++++++++++++ admin/smtp_servers.pl | 13 ------------- t/db_dependent/Koha/SMTP/Server.t | 32 ++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/Koha/SMTP/Server.pm b/Koha/SMTP/Server.pm index 1550d1ca4e..d357b89e24 100644 --- a/Koha/SMTP/Server.pm +++ b/Koha/SMTP/Server.pm @@ -31,6 +31,30 @@ Koha::SMTP::Server - Koha SMTP Server Object class =head2 Class methods +=head3 store + + $server->store; + +Overloaded store method. + +=cut + +sub store { + my ($self) = @_; + + $self->_result->result_source->schema->txn_do( + sub { + Koha::SMTP::Servers->search->update( { is_default => 0 }, + { no_triggers => 1 } ) + if $self->is_default; + + $self = $self->SUPER::store; + } + ); + + return $self; +} + =head3 transport my $transport = $smtp_server->transport; diff --git a/admin/smtp_servers.pl b/admin/smtp_servers.pl index ccad8e95ff..cd76cc5ff5 100755 --- a/admin/smtp_servers.pl +++ b/admin/smtp_servers.pl @@ -54,11 +54,7 @@ if ( $op eq 'add' ) { my $debug = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0; my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0; - my $schema = Koha::Database->new->schema; try { - $schema->storage->txn_begin; - - Koha::SMTP::Servers->search->update({ is_default => 0 }) if $is_default; Koha::SMTP::Server->new( { @@ -75,10 +71,8 @@ if ( $op eq 'add' ) { )->store; push @messages, { type => 'message', code => 'success_on_insert' }; - $schema->storage->txn_commit; } catch { - $schema->storage->txn_rollback; if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) { push @messages, { @@ -133,12 +127,7 @@ elsif ( $op eq 'edit_save' ) { my $debug = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0; my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0; - my $schema = Koha::Database->new->schema; - try { - $schema->storage->txn_begin; - - Koha::SMTP::Servers->search->update({ is_default => 0 }) if $is_default; $smtp_server->password( $password ) if defined $password and $password ne '****' @@ -162,10 +151,8 @@ elsif ( $op eq 'edit_save' ) { type => 'message', code => 'success_on_update' }; - $schema->storage->txn_commit; } catch { - $schema->storage->txn_rollback; push @messages, { type => 'alert', diff --git a/t/db_dependent/Koha/SMTP/Server.t b/t/db_dependent/Koha/SMTP/Server.t index c8922177dd..5ca2488455 100755 --- a/t/db_dependent/Koha/SMTP/Server.t +++ b/t/db_dependent/Koha/SMTP/Server.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Exception; use Test::Warn; @@ -104,3 +104,33 @@ subtest 'to_api() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'store() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + Koha::SMTP::Servers->search->delete; + + my $server_1 = $builder->build_object( + { class => 'Koha::SMTP::Servers', value => { is_default => 0 } } ); + my $server_2 = $builder->build_object( + { class => 'Koha::SMTP::Servers', value => { is_default => 0 } } ); + my $server_3 = $builder->build_object( + { class => 'Koha::SMTP::Servers', value => { is_default => 1 } } ); + + my $default_servers = Koha::SMTP::Servers->search( { is_default => 1 } ); + + is( $default_servers->count, 1, 'Only one default server' ); + is( $default_servers->next->id, $server_3->id, 'Server 3 is the default' ); + + $server_1->set( { is_default => 1 } )->store->discard_changes; + + $default_servers = Koha::SMTP::Servers->search( { is_default => 1 } ); + + is( $default_servers->count, 1, 'Only one default server' ); + is( $default_servers->next->id, $server_1->id, 'Server 1 is the default' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5