+$borrower = Koha::Patrons->find( $new_borrowernumber )->unblessed;
+ok( $borrower->{userid} ne 'tomasito', "Borrower with duplicate userid has new userid generated" );
+
+$data{ cardnumber } = "234567890";
+$data{userid} = 'a_user_id';
+$borrowernumber = AddMember( %data );
+$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
+
+subtest 'ModMember should not update userid if not true' => sub {
+ plan tests => 3;
+ ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => '' );
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an empty string' );
+ ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => 0 );
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an 0');
+ ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => undef );
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an undefined value');
+};
+
+#Regression tests for bug 10612
+my $library3 = $builder->build({
+ source => 'Branch',
+});
+$builder->build({
+ source => 'Category',
+ value => {
+ categorycode => 'STAFFER',
+ description => 'Staff dont batch del',
+ category_type => 'S',
+ },
+});
+
+$builder->build({
+ source => 'Category',
+ value => {
+ categorycode => 'CIVILIAN',
+ description => 'Civilian batch del',
+ category_type => 'A',
+ },
+});
+
+$builder->build({
+ source => 'Category',
+ value => {
+ categorycode => 'KIDclamp',
+ description => 'Kid to be guaranteed',
+ category_type => 'C',
+ },
+});
+
+my $borrower1 = $builder->build({
+ source => 'Borrower',
+ value => {
+ categorycode=>'STAFFER',
+ branchcode => $library3->{branchcode},
+ dateexpiry => '2015-01-01',
+ guarantorid=> undef,
+ },
+});
+my $bor1inlist = $borrower1->{borrowernumber};
+my $borrower2 = $builder->build({
+ source => 'Borrower',
+ value => {
+ categorycode=>'STAFFER',
+ branchcode => $library3->{branchcode},
+ dateexpiry => '2015-01-01',
+ guarantorid=> undef,
+ },
+});
+
+my $guarantee = $builder->build({
+ source => 'Borrower',
+ value => {
+ categorycode=>'KIDclamp',
+ branchcode => $library3->{branchcode},
+ dateexpiry => '2015-01-01',
+ guarantorid=> undef, # will be filled later
+ },
+});
+
+my $bor2inlist = $borrower2->{borrowernumber};
+
+$builder->build({
+ source => 'OldIssue',
+ value => {
+ borrowernumber => $bor2inlist,
+ timestamp => '2016-01-01',
+ },
+});
+
+# The following calls to GetBorrowersToExpunge are assuming that the pref
+# IndependentBranches is off.
+t::lib::Mocks::mock_preference('IndependentBranches', 0);
+
+my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
+my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
+my @listpatrons = ($bor1inlist, $bor2inlist);
+AddPatronsToList( { list => $list1, borrowernumbers => \@listpatrons });
+my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
+ModMember( borrowernumber => $bor2inlist, categorycode => 'CIVILIAN' );
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
+$patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
+$patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
+
+ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
+ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
+
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
+$patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
+$patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
+ModMember( borrowernumber => $guarantee->{borrowernumber}, guarantorid=>'' );
+
+$builder->build({
+ source => 'Issue',
+ value => {
+ borrowernumber => $bor2inlist,
+ },
+});
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
+$patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
+$patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
+$builder->schema->resultset( 'Issue' )->delete_all;
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
+$patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
+$patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
+
+# Test GetBorrowersToExpunge and TrackLastPatronActivity
+$dbh->do(q|UPDATE borrowers SET lastseen=NULL|);
+$builder->build({ source => 'Borrower', value => { lastseen => '2016-01-01 01:01:01', categorycode => 'CIVILIAN', guarantorid => undef } } );
+$builder->build({ source => 'Borrower', value => { lastseen => '2016-02-02 02:02:02', categorycode => 'CIVILIAN', guarantorid => undef } } );
+$builder->build({ source => 'Borrower', value => { lastseen => '2016-03-03 03:03:03', categorycode => 'CIVILIAN', guarantorid => undef } } );
+$patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
+is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
+$patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
+is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
+$patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' });
+is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
+my $patron2 = $builder->build({ source => 'Borrower', value => { lastseen => undef } });
+t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
+Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
+is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
+Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
+isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
+
+# Regression tests for BZ13502
+## Remove all entries with userid='' (should be only 1 max)
+$dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
+## And create a patron with a userid=''
+$borrowernumber = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
+$dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
+# Create another patron and verify the userid has been generated
+$borrowernumber = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
+ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
+$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ok( $borrower->{userid}, 'A userid should have been generated correctly' );