Bug 21395: Make perlcritic happy
[koha.git] / t / db_dependent / Serials_2.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3
4 use Test::More tests => 50;
5
6 use MARC::Record;
7
8 use C4::Biblio qw( AddBiblio );
9 use Koha::Database;
10 use Koha::Patrons;
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 $patron_category = $builder->build({ source => 'Category' });
31 my $dbh = C4::Context->dbh;
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 $bpid = AddBudgetPeriod({
42     budget_period_startdate   => '2015-01-01',
43     budget_period_enddate     => '2015-12-31',
44     budget_period_description => "budget desc"
45 });
46
47 my $budget_id = AddBudget({
48     budget_code        => "ABCD",
49     budget_amount      => "123.132",
50     budget_name        => "Périodiques",
51     budget_notes       => "This is a note",
52     budget_period_id   => $bpid
53 });
54
55 my $subscriptionid_from_my_branch = NewSubscription(
56     undef,      $my_branch,     undef, undef, $budget_id, $biblionumber,
57     '2013-01-01', undef, undef, undef,  undef,
58     undef,      undef,  undef, undef, undef, undef,
59     1,          "notes",undef, '2013-01-01', undef, undef,
60     undef,       undef,  0,    "intnotes",  0,
61     undef, undef, 0,          undef,         '2013-12-31', 0
62 );
63 die unless $subscriptionid_from_my_branch;
64
65 my $subscriptionid_from_another_branch = NewSubscription(
66     undef,      $another_branch,     undef, undef, $budget_id, $biblionumber,
67     '2013-01-01', undef, undef, undef,  undef,
68     undef,      undef,  undef, undef, undef, undef,
69     1,          "notes",undef, '2013-01-01', undef, undef,
70     undef,       undef,  0,    "intnotes",  0,
71     undef, undef, 0,          undef,         '2013-12-31', 0
72 );
73
74
75 my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
76 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
77 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
78
79 my $userid = 'my_userid';
80 my $borrowernumber = Koha::Patron->new({
81     firstname =>  'my fistname',
82     surname => 'my surname',
83     categorycode => $patron_category->{categorycode},
84     branchcode => $my_branch,
85     userid => $userid,
86 })->store->borrowernumber;
87
88 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
89
90 # Can edit a subscription
91
92 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
93 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
94
95 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
96
97 $userenv->{id} = $userid;
98 $userenv->{branch} = $my_branch;
99
100 # Branches are independent
101 t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
102 set_flags( 'superlibrarian', $borrowernumber );
103 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
104 "With IndependentBranches, superlibrarian can edit a subscription from his branch"
105 );
106 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
107 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
108 );
109 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
110 "With IndependentBranches, superlibrarian can show a subscription from his branch"
111 );
112 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
113 "With IndependentBranches, superlibrarian can show a subscription from another branch"
114 );
115 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
116 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
117 );
118 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
119 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
120 );
121
122
123 set_flags( 'superserials', $borrowernumber );
124 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
125 "With IndependentBranches, superserials can edit a subscription from his branch"
126 );
127 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
128 "With IndependentBranches, superserials can edit a subscription from another branch"
129 );
130 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
131 "With IndependentBranches, superserials can show a subscription from his branch"
132 );
133 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
134 "With IndependentBranches, superserials can show a subscription from another branch"
135 );
136 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
137 "With IndependentBranches, superserials can claim a subscription from his branch"
138 );
139 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
140 "With IndependentBranches, superserials can claim a subscription from another branch"
141 );
142
143
144
145 set_flags( 'edit_subscription', $borrowernumber );
146 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
147 "With IndependentBranches, edit_subscription can edit a subscription from his branch"
148 );
149 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
150 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
151 );
152 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
153 "With IndependentBranches, show_subscription can show a subscription from his branch"
154 );
155 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
156 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
157 );
158 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 0,
159 "With IndependentBranches, claim_subscription cannot claim a subscription from his branch with the edit_subscription permission"
160 );
161 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
162 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
163 );
164
165
166 set_flags( 'renew_subscription', $borrowernumber );
167 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
168 "With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
169 );
170 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
171 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
172 );
173 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
174 "With IndependentBranches, renew_subscription can show a subscription from his branch"
175 );
176 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
177 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
178 );
179
180 set_flags( 'claim_serials', $borrowernumber );
181 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
182 "With IndependentBranches, claim_subscription can claim a subscription from his branch with the edit_subscription permission"
183 );
184 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
185 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
186 );
187
188 # Branches are not independent
189 t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
190 set_flags( 'superlibrarian', $borrowernumber );
191 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
192 "Without IndependentBranches, superlibrarian can edit a subscription from his branch"
193 );
194 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
195 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
196 );
197 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
198 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
199 );
200 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
201 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
202 );
203
204 set_flags( 'superserials', $borrowernumber );
205 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
206 "Without IndependentBranches, superserials can edit a subscription from his branch"
207 );
208 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
209 "Without IndependentBranches, superserials can edit a subscription from another branch"
210 );
211 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
212 "Without IndependentBranches, superserials can show a subscription from his branch"
213 );
214 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
215 "Without IndependentBranches, superserials can show a subscription from another branch"
216 );
217
218 set_flags( 'edit_subscription', $borrowernumber );
219 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
220 "Without IndependentBranches, edit_subscription can edit a subscription from his branch"
221 );
222 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
223 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
224 );
225 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
226 "Without IndependentBranches, show_subscription can show a subscription from his branch"
227 );
228 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
229 "Without IndependentBranches, show_subscription can show a subscription from another branch"
230 );
231
232 set_flags( 'renew_subscription', $borrowernumber );
233 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
234 "Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
235 );
236 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
237 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
238 );
239 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
240 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
241 );
242 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
243 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
244 );
245
246 # GetPreviousSerialid
247 my $serialid1 = NewIssue( 1, $subscriptionid_from_my_branch, $biblionumber, 2 );
248 my $serialid2 = NewIssue( 2, $subscriptionid_from_my_branch, $biblionumber, 2 );
249 my $serialid3 = NewIssue( 3, $subscriptionid_from_my_branch, $biblionumber, 2 );
250 is( GetPreviousSerialid( $subscriptionid_from_my_branch ), $serialid2, "get previous serialid without parameter");
251 is( GetPreviousSerialid( $subscriptionid_from_my_branch, 1 ), $serialid2, "get previous serialid with 1" );
252 is( GetPreviousSerialid( $subscriptionid_from_my_branch, 2 ), $serialid1, "get previous serialid with 2" );
253 is( GetPreviousSerialid( $subscriptionid_from_my_branch, 3 ), undef, "get previous serialid with 3, does not exist" );
254
255 $schema->storage->txn_rollback;
256
257 # C4::Context->userenv
258 sub Mock_userenv {
259     return $userenv;
260 }
261
262 sub set_flags {
263     my ( $flags, $borrowernumber ) = @_;
264     my $superlibrarian_flags = 1;
265     if ( $flags eq 'superlibrarian' ) {
266         $dbh->do(
267             q|
268             UPDATE borrowers SET flags=? WHERE borrowernumber=?
269         |, {}, $superlibrarian_flags, $borrowernumber
270         );
271         $userenv->{flags} = $superlibrarian_flags;
272     }
273     else {
274         $dbh->do(
275             q|
276             UPDATE borrowers SET flags=? WHERE borrowernumber=?
277         |, {}, 0, $borrowernumber
278         );
279         $userenv->{flags} = 0;
280         my ( $module_bit, $code ) = ( '15', $flags );
281         $dbh->do(
282             q|
283             DELETE FROM user_permissions where borrowernumber=?
284         |, {}, $borrowernumber
285         );
286
287         $dbh->do(
288             q|
289             INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
290         |, {}, $borrowernumber, $module_bit, $code
291         );
292     }
293 }