Bug 14591: Update unit tests
[koha.git] / t / db_dependent / Koha / Patrons.t
index ca4ae7b..baaa075 100644 (file)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 34;
+use Test::More tests => 33;
 use Test::Warn;
 use Test::Exception;
 use Test::MockModule;
@@ -207,31 +207,6 @@ subtest 'has_overdues' => sub {
     $issue->delete();
 };
 
-subtest 'update_password' => sub {
-    plan tests => 7;
-
-    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
-    my $original_userid   = $new_patron_1->userid;
-    my $original_password = $new_patron_1->password;
-    warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
-    qr{Duplicate entry},
-      'Koha::Patron->update_password should warn if the userid is already used by another patron';
-    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
-    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
-
-    my $digest = $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
-    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
-    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $digest,             'Koha::Patron->update_password should have updated the password' );
-
-    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
-    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
-
-    t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
-    $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
-    $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
-    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
-};
-
 subtest 'is_expired' => sub {
     plan tests => 4;
     my $patron = $builder->build({ source => 'Borrower' });
@@ -288,16 +263,16 @@ subtest 'is_going_to_expire' => sub {
 
 
 subtest 'renew_account' => sub {
-    plan tests => 36;
+    plan tests => 48;
 
-    for my $date ( '2016-03-31', '2016-11-30', dt_from_string() ) {
+    for my $date ( '2016-03-31', '2016-11-30', '2019-01-31', dt_from_string() ) {
         my $dt = dt_from_string( $date, 'iso' );
         Time::Fake->offset( $dt->epoch );
         my $a_month_ago                = $dt->clone->subtract( months => 1, end_of_month => 'limit' )->truncate( to => 'day' );
         my $a_year_later               = $dt->clone->add( months => 12, end_of_month => 'limit' )->truncate( to => 'day' );
-        my $a_year_later_minus_a_month = $dt->clone->add( months => 11, end_of_month => 'limit' )->truncate( to => 'day' );
+        my $a_year_later_minus_a_month = $a_month_ago->clone->add( months => 12, end_of_month => 'limit' )->truncate( to => 'day' );
         my $a_month_later              = $dt->clone->add( months => 1 , end_of_month => 'limit' )->truncate( to => 'day' );
-        my $a_year_later_plus_a_month  = $dt->clone->add( months => 13, end_of_month => 'limit' )->truncate( to => 'day' );
+        my $a_year_later_plus_a_month  = $a_month_later->clone->add( months => 12, end_of_month => 'limit' )->truncate( to => 'day' );
         my $patron_category = $builder->build(
             {   source => 'Category',
                 value  => {
@@ -893,8 +868,8 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
             }
         );
 
-        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->{barcode}, undef, undef, undef, '2010-10-10' );
-        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->{barcode}, undef, undef, undef, '2011-11-11' );
+        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->{barcode}, undef, undef, dt_from_string('2010-10-10') );
+        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->{barcode}, undef, undef, dt_from_string('2011-11-11') );
         is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
 
         my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
@@ -957,7 +932,7 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
             }
         );
 
-        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
+        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, dt_from_string('2010-10-10') );
         is( $returned, 1, 'The item should have been returned' );
         my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
         ok( $rows_affected > 0, 'AnonymiseIssueHistory should not return any error if success' );
@@ -998,7 +973,7 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
             }
         );
 
-        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
+        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, dt_from_string('2010-10-10') );
         is( $returned, 1, 'The item should have been returned' );
         my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
         ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
@@ -1036,7 +1011,7 @@ subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
             }
         );
 
-        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
+        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, dt_from_string('2010-10-10') );
         is( Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->count, 0 );
         Koha::Patrons->find( $patron->{borrowernumber})->delete;
     };
@@ -1424,8 +1399,8 @@ $nb_of_patrons = Koha::Patrons->search->count;
 $retrieved_patron_1->delete;
 is( Koha::Patrons->search->count, $nb_of_patrons - 1, 'Delete should have deleted the patron' );
 
