View | Details | Raw Unified | Return to bug 40608
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Patron.t (-7 / +178 lines)
Lines 25-38 use Test::Exception; Link Here
25
use Test::Warn;
25
use Test::Warn;
26
use Time::Fake;
26
use Time::Fake;
27
27
28
use Koha::ArticleRequests;
29
use Koha::AuthUtils;
28
use Koha::CirculationRules;
30
use Koha::CirculationRules;
29
use Koha::Database;
31
use Koha::Database;
30
use Koha::DateUtils qw(dt_from_string);
32
use Koha::DateUtils    qw(dt_from_string);
31
use Koha::ArticleRequests;
33
use Koha::List::Patron qw(AddPatronList AddPatronsToList);
32
use Koha::Patrons;
34
use Koha::Notice::Templates;
33
use Koha::List::Patron       qw(AddPatronList AddPatronsToList);
34
use Koha::Patron::Debarments qw(AddDebarment);
35
use Koha::Patron::Debarments qw(AddDebarment);
35
use Koha::Patron::Relationships;
36
use Koha::Patron::Relationships;
37
use Koha::Patrons;
38
39
use C4::Auth;
36
use C4::Circulation qw( AddIssue AddReturn );
40
use C4::Circulation qw( AddIssue AddReturn );
37
41
38
use t::lib::TestBuilder;
42
use t::lib::TestBuilder;
Lines 1533-1541 subtest 'password expiration tests' => sub { Link Here
1533
        $schema->storage->txn_rollback;
1537
        $schema->storage->txn_rollback;
1534
    };
1538
    };
