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