Bug 19382: Adjust comment in test
[koha.git] / t / db_dependent / Koha / Suggestions.t
blobae93073e89778a6451cfdd5350b7e7bbf2def908
1 #!/usr/bin/perl
3 # Copyright 2015-2019 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 8;
23 use Test::Exception;
25 use Koha::Suggestion;
26 use Koha::Suggestions;
27 use Koha::Database;
28 use Koha::DateUtils;
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
36 my $biblio_1 = $builder->build_sample_biblio;
37 my $biblio_2 = $builder->build_sample_biblio;
38 my $patron = $builder->build( { source => 'Borrower' } );
39 my $nb_of_suggestions = Koha::Suggestions->search->count;
40 my $new_suggestion_1 = Koha::Suggestion->new(
41 { suggestedby => $patron->{borrowernumber},
42 biblionumber => $biblio_1->biblionumber,
44 )->store;
45 my $new_suggestion_2 = Koha::Suggestion->new(
46 { suggestedby => $patron->{borrowernumber},
47 biblionumber => $biblio_2->biblionumber,
49 )->store;
51 subtest 'store' => sub {
52 plan tests => 3;
53 my $suggestion = Koha::Suggestion->new(
54 { suggestedby => $patron->{borrowernumber},
55 biblionumber => $biblio_1->biblionumber,
57 )->store;
59 is( $suggestion->suggesteddate, dt_from_string()->ymd, "If suggesteddate not passed in, it will default to today" );
60 my $two_days_ago = dt_from_string->subtract( days => 2 );
61 my $two_days_ago_sql = output_pref({dt => $two_days_ago, dateformat => 'sql', dateonly => 1 });
62 $suggestion->suggesteddate($two_days_ago)->store;
63 $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
64 is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggesteddate passed in, it should be taken into account' );
65 $suggestion->reason('because!')->store;
66 $suggestion = Koha::Suggestions->find( $suggestion->suggestionid );
67 is( $suggestion->suggesteddate, $two_days_ago_sql, 'If suggestion id modified, suggesteddate should not be modified' );
68 $suggestion->delete;
71 like( $new_suggestion_1->suggestionid, qr|^\d+$|, 'Adding a new suggestion should have set the suggestionid' );
72 is( Koha::Suggestions->search->count, $nb_of_suggestions + 2, 'The 2 suggestions should have been added' );
74 my $retrieved_suggestion_1 = Koha::Suggestions->find( $new_suggestion_1->suggestionid );
75 is( $retrieved_suggestion_1->biblionumber, $new_suggestion_1->biblionumber, 'Find a suggestion by id should return the correct suggestion' );
77 $retrieved_suggestion_1->delete;
78 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
80 $schema->storage->txn_rollback;
82 subtest 'constraints' => sub {
83 plan tests => 11;
84 $schema->storage->txn_begin;
86 my $print_error = $schema->storage->dbh->{PrintError};
87 $schema->storage->dbh->{PrintError} = 0;
89 my $patron = $builder->build_object( { class => "Koha::Patrons" } );
90 my $biblio = $builder->build_sample_biblio();
91 my $branch = $builder->build_object( { class => "Koha::Libraries" } );
93 my $suggestion = $builder->build_object(
95 class => "Koha::Suggestions",
96 value => {
97 suggestedby => $patron->borrowernumber,
98 biblionumber => $biblio->biblionumber,
99 branchcode => $branch->branchcode,
100 managedby => undef,
101 acceptedby => undef,
102 rejectedby => undef,
103 budgetid => undef,
108 my $nonexistent_borrowernumber = $patron->borrowernumber;
109 # suggestedby
110 $patron->delete;
111 $suggestion = $suggestion->get_from_storage;
112 is( $suggestion->suggestedby, undef,
113 "The suggestion is not deleted when the related patron is deleted" );
115 # biblionumber
116 $biblio->delete;
117 $suggestion = $suggestion->get_from_storage;
118 is( $suggestion->biblionumber, undef,
119 "The suggestion is not deleted when the related biblio is deleted" );
121 # branchcode
122 $branch->delete;
123 $suggestion = $suggestion->get_from_storage;
124 is( $suggestion->branchcode, undef,
125 "The suggestion is not deleted when the related branch is deleted" );
127 # managerid
128 { # hide useless warnings
129 local *STDERR;
130 open STDERR, '>', '/dev/null';
131 throws_ok {
132 $suggestion->managedby($nonexistent_borrowernumber)->store;
134 'Koha::Exceptions::Object::FKConstraint',
135 'store raises an exception on invalid managerid';
136 close STDERR;
138 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
139 $suggestion->managedby( $manager->borrowernumber )->store;
140 $manager->delete;
141 $suggestion = $suggestion->get_from_storage;
142 is( $suggestion->managedby, undef,
143 "The suggestion is not deleted when the related manager is deleted" );
145 # acceptedby
146 { # hide useless warnings
147 local *STDERR;
148 open STDERR, '>', '/dev/null';
149 throws_ok {
150 $suggestion->acceptedby($nonexistent_borrowernumber)->store;
152 'Koha::Exceptions::Object::FKConstraint',
153 'store raises an exception on invalid acceptedby id';
154 close STDERR;
156 my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
157 $suggestion->acceptedby( $acceptor->borrowernumber )->store;
158 $acceptor->delete;
159 $suggestion = $suggestion->get_from_storage;
160 is( $suggestion->acceptedby, undef,
161 "The suggestion is not deleted when the related acceptor is deleted" );
163 # rejectedby
164 { # hide useless warnings
165 local *STDERR;
166 open STDERR, '>', '/dev/null';
167 throws_ok {
168 $suggestion->rejectedby($nonexistent_borrowernumber)->store;
170 'Koha::Exceptions::Object::FKConstraint',
171 'store raises an exception on invalid rejectedby id';
172 close STDERR;
174 my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
175 $suggestion->rejectedby( $rejecter->borrowernumber )->store;
176 $rejecter->delete;
177 $suggestion = $suggestion->get_from_storage;
178 is( $suggestion->rejectedby, undef,
179 "The suggestion is not deleted when the related rejecter is deleted" );
181 # budgetid
182 { # hide useless warnings
183 local *STDERR;
184 open STDERR, '>', '/dev/null';
186 throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
187 'Koha::Exceptions::Object::FKConstraint',
188 'store raises an exception on invalid budgetid';
189 close STDERR;
191 my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
192 $suggestion->budgetid( $fund->id )->store;
193 $fund->delete;
194 $suggestion = $suggestion->get_from_storage;
195 is( $suggestion->budgetid, undef,
196 "The suggestion is not deleted when the related budget is deleted" );
198 $schema->storage->dbh->{PrintError} = $print_error;
199 $schema->storage->txn_rollback;
202 subtest 'manager, suggester, rejecter, last_modifier' => sub {
203 plan tests => 8;
204 $schema->storage->txn_begin;
206 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
208 is( ref( $suggestion->manager ),
209 'Koha::Patron',
210 '->manager should have returned a Koha::Patron object' );
211 is( ref( $suggestion->rejecter ),
212 'Koha::Patron',
213 '->rejecter should have returned a Koha::Patron object' );
214 is( ref( $suggestion->suggester ),
215 'Koha::Patron',
216 '->suggester should have returned a Koha::Patron object' );
217 is( ref( $suggestion->last_modifier ),
218 'Koha::Patron',
219 '->last_modifier should have returned a Koha::Patron object' );
221 $suggestion->set(
223 managedby => undef,
224 rejectedby => undef,
225 suggestedby => undef,
226 lastmodificationby => undef
230 is( $suggestion->manager, undef,
231 '->manager should have returned undef if no manager set' );
232 is( $suggestion->rejecter, undef,
233 '->rejecter should have returned undef if no rejecter set' );
234 is( $suggestion->suggester, undef,
235 '->suggester should have returned undef if no suggester set' );
236 is( $suggestion->last_modifier,
237 undef,
238 '->last_modifier should have returned undef if no last_modifier set' );
240 $schema->storage->txn_rollback;
243 subtest 'fund' => sub {
244 plan tests => 2;
246 $schema->storage->txn_begin;
248 my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
249 is( ref( $suggestion->fund ),
250 'Koha::Acquisition::Fund',
251 '->fund should have returned a Koha::Acquisition::Fund object' );
253 $suggestion->set( { budgetid => undef } );
255 is( $suggestion->fund, undef,
256 '->fund should have returned undef if not fund set' );
258 $schema->storage->txn_rollback;