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 <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomas Cohen Arazi 2022-02-04 14:24:18 -03:00
parent 37fcf837ce
commit 29c263241d
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
3 changed files with 55 additions and 14 deletions

View File

@ -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;

View File

@ -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',

View File

@ -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;
};