Bug 15181: Rename marc21 fast add framework sql file
[koha.git] / t / db_dependent / Serials_2.t
bloba73ef1fdf5ffbde3fb4870f6203adecf6d7c791a
1 #!/usr/bin/perl
2 use Modern::Perl;
4 use Test::More tests => 46;
6 use MARC::Record;
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');
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 $dbh = C4::Context->dbh;
31 $dbh->{RaiseError} = 1;
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 $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 });
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 });
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
64 die unless $subscriptionid_from_my_branch;
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
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");
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,
89 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
91 # Can edit a subscription
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");
96 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
98 $userenv->{id} = $userid;
99 $userenv->{branch} = $my_branch;
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"
107 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
108 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
110 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
111 "With IndependentBranches, superlibrarian can show a subscription from his branch"
113 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
114 "With IndependentBranches, superlibrarian can show a subscription from another branch"
116 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
117 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
119 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
120 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
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"
128 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
129 "With IndependentBranches, superserials can edit a subscription from another branch"
131 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
132 "With IndependentBranches, superserials can show a subscription from his branch"
134 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
135 "With IndependentBranches, superserials can show a subscription from another branch"
137 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
138 "With IndependentBranches, superserials can claim a subscription from his branch"
140 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
141 "With IndependentBranches, superserials can claim a subscription from another branch"
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"
150 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
151 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
153 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
154 "With IndependentBranches, show_subscription can show a subscription from his branch"
156 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
157 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
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"
162 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
163 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
171 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
172 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
174 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
175 "With IndependentBranches, renew_subscription can show a subscription from his branch"
177 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
178 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
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"
185 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
186 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
195 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
196 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
198 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
199 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
201 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
202 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
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"
209 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
210 "Without IndependentBranches, superserials can edit a subscription from another branch"
212 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
213 "Without IndependentBranches, superserials can show a subscription from his branch"
215 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
216 "Without IndependentBranches, superserials can show a subscription from another branch"
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"
223 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
224 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
226 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
227 "Without IndependentBranches, show_subscription can show a subscription from his branch"
229 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
230 "Without IndependentBranches, show_subscription can show a subscription from another branch"
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"
237 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
238 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
240 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
241 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
243 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
244 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
247 $schema->storage->txn_rollback;
249 # C4::Context->userenv
250 sub Mock_userenv {
251 return $userenv;
254 sub set_flags {
255 my ( $flags, $borrowernumber ) = @_;
256 my $superlibrarian_flags = 1;
257 if ( $flags eq 'superlibrarian' ) {
258 $dbh->do(
260 UPDATE borrowers SET flags=? WHERE borrowernumber=?
261 |, {}, $superlibrarian_flags, $borrowernumber
263 $userenv->{flags} = $superlibrarian_flags;
265 else {
266 $dbh->do(
268 UPDATE borrowers SET flags=? WHERE borrowernumber=?
269 |, {}, 0, $borrowernumber
271 $userenv->{flags} = 0;
272 my ( $module_bit, $code ) = ( '15', $flags );
273 $dbh->do(
275 DELETE FROM user_permissions where borrowernumber=?
276 |, {}, $borrowernumber
279 $dbh->do(
281 INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
282 |, {}, $borrowernumber, $module_bit, $code