Bug 12744 - Set language in staff client should have 'Cancel' link
[koha.git] / t / db_dependent / Serials_2.t
blob51b59955b0cf592f3c8072e8e45516523044bc58
1 #!/usr/bin/perl
2 use Modern::Perl;
4 use Test::More tests => 37;
6 use MARC::Record;
8 use C4::Biblio qw( AddBiblio );
9 use C4::Members qw( AddMember );
10 use t::lib::Mocks;
11 use_ok('C4::Serials');
12 use_ok('C4::Budgets');
14 # Mock userenv
15 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
16 my $userenv;
17 *C4::Context::userenv = \&Mock_userenv;
19 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
24 my $supplierlist=eval{GetSuppliersWithLateIssues()};
25 ok(length($@)==0,"No SQL problem in GetSuppliersWithLateIssues");
27 my $record = MARC::Record->new();
28 $record->append_fields(
29 MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' )
31 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
33 my $my_branch = 'CPL';
34 my $another_branch = 'MPL';
35 my $budgetid;
36 my $bpid = AddBudgetPeriod({
37 budget_period_startdate => '2015-01-01',
38 budget_period_enddate => '2015-12-31',
39 budget_period_description => "budget desc"
40 });
42 my $budget_id = AddBudget({
43 budget_code => "ABCD",
44 budget_amount => "123.132",
45 budget_name => "Périodiques",
46 budget_notes => "This is a note",
47 budget_period_id => $bpid
48 });
50 my $subscriptionid_from_my_branch = NewSubscription(
51 undef, $my_branch, undef, undef, $budget_id, $biblionumber,
52 '2013-01-01', undef, undef, undef, undef,
53 undef, undef, undef, undef, undef, undef,
54 1, "notes",undef, '2013-01-01', undef, undef,
55 undef, undef, 0, "intnotes", 0,
56 undef, undef, 0, undef, '2013-12-31', 0
58 die unless $subscriptionid_from_my_branch;
60 my $subscriptionid_from_another_branch = NewSubscription(
61 undef, $another_branch, undef, undef, $budget_id, $biblionumber,
62 '2013-01-01', undef, undef, undef, undef,
63 undef, undef, undef, undef, undef, undef,
64 1, "notes",undef, '2013-01-01', undef, undef,
65 undef, undef, 0, "intnotes", 0,
66 undef, undef, 0, undef, '2013-12-31', 0
70 my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
71 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
73 my $userid = 'my_userid';
74 my $borrowernumber = C4::Members::AddMember(
75 firstname => 'my fistname',
76 surname => 'my surname',
77 categorycode => 'S',
78 branchcode => $my_branch,
79 userid => $userid,
82 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
84 # Can edit a subscription
86 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
88 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
90 $userenv->{id} = $userid;
91 $userenv->{branch} = $my_branch;
93 # Branches are independent
94 t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
95 set_flags( 'superlibrarian', $borrowernumber );
96 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
97 "With IndependentBranches, superlibrarian can edit a subscription from his branch"
99 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
100 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
102 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
103 "With IndependentBranches, superlibrarian can show a subscription from his branch"
105 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
106 "With IndependentBranches, superlibrarian can show a subscription from another branch"
109 set_flags( 'superserials', $borrowernumber );
110 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
111 "With IndependentBranches, superserials can edit a subscription from his branch"
113 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
114 "With IndependentBranches, superserials can edit a subscription from another branch"
116 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
117 "With IndependentBranches, superserials can show a subscription from his branch"
119 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
120 "With IndependentBranches, superserials can show a subscription from another branch"
124 set_flags( 'edit_subscription', $borrowernumber );
125 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
126 "With IndependentBranches, edit_subscription can edit a subscription from his branch"
128 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
129 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
131 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
132 "With IndependentBranches, show_subscription can show a subscription from his branch"
134 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
135 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
138 set_flags( 'renew_subscription', $borrowernumber );
139 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
140 "With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
142 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
143 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
145 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
146 "With IndependentBranches, renew_subscription can show a subscription from his branch"
148 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
149 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
153 # Branches are not independent
154 t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
155 set_flags( 'superlibrarian', $borrowernumber );
156 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
157 "Without IndependentBranches, superlibrarian can edit a subscription from his branch"
159 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
160 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
162 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
163 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
165 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
166 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
169 set_flags( 'superserials', $borrowernumber );
170 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
171 "Without IndependentBranches, superserials can edit a subscription from his branch"
173 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
174 "Without IndependentBranches, superserials can edit a subscription from another branch"
176 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
177 "Without IndependentBranches, superserials can show a subscription from his branch"
179 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
180 "Without IndependentBranches, superserials can show a subscription from another branch"
183 set_flags( 'edit_subscription', $borrowernumber );
184 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
185 "Without IndependentBranches, edit_subscription can edit a subscription from his branch"
187 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
188 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
190 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
191 "Without IndependentBranches, show_subscription can show a subscription from his branch"
193 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
194 "Without IndependentBranches, show_subscription can show a subscription from another branch"
197 set_flags( 'renew_subscription', $borrowernumber );
198 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
199 "Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
201 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
202 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
204 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
205 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
207 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
208 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
211 $dbh->rollback;
213 # C4::Context->userenv
214 sub Mock_userenv {
215 return $userenv;
218 sub set_flags {
219 my ( $flags, $borrowernumber ) = @_;
220 my $superlibrarian_flags = 1;
221 if ( $flags eq 'superlibrarian' ) {
222 $dbh->do(
224 UPDATE borrowers SET flags=? WHERE borrowernumber=?
225 |, {}, $superlibrarian_flags, $borrowernumber
227 $userenv->{flags} = $superlibrarian_flags;
229 else {
230 $dbh->do(
232 UPDATE borrowers SET flags=? WHERE borrowernumber=?
233 |, {}, 0, $borrowernumber
235 $userenv->{flags} = 0;
236 my ( $module_bit, $code ) = ( '15', $flags );
237 $dbh->do(
239 DELETE FROM user_permissions where borrowernumber=?
240 |, {}, $borrowernumber
243 $dbh->do(
245 INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
246 |, {}, $borrowernumber, $module_bit, $code