use Modern::Perl;
-use Test::More tests => 61;
+use Test::More tests => 47;
use Test::MockModule;
use Test::Exception;
userid => 'tomasito'
);
-my $addmem=AddMember(%data);
-ok($addmem, "AddMember()");
+my $addmem=Koha::Patron->new(\%data)->store->borrowernumber;
+ok($addmem, "Koha::Patron->store()");
my $member = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
or BAIL_OUT("Cannot read member with card $CARDNUMBER");
is ($checkcardnum, "2", "Card number is too long");
-# Check_Userid tests
+# Add a new borrower
%data = (
cardnumber => "123456789",
firstname => "Tomasito",
dateexpiry => '',
dateenrolled => '',
);
-# Add a new borrower
-my $borrowernumber = AddMember( %data );
-is( Check_Userid( 'tomasito.non', $borrowernumber ), 1,
- 'recently created userid -> unique (borrowernumber passed)' );
-is( Check_Userid( 'tomasitoxxx', $borrowernumber ), 1,
- 'non-existent userid -> unique (borrowernumber passed)' );
-is( Check_Userid( 'tomasito.none', '' ), 0,
- 'userid exists (blank borrowernumber)' );
-is( Check_Userid( 'tomasitoxxx', '' ), 1,
- 'non-existent userid -> unique (blank borrowernumber)' );
-
+my $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
-is( $borrower->{dateofbirth}, undef, 'AddMember should undef dateofbirth if empty string is given');
-is( $borrower->{debarred}, undef, 'AddMember should undef debarred if empty string is given');
-isnt( $borrower->{dateexpiry}, '0000-00-00', 'AddMember should not set dateexpiry to 0000-00-00 if empty string is given');
-isnt( $borrower->{dateenrolled}, '0000-00-00', 'AddMember should not set dateenrolled to 0000-00-00 if empty string is given');
+is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
+is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
+isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
+isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
ModMember( borrowernumber => $borrowernumber, dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' );
$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
is( $borrower->{dateexpiry}, '9999-12-31', 'ModMember should correctly set dateexpiry if a valid date is given');
is( $borrower->{dateenrolled}, '2015-09-06', 'ModMember should correctly set dateenrolled if a valid date is given');
-# Add a new borrower with the same userid but different cardnumber
-$data{ cardnumber } = "987654321";
-my $new_borrowernumber = AddMember( %data );
-is( Check_Userid( 'tomasito.none', '' ), 0,
- 'userid not unique (blank borrowernumber)' );
-is( Check_Userid( 'tomasito.none', $new_borrowernumber ), 0,
- 'userid not unique (second borrowernumber passed)' );
-$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;
+
+ $data{ cardnumber } = "234567890";
+ $data{userid} = 'a_user_id';
+ $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+
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' );
# IndependentBranches is off.
t::lib::Mocks::mock_preference('IndependentBranches', 0);
-my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
+my $owner = Koha::Patron->new({ categorycode => 'STAFFER', branchcode => $library2->{branchcode} })->store->borrowernumber;
my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
my @listpatrons = ($bor1inlist, $bor2inlist);
AddPatronsToList( { list => $list1, borrowernumbers => \@listpatrons });
## 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} );
+$borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
$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' );
+$borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
+ok( $borrowernumber > 0, 'Koha::Patron->store 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' );
-# Regression tests for BZ12226
-is( Check_Userid( C4::Context->config('user'), '' ), 0,
- 'Check_Userid should return 0 for the DB user (Bug 12226)');
-
-subtest 'GetMemberAccountBalance' => sub {
-
- plan tests => 6;
-
- my $members_mock = new Test::MockModule('C4::Members');
- $members_mock->mock( 'GetMemberAccountRecords', sub {
- my ($borrowernumber) = @_;
- if ($borrowernumber) {
- my @accountlines = (
- { amountoutstanding => '7', accounttype => 'Rent' },
- { amountoutstanding => '5', accounttype => 'Res' },
- { amountoutstanding => '3', accounttype => 'Pay' } );
- return ( 15, \@accountlines );
- }
- else {
- my @accountlines;
- return ( 0, \@accountlines );
- }
- });
-
- # do not count holds charges
- t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' );
- t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' );
- my ($total, $total_minus_charges,
- $other_charges) = C4::Members::GetMemberAccountBalance(123);
- is( $total, 15 , "Total calculated correctly");
- is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
- is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
-
- t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' );
- ($total, $total_minus_charges,
- $other_charges) = C4::Members::GetMemberAccountBalance(123);
- is( $total, 15 , "Total calculated correctly");
- is( $total_minus_charges, 10, "Holds charges are count if HoldsInNoissuesCharge=0");
- is( $other_charges, 5, "Holds charges are considered if HoldsInNoissuesCharge=1");
-};
-
subtest 'purgeSelfRegistration' => sub {
plan tests => 2;
return $found;
}
-# Regression tests for BZ15343
-my $password="";
-( $borrowernumber, $password ) = AddMember_Opac(surname=>"Dick",firstname=>'Philip',branchcode => $library2->{branchcode});
-is( $password =~ /^[a-zA-Z]{10}$/ , 1, 'Test for autogenerated password if none submitted');
-( $borrowernumber, $password ) = AddMember_Opac(surname=>"Deckard",firstname=>"Rick",password=>"Nexus-6",branchcode => $library2->{branchcode});
-is( $password eq "Nexus-6", 1, 'Test password used if submitted');
-$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
-my $hashed_up = Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
-is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
-
-subtest 'Trivial test for AddMember_Auto' => sub {
- plan tests => 3;
- my $members_mock = Test::MockModule->new( 'C4::Members' );
- $members_mock->mock( 'fixup_cardnumber', sub { 12345; } );
- my $library = $builder->build({ source => 'Branch' });
- my $category = $builder->build({ source => 'Category' });
- my %borr = AddMember_Auto( surname=> 'Dick3', firstname => 'Philip', branchcode => $library->{branchcode}, categorycode => $category->{categorycode}, password => '34567890' );
- ok( $borr{borrowernumber}, 'Borrower hash contains borrowernumber' );
- is( $borr{cardnumber}, 12345, 'Borrower hash contains cardnumber' );
- my $patron = Koha::Patrons->find( $borr{borrowernumber} );
- isnt( $patron, undef, 'Patron found' );
-};
-
$schema->storage->txn_rollback;
-subtest 'AddMember (invalid categorycode) tests' => sub {
+subtest 'Koha::Patron->store (invalid categorycode) tests' => sub {
plan tests => 1;
$schema->storage->txn_begin;
};
throws_ok
- { AddMember( %{ $patron_data } ); }
- 'Koha::Exceptions::BadParameter',
+ { Koha::Patron->new( $patron_data )->store; }
+ 'Koha::Exceptions::Object::FKConstraint',
'AddMember raises an exception on invalid categorycode';
$schema->storage->txn_rollback;