|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 31; |
22 |
use Test::More tests => 32; |
| 23 |
use Test::Warn; |
23 |
use Test::Warn; |
| 24 |
use Time::Fake; |
24 |
use Time::Fake; |
| 25 |
use DateTime; |
25 |
use DateTime; |
|
Lines 1377-1382
subtest 'Log cardnumber change' => sub {
Link Here
|
| 1377 |
is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' ); |
1377 |
is( scalar @logs, 2, 'With BorrowerLogs, Change in cardnumber should be logged, as well as general alert of patron mod.' ); |
| 1378 |
}; |
1378 |
}; |
| 1379 |
|
1379 |
|
|
|
1380 |
subtest 'complex filters' => sub { |
| 1381 |
plan tests => 7; |
| 1382 |
|
| 1383 |
my $now = DateTime->now; |
| 1384 |
my $new_patron_cf_1 = Koha::Patron->new( |
| 1385 |
{ |
| 1386 |
cardnumber => 'test_cn_cf_1', |
| 1387 |
branchcode => $library->{branchcode}, |
| 1388 |
categorycode => $category->{categorycode}, |
| 1389 |
surname => 'surname for patron1', |
| 1390 |
firstname => 'firstname for patron1', |
| 1391 |
userid => 'a_nonexistent_userid_cf_1', |
| 1392 |
dateexpiry => $now->clone->subtract( days => 14 ) |
| 1393 |
} |
| 1394 |
)->store; |
| 1395 |
my $new_patron_cf_2 = Koha::Patron->new( |
| 1396 |
{ |
| 1397 |
cardnumber => 'test_cn_cf_2', |
| 1398 |
branchcode => $library->{branchcode}, |
| 1399 |
categorycode => $category->{categorycode}, |
| 1400 |
surname => 'surname for patron2', |
| 1401 |
firstname => 'firstname for patron2', |
| 1402 |
userid => 'a_nonexistent_userid_cf_2', |
| 1403 |
dateexpiry => $now->clone->subtract( days => 7 ) |
| 1404 |
} |
| 1405 |
)->store; |
| 1406 |
my $new_patron_cf_3 = Koha::Patron->new( |
| 1407 |
{ |
| 1408 |
cardnumber => 'test_cn_cf_3', |
| 1409 |
branchcode => $library->{branchcode}, |
| 1410 |
categorycode => $category->{categorycode}, |
| 1411 |
surname => 'surname for patron3', |
| 1412 |
firstname => 'firstname for patron3', |
| 1413 |
userid => 'a_nonexistent_userid_cf_3', |
| 1414 |
} |
| 1415 |
)->store; |
| 1416 |
|
| 1417 |
my $results = Koha::Patrons->search( |
| 1418 |
{ |
| 1419 |
'me.borrowernumber' => [ |
| 1420 |
$new_patron_cf_1->borrowernumber, |
| 1421 |
$new_patron_cf_2->borrowernumber, |
| 1422 |
$new_patron_cf_3->borrowernumber |
| 1423 |
] |
| 1424 |
} |
| 1425 |
); |
| 1426 |
|
| 1427 |
subtest 'filter_by_is_guarantor' => sub { |
| 1428 |
plan tests => 6; |
| 1429 |
|
| 1430 |
# Set new_patron_cf_1 to be the guarantor for new_patron_cf_2 |
| 1431 |
$new_patron_cf_2->guarantorid( $new_patron_cf_1->borrowernumber ) |
| 1432 |
->store; |
| 1433 |
|
| 1434 |
my $filtered = $results->filter_by_is_guarantor(); |
| 1435 |
is( ref($filtered), 'Koha::Patrons', |
| 1436 |
'Koha::Patrons->filter_by_is_guarantor should return a Koha::Patrons result set in a scalar context' |
| 1437 |
); |
| 1438 |
is( $filtered->count, 3, |
| 1439 |
'filter_by_is_guarantor() found both patrons' ); |
| 1440 |
|
| 1441 |
$filtered = $results->filter_by_is_guarantor(1); |
| 1442 |
is( $filtered->count, 1, 'filter_by_is_guarantor(1) found one patron' ); |
| 1443 |
is( $filtered->next->guarantees->count, |
| 1444 |
1, 'filter_by_is_guarantor(1) found the correct patron' ); |
| 1445 |
|
| 1446 |
$filtered = $results->filter_by_is_guarantor(0); |
| 1447 |
is( $filtered->count, 2, |
| 1448 |
'filter_by_is_guarantor(0) found two patrons' ); |
| 1449 |
is( $filtered->next->guarantees->count, |
| 1450 |
0, 'filter_by_is_guarantor(0) found the correct patrons' ); |
| 1451 |
}; |
| 1452 |
|
| 1453 |
subtest 'filter_by_has_guarantor' => sub { |
| 1454 |
plan tests => 6; |
| 1455 |
|
| 1456 |
my $filtered = $results->filter_by_has_guarantor(); |
| 1457 |
is( ref($filtered), 'Koha::Patrons', |
| 1458 |
'filter_by_has_guarantor() should return a Koha::Patrons result set in a scalar context' |
| 1459 |
); |
| 1460 |
is( $filtered->count, 3, |
| 1461 |
'filter_by_has_guarantor() found both patrons' ); |
| 1462 |
|
| 1463 |
$filtered = $results->filter_by_has_guarantor(1); |
| 1464 |
is( $filtered->count, 1, |
| 1465 |
'filter_by_has_guarantor(1) found one patron' ); |
| 1466 |
is( |
| 1467 |
$filtered->next->guarantorid, |
| 1468 |
$new_patron_cf_1->borrowernumber, |
| 1469 |
'filter_by_has_guarantor(1) found the correct patron' |
| 1470 |
); |
| 1471 |
|
| 1472 |
$filtered = $results->filter_by_has_guarantor(0); |
| 1473 |
is( $filtered->count, 2, |
| 1474 |
'filter_by_has_guarantor(0) found two patrons' ); |
| 1475 |
is( $filtered->next->guarantorid, |
| 1476 |
undef, 'filter_by_has_guarantor(0) found the correct patrons' ); |
| 1477 |
}; |
| 1478 |
|
| 1479 |
subtest 'filter_by_last_issued' => sub { |
| 1480 |
plan tests => 4; |
| 1481 |
|
| 1482 |
my $first = DateTime->now()->subtract( days => 5 ); |
| 1483 |
my $last = $first->clone->add( days => 2 ); |
| 1484 |
my $old_issue_1 = $builder->build( |
| 1485 |
{ |
| 1486 |
source => 'OldIssue', |
| 1487 |
value => { |
| 1488 |
borrowernumber => $new_patron_cf_1->borrowernumber, |
| 1489 |
timestamp => $first->clone->subtract( days => 1 ) |
| 1490 |
}, |
| 1491 |
} |
| 1492 |
); |
| 1493 |
my $old_issue_2 = $builder->build( |
| 1494 |
{ |
| 1495 |
source => 'OldIssue', |
| 1496 |
value => { |
| 1497 |
borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1498 |
timestamp => $last->clone->add( days => 1 ) |
| 1499 |
}, |
| 1500 |
} |
| 1501 |
); |
| 1502 |
my $old_issue_3 = $builder->build( |
| 1503 |
{ |
| 1504 |
source => 'OldIssue', |
| 1505 |
value => { |
| 1506 |
borrowernumber => $new_patron_cf_3->borrowernumber, |
| 1507 |
timestamp => $first->clone->add( days => 1 ) |
| 1508 |
}, |
| 1509 |
} |
| 1510 |
); |
| 1511 |
|
| 1512 |
my $filtered = $results->filter_by_last_issued(); |
| 1513 |
is( ref($filtered), 'Koha::Patrons', |
| 1514 |
'filter_by_last_issued() should return a Koha::Patrons result set in a scalar context' |
| 1515 |
); |
| 1516 |
|
| 1517 |
$filtered = $results->filter_by_last_issued( { before => $last } ); |
| 1518 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1519 |
2, "filter_by_last_issued({ before => $last }) found two patrons" ); |
| 1520 |
|
| 1521 |
$filtered = $results->filter_by_last_issued( { after => $first } ); |
| 1522 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1523 |
2, "filter_by_last_issued({ after => $first }) found two patrons" ); |
| 1524 |
|
| 1525 |
$filtered = $results->filter_by_last_issued( |
| 1526 |
{ after => $first, before => $last } ); |
| 1527 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1528 |
"filter_by_last_issued({ after => $first, before => $last }) found two patrons" |
| 1529 |
); |
| 1530 |
}; |
| 1531 |
|
| 1532 |
subtest 'filter_by_has_issues' => sub { |
| 1533 |
plan tests => 3; |
| 1534 |
|
| 1535 |
my $issue_1 = $builder->build( |
| 1536 |
{ |
| 1537 |
source => 'Issue', |
| 1538 |
value => { |
| 1539 |
borrowernumber => $new_patron_cf_1->borrowernumber |
| 1540 |
}, |
| 1541 |
} |
| 1542 |
); |
| 1543 |
|
| 1544 |
my $filtered = $results->filter_by_has_issues(); |
| 1545 |
is( ref($filtered), 'Koha::Patrons', |
| 1546 |
'filter_by_has_issues() should return a Koha::Patrons result set in a scalar context' |
| 1547 |
); |
| 1548 |
|
| 1549 |
$filtered = $results->filter_by_has_issues(1); |
| 1550 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1551 |
1, "filter_by_has_issues(1) found one patron" ); |
| 1552 |
|
| 1553 |
$filtered = $results->filter_by_has_issues(0); |
| 1554 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1555 |
2, "filter_by_has_issues(0) found two patrons" ); |
| 1556 |
}; |
| 1557 |
|
| 1558 |
subtest 'filter_by_when_expired' => sub { |
| 1559 |
plan tests => 4; |
| 1560 |
|
| 1561 |
my $first = $now->clone->subtract( days => 12 ); |
| 1562 |
my $second = $now->clone->subtract( days => 4 ); |
| 1563 |
|
| 1564 |
my $filtered = $results->filter_by_when_expired(); |
| 1565 |
is( ref($filtered), 'Koha::Patrons', |
| 1566 |
'Koha::Patrons->filter_by_when_expired should return a Koha::Patrons result set in a scalar context' |
| 1567 |
); |
| 1568 |
|
| 1569 |
$filtered = $results->filter_by_when_expired( { before => $second } ); |
| 1570 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1571 |
2, |
| 1572 |
"filter_by_when_expired({ before => $second }) found two patrons" ); |
| 1573 |
|
| 1574 |
$filtered = $results->filter_by_when_expired( { after => $first } ); |
| 1575 |
is( $filtered->_resultset->as_subselect_rs->count, |
| 1576 |
2, |
| 1577 |
"filter_by_when_expired({ after => $first }) found two patrons" ); |
| 1578 |
|
| 1579 |
$filtered = $results->filter_by_when_expired( |
| 1580 |
{ after => $first, before => $second } ); |
| 1581 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1582 |
"filter_by_when_expired({ after => $first, before => $second }) found one patrons" |
| 1583 |
); |
| 1584 |
}; |
| 1585 |
|
| 1586 |
subtest 'filter_by_amount_owed' => sub { |
| 1587 |
plan tests => 4; |
| 1588 |
|
| 1589 |
my $fine1 = $builder->build( |
| 1590 |
{ |
| 1591 |
source => 'Accountline', |
| 1592 |
value => { |
| 1593 |
borrowernumber => $new_patron_cf_1->borrowernumber, |
| 1594 |
amountoutstanding => 12.00 |
| 1595 |
}, |
| 1596 |
} |
| 1597 |
); |
| 1598 |
my $fine2 = $builder->build( |
| 1599 |
{ |
| 1600 |
source => 'Accountline', |
| 1601 |
value => { |
| 1602 |
borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1603 |
amountoutstanding => 8.00 |
| 1604 |
}, |
| 1605 |
} |
| 1606 |
); |
| 1607 |
my $fine3 = $builder->build( |
| 1608 |
{ |
| 1609 |
source => 'Accountline', |
| 1610 |
value => { |
| 1611 |
borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1612 |
amountoutstanding => 10.00 |
| 1613 |
}, |
| 1614 |
} |
| 1615 |
); |
| 1616 |
|
| 1617 |
my $filtered = $results->filter_by_amount_owed(); |
| 1618 |
is( ref($filtered), 'Koha::Patrons', |
| 1619 |
'Koha::Patrons->filter_by_amount_owed should return a Koha::Patrons result set in a scalar context' |
| 1620 |
); |
| 1621 |
|
| 1622 |
my $lower_limit = 12.00; |
| 1623 |
my $upper_limit = 16.00; |
| 1624 |
|
| 1625 |
# Catch user with 1 x 12.00 fine and user with no fines. |
| 1626 |
$filtered = |
| 1627 |
$results->filter_by_amount_owed( { less_than => $upper_limit } ); |
| 1628 |
is( $filtered->_resultset->as_subselect_rs->count, 2, |
| 1629 |
"filter_by_amount_owed({ less_than => $upper_limit }) found two patrons" |
| 1630 |
); |
| 1631 |
|
| 1632 |
# Catch user with 1 x 8.00 and 1 x 10.00 fine |
| 1633 |
$filtered = |
| 1634 |
$results->filter_by_amount_owed( { more_than => $lower_limit } ); |
| 1635 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1636 |
"filter_by_amount_owed({ more_than => $lower_limit }) found two patrons" |
| 1637 |
); |
| 1638 |
|
| 1639 |
# User with 2 fines falls above upper limit, |
| 1640 |
# user with 1 fine falls below lower limit |
| 1641 |
# and user with no fines falls below lower limit. |
| 1642 |
$filtered = $results->filter_by_amount_owed( |
| 1643 |
{ more_than => $lower_limit, less_than => $upper_limit } ); |
| 1644 |
is( $filtered->_resultset->as_subselect_rs->count, 0, |
| 1645 |
"filter_by_amount_owed({ more_than => $lower_limit, less_than => $upper_limit }) found one patrons" |
| 1646 |
); |
| 1647 |
}; |
| 1648 |
|
| 1649 |
subtest 'filter_by_lists' => sub { |
| 1650 |
plan tests => 6; |
| 1651 |
|
| 1652 |
my $patronListPatron1 = $builder->build( |
| 1653 |
{ |
| 1654 |
source => 'PatronListPatron', |
| 1655 |
value => { |
| 1656 |
borrowernumber => $new_patron_cf_1->borrowernumber |
| 1657 |
}, |
| 1658 |
} |
| 1659 |
); |
| 1660 |
my $patronListPatron2 = $builder->build( |
| 1661 |
{ |
| 1662 |
source => 'PatronListPatron', |
| 1663 |
value => { |
| 1664 |
borrowernumber => $new_patron_cf_2->borrowernumber |
| 1665 |
}, |
| 1666 |
} |
| 1667 |
); |
| 1668 |
#my $patronListPatron3 = $builder->build( |
| 1669 |
# { |
| 1670 |
# source => 'PatronListPatron', |
| 1671 |
# value => { |
| 1672 |
# borrowernumber => $new_patron_cf_2->borrowernumber, |
| 1673 |
# patron_list_id => $patronListPatron1->patron_list_id |
| 1674 |
# }, |
| 1675 |
# } |
| 1676 |
#); |
| 1677 |
|
| 1678 |
my $filtered = $results->filter_by_in_lists(); |
| 1679 |
is( ref($filtered), 'Koha::Patrons', |
| 1680 |
'Koha::Patrons->filter_by_in_lists should return a Koha::Patrons result set in a scalar context' |
| 1681 |
); |
| 1682 |
|
| 1683 |
$filtered = $results->filter_by_not_in_lists(); |
| 1684 |
is( ref($filtered), 'Koha::Patrons', |
| 1685 |
'Koha::Patrons->filter_by_not_in_lists should return a Koha::Patrons result set in a scalar context' |
| 1686 |
); |
| 1687 |
|
| 1688 |
$filtered = |
| 1689 |
$results->filter_by_in_lists( |
| 1690 |
[ $patronListPatron1->{patron_list_id} ] ); |
| 1691 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1692 |
"filter_by_in_lists([$patronListPatron1->patron_list_id]) found one patron" |
| 1693 |
); |
| 1694 |
is( $filtered->next->borrowernumber, $new_patron_cf_1->borrowernumber, |
| 1695 |
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found the correct patron" |
| 1696 |
); |
| 1697 |
|
| 1698 |
$filtered = |
| 1699 |
$results->filter_by_in_lists( |
| 1700 |
[ $patronListPatron1->{patron_list_id} ] ); |
| 1701 |
is( $filtered->_resultset->as_subselect_rs->count, 1, |
| 1702 |
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found one patron" |
| 1703 |
); |
| 1704 |
is( $filtered->next->borrowernumber, $new_patron_cf_1->borrowernumber, |
| 1705 |
"filter_by_in_lists([$patronListPatron1->{patron_list_id}]) found the correct patron" |
| 1706 |
); |
| 1707 |
|
| 1708 |
}; |
| 1709 |
|
| 1710 |
# subtest 'chaining' => sub { |
| 1711 |
# plan tests => 0; |
| 1712 |
# |
| 1713 |
#}; |
| 1714 |
}; |
| 1715 |
|
| 1380 |
$schema->storage->txn_rollback; |
1716 |
$schema->storage->txn_rollback; |
| 1381 |
|
1717 |
|
| 1382 |
subtest 'Test Koha::Patrons::merge' => sub { |
1718 |
subtest 'Test Koha::Patrons::merge' => sub { |
| 1383 |
- |
|
|