Bug 8438: Users can only claim for serials related to their branch.
[koha.git] / t / db_dependent / Serials_2.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3
4 use Test::More tests => 46;
5
6 use MARC::Record;
7
8 use C4::Biblio qw( AddBiblio );
9 use C4::Members qw( AddMember );
10 use t::lib::Mocks;
11 use_ok('C4::Serials');
12 use_ok('C4::Budgets');
13
14 # Mock userenv
15 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
16 my $userenv;
17 *C4::Context::userenv = \&Mock_userenv;
18
19 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
22
23 my $record = MARC::Record->new();
24 $record->append_fields(
25     MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' )
26 );
27 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
28
29 my $my_branch = 'CPL';
30 my $another_branch = 'MPL';
31 my $budgetid;
32 my $bpid = AddBudgetPeriod({
33     budget_period_startdate   => '2015-01-01',
34     budget_period_enddate     => '2015-12-31',
35     budget_period_description => "budget desc"
36 });
37
38 my $budget_id = AddBudget({
39     budget_code        => "ABCD",
40     budget_amount      => "123.132",
41     budget_name        => "Périodiques",
42     budget_notes       => "This is a note",
43     budget_period_id   => $bpid
44 });
45
46 my $subscriptionid_from_my_branch = NewSubscription(
47     undef,      $my_branch,     undef, undef, $budget_id, $biblionumber,
48     '2013-01-01', undef, undef, undef,  undef,
49     undef,      undef,  undef, undef, undef, undef,
50     1,          "notes",undef, '2013-01-01', undef, undef,
51     undef,       undef,  0,    "intnotes",  0,
52     undef, undef, 0,          undef,         '2013-12-31', 0
53 );
54 die unless $subscriptionid_from_my_branch;
55
56 my $subscriptionid_from_another_branch = NewSubscription(
57     undef,      $another_branch,     undef, undef, $budget_id, $biblionumber,
58     '2013-01-01', undef, undef, undef,  undef,
59     undef,      undef,  undef, undef, undef, undef,
60     1,          "notes",undef, '2013-01-01', undef, undef,
61     undef,       undef,  0,    "intnotes",  0,
62     undef, undef, 0,          undef,         '2013-12-31', 0
63 );
64
65
66 my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
67 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
68 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
69
70 my $userid = 'my_userid';
71 my $borrowernumber = C4::Members::AddMember(
72     firstname =>  'my fistname',
73     surname => 'my surname',
74     categorycode => 'S',
75     branchcode => $my_branch,
76     userid => $userid,
77 );
78
79 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
80
81 # Can edit a subscription
82
83 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
84 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
85
86 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
87
88 $userenv->{id} = $userid;
89 $userenv->{branch} = $my_branch;
90
91 # Branches are independent
92 t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
93 set_flags( 'superlibrarian', $borrowernumber );
94 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
95 "With IndependentBranches, superlibrarian can edit a subscription from his branch"
96 );
97 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
98 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
99 );
100 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
101 "With IndependentBranches, superlibrarian can show a subscription from his branch"
102 );
103 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
104 "With IndependentBranches, superlibrarian can show a subscription from another branch"
105 );
106 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
107 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
108 );
109 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
110 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
111 );
112
113
114 set_flags( 'superserials', $borrowernumber );
115 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
116 "With IndependentBranches, superserials can edit a subscription from his branch"
117 );
118 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
119 "With IndependentBranches, superserials can edit a subscription from another branch"
120 );
121 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
122 "With IndependentBranches, superserials can show a subscription from his branch"
123 );
124 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
125 "With IndependentBranches, superserials can show a subscription from another branch"
126 );
127 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
128 "With IndependentBranches, superserials can claim a subscription from his branch"
129 );
130 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
131 "With IndependentBranches, superserials can claim a subscription from another branch"
132 );
133
134
135
136 set_flags( 'edit_subscription', $borrowernumber );
137 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
138 "With IndependentBranches, edit_subscription can edit a subscription from his branch"
139 );
140 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
141 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
142 );
143 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
144 "With IndependentBranches, show_subscription can show a subscription from his branch"
145 );
146 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
147 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
148 );
149 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 0,
150 "With IndependentBranches, claim_subscription cannot claim a subscription from his branch with the edit_subscription permission"
151 );
152 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
153 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
154 );
155
156
157 set_flags( 'renew_subscription', $borrowernumber );
158 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
159 "With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
160 );
161 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
162 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
163 );
164 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
165 "With IndependentBranches, renew_subscription can show a subscription from his branch"
166 );
167 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
168 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
169 );
170
171 set_flags( 'claim_serials', $borrowernumber );
172 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
173 "With IndependentBranches, claim_subscription can claim a subscription from his branch with the edit_subscription permission"
174 );
175 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
176 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
177 );
178
179 # Branches are not independent
180 t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
181 set_flags( 'superlibrarian', $borrowernumber );
182 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
183 "Without IndependentBranches, superlibrarian can edit a subscription from his branch"
184 );
185 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
186 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
187 );
188 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
189 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
190 );
191 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
192 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
193 );
194
195 set_flags( 'superserials', $borrowernumber );
196 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
197 "Without IndependentBranches, superserials can edit a subscription from his branch"
198 );
199 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
200 "Without IndependentBranches, superserials can edit a subscription from another branch"
201 );
202 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
203 "Without IndependentBranches, superserials can show a subscription from his branch"
204 );
205 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
206 "Without IndependentBranches, superserials can show a subscription from another branch"
207 );
208
209 set_flags( 'edit_subscription', $borrowernumber );
210 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
211 "Without IndependentBranches, edit_subscription can edit a subscription from his branch"
212 );
213 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
214 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
215 );
216 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
217 "Without IndependentBranches, show_subscription can show a subscription from his branch"
218 );
219 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
220 "Without IndependentBranches, show_subscription can show a subscription from another branch"
221 );
222
223 set_flags( 'renew_subscription', $borrowernumber );
224 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
225 "Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
226 );
227 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
228 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
229 );
230 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
231 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
232 );
233 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
234 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
235 );
236
237 $dbh->rollback;
238
239 # C4::Context->userenv
240 sub Mock_userenv {
241     return $userenv;
242 }
243
244 sub set_flags {
245     my ( $flags, $borrowernumber ) = @_;
246     my $superlibrarian_flags = 1;
247     if ( $flags eq 'superlibrarian' ) {
248         $dbh->do(
249             q|
250             UPDATE borrowers SET flags=? WHERE borrowernumber=?
251         |, {}, $superlibrarian_flags, $borrowernumber
252         );
253         $userenv->{flags} = $superlibrarian_flags;
254     }
255     else {
256         $dbh->do(
257             q|
258             UPDATE borrowers SET flags=? WHERE borrowernumber=?
259         |, {}, 0, $borrowernumber
260         );
261         $userenv->{flags} = 0;
262         my ( $module_bit, $code ) = ( '15', $flags );
263         $dbh->do(
264             q|
265             DELETE FROM user_permissions where borrowernumber=?
266         |, {}, $borrowernumber
267         );
268
269         $dbh->do(
270             q|
271             INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
272         |, {}, $borrowernumber, $module_bit, $code
273         );
274     }
275 }