Bug 12098: (follow-up) deal with FIXMEs in t/db_dependent/Serials_2.t
[koha.git] / t / db_dependent / Serials_2.t
1 #!/usr/bin/perl
2 use Modern::Perl;
3
4 use Test::More tests => 37;
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
24 my $supplierlist=eval{GetSuppliersWithLateIssues()};
25 ok(length($@)==0,"No SQL problem in GetSuppliersWithLateIssues");
26
27 my $record = MARC::Record->new();
28 $record->append_fields(
29     MARC::Field->new( '952', '0', '0', a => 'CPL', b => 'CPL' )
30 );
31 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
32
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_description      => "budget desc"
40 });
41
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_description => "Serials",
48     budget_active      => 1,
49     budget_period_id   => $bpid
50 });
51
52 my $subscriptionid_from_my_branch = NewSubscription(
53     undef,      $my_branch,     undef, undef, $budget_id, $biblionumber,
54     '2013-01-01', undef, undef, undef,  undef,
55     undef,      undef,  undef, undef, undef, undef,
56     1,          "notes",undef, '2013-01-01', undef, undef,
57     undef,       undef,  0,    "intnotes",  0,
58     undef, undef, 0,          undef,         '2013-12-31', 0
59 );
60 die unless $subscriptionid_from_my_branch;
61
62 my $subscriptionid_from_another_branch = NewSubscription(
63     undef,      $another_branch,     undef, undef, $budget_id, $biblionumber,
64     '2013-01-01', undef, undef, undef,  undef,
65     undef,      undef,  undef, undef, undef, undef,
66     1,          "notes",undef, '2013-01-01', undef, undef,
67     undef,       undef,  0,    "intnotes",  0,
68     undef, undef, 0,          undef,         '2013-12-31', 0
69 );
70
71
72 my $subscription_from_my_branch = GetSubscription( $subscriptionid_from_my_branch );
73 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0, "cannot edit a subscription without userenv set");
74
75 my $userid = 'my_userid';
76 my $borrowernumber = C4::Members::AddMember(
77     firstname =>  'my fistname',
78     surname => 'my surname',
79     categorycode => 'S',
80     branchcode => $my_branch,
81     userid => $userid,
82 );
83
84 $userenv = { flags => 1, id => $borrowernumber, branch => '' };
85
86 # Can edit a subscription
87
88 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1, "User can edit a subscription with an empty branchcode");
89
90 my $subscription_from_another_branch = GetSubscription( $subscriptionid_from_another_branch );
91
92 $userenv->{id} = $userid;
93 $userenv->{branch} = $my_branch;
94
95 # Branches are independent
96 t::lib::Mocks::mock_preference( "IndependentBranches", 1 );
97 set_flags( 'superlibrarian', $borrowernumber );
98 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
99 "With IndependentBranches, superlibrarian can edit a subscription from his branch"
100 );
101 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
102 "With IndependentBranches, superlibrarian can edit a subscription from another branch"
103 );
104 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
105 "With IndependentBranches, superlibrarian can show a subscription from his branch"
106 );
107 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
108 "With IndependentBranches, superlibrarian can show a subscription from another branch"
109 );
110
111 set_flags( 'superserials', $borrowernumber );
112 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
113 "With IndependentBranches, superserials can edit a subscription from his branch"
114 );
115 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
116 "With IndependentBranches, superserials can edit a subscription from another branch"
117 );
118 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
119 "With IndependentBranches, superserials can show a subscription from his branch"
120 );
121 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
122 "With IndependentBranches, superserials can show a subscription from another branch"
123 );
124
125
126 set_flags( 'edit_subscription', $borrowernumber );
127 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
128 "With IndependentBranches, edit_subscription can edit a subscription from his branch"
129 );
130 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
131 "With IndependentBranches, edit_subscription cannot edit a subscription from another branch"
132 );
133 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
134 "With IndependentBranches, show_subscription can show a subscription from his branch"
135 );
136 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
137 "With IndependentBranches, show_subscription cannot show a subscription from another branch"
138 );
139
140 set_flags( 'renew_subscription', $borrowernumber );
141 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
142 "With IndependentBranches, renew_subscription cannot edit a subscription from his branch"
143 );
144 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
145 "With IndependentBranches, renew_subscription cannot edit a subscription from another branch"
146 );
147 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
148 "With IndependentBranches, renew_subscription can show a subscription from his branch"
149 );
150 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 0,
151 "With IndependentBranches, renew_subscription cannot show a subscription from another branch"
152 );
153
154
155 # Branches are not independent
156 t::lib::Mocks::mock_preference( "IndependentBranches", 0 );
157 set_flags( 'superlibrarian', $borrowernumber );
158 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
159 "Without IndependentBranches, superlibrarian can edit a subscription from his branch"
160 );
161 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
162 "Without IndependentBranches, superlibrarian can edit a subscription from another branch"
163 );
164 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
165 "Without IndependentBranches, superlibrarian can show a subscription from his branch"
166 );
167 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
168 "Without IndependentBranches, superlibrarian can show a subscription from another branch"
169 );
170
171 set_flags( 'superserials', $borrowernumber );
172 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
173 "Without IndependentBranches, superserials can edit a subscription from his branch"
174 );
175 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
176 "Without IndependentBranches, superserials can edit a subscription from another branch"
177 );
178 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
179 "Without IndependentBranches, superserials can show a subscription from his branch"
180 );
181 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
182 "Without IndependentBranches, superserials can show a subscription from another branch"
183 );
184
185 set_flags( 'edit_subscription', $borrowernumber );
186 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 1,
187 "Without IndependentBranches, edit_subscription can edit a subscription from his branch"
188 );
189 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 1,
190 "Without IndependentBranches, edit_subscription can edit a subscription from another branch"
191 );
192 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
193 "Without IndependentBranches, show_subscription can show a subscription from his branch"
194 );
195 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
196 "Without IndependentBranches, show_subscription can show a subscription from another branch"
197 );
198
199 set_flags( 'renew_subscription', $borrowernumber );
200 is( C4::Serials::can_edit_subscription($subscription_from_my_branch), 0,
201 "Without IndependentBranches, renew_subscription cannot edit a subscription from his branch"
202 );
203 is( C4::Serials::can_edit_subscription($subscription_from_another_branch), 0,
204 "Without IndependentBranches, renew_subscription cannot edit a subscription from another branch"
205 );
206 is( C4::Serials::can_show_subscription($subscription_from_my_branch), 1,
207 "Without IndependentBranches, renew_subscription cannot show a subscription from his branch"
208 );
209 is( C4::Serials::can_show_subscription($subscription_from_another_branch), 1,
210 "Without IndependentBranches, renew_subscription cannot show a subscription from another branch"
211 );
212
213 $dbh->rollback;
214
215 # C4::Context->userenv
216 sub Mock_userenv {
217     return $userenv;
218 }
219
220 sub set_flags {
221     my ( $flags, $borrowernumber ) = @_;
222     my $superlibrarian_flags = 1;
223     if ( $flags eq 'superlibrarian' ) {
224         $dbh->do(
225             q|
226             UPDATE borrowers SET flags=? WHERE borrowernumber=?
227         |, {}, $superlibrarian_flags, $borrowernumber
228         );
229         $userenv->{flags} = $superlibrarian_flags;
230     }
231     else {
232         $dbh->do(
233             q|
234             UPDATE borrowers SET flags=? WHERE borrowernumber=?
235         |, {}, 0, $borrowernumber
236         );
237         $userenv->{flags} = 0;
238         my ( $module_bit, $code ) = ( '15', $flags );
239         $dbh->do(
240             q|
241             DELETE FROM user_permissions where borrowernumber=?
242         |, {}, $borrowernumber
243         );
244
245         $dbh->do(
246             q|
247             INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES ( ?, ?, ? )
248         |, {}, $borrowernumber, $module_bit, $code
249         );
250     }
251 }