Bug 26157: Hide expected DBI warnings from tests
[koha.git] / t / db_dependent / Serials_2.t
blobcaf3d432b15354cfcca755cd9b763dd07fa96f0d
1 #!/usr/bin/perl
2 use Modern::Perl;
4 use Test::More tests => 50;
6 use MARC::Record;
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');
16 # Mock userenv
17 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
18 my $userenv;
19 *C4::Context::userenv = \&Mock_userenv;
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;
33 my $record = MARC::Record->new();
34 $record->append_fields(
35 MARC::Field->new( '952', '0', '0', a => $library1->{branchcode}, b => $library1->{branchcode} )
37 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
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 });
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 });
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
63 die unless $subscriptionid_from_my_branch;
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
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");
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;
88 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
90 # Can edit a subscription
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");
95 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
97 $userenv->{id} = $userid;
98 $userenv->{branch} = $my_branch;
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"
106 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
107 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
109 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
110 "With IndependentBranches, superlibrarian can show a subscription from his branch"
112 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
113 "With IndependentBranches, superlibrarian can show a subscription from another branch"
115 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
116 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
118 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
119 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
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"
127 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
128 "With IndependentBranches, superserials can edit a subscription from another branch"
130 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
131 "With IndependentBranches, superserials can show a subscription from his branch"
133 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
134 "With IndependentBranches, superserials can show a subscription from another branch"
136 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
137 "With IndependentBranches, superserials can claim a subscription from his branch"
139 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
140 "With IndependentBranches, superserials can claim a subscription from another branch"
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"
149 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
150 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
152 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
153 "With IndependentBranches, show_subscription can show a subscription from his branch"
155 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
156 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
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"
161 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
162 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
170 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
171 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
173 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
174 "With IndependentBranches, renew_subscription can show a subscription from his branch"
176 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
177 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
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"
184 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
185 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
194 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
195 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
197 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
198 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
200 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
201 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
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"
208 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
209 "Without IndependentBranches, superserials can edit a subscription from another branch"
211 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
212 "Without IndependentBranches, superserials can show a subscription from his branch"
214 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
215 "Without IndependentBranches, superserials can show a subscription from another branch"
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"
222 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
223 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
225 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
226 "Without IndependentBranches, show_subscription can show a subscription from his branch"
228 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
229 "Without IndependentBranches, show_subscription can show a subscription from another branch"
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"
236 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
237 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
239 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
240 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
242 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
243 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
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" );
255 $schema->storage->txn_rollback;
257 # C4::Context->userenv
258 sub Mock_userenv {
259 return $userenv;
262 sub set_flags {
263 my ( $flags, $borrowernumber ) = @_;
264 my $superlibrarian_flags = 1;
265 if ( $flags eq 'superlibrarian' ) {
266 $dbh->do(
268 UPDATE borrowers SET flags=? WHERE borrowernumber=?
269 |, {}, $superlibrarian_flags, $borrowernumber
271 $userenv->{flags} = $superlibrarian_flags;
273 else {
274 $dbh->do(
276 UPDATE borrowers SET flags=? WHERE borrowernumber=?
277 |, {}, 0, $borrowernumber
279 $userenv->{flags} = 0;
280 my ( $module_bit, $code ) = ( '15', $flags );
281 $dbh->do(
283 DELETE FROM user_permissions where borrowernumber=?
284 |, {}, $borrowernumber
287 $dbh->do(
289 INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
290 |, {}, $borrowernumber, $module_bit, $code