f4c4974548755c9bb83c8dab2e1f3d7223a690ba
[koha.git] / t / db_dependent / Koha / Suggestions.t
1 #!/usr/bin/perl
2
3 # Copyright 2015-2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 8;
23 use Test::Exception;
24
25 use Koha::Suggestion;
26 use Koha::Suggestions;
27 use Koha::Database;
28 use Koha::DateUtils;
29
30 use t::lib::TestBuilder;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder           = t::lib::TestBuilder->new;
36 my $biblio_1          = $builder->build( { source => 'Biblio' } );
37 my $biblio_2          = $builder->build( { source => '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},
43     }
44 )->store;
45 my $new_suggestion_2 = Koha::Suggestion->new(
46     {   suggestedby  => $patron->{borrowernumber},
47         biblionumber => $biblio_2->{biblionumber},
48     }
49 )->store;
50
51 subtest 'store' => sub {
52     plan tests => 3;
53     my $suggestion  = Koha::Suggestion->new(
54         {   suggestedby  => $patron->{borrowernumber},
55             biblionumber => $biblio_1->{biblionumber},
56         }
57     )->store;
58
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;
69 };
70
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' );
73
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' );
76
77 $retrieved_suggestion_1->delete;
78 is( Koha::Suggestions->search->count, $nb_of_suggestions + 1, 'Delete should have deleted the suggestion' );
79
80 $schema->storage->txn_rollback;
81
82 subtest 'constraints' => sub {
83     plan tests => 11;
84     $schema->storage->txn_begin;
85
86     my $print_error = $schema->storage->dbh->{PrintError};
87     $schema->storage->dbh->{PrintError} = 0;
88
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" } );
92
93     my $suggestion = $builder->build_object(
94         {
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,
104             }
105         }
106     );
107
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" );
114
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" );
120
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" );
126
127     # managerid
128     throws_ok { $suggestion->managedby($nonexistent_borrowernumber)->store; }
129     'Koha::Exceptions::Object::FKConstraint',
130       'store raises an exception on invalid managerid';
131     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
132     $suggestion->managedby( $manager->borrowernumber )->store;
133     $manager->delete;
134     $suggestion = $suggestion->get_from_storage;
135     is( $suggestion->managedby, undef,
136         "The suggestion is not deleted when the related manager is deleted" );
137
138     # acceptedby
139     throws_ok { $suggestion->acceptedby($nonexistent_borrowernumber)->store; }
140     'Koha::Exceptions::Object::FKConstraint',
141       'store raises an exception on invalid acceptedby id';
142     my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
143     $suggestion->acceptedby( $acceptor->borrowernumber )->store;
144     $acceptor->delete;
145     $suggestion = $suggestion->get_from_storage;
146     is( $suggestion->acceptedby, undef,
147         "The suggestion is not deleted when the related acceptor is deleted" );
148
149     # rejectedby
150     throws_ok { $suggestion->rejectedby($nonexistent_borrowernumber)->store; }
151     'Koha::Exceptions::Object::FKConstraint',
152       'store raises an exception on invalid rejectedby id';
153     my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
154     $suggestion->rejectedby( $rejecter->borrowernumber )->store;
155     $rejecter->delete;
156     $suggestion = $suggestion->get_from_storage;
157     is( $suggestion->rejectedby, undef,
158         "The suggestion is not deleted when the related rejecter is deleted" );
159
160     # budgetid
161     throws_ok { $suggestion->budgetid($nonexistent_borrowernumber)->store; }
162     'Koha::Exceptions::Object::FKConstraint',
163       'store raises an exception on invalid budgetid';
164     my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
165     $suggestion->budgetid( $fund->id )->store;
166     $fund->delete;
167     $suggestion = $suggestion->get_from_storage;
168     is( $suggestion->budgetid, undef,
169         "The suggestion is not deleted when the related budget is deleted" );
170
171     $schema->storage->dbh->{PrintError} = $print_error;
172     $schema->storage->txn_rollback;
173 };
174
175 subtest 'manager, suggester, rejecter, last_modifier' => sub {
176     plan tests => 8;
177     $schema->storage->txn_begin;
178
179     my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
180
181     is( ref( $suggestion->manager ),
182         'Koha::Patron',
183         '->manager should have returned a Koha::Patron object' );
184     is( ref( $suggestion->rejecter ),
185         'Koha::Patron',
186         '->rejecter should have returned a Koha::Patron object' );
187     is( ref( $suggestion->suggester ),
188         'Koha::Patron',
189         '->suggester should have returned a Koha::Patron object' );
190     is( ref( $suggestion->last_modifier ),
191         'Koha::Patron',
192         '->last_modifier should have returned a Koha::Patron object' );
193
194     $suggestion->set(
195         {
196             managedby          => undef,
197             rejectedby         => undef,
198             suggestedby        => undef,
199             lastmodificationby => undef
200         }
201     );
202
203     is( $suggestion->manager, undef,
204         '->manager should have returned undef if no manager set' );
205     is( $suggestion->rejecter, undef,
206         '->rejecter should have returned undef if no rejecter set' );
207     is( $suggestion->suggester, undef,
208         '->suggester should have returned undef if no suggester set' );
209     is( $suggestion->last_modifier,
210         undef,
211         '->last_modifier should have returned undef if no last_modifier set' );
212
213     $schema->storage->txn_rollback;
214 };
215
216 subtest 'fund' => sub {
217     plan tests => 2;
218
219     $schema->storage->txn_begin;
220
221     my $suggestion = $builder->build_object( { class => 'Koha::Suggestions' } );
222     is( ref( $suggestion->fund ),
223         'Koha::Acquisition::Fund',
224         '->fund should have returned a Koha::Acquisition::Fund object' );
225
226     $suggestion->set( { budgetid => undef } );
227
228     is( $suggestion->fund, undef,
229         '->fund should have returned undef if not fund set' );
230
231     $schema->storage->txn_rollback;
232 };