1535
1539
1536
    subtest 'set_password' => sub {
1540
    subtest 'set_password() tests' => sub {
1537
1541
1538
        plan tests => 4;
1542
        plan tests => 7;
1539
1543
1540
        $schema->storage->txn_begin;
1544
        $schema->storage->txn_begin;
1541
1545
Lines 1568-1573 subtest 'password expiration tests' => sub { Link Here
1568
            "Password expiration date is unset if category does not have expiry days"
1572
            "Password expiration date is unset if category does not have expiry days"
1569
        );
1573
        );
1570
1574
1575
        # Test password change notification - missing letter template should not prevent password change
1576
        subtest 'Password change works even when PASSWORD_CHANGE letter is missing' => sub {
1577
1578
            plan tests => 4;
1579
1580
            # Enable NotifyPasswordChange preference
1581
            t::lib::Mocks::mock_preference( 'NotifyPasswordChange', 1 );
1582
1583
            # Create a patron with a valid email address
1584
            my $patron = $builder->build_object(
1585
                {
1586
                    class => 'Koha::Patrons',
1587
                    value => {
1588
                        email    => 'patron@example.com',
1589
                        password => Koha::AuthUtils::hash_password('OldPassword123!')
1590
                    }
1591
                }
1592
            );
1593
1594
            # Store the original password hash for comparison
1595
            my $original_password_hash = $patron->password;
1596
1597
            # Ensure there is NO PASSWORD_CHANGE letter template
1598
            # (This was the bug condition - should not prevent password change)
1599
            Koha::Notice::Templates->search(
1600
                {
1601
                    code   => 'PASSWORD_CHANGE',
1602
                    module => 'members'
1603
                }
1604
            )->delete;
1605
1606
            # Attempt to set a new password
1607
            my $result;
1608
1609
            warning_like { $result = $patron->set_password( { password => 'NewPassword123!', skip_validation => 1 } ); }
1610
            qr/No members PASSWORD_CHANGE letter transported by email/,
1611
                'No letter warning correctly printed';
1612
1613
            # FIXED: The method should return the patron object even when letter is missing
1614
            isa_ok(
1615
                $result, 'Koha::Patron',
1616
                'set_password returns patron object even when PASSWORD_CHANGE letter is missing'
1617
            );
1618
1619
            # Reload patron from database to check if password was actually changed
1620
            $patron->discard_changes;
1621
1622
            # FIXED: Password should be changed even when letter template is missing
1623
            isnt(
1624
                $patron->password, $original_password_hash,
1625
                'Password was changed even though letter template is missing'
1626
            );
1627
1628
            # Verify the new password works
1629
            my $new_auth_result = C4::Auth::checkpw_hash( 'NewPassword123!', $patron->password );
1630
            is( $new_auth_result, 1, 'New password authenticates correctly' );
1631
        };
1632
1633
        subtest 'Password change works when NotifyPasswordChange is disabled' => sub {
1634
1635
            plan tests => 3;
1636
1637
            # Disable NotifyPasswordChange preference
1638
            t::lib::Mocks::mock_preference( 'NotifyPasswordChange', 0 );
1639
1640
            # Create a patron with a valid email address
1641
            my $patron = $builder->build_object(
1642
                {
1643
                    class => 'Koha::Patrons',
1644
                    value => {
1645
                        email    => 'patron@example.com',
1646
                        password => Koha::AuthUtils::hash_password('OldPassword123!')
1647
                    }
1648
                }
1649
            );
1650
1651
            # Store the original password hash for comparison
1652
            my $original_password_hash = $patron->password;
1653
1654
            # Ensure there is NO PASSWORD_CHANGE letter template
1655
            Koha::Notice::Templates->search(
1656
                {
1657
                    code   => 'PASSWORD_CHANGE',
1658
                    module => 'members'
1659
                }
1660
            )->delete;
1661
1662
            # Attempt to set a new password
1663
            my $result = $patron->set_password( { password => 'NewPassword123!', skip_validation => 1 } );
1664
1665
            # Should work fine when NotifyPasswordChange is disabled
1666
            isa_ok( $result, 'Koha::Patron', 'set_password returns patron object when notification disabled' );
1667
1668
            # Reload patron from database to check if password was actually changed
1669
            $patron->discard_changes;
1670
1671
            # Password should be changed
1672
            isnt( $patron->password, $original_password_hash, 'Password was changed when notification disabled' );
1673
1674
            # Verify the new password works
1675
            my $auth_result = C4::Auth::checkpw_hash( 'NewPassword123!', $patron->password );
1676
            is( $auth_result, 1, 'New password authenticates correctly' );
1677
        };
1678
1679
        subtest 'Password change works when PASSWORD_CHANGE letter exists' => sub {
1680
1681
            plan tests => 3;
1682
1683
            # Enable NotifyPasswordChange preference
1684
            t::lib::Mocks::mock_preference( 'NotifyPasswordChange', 1 );
1685
1686
            # Create a patron with a valid email address
1687
            my $patron = $builder->build_object(
1688
                {
1689
                    class => 'Koha::Patrons',
1690
                    value => {
1691
                        email    => 'patron@example.com',
1692
                        password => Koha::AuthUtils::hash_password('OldPassword123!')
1693
                    }
1694
                }
1695
            );
1696
1697
            # Store the original password hash for comparison
1698
            my $original_password_hash = $patron->password;
1699
1700
            # Delete any existing PASSWORD_CHANGE letters to avoid constraint violations
1701
            Koha::Notice::Templates->search(
1702
                {
1703
                    code   => 'PASSWORD_CHANGE',
1704
                    module => 'members'
1705
                }
1706
            )->delete;
1707
1708
            # Create a PASSWORD_CHANGE letter template
1709
            $builder->build_object(
1710
                {
1711
                    class => 'Koha::Notice::Templates',
1712
                    value => {
1713
                        module                 => 'members',
1714
                        code                   => 'PASSWORD_CHANGE',
1715
                        branchcode             => '',
1716
                        name                   => 'Password Change Notification',
1717
                        is_html                => 0,
1718
                        title                  => 'Your password has been changed',
1719
                        content                => 'Dear [% borrower.firstname %], your password has been changed.',
1720
                        message_transport_type => 'email',
1721
                        lang                   => 'default'
1722
                    }
1723
                }
1724
            );
1725
1726
            # Attempt to set a new password
1727
            my $result = $patron->set_password( { password => 'NewPassword123!', skip_validation => 1 } );
1728
1729
            # Should work fine when PASSWORD_CHANGE letter exists
1730
            isa_ok( $result, 'Koha::Patron', 'set_password returns patron object when letter template exists' );
1731
1732
            # Reload patron from database to check if password was actually changed
1733
            $patron->discard_changes;
1734
1735
            # Password should be changed
1736
            isnt( $patron->password, $original_password_hash, 'Password was changed when letter template exists' );
1737
1738
            # Verify the new password works
1739
            my $auth_result = C4::Auth::checkpw_hash( 'NewPassword123!', $patron->password );
1740
            is( $auth_result, 1, 'New password authenticates correctly' );
1741
        };
1742
1571
        $schema->storage->txn_rollback;
1743
        $schema->storage->txn_rollback;
1572
    };
1744
    };
1573
1745
1574
- 

Return to bug 40608