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