Bug 20434: Update UNIMARC framework - authorised values
[koha.git] / t / db_dependent / Serials_2.t
blob95c36f7d8e5fd284b6ec7ac841dd8aa4c5677207
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;
32 $dbh->{RaiseError} = 1;
34 my $record = MARC::Record->new();
35 $record->append_fields(
36 MARC::Field->new( '952', '0', '0', a => $library1->{branchcode}, b => $library1->{branchcode} )
38 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
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 });
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 });
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
65 die unless $subscriptionid_from_my_branch;
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
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");
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;
90 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
92 # Can edit a subscription
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");
97 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
99 $userenv->{id} = $userid;
100 $userenv->{branch} = $my_branch;
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"
108 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
109 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
111 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
112 "With IndependentBranches, superlibrarian can show a subscription from his branch"
114 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
115 "With IndependentBranches, superlibrarian can show a subscription from another branch"
117 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
118 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
120 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
121 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
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"
129 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
130 "With IndependentBranches, superserials can edit a subscription from another branch"
132 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
133 "With IndependentBranches, superserials can show a subscription from his branch"
135 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
136 "With IndependentBranches, superserials can show a subscription from another branch"
138 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
139 "With IndependentBranches, superserials can claim a subscription from his branch"
141 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
142 "With IndependentBranches, superserials can claim a subscription from another branch"
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"
151 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
152 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
154 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
155 "With IndependentBranches, show_subscription can show a subscription from his branch"
157 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
158 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
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"
163 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
164 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
172 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
173 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
175 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
176 "With IndependentBranches, renew_subscription can show a subscription from his branch"
178 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
179 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
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"
186 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
187 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
196 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
197 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
199 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
200 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
202 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
203 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
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"
210 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
211 "Without IndependentBranches, superserials can edit a subscription from another branch"
213 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
214 "Without IndependentBranches, superserials can show a subscription from his branch"
216 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
217 "Without IndependentBranches, superserials can show a subscription from another branch"
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"
224 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
225 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
227 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
228 "Without IndependentBranches, show_subscription can show a subscription from his branch"
230 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
231 "Without IndependentBranches, show_subscription can show a subscription from another branch"
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"
238 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
239 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
241 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
242 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
244 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
245 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
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" );
257 $schema->storage->txn_rollback;
259 # C4::Context->userenv
260 sub Mock_userenv {
261 return $userenv;
264 sub set_flags {
265 my ( $flags, $borrowernumber ) = @_;
266 my $superlibrarian_flags = 1;
267 if ( $flags eq 'superlibrarian' ) {
268 $dbh->do(
270 UPDATE borrowers SET flags=? WHERE borrowernumber=?
271 |, {}, $superlibrarian_flags, $borrowernumber
273 $userenv->{flags} = $superlibrarian_flags;
275 else {
276 $dbh->do(
278 UPDATE borrowers SET flags=? WHERE borrowernumber=?
279 |, {}, 0, $borrowernumber
281 $userenv->{flags} = 0;
282 my ( $module_bit, $code ) = ( '15', $flags );
283 $dbh->do(
285 DELETE FROM user_permissions where borrowernumber=?
286 |, {}, $borrowernumber
289 $dbh->do(
291 INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
292 |, {}, $borrowernumber, $module_bit, $code