Bug 1917 [QA Followup] - Use html filter, only show 'by' if author exists, change...
[koha.git] / t / db_dependent / Serials_2.t
blob0a233fc41253c043710c6dc1c5f217ff3fc8ce3b
1 #!/usr/bin/perl
2 use Modern::Perl;
4 use Test::More tests => 36;
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;
23 my $record = MARC::Record->new();
24 $record->append_fields(
25 MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' )
27 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
29 my $my_branch = 'CPL';
30 my $another_branch = 'MPL';
31 my $budgetid;
32 my $bpid = AddBudgetPeriod({
33 budget_period_startdate => '2015-01-01',
34 budget_period_enddate => '2015-12-31',
35 budget_period_description => "budget desc"
36 });
38 my $budget_id = AddBudget({
39 budget_code => "ABCD",
40 budget_amount => "123.132",
41 budget_name => "Périodiques",
42 budget_notes => "This is a note",
43 budget_period_id => $bpid
44 });
46 my $subscriptionid_from_my_branch = NewSubscription(
47 undef, $my_branch, undef, undef, $budget_id, $biblionumber,
48 '2013-01-01', undef, undef, undef, undef,
49 undef, undef, undef, undef, undef, undef,
50 1, "notes",undef, '2013-01-01', undef, undef,
51 undef, undef, 0, "intnotes", 0,
52 undef, undef, 0, undef, '2013-12-31', 0
54 die unless $subscriptionid_from_my_branch;
56 my $subscriptionid_from_another_branch = NewSubscription(
57 undef, $another_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
66 my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
67 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
69 my $userid = 'my_userid';
70 my $borrowernumber = C4::Members::AddMember(
71 firstname => 'my fistname',
72 surname => 'my surname',
73 categorycode => 'S',
74 branchcode => $my_branch,
75 userid => $userid,
78 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
80 # Can edit a subscription
82 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
84 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
86 $userenv->{id} = $userid;
87 $userenv->{branch} = $my_branch;
89 # Branches are independent
90 t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
91 set_flags( 'superlibrarian', $borrowernumber );
92 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
93 "With IndependentBranches, superlibrarian can edit a subscription from his branch"
95 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
96 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
98 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
99 "With IndependentBranches, superlibrarian can show a subscription from his branch"
101 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
102 "With IndependentBranches, superlibrarian can show a subscription from another branch"
105 set_flags( 'superserials', $borrowernumber );
106 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
107 "With IndependentBranches, superserials can edit a subscription from his branch"
109 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
110 "With IndependentBranches, superserials can edit a subscription from another branch"
112 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
113 "With IndependentBranches, superserials can show a subscription from his branch"
115 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
116 "With IndependentBranches, superserials can show a subscription from another branch"
120 set_flags( 'edit_subscription', $borrowernumber );
121 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
122 "With IndependentBranches, edit_subscription can edit a subscription from his branch"
124 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
125 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
127 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
128 "With IndependentBranches, show_subscription can show a subscription from his branch"
130 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
131 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
134 set_flags( 'renew_subscription', $borrowernumber );
135 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
136 "With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
138 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
139 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
141 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
142 "With IndependentBranches, renew_subscription can show a subscription from his branch"
144 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
145 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
149 # Branches are not independent
150 t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
151 set_flags( 'superlibrarian', $borrowernumber );
152 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
153 "Without IndependentBranches, superlibrarian can edit a subscription from his branch"
155 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
156 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
158 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
159 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
161 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
162 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
165 set_flags( 'superserials', $borrowernumber );
166 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
167 "Without IndependentBranches, superserials can edit a subscription from his branch"
169 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
170 "Without IndependentBranches, superserials can edit a subscription from another branch"
172 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
173 "Without IndependentBranches, superserials can show a subscription from his branch"
175 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
176 "Without IndependentBranches, superserials can show a subscription from another branch"
179 set_flags( 'edit_subscription', $borrowernumber );
180 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
181 "Without IndependentBranches, edit_subscription can edit a subscription from his branch"
183 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
184 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
186 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
187 "Without IndependentBranches, show_subscription can show a subscription from his branch"
189 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
190 "Without IndependentBranches, show_subscription can show a subscription from another branch"
193 set_flags( 'renew_subscription', $borrowernumber );
194 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
195 "Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
197 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
198 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
200 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
201 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
203 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
204 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
207 $dbh->rollback;
209 # C4::Context->userenv
210 sub Mock_userenv {
211 return $userenv;
214 sub set_flags {
215 my ( $flags, $borrowernumber ) = @_;
216 my $superlibrarian_flags = 1;
217 if ( $flags eq 'superlibrarian' ) {
218 $dbh->do(
220 UPDATE borrowers SET flags=? WHERE borrowernumber=?
221 |, {}, $superlibrarian_flags, $borrowernumber
223 $userenv->{flags} = $superlibrarian_flags;
225 else {
226 $dbh->do(
228 UPDATE borrowers SET flags=? WHERE borrowernumber=?
229 |, {}, 0, $borrowernumber
231 $userenv->{flags} = 0;
232 my ( $module_bit, $code ) = ( '15', $flags );
233 $dbh->do(
235 DELETE FROM user_permissions where borrowernumber=?
236 |, {}, $borrowernumber
239 $dbh->do(
241 INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
242 |, {}, $borrowernumber, $module_bit, $code