4 use Test::More tests => 50;
8 use C4::Biblio qw( AddBiblio );
9 use C4::Members qw( AddMember );
12 use t::lib::TestBuilder;
13 use_ok('C4::Serials');
14 use_ok('C4::Budgets');
17 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
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({
27 my $library2 = $builder->build({
30 my $dbh = C4::Context->dbh;
31 $dbh->{RaiseError} = 1;
33 my $record = MARC::Record->new();
34 $record->append_fields(
35 MARC::Field->new( '952', '0', '0', a => $library1->{branchcode}, b => $library1->{branchcode} )
37 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
39 my $my_branch = $library1->{branchcode};
40 my $another_branch = $library2->{branchcode};
42 my $bpid = AddBudgetPeriod({
43 budget_period_startdate => '2015-01-01',
44 budget_period_enddate => '2015-12-31',
45 budget_period_description => "budget desc"
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
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
64 die unless $subscriptionid_from_my_branch;
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
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");
80 my $userid = 'my_userid';
81 my $borrowernumber = C4::Members::AddMember(
82 firstname => 'my fistname',
83 surname => 'my surname',
85 branchcode => $my_branch,
89 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
91 # Can edit a subscription
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");
96 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
98 $userenv->{id} = $userid;
99 $userenv->{branch} = $my_branch;
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"
107 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
108 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
110 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
111 "With IndependentBranches, superlibrarian can show a subscription from his branch"
113 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
114 "With IndependentBranches, superlibrarian can show a subscription from another branch"
116 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
117 "With IndependentBranches, superlibrarian can claim a subscription from his branch"
119 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
120 "With IndependentBranches, superlibrarian can claim a subscription from another branch"
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"
128 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
129 "With IndependentBranches, superserials can edit a subscription from another branch"
131 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
132 "With IndependentBranches, superserials can show a subscription from his branch"
134 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
135 "With IndependentBranches, superserials can show a subscription from another branch"
137 is( C4::Serials::can_claim_subscription($subscription_from_my_branch), 1,
138 "With IndependentBranches, superserials can claim a subscription from his branch"
140 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 1,
141 "With IndependentBranches, superserials can claim a subscription from another branch"
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"
150 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
151 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
153 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
154 "With IndependentBranches, show_subscription can show a subscription from his branch"
156 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
157 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
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"
162 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
163 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
171 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
172 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
174 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
175 "With IndependentBranches, renew_subscription can show a subscription from his branch"
177 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
178 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
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"
185 is( C4::Serials::can_claim_subscription($subscription_from_another_branch), 0,
186 "With IndependentBranches, claim_subscription cannot claim a subscription from another branch"
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"
195 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
196 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
198 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
199 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
201 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
202 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
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"
209 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
210 "Without IndependentBranches, superserials can edit a subscription from another branch"
212 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
213 "Without IndependentBranches, superserials can show a subscription from his branch"
215 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
216 "Without IndependentBranches, superserials can show a subscription from another branch"
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"
223 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
224 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
226 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
227 "Without IndependentBranches, show_subscription can show a subscription from his branch"
229 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
230 "Without IndependentBranches, show_subscription can show a subscription from another branch"
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"
237 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
238 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
240 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
241 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
243 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
244 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
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" );
256 $schema->storage->txn_rollback;
258 # C4::Context->userenv
264 my ( $flags, $borrowernumber ) = @_;
265 my $superlibrarian_flags = 1;
266 if ( $flags eq 'superlibrarian' ) {
269 UPDATE borrowers SET flags=? WHERE borrowernumber=?
270 |, {}, $superlibrarian_flags, $borrowernumber
272 $userenv->{flags} = $superlibrarian_flags;
277 UPDATE borrowers SET flags=? WHERE borrowernumber=?
278 |, {}, 0, $borrowernumber
280 $userenv->{flags} = 0;
281 my ( $module_bit, $code ) = ( '15', $flags );
284 DELETE FROM user_permissions where borrowernumber=?
285 |, {}, $borrowernumber
290 INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
291 |, {}, $borrowernumber, $module_bit, $code