Skip to content

Commit

Permalink
Fix, refactor and add more account menu tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Dec 19, 2024
1 parent acefd7c commit 14d342c
Show file tree
Hide file tree
Showing 4 changed files with 465 additions and 297 deletions.
10 changes: 10 additions & 0 deletions config/vufind/Demo.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ services[] = 'custom'
; driver.
;historicTransactions = '[{"id":"1234", ... "dueDate": "01/01/2017"}]';

; This setting can be used to create fake fines for specific records.
; The value is a JSON document representing the fine information returned by the
; driver.
;fines = '[{"amount": 123, "checkout": "2024-12-01", "createdate": "2024-12-19", "description": "Overdue fee", "id":"1234", "title": "Record"}]';

; This setting can be used to create fake holds for specific records.
; The value is a JSON document representing the hold information returned by the
; driver.
;holds = '[{"reqnum": 1, "location": "Main Library", "create": "2024-12-01", "expire": "2025-12-01", "available": false, "id":"1234", "title": "Record"}]';

; This setting can be used to flag specific records as recently returned; if
; commented out, a random set of IDs will be selected.
;recently_returned[] = myBibId001
Expand Down
115 changes: 66 additions & 49 deletions module/VuFind/src/VuFind/ILS/Driver/Demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ protected function getRandomItemIdentifier()
*/
protected function createRequestList($requestType)
{
$key = strtolower($requestType);
if ($records = $this->config['Records'][$key] ?? null) {
return json_decode($records, true);
}

// How many items are there? %10 - 1 = 10% chance of none,
// 90% of 1-9 (give or take some odd maths)
$items = rand() % 10 - 1;
Expand Down Expand Up @@ -1053,6 +1058,64 @@ public function getMyProfile($patron)
return $patron;
}

/**
* Generate random fines
*
* @return array
*/
protected function getRandomFines(): array
{
// How many items are there? %20 - 2 = 10% chance of none,
// 90% of 1-18 (give or take some odd maths)
$fines = rand() % 20 - 2;

$fineList = [];
for ($i = 0; $i < $fines; $i++) {
// How many days overdue is the item?
$day_overdue = rand() % 30 + 5;
// Calculate checkout date:
$checkout = strtotime('now - ' . ($day_overdue + 14) . ' days');
// 1 in 10 chance of this being a "Manual Fee":
if (rand(1, 10) === 1) {
$fine = 2.50;
$type = 'Manual Fee';
} else {
// 50c a day fine
$fine = $day_overdue * 0.50;
// After 20 days it becomes 'Long Overdue'
$type = $day_overdue > 20 ? 'Long Overdue' : 'Overdue';
}

$fineList[] = [
'amount' => $fine * 100,
'checkout' => $this->dateConverter
->convertToDisplayDate('U', $checkout),
'createdate' => $this->dateConverter
->convertToDisplayDate('U', time()),
'fine' => $type,
// Additional description for long overdue fines:
'description' => 'Manual Fee' === $type ? 'Interlibrary loan request fee' : '',
// 50% chance they've paid half of it
'balance' => (rand() % 100 > 49 ? $fine / 2 : $fine) * 100,
'duedate' => $this->dateConverter->convertToDisplayDate(
'U',
strtotime("now - $day_overdue days")
),
];
// Some fines will have no id or title:
if (rand() % 3 != 1) {
if ($this->idsInMyResearch) {
[$fineList[$i]['id'], $fineList[$i]['title']]
= $this->getRandomBibIdAndTitle();
$fineList[$i]['source'] = $this->getRecordSource();
} else {
$fineList[$i]['title'] = 'Demo Title ' . $i;
}
}
}
return $fineList;
}

