Bug 13264: Follow up: in opac_utf8.t insert also delete of biblio
[koha.git] / t / db_dependent / Serials_2.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3
4 use Test::More tests => 36;
5
6 use MARC::Record;
7
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');
13
14 # Mock userenv
15 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
16 my $userenv;
17 *C4::Context::userenv = \&Mock_userenv;
18
19 my $dbh = C4::Context->dbh;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
22
23 my $record = MARC::Record->new();
24 $record->append_fields(
25     MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' )
26 );
27 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
28
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 });
37
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 });
45
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
53 );
54 die unless $subscriptionid_from_my_branch;
55
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
63 );
64
65
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");
68
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,
76 );
77
78 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
79
80 # Can edit a subscription
81
82 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
83
84 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
85
86 $userenv->{id} = $userid;
87 $userenv->{branch} = $my_branch;
88
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"
94 );
95 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
96 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
97 );
98 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
99 "With IndependentBranches, superlibrarian can show a subscription from his branch"
100 );
101 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
102 "With IndependentBranches, superlibrarian can show a subscription from another branch"
103 );
104
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"
108 );
109 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
110 "With IndependentBranches, superserials can edit a subscription from another branch"
111 );
112 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
113 "With IndependentBranches, superserials can show a subscription from his branch"
114 );
115 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
116 "With IndependentBranches, superserials can show a subscription from another branch"
117 );
118
119
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"
123 );
124 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
125 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
126 );
127 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
128 "With IndependentBranches, show_subscription can show a subscription from his branch"
129 );
130 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
131 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
132 );
133
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"
137 );
138 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
139 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
140 );
141 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
142 "With IndependentBranches, renew_subscription can show a subscription from his branch"
143 );
144 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
145 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
146 );
147
148
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"
154 );
155 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
156 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
157 );
158 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
159 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
160 );
161 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
162 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
163 );
164
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"
168 );
169 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
170 "Without IndependentBranches, superserials can edit a subscription from another branch"
171 );
172 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
173 "Without IndependentBranches, superserials can show a subscription from his branch"
174 );
175 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
176 "Without IndependentBranches, superserials can show a subscription from another branch"
177 );
178
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"
182 );
183 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
184 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
185 );
186 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
187 "Without IndependentBranches, show_subscription can show a subscription from his branch"
188 );
189 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
190 "Without IndependentBranches, show_subscription can show a subscription from another branch"
191 );
192
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"
196 );
197 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
198 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
199 );
200 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
201 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
202 );
203 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
204 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
205 );
206
207 $dbh->rollback;
208
209 # C4::Context->userenv
210 sub Mock_userenv {
211     return $userenv;
212 }
213
214 sub set_flags {
215     my ( $flags, $borrowernumber ) = @_;
216     my $superlibrarian_flags = 1;
217     if ( $flags eq 'superlibrarian' ) {
218         $dbh->do(
219             q|
220             UPDATE borrowers SET flags=? WHERE borrowernumber=?
221         |, {}, $superlibrarian_flags, $borrowernumber
222         );
223         $userenv->{flags} = $superlibrarian_flags;
224     }
225     else {
226         $dbh->do(
227             q|
228             UPDATE borrowers SET flags=? WHERE borrowernumber=?
229         |, {}, 0, $borrowernumber
230         );
231         $userenv->{flags} = 0;
232         my ( $module_bit, $code ) = ( '15', $flags );
233         $dbh->do(
234             q|
235             DELETE FROM user_permissions where borrowernumber=?
236         |, {}, $borrowernumber
237         );
238
239         $dbh->do(
240             q|
241             INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
242         |, {}, $borrowernumber, $module_bit, $code
243         );
244     }
245 }