View | Details | Raw Unified | Return to bug 27424
Collapse All | Expand All

(-)a/Koha/SMTP/Server.pm (+24 lines)
Lines 31-36 Koha::SMTP::Server - Koha SMTP Server Object class Link Here
31
31
32
=head2 Class methods
32
=head2 Class methods
33
33
34
=head3 store
35
36
    $server->store;
37
38
Overloaded store method.
39
40
=cut
41
42
sub store {
43
    my ($self) = @_;
44
45
    $self->_result->result_source->schema->txn_do(
46
        sub {
47
            Koha::SMTP::Servers->search->update( { is_default => 0 },
48
                { no_triggers => 1 } )
49
              if $self->is_default;
50
51
            $self = $self->SUPER::store;
52
        }
53
    );
54
55
    return $self;
56
}
57
34
=head3 transport
58
=head3 transport
35
59
36
    my $transport = $smtp_server->transport;
60
    my $transport = $smtp_server->transport;
(-)a/admin/smtp_servers.pl (-13 lines)
Lines 54-64 if ( $op eq 'add' ) { Link Here
54
    my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
54
    my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
55
    my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
55
    my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
56
56
57
    my $schema = Koha::Database->new->schema;
58
    try {
57
    try {
59
        $schema->storage->txn_begin;
60
61
        Koha::SMTP::Servers->search->update({ is_default => 0 }) if $is_default;
62
58
63
        Koha::SMTP::Server->new(
59
        Koha::SMTP::Server->new(
64
            {
60
            {
Lines 75-84 if ( $op eq 'add' ) { Link Here
75
        )->store;
71
        )->store;
76
72
77
        push @messages, { type => 'message', code => 'success_on_insert' };
73
        push @messages, { type => 'message', code => 'success_on_insert' };
78
        $schema->storage->txn_commit;
79
    }
74
    }
80
    catch {
75
    catch {
81
        $schema->storage->txn_rollback;
82
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
76
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
83
            push @messages,
77
            push @messages,
84
              {
78
              {
Lines 133-144 elsif ( $op eq 'edit_save' ) { Link Here
133
        my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
127
        my $debug      = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
134
        my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
128
        my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
135
129
136
        my $schema = Koha::Database->new->schema;
137
138
        try {
130
        try {
139
            $schema->storage->txn_begin;
140
141
            Koha::SMTP::Servers->search->update({ is_default => 0 }) if $is_default;
142
131
143
            $smtp_server->password( $password )
132
            $smtp_server->password( $password )
144
                if defined $password and $password ne '****'
133
                if defined $password and $password ne '****'
Lines 162-171 elsif ( $op eq 'edit_save' ) { Link Here
162
                type => 'message',
151
                type => 'message',
163
                code => 'success_on_update'
152
                code => 'success_on_update'
164
            };
153
            };
165
            $schema->storage->txn_commit;
166
        }
154
        }
167
        catch {
155
        catch {
168
            $schema->storage->txn_rollback;
169
            push @messages,
156
            push @messages,
170
            {
157
            {
171
                type   => 'alert',
158
                type   => 'alert',
(-)a/t/db_dependent/Koha/SMTP/Server.t (-2 / +31 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 3;
20
use Test::More tests => 4;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 104-106 subtest 'to_api() tests' => sub { Link Here
104
104
105
    $schema->storage->txn_rollback;
105
    $schema->storage->txn_rollback;
106
};
106
};
107
- 
107
108
subtest 'store() tests' => sub {
109
110
    plan tests => 4;
111
112
    $schema->storage->txn_begin;
113
114
    Koha::SMTP::Servers->search->delete;
115
116
    my $server_1 = $builder->build_object(
117
        { class => 'Koha::SMTP::Servers', value => { is_default => 0 } } );
118
    my $server_2 = $builder->build_object(
119
        { class => 'Koha::SMTP::Servers', value => { is_default => 0 } } );
120
    my $server_3 = $builder->build_object(
121
        { class => 'Koha::SMTP::Servers', value => { is_default => 1 } } );
122
123
    my $default_servers = Koha::SMTP::Servers->search( { is_default => 1 } );
124
125
    is( $default_servers->count,    1,             'Only one default server' );
126
    is( $default_servers->next->id, $server_3->id, 'Server 3 is the default' );
127
128
    $server_1->set( { is_default => 1 } )->store->discard_changes;
129
130
    $default_servers = Koha::SMTP::Servers->search( { is_default => 1 } );
131
132
    is( $default_servers->count,    1,             'Only one default server' );
133
    is( $default_servers->next->id, $server_1->id, 'Server 1 is the default' );
134
135
    $schema->storage->txn_rollback;
136
};

Return to bug 27424