From 5e57ca9f7a5020473d8ff4aba602bc2a4271deb0 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Fri, 30 Jun 2017 10:37:14 -0400
Subject: [PATCH] Bug 18882 [QA Followup] - Ensure stats with no or undef
 location are set to NULL in db

Confirm that only NULL or a string are in the location field of the
statistics table. Test with two items, one with shelving location set,
and one without it set.

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
---
 C4/Stats.pm            | 16 ++++++++--------
 t/db_dependent/Stats.t | 41 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/C4/Stats.pm b/C4/Stats.pm
index 23e5807..2ad685c 100644
--- a/C4/Stats.pm
+++ b/C4/Stats.pm
@@ -118,14 +118,14 @@ sub UpdateStats {
 # get the parameters
     my $branch            = $params->{branch};
     my $type              = $params->{type};
-    my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} :'';
-    my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber} :'';
-    my $amount            = exists $params->{amount}         ? $params->{amount} :'';
-    my $other             = exists $params->{other}          ? $params->{other} :'';
-    my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype} :'';
-    my $location          = exists $params->{location}       ? $params->{location} :'';
-    my $accountno         = exists $params->{accountno}      ? $params->{accountno} :'';
-    my $ccode             = exists $params->{ccode}          ? $params->{ccode} :'';
+    my $borrowernumber    = exists $params->{borrowernumber} ? $params->{borrowernumber} : '';
+    my $itemnumber        = exists $params->{itemnumber}     ? $params->{itemnumber}     : '';
+    my $amount            = exists $params->{amount}         ? $params->{amount}         : '';
+    my $other             = exists $params->{other}          ? $params->{other}          : '';
+    my $itemtype          = exists $params->{itemtype}       ? $params->{itemtype}       : '';
+    my $location          = exists $params->{location}       ? $params->{location}       : undef;
+    my $accountno         = exists $params->{accountno}      ? $params->{accountno}      : '';
+    my $ccode             = exists $params->{ccode}          ? $params->{ccode}          : '';
 
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare(
diff --git a/t/db_dependent/Stats.t b/t/db_dependent/Stats.t
index 21a3226..7675a16 100644
--- a/t/db_dependent/Stats.t
+++ b/t/db_dependent/Stats.t
@@ -3,7 +3,7 @@
 use Modern::Perl;
 use C4::Stats;
 
-use Test::More tests => 18;
+use Test::More tests => 20;
 
 BEGIN {
     use_ok('C4::Stats');
@@ -125,6 +125,45 @@ is ($params->{location},       $line->{location},       "UpdateStats save locati
 is ($params->{accountno},      $line->{proccode},       "UpdateStats save accountno param in proccode field of statistics table");
 is ($params->{ccode},          $line->{ccode},          "UpdateStats save ccode param in ccode field of statistics table");
 
+$dbh->do(q|DELETE FROM statistics|);
+$params = {
+    branch         => "BRA",
+    itemnumber     => 31,
+    borrowernumber => 5,
+    amount         => 5.1,
+    other          => "bla",
+    itemtype       => "BK",
+    accountno      => 51,
+    ccode          => "CODE",
+    type           => "return"
+};
+UpdateStats($params);
+$sth = $dbh->prepare("SELECT * FROM statistics");
+$sth->execute();
+$line = ${ $sth->fetchall_arrayref( {} ) }[0];
+is( $line->{location}, undef,
+    "UpdateStats sets location to NULL if no location is passed in." );
+
+$dbh->do(q|DELETE FROM statistics|);
+$params = {
+    branch         => "BRA",
+    itemnumber     => 31,
+    borrowernumber => 5,
+    amount         => 5.1,
+    other          => "bla",
+    itemtype       => "BK",
+    location       => undef,
+    accountno      => 51,
+    ccode          => "CODE",
+    type           => "return"
+};
+UpdateStats($params);
+$sth = $dbh->prepare("SELECT * FROM statistics");
+$sth->execute();
+$line = ${ $sth->fetchall_arrayref( {} ) }[0];
+is( $line->{location}, undef,
+    "UpdateStats sets location to NULL if undef is passed in." );
+
 #
 # Test TotalPaid
 #
-- 
2.1.4