/**
* Get Patron Fines
*
Expand All @@ -1069,55 +1132,9 @@ public function getMyFines($patron)
$this->checkIntermittentFailure();
$session = $this->getSession($patron['id'] ?? null);
if (!isset($session->fines)) {
// How many items are there? %20 - 2 = 10% chance of none,
// 90% of 1-18 (give or take some odd maths)
$fines = rand() % 20 - 2;

$fineList = [];
for ($i = 0; $i < $fines; $i++) {
// How many days overdue is the item?
$day_overdue = rand() % 30 + 5;
// Calculate checkout date:
$checkout = strtotime('now - ' . ($day_overdue + 14) . ' days');
// 1 in 10 chance of this being a "Manual Fee":
if (rand(1, 10) === 1) {
$fine = 2.50;
$type = 'Manual Fee';
} else {
// 50c a day fine
$fine = $day_overdue * 0.50;
// After 20 days it becomes 'Long Overdue'
$type = $day_overdue > 20 ? 'Long Overdue' : 'Overdue';
}

$fineList[] = [
'amount' => $fine * 100,
'checkout' => $this->dateConverter
->convertToDisplayDate('U', $checkout),
'createdate' => $this->dateConverter
->convertToDisplayDate('U', time()),
'fine' => $type,
// Additional description for long overdue fines:
'description' => 'Manual Fee' === $type ? 'Interlibrary loan request fee' : '',
// 50% chance they've paid half of it
'balance' => (rand() % 100 > 49 ? $fine / 2 : $fine) * 100,
'duedate' => $this->dateConverter->convertToDisplayDate(
'U',
strtotime("now - $day_overdue days")
),
];
// Some fines will have no id or title:
if (rand() % 3 != 1) {
if ($this->idsInMyResearch) {
[$fineList[$i]['id'], $fineList[$i]['title']]
= $this->getRandomBibIdAndTitle();
$fineList[$i]['source'] = $this->getRecordSource();
} else {
$fineList[$i]['title'] = 'Demo Title ' . $i;
}
}
}
$session->fines = $fineList;
$session->fines = ($records = $this->config['Records']['fines'] ?? null)
? json_decode($records, true)
: $this->getRandomFines();
}
return $session->fines;
}
Expand Down
105 changes: 103 additions & 2 deletions module/VuFind/src/VuFindTest/Feature/DemoDriverTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,106 @@ protected function getFakeHistoricTransactions($bibId, $bibId2)
);
}

/**
* Get fine JSON for Demo.ini.
*
* @param string $bibId Bibliographic record ID to create fake item info for.
*
* @return array
*/
protected function getFakeFines(string $bibId)
{
$checkoutDate = strtotime('now -30 days');
$returnDate = strtotime('now -2 days');
$dueDate = strtotime('now -5 days');
return json_encode([
[
'amount' => 123,
'balance' => 123,
'checkout' => date('Y-m-d', $checkoutDate),
'createdate' => date('Y-m-d', $returnDate),
'duedate' => date('Y-m-d', $dueDate),
'description' => 'Overdue fee',
'id' => $bibId,
],
]);
}

/**
* Get hold JSON for Demo.ini.
*
* @param string $bibId Bibliographic record ID to create fake item info for.
* @param string $bibId2 Second bibliographic record ID to create fake item info for.
*
* @return array
*/
protected function getFakeHolds(string $bibId, string $bibId2)
{
$createDate = strtotime('now -30 days');
$expireDate = strtotime('now +1 year');
return json_encode([
[
'reqnum' => 1,
'location' => 'Main Library',
'create' => date('Y-m-d', $createDate),
'expire' => date('Y-m-d', $expireDate),
'id' => $bibId,
'available' => true,
'in_transit' => false,
],
[
'reqnum' => 2,
'item_id' => 1,
'location' => 'Main Library',
'create' => date('Y-m-d', $createDate),
'expire' => date('Y-m-d', $expireDate),
'id' => $bibId2,
'available' => false,
'in_transit' => true,
],
[
'reqnum' => 3,
'item_id' => 3,
'location' => 'Main Library',
'create' => date('Y-m-d', $createDate),
'expire' => date('Y-m-d', $expireDate),
'id' => $bibId2,
'available' => false,
'in_transit' => true,
],
[
'reqnum' => 4,
'item_id' => 7,
'location' => 'Main Library',
'create' => date('Y-m-d', $createDate),
'expire' => date('Y-m-d', $expireDate),
'id' => $bibId2,
'available' => false,
'in_transit' => false,
],
[
'reqnum' => 5,
'item_id' => 17,
'location' => 'Main Library',
'create' => date('Y-m-d', $createDate),
'expire' => date('Y-m-d', $expireDate),
'id' => $bibId2,
'available' => false,
'in_transit' => false,
],
[
'reqnum' => 6,
'item_id' => 27,
'location' => 'Main Library',
'create' => date('Y-m-d', $createDate),
'expire' => date('Y-m-d', $expireDate),
'id' => $bibId2,
'available' => false,
'in_transit' => false,
],
]);
}

/**
* Get Demo.ini override settings for testing ILS functions.
*
Expand All @@ -136,8 +236,9 @@ protected function getDemoIniOverrides(
return [
'Records' => [
'transactions' => $this->getFakeTransactions($bibId),
'historicTransactions'
=> $this->getFakeHistoricTransactions($bibId, $bibId2),
'historicTransactions' => $this->getFakeHistoricTransactions($bibId, $bibId2),
'fines' => $this->getFakeFines($bibId),
'holds' => $this->getFakeHolds($bibId, $bibId2),
],
'Failure_Probabilities' => [
'cancelHolds' => 0,
Expand Down
Loading

0 comments on commit 14d342c

Please sign in to comment.