From c3f0e7db6657f49b2d122381672dd355e9a3d255 Mon Sep 17 00:00:00 2001
From: Mark Tompsett <mtompset@hotmail.com>
Date: Mon, 16 Jan 2017 14:24:48 -0500
Subject: [PATCH] Bug 17917: Fix Jenkins tests failing on C4::Member.

C4/Members.pm line 502 references:
C4::Context->userenv->{'branch'}.
As the tests are not logged in userenv is not defined, and triggers test failures.
This patches it by giving a reasonable branch value.

Test Plan
---------
1) prove t/db_dependent/Holds.t
   -- fails
2) apply patch
3) prove t/db_dependent/Holds.t
   -- passes
4) run koha qa test tools
---
 C4/Items.pm            | 30 -----------------------------
 C4/Members.pm          |  3 ++-
 t/db_dependent/Items.t | 52 +-------------------------------------------------
 3 files changed, 3 insertions(+), 82 deletions(-)

diff --git a/C4/Items.pm b/C4/Items.pm
index a1633e4..3330a1f 100644
--- a/C4/Items.pm
+++ b/C4/Items.pm
@@ -2071,7 +2071,6 @@ sub _koha_new_item {
     my $dbh=C4::Context->dbh;  
     my $error;
     $item->{permanent_location} //= $item->{location};
-    _mod_item_dates( $item );
     my $query =
            "INSERT INTO items SET
             biblionumber        = ?,
@@ -2335,7 +2334,6 @@ sub _koha_modify_item {
 
     my $query = "UPDATE items SET ";
     my @bind;
-    _mod_item_dates( $item );
     for my $key ( keys %$item ) {
         next if ( $key eq 'itemnumber' );
         $query.="$key=?,";
@@ -2353,34 +2351,6 @@ sub _koha_modify_item {
     return ($item->{'itemnumber'},$error);
 }
 
-sub _mod_item_dates { # date formatting for date fields in item hash
-    my ( $item ) = @_;
-    return if !$item || ref($item) ne 'HASH';
-
-    my @keys = grep
-        { $_ =~ /^onloan$|^date|date$|datetime$/ }
-        keys %$item;
-    # Incl. dateaccessioned,replacementpricedate,datelastborrowed,datelastseen
-    # NOTE: We do not (yet) have items fields ending with datetime
-    # Fields with _on$ have been handled already
-
-    foreach my $key ( @keys ) {
-        next if !defined $item->{$key}; # skip undefs
-        my $dt = eval { dt_from_string( $item->{$key} ) };
-            # eval: dt_from_string will die on us if we pass illegal dates
-
-        my $newstr;
-        if( defined $dt  && ref($dt) eq 'DateTime' ) {
-            if( $key =~ /datetime/ ) {
-                $newstr = DateTime::Format::MySQL->format_datetime($dt);
-            } else {
-                $newstr = DateTime::Format::MySQL->format_date($dt);
-            }
-        }
-        $item->{$key} = $newstr; # might be undef to clear garbage
-    }
-}
-
 =head2 _koha_delete_item
 
   _koha_delete_item( $itemnum );
diff --git a/C4/Members.pm b/C4/Members.pm
index e7609e5..f75c121 100644
--- a/C4/Members.pm
+++ b/C4/Members.pm
@@ -499,7 +499,8 @@ sub AddMember {
 
     if ( C4::Context->preference("autoMemberNum") ) {
         if ( not exists $data{cardnumber} or not defined $data{cardnumber} or $data{cardnumber} eq '' ) {
-            $data{cardnumber} = fixup_cardnumber( $data{cardnumber}, C4::Context->userenv->{'branch'}, $data{categorycode} );
+            my $user_branch = C4::Context->userenv ? C4::Context->userenv->{'branch'} : q{};
+            $data{cardnumber} = fixup_cardnumber( $data{cardnumber}, $user_branch, $data{categorycode} );
         }
     }
 
diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t
index f85d48a..9e25704 100755
--- a/t/db_dependent/Items.t
+++ b/t/db_dependent/Items.t
@@ -26,7 +26,7 @@ use Koha::Library;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
-use Test::More tests => 11;
+use Test::More tests => 10;
 
 use Test::Warn;
 
@@ -732,56 +732,6 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest '_mod_item_dates' => sub {
-    plan tests => 11;
-
-    is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
-    is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
-
-    my $orgitem;
-    my $item = {
-        itemcallnumber  => 'V II 149 1963',
-        barcode         => '109304',
-    };
-    $orgitem = { %$item };
-    C4::Items::_mod_item_dates($item);
-    is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
-
-    # add two correct dates
-    t::lib::Mocks::mock_preference('dateformat', 'us');
-    $item->{dateaccessioned} = '01/31/2016';
-    $item->{onloan} =  $item->{dateaccessioned};
-    $orgitem = { %$item };
-    C4::Items::_mod_item_dates($item);
-    is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
-    is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
-
-
-    # add some invalid dates
-    $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
-    $item->{anotherdate} = 'tralala'; # even worse
-    $item->{myzerodate} = '0000-00-00'; # wrong too
-    C4::Items::_mod_item_dates($item);
-    is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
-    is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
-    is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
-
-    # check if itemlost_on was not touched
-    $item->{itemlost_on} = '12345678';
-    $item->{withdrawn_on} = '12/31/2015 23:59:00';
-    $orgitem = { %$item };
-    C4::Items::_mod_item_dates($item);
-    is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
-
-    t::lib::Mocks::mock_preference('dateformat', 'metric');
-    $item->{dateaccessioned} = '01/31/2016'; #wrong
-    $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
-    C4::Items::_mod_item_dates($item);
-    is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
-    is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
-        'yetanotherdatetime is ok' );
-};
-
 # Helper method to set up a Biblio.
 sub get_biblio {
     my ( $frameworkcode ) = @_;
-- 
2.1.4