-subtest 'Log cardnumber change' => sub {
-    plan tests => 3;
+subtest 'BorrowersLog tests' => sub {
+    plan tests => 4;
 
     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
@@ -1436,9 +1411,14 @@ subtest 'Log cardnumber change' => sub {
 
     my @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->borrowernumber } );
     my $log_info = from_json( $logs[0]->info );
-    is( $log_info->{cardnumber_replaced}->{new_cardnumber}, 'TESTCARDNUMBER', 'Got correct new cardnumber' );
-    is( $log_info->{cardnumber_replaced}->{previous_cardnumber}, $cardnumber, 'Got correct old cardnumber' );
-    is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' );
+    is( $log_info->{cardnumber}->{after}, 'TESTCARDNUMBER', 'Got correct new cardnumber' );
+    is( $log_info->{cardnumber}->{before}, $cardnumber, 'Got correct old cardnumber' );
+    is( scalar @logs, 1, 'With BorrowerLogs, one detailed MODIFY action should be logged for the modification.' );
+
+    t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', 1 );
+    $patron->track_login();
+    @logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'MODIFY', object => $patron->borrowernumber } );
+    is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs');
 };
 
 $schema->storage->txn_rollback;
@@ -1505,8 +1485,9 @@ subtest '->store' => sub {
         'Koha::Patron->store raises an exception on duplicate ID';
 
     # Test password
+    t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
     my $password = 'password';
-    $patron_1->update_password($patron_1->userid, $password);
+    $patron_1->set_password({ password => $password });
     like( $patron_1->password, qr|^\$2|, 'Password should be hashed using bcrypt (start with $2)' );
     my $digest = $patron_1->password;
     $patron_1->surname('xxx')->store;
@@ -1518,7 +1499,7 @@ subtest '->store' => sub {
 
 subtest '->set_password' => sub {
 
-    plan tests => 13;
+    plan tests => 14;
 
     $schema->storage->txn_begin;
 
@@ -1529,7 +1510,7 @@ subtest '->set_password' => sub {
 
     # Password-length tests
     t::lib::Mocks::mock_preference( 'minPasswordLength', undef );
-    throws_ok { $patron->set_password('ab'); }
+    throws_ok { $patron->set_password({ password => 'ab' }); }
         'Koha::Exceptions::Password::TooShort',
         'minPasswordLength is undef, fall back to 3, fail test';
     is( "$@",
@@ -1538,23 +1519,23 @@ subtest '->set_password' => sub {
     );
 
     t::lib::Mocks::mock_preference( 'minPasswordLength', 2 );
-    throws_ok { $patron->set_password('ab'); }
+    throws_ok { $patron->set_password({ password => 'ab' }); }
         'Koha::Exceptions::Password::TooShort',
         'minPasswordLength is 2, fall back to 3, fail test';
 
     t::lib::Mocks::mock_preference( 'minPasswordLength', 5 );
-    throws_ok { $patron->set_password('abcb'); }
+    throws_ok { $patron->set_password({ password => 'abcb' }); }
         'Koha::Exceptions::Password::TooShort',
         'minPasswordLength is 5, fail test';
 
     # Trailing spaces tests
-    throws_ok { $patron->set_password('abcD12d   '); }
+    throws_ok { $patron->set_password({ password => 'abcD12d   ' }); }
         'Koha::Exceptions::Password::WhitespaceCharacters',
         'Password contains trailing spaces, exception is thrown';
 
     # Require strong password tests
     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 1 );
-    throws_ok { $patron->set_password('abcd   a'); }
+    throws_ok { $patron->set_password({ password => 'abcd   a' }); }
         'Koha::Exceptions::Password::TooWeak',
         'Password is too weak, exception is thrown';
 
@@ -1562,16 +1543,19 @@ subtest '->set_password' => sub {
     $patron->discard_changes;
     is( $patron->login_attempts, 3, 'Previous tests kept login attemps count' );
 
-    $patron->set_password('abcD12 34');
+    $patron->set_password({ password => 'abcD12 34' });
     $patron->discard_changes;
 
     is( $patron->login_attempts, 0, 'Changing the password resets the login attempts count' );
 
+    lives_ok { $patron->set_password({ password => 'abcd   a', skip_validation => 1 }) }
+        'Password is weak, but skip_validation was passed, so no exception thrown';
+
     # Completeness
     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
     $patron->login_attempts(3)->store;
     my $old_digest = $patron->password;
-    $patron->set_password('abcd   a');
+    $patron->set_password({ password => 'abcd   a' });
     $patron->discard_changes;
 
     isnt( $patron->password, $old_digest, 'Password has been updated' );
@@ -1583,7 +1567,7 @@ subtest '->set_password' => sub {
 
     # Enable logging password changes
     t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
-    $patron->set_password('abcd   b');
+    $patron->set_password({ password => 'abcd   b' });
 
     $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $patron->borrowernumber } )->count;
     is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->set_password does log password changes' );