Bug 26595: Add Koha::Library->smtp_server_info
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 1 Oct 2020 15:06:13 +0000 (12:06 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 14 Oct 2020 13:53:38 +0000 (15:53 +0200)
This patch adds a method for retrieving brief information about the
linked (or not) SMTP server for embedding on API requests.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Library.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Library.pm
t/db_dependent/Koha/Library.t

index 165d974..90da8e2 100644 (file)
@@ -115,6 +115,23 @@ sub smtp_server {
     return $self;
 }
 
+=head3 smtp_server_info
+
+Returns the SMTP server info for the library, or 'system_default' if it is the system default.
+
+=cut
+
+sub smtp_server_info {
+    my ($self) = @_;
+
+    my $smtp_server = $self->smtp_server;
+
+    return { name => 'system_default' }
+        if $smtp_server->is_system_default;
+
+    return { name => $smtp_server->name, smtp_server_id => $smtp_server->id };
+}
+
 =head3 inbound_email_address
 
   my $to_email = Koha::Library->inbound_email_address;
index 23e8857..8c1d12a 100755 (executable)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 1;
+use Test::More tests => 2;
 
 use Koha::Database;
 use Koha::SMTP::Servers;
@@ -93,3 +93,22 @@ subtest 'smtp_server() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'smtp_server_info() tests' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $library     = $builder->build_object({ class => 'Koha::Libraries' });
+    my $smtp_server = $builder->build_object({ class => 'Koha::SMTP::Servers' });
+
+    # No SMTP server assigned to library, return system default
+    is_deeply( $library->smtp_server_info, { name => 'system_default' }, 'System default is returned' );
+
+    # Assign an SMTP server
+    $library->smtp_server({ smtp_server => $smtp_server });
+    is_deeply( $library->smtp_server_info, { name => $smtp_server->name, smtp_server_id => $smtp_server->id }, 'The right information is returned when SMTP server is assigned' );
+
+    $schema->storage->txn_rollback;
+};