|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 41; |
22 |
use Test::More tests => 42; |
| 23 |
use Test::Warn; |
23 |
use Test::Warn; |
| 24 |
use Test::Exception; |
24 |
use Test::Exception; |
| 25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
|
Lines 1602-1607
subtest 'BorrowersLog tests' => sub {
Link Here
|
| 1602 |
is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs'); |
1602 |
is( scalar @logs, 1, 'With BorrowerLogs and TrackLastPatronActivity we should not spam the logs'); |
| 1603 |
}; |
1603 |
}; |
| 1604 |
|
1604 |
|
|
|
1605 |
subtest 'complex filters' => sub { |
| 1606 |
plan tests => 7; |
| 1607 |
|
| 1608 |
my $now = DateTime->now; |
| 1609 |
my $new_patron_cf_1 = Koha::Patron->new( |
| 1610 |
{ |
| 1611 |
cardnumber => 'test_cn_cf_1', |
| 1612 |
branchcode => $library->{branchcode}, |
| 1613 |
categorycode => $category->{categorycode}, |
| 1614 |
surname => 'surname for patron1', |
| 1615 |
firstname => 'firstname for patron1', |
| 1616 |
userid => 'a_nonexistent_userid_cf_1', |
| 1617 |
dateexpiry => $now->clone->subtract( days => 14 ) |
| 1618 |
} |
| 1619 |
)->store; |
| 1620 |
my $new_patron_cf_2 = Koha::Patron->new( |
| 1621 |
{ |
| 1622 |
cardnumber => 'test_cn_cf_2', |
| 1623 |
branchcode => $library->{branchcode}, |
| 1624 |
categorycode => $category->{categorycode}, |
| 1625 |
surname => 'surname for patron2', |
| 1626 |
firstname => 'firstname for patron2', |
| 1627 |
userid => 'a_nonexistent_userid_cf_2', |
| 1628 |
dateexpiry => $now->clone->subtract( days => 7 ) |
| 1629 |
} |
| 1630 |
)->store; |
| 1631 |
my $new_patron_cf_3 = Koha::Patron->new( |
| 1632 |
{ |
| 1633 |
cardnumber => 'test_cn_cf_3', |
| 1634 |
branchcode => $library->{branchcode}, |
| 1635 |
categorycode => $category->{categorycode}, |
| 1636 |
surname => 'surname for patron3', |
| 1637 |
firstname => 'firstname for patron3', |
| 1638 |
userid => 'a_nonexistent_userid_cf_3', |
| 1639 |
} |
| 1640 |
)->store; |
| 1641 |
|
| 1642 |
my $results = Koha::Patrons->search( |
| 1643 |
{ |
| 1644 |
'me.borrowernumber' => [ |
| 1645 |
$new_patron_cf_1->borrowernumber, |
| 1646 |
$new_patron_cf_2->borrowernumber, |
| 1647 |
$new_patron_cf_3->borrowernumber |
| 1648 |
] |
| 1649 |
} |
| 1650 |
); |
| 1651 |
|
| 1652 |
# Set new_patron_cf_1 to be the guarantor for new_patron_cf_2 |
| 1653 |
$new_patron_cf_2->add_guarantor( |
| 1654 |
{ |
| 1655 |
guarantor_id => $new_patron_cf_1->borrowernumber, |
| 1656 |
relationship => 'test' |
| 1657 |
} |
| 1658 |
); |
| 1659 |
|
| 1660 |
subtest 'filter_by_is_guarantor' => sub { |
| 1661 |
plan tests => 6; |
| 1662 |
|
| 1663 |
my $filtered = $results->filter_by_is_guarantor(); |
| 1664 |
is( ref($filtered), 'Koha::Patrons', |
| 1665 |
'Koha::Patrons->filter_by_is_guarantor should return a Koha::Patrons result set in a scalar context' |
| 1666 |
); |
| 1667 |
is( $filtered->count, 3, |
| 1668 |
'filter_by_is_guarantor() found both patrons' ); |
| 1669 |
|
| 1670 |
$filtered = $results->filter_by_is_guarantor(1); |
| 1671 |
is( $filtered->count, 1, 'filter_by_is_guarantor(1) found one patron' ); |
| 1672 |
is( $filtered->next->guarantee_relationships->count, |
| 1673 |
1, 'filter_by_is_guarantor(1) found the correct patron' ); |
| 1674 |
|
| 1675 |
$filtered = $results->filter_by_is_guarantor(0); |
| 1676 |
is( $filtered->count, 2, |
| 1677 |
'filter_by_is_guarantor(0) found two patrons' ); |
| 1678 |
is( $filtered->next->guarantee_relationships->count, |
| 1679 |
0, 'filter_by_is_guarantor(0) found the correct patrons' ); |
| 1680 |
}; |
| 1681 |
|
| 1682 |
subtest 'filter_by_has_guarantor' => sub { |
| 1683 |
plan tests => 6; |
| 1684 |
|
| 1685 |
my $filtered = $results->filter_by_has_guarantor(); |
| 1686 |
is( ref($filtered), 'Koha::Patrons', |
| 1687 |
'filter_by_has_guarantor() should return a Koha::Patrons result set in a scalar context' |
| 1688 |
); |
| 1689 |
is( $filtered->count, 3, |
| 1690 |
'filter_by_has_guarantor() found both patrons' ); |
| 1691 |
|
| 1692 |
$filtered = $results->filter_by_has_guarantor(1); |
| 1693 |
is( $filtered->count, 1, |
| 1694 |
'filter_by_has_guarantor(1) found one patron' ); |
| 1695 |
is( |
| 1696 |
$filtered->next->borrowernumber, |
| 1697 |
$new_patron_cf_2->borrowernumber, |
| 1698 |
'filter_by_has_guarantor(1) found the correct patron' |
| 1699 |
); |
| 1700 |
|
| 1701 |
$filtered = $results->filter_by_has_guarantor(0); |
| 1702 |
is( $filtered->count, 2, |
| 1703 |
'filter_by_has_guarantor(0) found two patrons' ); |
| 1704 |
is_deeply( |
| 1705 |
[ |
| 1706 |
sort( $filtered->next->borrowernumber, |
| 1707 |
$filtered->next->borrowernumber ) |
| 1708 |
], |
| 1709 |
[ |
| 1710 |
sort ( $new_patron_cf_1->borrowernumber, |
| 1711 |
$new_patron_cf_3->borrowernumber ) |
| 1712 |
], |
| 1713 |
'filter_by_has_guarantor(0) found the correct patrons' |
| 1714 |
); |
| 1715 |
}; |
| 1716 |
|
| 1717 |
subtest 'filter_by_last_issued' => sub { |
| 1718 |
plan tests => 4; |
| 1719 |
|
| 1720 |
my $first = DateTime->now()->subtract( days => 5 ); |
| 1721 |
my $last = $first->clone->add( days => 2 ); |
| 1722 |
my $old_issue_1 = $builder->build( |
| 1723 |
{ |
| 1724 |
source => 'OldIssue', |
| 1725 |
value => { |
| 1726 |
borrowernumber => $new_patron_cf_1->borrowernumber, |
| 1727 |
timestamp => $first->clone->subtract( days => 1 ) |
| 1728 |
}, |
| 1729 |
} |
| 1730 |
); |
| 1731 |
my $old_issue_2 = $builder->build( |
| 1732 |
{ |
| 1733 |
source => 'OldIssue', |
| 1734 |
value => { |
| 1735 |
borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1736 |
timestamp => $last->clone->add( days => 1 ) |
| 1737 |
}, |
| 1738 |
} |
| 1739 |
); |
| 1740 |
my $old_issue_3 = $builder->build( |
| 1741 |
{ |
| 1742 |
source => 'OldIssue', |
| 1743 |
value => { |
| 1744 |
borrowernumber => $new_patron_cf_3->borrowernumber, |
| 1745 |
timestamp => $first->clone->add( days => 1 ) |
| 1746 |
}, |
| 1747 |
} |
| 1748 |
); |
| 1749 |
|
| 1750 |
my $filtered = $results->filter_by_last_issued(); |
| 1751 |
is( ref($filtered), 'Koha::Patrons', |
| 1752 |
'filter_by_last_issued() should return a Koha::Patrons result set in a scalar context' |
| 1753 |
); |
| 1754 |
|
| 1755 |
$filtered = $results->filter_by_last_issued( { before => $last } ); |
| 1756 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1757 |
2, "filter_by_last_issued({ before => $last }) found two patrons" ); |
| 1758 |
|
| 1759 |
$filtered = $results->filter_by_last_issued( { after => $first } ); |
| 1760 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1761 |
2, "filter_by_last_issued({ after => $first }) found two patrons" ); |
| 1762 |
|
| 1763 |
$filtered = $results->filter_by_last_issued( |
| 1764 |
{ after => $first, before => $last } ); |
| 1765 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1766 |
"filter_by_last_issued({ after => $first, before => $last }) found two patrons" |
| 1767 |
); |
| 1768 |
}; |
| 1769 |
|
| 1770 |
subtest 'filter_by_has_issues' => sub { |
| 1771 |
plan tests => 3; |
| 1772 |
|
| 1773 |
my $issue_1 = $builder->build( |
| 1774 |
{ |
| 1775 |
source => 'Issue', |
| 1776 |
value => { |
| 1777 |
borrowernumber => $new_patron_cf_1->borrowernumber |
| 1778 |
}, |
| 1779 |
} |
| 1780 |
); |
| 1781 |
|
| 1782 |
my $filtered = $results->filter_by_has_issues(); |
| 1783 |
is( ref($filtered), 'Koha::Patrons', |
| 1784 |
'filter_by_has_issues() should return a Koha::Patrons result set in a scalar context' |
| 1785 |
); |
| 1786 |
|
| 1787 |
$filtered = $results->filter_by_has_issues(1); |
| 1788 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1789 |
1, "filter_by_has_issues(1) found one patron" ); |
| 1790 |
|
| 1791 |
$filtered = $results->filter_by_has_issues(0); |
| 1792 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1793 |
2, "filter_by_has_issues(0) found two patrons" ); |
| 1794 |
}; |
| 1795 |
|
| 1796 |
subtest 'filter_by_when_expired' => sub { |
| 1797 |
plan tests => 4; |
| 1798 |
|
| 1799 |
my $first = $now->clone->subtract( days => 12 ); |
| 1800 |
my $second = $now->clone->subtract( days => 4 ); |
| 1801 |
|
| 1802 |
my $filtered = $results->filter_by_when_expired(); |
| 1803 |
is( ref($filtered), 'Koha::Patrons', |
| 1804 |
'Koha::Patrons->filter_by_when_expired should return a Koha::Patrons result set in a scalar context' |
| 1805 |
); |
| 1806 |
|
| 1807 |
$filtered = $results->filter_by_when_expired( { before => $second } ); |
| 1808 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1809 |
2, |
| 1810 |
"filter_by_when_expired({ before => $second }) found two patrons" ); |
| 1811 |
|
| 1812 |
$filtered = $results->filter_by_when_expired( { after => $first } ); |
| 1813 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1814 |
2, |
| 1815 |
"filter_by_when_expired({ after => $first }) found two patrons" ); |
| 1816 |
|
| 1817 |
$filtered = $results->filter_by_when_expired( |
| 1818 |
{ after => $first, before => $second } ); |
| 1819 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1820 |
"filter_by_when_expired({ after => $first, before => $second }) found one patrons" |
| 1821 |
); |
| 1822 |
}; |
| 1823 |
|
| 1824 |
subtest 'filter_by_amount_owed' => sub { |
| 1825 |
plan tests => 4; |
| 1826 |
|
| 1827 |
my $fine1 = $builder->build( |
| 1828 |
{ |
| 1829 |
source => 'Accountline', |
| 1830 |
value => { |
| 1831 |
borrowernumber => $new_patron_cf_1->borrowernumber, |
| 1832 |
amountoutstanding => 12.00 |
| 1833 |
}, |
| 1834 |
} |
| 1835 |
); |
| 1836 |
my $fine2 = $builder->build( |
| 1837 |
{ |
| 1838 |
source => 'Accountline', |
| 1839 |
value => { |
| 1840 |
borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1841 |
amountoutstanding => 8.00 |
| 1842 |
}, |
| 1843 |
} |
| 1844 |
); |
| 1845 |
my $fine3 = $builder->build( |
| 1846 |
{ |
| 1847 |
source => 'Accountline', |
| 1848 |
value => { |
| 1849 |
borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1850 |
amountoutstanding => 10.00 |
| 1851 |
}, |
| 1852 |
} |
| 1853 |
); |
| 1854 |
|
| 1855 |
my $filtered = $results->filter_by_amount_owed(); |
| 1856 |
is( ref($filtered), 'Koha::Patrons', |
| 1857 |
'Koha::Patrons->filter_by_amount_owed should return a Koha::Patrons result set in a scalar context' |
| 1858 |
); |
| 1859 |
|
| 1860 |
my $lower_limit = 12.00; |
| 1861 |
my $upper_limit = 16.00; |
| 1862 |
|
| 1863 |
# Catch user with 1 x 12.00 fine and user with no fines. |
| 1864 |
$filtered = |
| 1865 |
$results->filter_by_amount_owed( { less_than => $upper_limit } ); |
| 1866 |
is( $filtered->_resultset->as_subselect_rs->count, 2, |
| 1867 |
"filter_by_amount_owed({ less_than => $upper_limit }) found two patrons" |
| 1868 |
); |
| 1869 |
|
| 1870 |
# Catch user with 1 x 8.00 and 1 x 10.00 fine |
| 1871 |
$filtered = |
| 1872 |
$results->filter_by_amount_owed( { more_than => $lower_limit } ); |
| 1873 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1874 |
"filter_by_amount_owed({ more_than => $lower_limit }) found two patrons" |
| 1875 |
); |
| 1876 |
|
| 1877 |
# User with 2 fines falls above upper limit, |
| 1878 |
# user with 1 fine falls below lower limit |
| 1879 |
# and user with no fines falls below lower limit. |
| 1880 |
$filtered = $results->filter_by_amount_owed( |
| 1881 |
{ more_than => $lower_limit, less_than => $upper_limit } ); |
| 1882 |
is( $filtered->_resultset->as_subselect_rs->count, 0, |
| 1883 |
"filter_by_amount_owed({ more_than => $lower_limit, less_than => $upper_limit }) found one patrons" |
| 1884 |
); |
| 1885 |
}; |
| 1886 |
|
| 1887 |
subtest 'filter_by_lists' => sub { |
| 1888 |
plan tests => 6; |
| 1889 |
|
| 1890 |
my $patronListPatron1 = $builder->build( |
| 1891 |
{ |
| 1892 |
source => 'PatronListPatron', |
| 1893 |
value => { |
| 1894 |
borrowernumber => $new_patron_cf_1->borrowernumber |
| 1895 |
}, |
| 1896 |
} |
| 1897 |
); |
| 1898 |
my $patronListPatron2 = $builder->build( |
| 1899 |
{ |
| 1900 |
source => 'PatronListPatron', |
| 1901 |
value => { |
| 1902 |
borrowernumber => $new_patron_cf_2->borrowernumber |
| 1903 |
}, |
| 1904 |
} |
| 1905 |
); |
| 1906 |
#my $patronListPatron3 = $builder->build( |
| 1907 |
# { |
| 1908 |
# source => 'PatronListPatron', |
| 1909 |
# value => { |
| 1910 |
# borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1911 |
# patron_list_id => $patronListPatron1->patron_list_id |
| 1912 |
# }, |
| 1913 |
# } |
| 1914 |
#); |
| 1915 |
|
| 1916 |
my $filtered = $results->filter_by_in_lists(); |
| 1917 |
is( ref($filtered), 'Koha::Patrons', |
| 1918 |
'Koha::Patrons->filter_by_in_lists should return a Koha::Patrons result set in a scalar context' |
| 1919 |
); |
| 1920 |
|
| 1921 |
$filtered = $results->filter_by_not_in_lists(); |
| 1922 |
is( ref($filtered), 'Koha::Patrons', |
| 1923 |
'Koha::Patrons->filter_by_not_in_lists should return a Koha::Patrons result set in a scalar context' |
| 1924 |
); |
| 1925 |
|
| 1926 |
$filtered = |
| 1927 |
$results->filter_by_in_lists( |
| 1928 |
[ $patronListPatron1->{patron_list_id} ] ); |
| 1929 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1930 |
"filter_by_in_lists([$patronListPatron1->patron_list_id]) found one patron" |
| 1931 |
); |
| 1932 |
is( $filtered->next->borrowernumber, $new_patron_cf_1->borrowernumber, |
| 1933 |
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found the correct patron" |
| 1934 |
); |
| 1935 |
|
| 1936 |
$filtered = |
| 1937 |
$results->filter_by_in_lists( |
| 1938 |
[ $patronListPatron1->{patron_list_id} ] ); |
| 1939 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1940 |
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found one patron" |
| 1941 |
); |
| 1942 |
is( $filtered->next->borrowernumber, $new_patron_cf_1->borrowernumber, |
| 1943 |
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found the correct patron" |
| 1944 |
); |
| 1945 |
|
| 1946 |
}; |
| 1947 |
|
| 1948 |
# subtest 'chaining' => sub { |
| 1949 |
# plan tests => 0; |
| 1950 |
# |
| 1951 |
#}; |
| 1952 |
}; |
| 1953 |
|
| 1605 |
$schema->storage->txn_rollback; |
1954 |
$schema->storage->txn_rollback; |
| 1606 |
|
1955 |
|
| 1607 |
subtest 'Test Koha::Patrons::merge' => sub { |
1956 |
subtest 'Test Koha::Patrons::merge' => sub { |
| 1608 |
- |
|
|