ba91219b72c4696af0b90a110396ae68f36f1ff8
[koha.git] / t / db_dependent / Serials_2.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3
4 use Test::More tests => 50;
5
6 use MARC::Record;
7
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');
15
16 # Mock userenv
17 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
18 my $userenv;
19 *C4::Context::userenv = \&Mock_userenv;
20
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
33 my $record = MARC::Record->new();
34 $record->append_fields(
35     MARC::Field->new( '952', '0', '0', a => $library1->{branchcode}, b => $library1->{branchcode} )
36 );
37 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
38
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 });
47
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 });
55
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
63 );
64 die unless $subscriptionid_from_my_branch;
65
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
73 );
74
75
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");
79
80 my $userid = 'my_userid';
81 my $borrowernumber = Koha::Patron->new({
82     firstname =>  'my fistname',
83     surname => 'my surname',
84     categorycode => $patron_category->{categorycode},
85     branchcode => $my_branch,
86     userid => $userid,
87 })->store->borrowernumber;
88
89 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
90
91 # Can edit a subscription
92
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");
95
96 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
97
98 $userenv->{id} = $userid;
99 $userenv->{branch} = $my_branch;
100
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"
106 );
107 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
108 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
109 );
110 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
111 "With IndependentBranches, superlibrarian can show a subscription from his branch"
112 );
113 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
114 "With IndependentBranches, superlibrarian can show a subscription from another branch"
115 );
116 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
117 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
118 );
119 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
120 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
121 );
122
123
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"
127 );
128 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
129 "With IndependentBranches, superserials can edit a subscription from another branch"
130 );
131 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
132 "With IndependentBranches, superserials can show a subscription from his branch"
133 );
134 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
135 "With IndependentBranches, superserials can show a subscription from another branch"
136 );
137 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
138 "With IndependentBranches, superserials can claim a subscription from his branch"
139 );
140 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
141 "With IndependentBranches, superserials can claim a subscription from another branch"
142 );
143
144
145
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"
149 );
150 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
151 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
152 );
153 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
154 "With IndependentBranches, show_subscription can show a subscription from his branch"
155 );
156 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
157 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
158 );
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"
161 );
162 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
163 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
164 );
165
166
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"
170 );
171 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
172 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
173 );
174 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
175 "With IndependentBranches, renew_subscription can show a subscription from his branch"
176 );
177 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
178 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
179 );
180
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"
184 );
185 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
186 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
187 );
188
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"
194 );
195 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
196 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
197 );
198 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
199 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
200 );
201 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
202 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
203 );
204
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"
208 );
209 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
210 "Without IndependentBranches, superserials can edit a subscription from another branch"
211 );
212 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
213 "Without IndependentBranches, superserials can show a subscription from his branch"
214 );
215 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
216 "Without IndependentBranches, superserials can show a subscription from another branch"
217 );
218
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"
222 );
223 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
224 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
225 );
226 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
227 "Without IndependentBranches, show_subscription can show a subscription from his branch"
228 );
229 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
230 "Without IndependentBranches, show_subscription can show a subscription from another branch"
231 );
232
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"
236 );
237 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
238 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
239 );
240 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
241 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
242 );
243 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
244 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
245 );
246
247 # GetPreviousSerialid
248 my $serialid1 = NewIssue( 1, $subscriptionid_from_my_branch, $biblionumber, 2 );
249 my $serialid2 = NewIssue( 2, $subscriptionid_from_my_branch, $biblionumber, 2 );
250 my $serialid3 = NewIssue( 3, $subscriptionid_from_my_branch, $biblionumber, 2 );
251 is( GetPreviousSerialid( $subscriptionid_from_my_branch ), $serialid2, "get previous serialid without parameter");
252 is( GetPreviousSerialid( $subscriptionid_from_my_branch, 1 ), $serialid2, "get previous serialid with 1" );
253 is( GetPreviousSerialid( $subscriptionid_from_my_branch, 2 ), $serialid1, "get previous serialid with 2" );
254 is( GetPreviousSerialid( $subscriptionid_from_my_branch, 3 ), undef, "get previous serialid with 3, does not exist" );
255
256 $schema->storage->txn_rollback;
257
258 # C4::Context->userenv
259 sub Mock_userenv {
260     return $userenv;
261 }
262
263 sub set_flags {
264     my ( $flags, $borrowernumber ) = @_;
265     my $superlibrarian_flags = 1;
266     if ( $flags eq 'superlibrarian' ) {
267         $dbh->do(
268             q|
269             UPDATE borrowers SET flags=? WHERE borrowernumber=?
270         |, {}, $superlibrarian_flags, $borrowernumber
271         );
272         $userenv->{flags} = $superlibrarian_flags;
273     }
274     else {
275         $dbh->do(
276             q|
277             UPDATE borrowers SET flags=? WHERE borrowernumber=?
278         |, {}, 0, $borrowernumber
279         );
280         $userenv->{flags} = 0;
281         my ( $module_bit, $code ) = ( '15', $flags );
282         $dbh->do(
283             q|
284             DELETE FROM user_permissions where borrowernumber=?
285         |, {}, $borrowernumber
286         );
287
288         $dbh->do(
289             q|
290             INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
291         |, {}, $borrowernumber, $module_bit, $code
292         );
293     }
294 }