]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/12-lp1533329-opt-in.t
cdf90196568b8c4618b80cc36b67c54186fe7623
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 12-lp1533329-opt-in.t
1 #!perl
2
3 use Test::More tests => 12;
4
5 diag("Test checking for, creating, and restricting patron opt-in.");
6
7 use constant WORKSTATION_NAME => 'BR1-test-12-lp1533329-opt-in.t';
8 use constant WORKSTATION_LIB => 4; # BR1, a branch of SYS1
9 use constant PATRON_LIB => 6; # BR3, a branch of SYS2
10 use constant PATRON_SYS => 3; # SYS2
11 use constant SYS_DEPTH => 1; # depth of "System" org type
12 use constant PATRON_BARCODE => '99999359616';
13
14 use strict; use warnings;
15
16 use OpenILS::Utils::TestUtils;
17 use OpenILS::Utils::CStoreEditor qw/:funcs/;
18 use OpenILS::Utils::Fieldmapper;
19
20 my $script = OpenILS::Utils::TestUtils->new();
21 $script->bootstrap;
22
23 our $U = "OpenILS::Application::AppUtils";
24
25 my $e = new_editor(xact => 1);
26 $e->init;
27
28 # initialize a new aous object for insertion into the db
29 sub new_org_setting {
30     my ($org_unit, $name, $value) = @_;
31     my $set = Fieldmapper::actor::org_unit_setting->new();
32     $set->org_unit($org_unit);
33     $set->name($name);
34     $set->value($value);
35     return $set;
36 }
37
38 # do an opt-in check
39 sub opt_in_check {
40     my ($authtoken, $usr_id) = @_;
41     my $resp = $U->simplereq(
42         'open-ils.actor',
43         'open-ils.actor.user.org_unit_opt_in.check',
44         $authtoken, $usr_id);
45     return $resp;
46 }
47
48 #----------------------------------------------------------------
49 # 1. Login, register workstation, get authtoken.
50 #----------------------------------------------------------------
51 $script->authenticate({
52     username => 'admin',
53     password => 'demo123',
54     type => 'staff'});
55 ok(
56     $script->authtoken,
57     'Have an authtoken'
58 );
59 my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
60 ok(
61     ! ref $ws,
62     'Registered a new workstation'
63 );
64 $script->logout();
65 $script->authenticate({
66     username => 'admin',
67     password => 'demo123',
68     type => 'staff',
69     workstation => WORKSTATION_NAME});
70 ok(
71     $script->authtoken,
72     'Have an authtoken associated with the workstation'
73 );
74
75 #----------------------------------------------------------------
76 # 2. Set org.patron_opt_boundary for SYS2, so that BR1 is outside
77 # the boundary.
78 #----------------------------------------------------------------
79 $e->xact_begin;
80 my $boundary = new_org_setting(PATRON_SYS, 'org.patron_opt_boundary', SYS_DEPTH);
81 my $boundary_stat = $e->create_actor_org_unit_setting($boundary);
82 ok($boundary_stat, 'Opt boundary setting created successfully');
83 $e->xact_commit;
84
85 #----------------------------------------------------------------
86 # 3. Check opt-in for test patron.  It should return 0.
87 #----------------------------------------------------------------
88 my $patron = $U->fetch_user_by_barcode(PATRON_BARCODE);
89 is(
90     opt_in_check($script->authtoken, $patron->id),
91     '0',
92     'Opt-in check for non-opted-in patron correctly returned 0'
93 );
94
95 #----------------------------------------------------------------
96 # 4. Set org.restrict_opt_to_depth at SYS2, so that BR1 is
97 # outside SYS2's section of the tree at the specified depth (thus
98 # preventing opt-in).
99 #----------------------------------------------------------------
100 $e->xact_begin;
101 my $restrict = new_org_setting(PATRON_SYS, 'org.restrict_opt_to_depth', SYS_DEPTH);
102 my $restrict_stat = $e->create_actor_org_unit_setting($restrict);
103 ok($restrict_stat, 'Opt restrict depth setting created successfully');
104 $e->xact_commit;
105
106 #----------------------------------------------------------------
107 # 5. Check opt-in for test patron.  It should return 2.
108 #----------------------------------------------------------------
109 is(
110     opt_in_check($script->authtoken, $patron->id),
111     '2',
112     'Opt-in check for patron at restricted opt-in library correctly returned 2'
113 );
114
115 #----------------------------------------------------------------
116 # 6. Remove the org.restrict_opt_to_depth setting for SYS2.
117 #----------------------------------------------------------------
118 $e->xact_begin;
119 my $delete_restrict_stat = $e->delete_actor_org_unit_setting($restrict);
120 ok($delete_restrict_stat, 'Opt restrict depth setting deleted successfully');
121 $e->xact_commit;
122
123 #----------------------------------------------------------------
124 # 7. Create opt-in for test patron.
125 #----------------------------------------------------------------
126 my $opt_id = $U->simplereq(
127     'open-ils.actor',
128     'open-ils.actor.user.org_unit_opt_in.create',
129     $script->authtoken, $patron->id, WORKSTATION_LIB);
130 ok($opt_id, 'Patron successfully opted in');
131
132 #----------------------------------------------------------------
133 # 8. Check opt-in for test patron.  It should return 1.
134 #----------------------------------------------------------------
135 is(
136     opt_in_check($script->authtoken, $patron->id),
137     '1',
138     'Opt-in check for opted-in patron correctly returned 1'
139 );
140
141 #----------------------------------------------------------------
142 # 9. Delete opt-in.
143 #----------------------------------------------------------------
144 my $opt = $U->simplereq(
145     'open-ils.cstore',
146     'open-ils.cstore.direct.actor.usr_org_unit_opt_in.retrieve',
147     $opt_id
148 );
149 $e->xact_begin;
150 my $delete_opt_stat = $e->delete_actor_usr_org_unit_opt_in($opt);
151 ok($delete_opt_stat, 'Opt-in deleted successfully');
152 $e->xact_commit;
153
154 #----------------------------------------------------------------
155 # 10. Remove opt boundary setting.
156 #----------------------------------------------------------------
157 $e->xact_begin;
158 my $delete_setting_stat = $e->delete_actor_org_unit_setting($boundary);
159 ok($delete_setting_stat, 'Opt boundary setting deleted successfully');
160 $e->xact_commit;
161
162