]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/live_t/20-hold-targeter.t
LP2042879 Shelving Location Groups Admin accessibility
[working/Evergreen.git] / Open-ILS / src / perlmods / live_t / 20-hold-targeter.t
1 #!perl
2 use strict;
3 use warnings;
4
5 use Test::More tests => 15;
6 diag("General hold targeter tests");
7
8 use OpenILS::Const qw/:const/;
9 use OpenILS::Utils::TestUtils;
10 use OpenILS::Application::AppUtils;
11 use OpenILS::Utils::CStoreEditor qw/:funcs/;
12
13 my $script = OpenILS::Utils::TestUtils->new();
14 my $U = 'OpenILS::Application::AppUtils';
15 my $e = new_editor();
16
17 $script->bootstrap;
18 $e->init;
19
20 sub target {
21     return $U->simplereq(
22         'open-ils.hold-targeter', 
23         'open-ils.hold-targeter.target', 
24         @_
25     );
26 }
27
28 # == Targeting Concerto hold 67.  Title hold.
29
30 my $hold_id = 67;
31 my $result = target({hold => $hold_id});
32
33 ok($result->{success}, "Targeting hold $hold_id returned success");
34
35 # Concerto hold 67 targets record 70 with a pickup_lib of 4.  
36 # There are several viable copies with circ lib 4.
37 my $current_copy = $e->retrieve_asset_copy($result->{target});
38 is($current_copy->circ_lib.'', '4', 'Targeted copy lives at pickup lib');
39
40 my $maps = $e->search_action_hold_copy_map([
41     {hold => $hold_id},
42     {
43         flesh => 2, 
44         flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
45     }
46 ]);
47
48 is(scalar(@$maps), 29, "Hold $hold_id has 29 mapped potential copies");
49
50 is(scalar(grep {$_->target_copy->call_number->record != 70} @$maps), 0,
51     'All targeted copies belong to the targeted bib record');
52
53 # Retarget to confirm a new copy is selected and that the previously
54 # targeted item has a new entry in action.unfulfilled_hold_list.
55
56 $result = target({hold => $hold_id});
57
58 isnt($result->{target}, $current_copy->id, 
59     'Second targeter run on hold 67 selected targeted a different copy');
60
61 my $unfulfilled = $e->search_action_unfulfilled_hold_list(
62     {hold => $hold_id, current_copy => $current_copy->id})->[0];
63
64 isnt($unfulfilled, undef, 'Previous copy has unfulfilled hold entry');
65
66 my $prev_target = $result->{target};
67
68 $result = target({hold => $hold_id, soft_retarget_interval => '0s'});
69
70 is($result->{target}, $prev_target, 
71     "Hold $hold_id target remains the same with soft_retarget_interval");
72
73 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
74
75 is(scalar(@$maps), 29, 
76     "Hold $hold_id retains 29 mapped potential copies with soft_retarget_interval");
77
78
79 # == Metarecord hold tests
80 #
81 # Concerto hold 263 is a metarecord hold with pickup_lib 4, target 42, and 
82 # holdable_format '{"0":[{"_attr":"mr_hold_format","_val":"score"}]}'.
83
84 $hold_id = 263;
85 $result = target({hold => $hold_id});
86
87 ok($result->{success}, "Targeting hold $hold_id returned success");
88
89 $current_copy = $e->retrieve_asset_copy($result->{target});
90 is($current_copy->circ_lib.'', '9', 'Targeted copy lives at pickup lib');
91
92 $maps = $e->search_action_hold_copy_map([
93     {hold => $hold_id},
94     {
95         flesh => 2, 
96         flesh_fields => {ahcm => ['target_copy'], acp => ['call_number']}
97     }
98 ]);
99
100 is(scalar(@$maps), 24, "Hold $hold_id has 24 mapped potential copies");
101
102 # Only 1 bib record (42) links to metarecord 42.  It also satisfies the 
103 # holdable_format criteria.
104 is(scalar(grep {$_->target_copy->call_number->record != 42} @$maps), 0,
105     'All targeted copies belong to the targeted bib record');
106
107 # Bib 101 has mr_hold_format 'book'.  Link it to the targeted metabib
108 # and confirm the targeter does not select it.
109
110 $e->xact_begin;
111 my $mrmap_101 = $e->search_metabib_metarecord_source_map({source => 101})->[0];
112 my $orig_101_mr = $mrmap_101->metarecord;
113 $mrmap_101->metarecord(42);
114 $e->update_metabib_metarecord_source_map($mrmap_101) or die $e->die_event;
115
116 # Temporarily point the original bib (42) at another metarecord
117
118 my $mrmap_42 = $e->search_metabib_metarecord_source_map({source => 42})->[0];
119 my $orig_42_mr = $mrmap_42->metarecord;
120 $mrmap_42->metarecord(1);
121 $e->update_metabib_metarecord_source_map($mrmap_42) or die $e->die_event;
122 $e->xact_commit;
123
124 # This time no copies should be targeted, since no records match
125 # the holdable_formats criteria.
126 $result = target({hold => $hold_id});
127
128 isnt($result->{success}, 1, 
129     'Unable to target MR hold without copies matching holdable_format');
130
131 $maps = $e->search_action_hold_copy_map({hold => $hold_id});
132
133 is(scalar(@$maps), 0, 
134     'No potential copies exist that match the holdable_format criteria');
135
136 # Now remove the holdable format restriction and copies belonging to
137 # record 101 should now be acceptable potential copies.
138 $e->xact_begin;
139 my $hold = $e->retrieve_action_hold_request($hold_id);
140 $hold->clear_holdable_formats;
141 $e->update_action_hold_request($hold) or die $e->die_event;
142 $e->xact_commit;
143
144 $result = target({hold => $hold_id});
145
146 $current_copy = $e->retrieve_asset_copy([
147     $result->{target},
148     {flesh => 1, flesh_fields => {acp => ['call_number']}}
149 ]);
150
151 is($current_copy->call_number->record.'', '101', 
152     'Metarecord hold targeted after removing holdable_format restriction');
153
154 # Return the hold and bib records to their original metarecord state 
155 # for re-test-ability.
156 $e->xact_begin;
157 $hold->holdable_formats('{"0":[{"_attr":"mr_hold_format","_val":"score"}]}');
158 $e->update_action_hold_request($hold) or die $e->die_event;
159 $mrmap_101->metarecord($orig_101_mr);
160 $mrmap_42->metarecord(42);
161 $e->update_metabib_metarecord_source_map($mrmap_101) or die $e->die_event;
162 $e->update_metabib_metarecord_source_map($mrmap_42) or die $e->die_event;
163 $e->xact_commit;
164
165