Add KALAMAR_PLUGINS environment variable

Change-Id: I05e39411c089bb6d6e24368314202340ebea17f6
diff --git a/Changes b/Changes
index d8418a1..31e8bab 100755
--- a/Changes
+++ b/Changes
@@ -5,6 +5,9 @@
           configuration parameter - use 'Auth' plugin instead. (diewald)
         - Support environment variable KALAMAR_CLIENT_FILE for
           client information.
+        - Support KALAMAR_PLUGINS as a comma-separated environment
+          variable to load plugins in addition to the 'plugins'
+          parameter in the configuration file. (diewald)
 
 0.47 2022-11-22
         - Add command to generate super_client_info file. (diewald)
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index d782dda..ffb5499 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -5,7 +5,7 @@
 use Mojo::File;
 use Mojo::JSON qw/decode_json encode_json/;
 use Mojo::Util qw/url_escape deprecated slugify/;
-use List::Util 'none';
+use List::Util qw!none uniq!;
 
 # Minor version - may be patched from package.json
 our $VERSION = '0.48';
@@ -288,11 +288,17 @@
     $self->plugin('MailException' => $self->config('MailException'));
   };
 
+  # Load plugins defined in environment variables
+  if ($ENV{'KALAMAR_PLUGINS'}) {
+    $conf->{'plugins'} //= [];
+    push @{$conf->{'plugins'}}, split(/\s*,\s*/, $ENV{'KALAMAR_PLUGINS'} // '');
+  };
+
   # Load further plugins,
   # that can override core functions,
   # therefore order may be of importance
   if (exists $conf->{'plugins'}) {
-    foreach (@{$conf->{'plugins'}}) {
+    foreach (uniq @{$conf->{'plugins'}}) {
       $self->plugin('Kalamar::Plugin::' . $_);
     };
   };
diff --git a/lib/Kalamar/Plugin/Piwik.pm b/lib/Kalamar/Plugin/Piwik.pm
index 70687ed..9f0db81 100644
--- a/lib/Kalamar/Plugin/Piwik.pm
+++ b/lib/Kalamar/Plugin/Piwik.pm
@@ -26,6 +26,8 @@
 
   my $url = $piwik_conf->{url};
 
+  return unless $url;
+
   $piwik_conf->{append} .= <<APPEND;
 ;window.addEventListener('korapRequest', function(e) {
     let _paq=window._paq=window._paq||[];
diff --git a/t/plugin/environment.t b/t/plugin/environment.t
new file mode 100644
index 0000000..d7be4db
--- /dev/null
+++ b/t/plugin/environment.t
@@ -0,0 +1,27 @@
+use Test::More;
+use Test::Mojo;
+use Test::Output;
+use Data::Dumper;
+
+my @loaded = grep( /Kalamar\//, keys %INC);
+
+is(scalar (@loaded), 0, 'No Kalamar libraries loaded');
+
+$ENV{KALAMAR_PLUGINS} = 'Piwik,Auth';
+
+my $t = Test::Mojo->new(Kalamar => {
+  Kalamar => {
+    plugins => ['Auth']
+  }
+});
+
+my @loaded = grep( /Kalamar[\/\\]Plugin/, keys %INC);
+
+isnt(scalar (@loaded), 0, 'Kalamar libraries loaded');
+
+is(scalar grep( /Auth/, @loaded), 1, 'Auth plugin loaded');
+is(scalar grep( /Piwik/, @loaded), 1, 'Auth plugin loaded');
+is(scalar grep( /Unknown/, @loaded), 0, 'Unknown plugin not loaded');
+
+done_testing;
+__END__