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

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

Return to bug 40608