3 # Copyright 2015 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>.
22 use Test::More tests => 6;
26 use Koha::Suggestions;
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( { 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},
45 my $new_suggestion_2 = Koha::Suggestion->new(
46 { suggestedby => $patron->{borrowernumber},
47 biblionumber => $biblio_2->{biblionumber},
51 subtest 'store' => sub {
53 my $suggestion = Koha::Suggestion->new(
54 { suggestedby => $patron->{borrowernumber},
55 biblionumber => $biblio_1->{biblionumber},
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' );
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 {
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",
97 suggestedby => $patron->borrowernumber,
98 biblionumber => $biblio->biblionumber,
99 branchcode => $branch->branchcode,
110 $suggestion = $suggestion->get_from_storage;
111 is( $suggestion->suggestedby, undef,
112 "The suggestion is not deleted when the related patron is deleted" );
116 $suggestion = $suggestion->get_from_storage;
117 is( $suggestion->biblionumber, undef,
118 "The suggestion is not deleted when the related biblio is deleted" );
122 $suggestion = $suggestion->get_from_storage;
123 is( $suggestion->branchcode, undef,
124 "The suggestion is not deleted when the related branch is deleted" );
127 throws_ok { $suggestion->managedby(1029384756)->store; }
128 'Koha::Exceptions::Object::FKConstraint',
129 'store raises an exception on invalid managerid';
130 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
131 $suggestion->managedby( $manager->borrowernumber )->store;
133 $suggestion = $suggestion->get_from_storage;
134 is( $suggestion->managedby, undef,
135 "The suggestion is not deleted when the related manager is deleted" );
138 throws_ok { $suggestion->acceptedby(1029384756)->store; }
139 'Koha::Exceptions::Object::FKConstraint',
140 'store raises an exception on invalid acceptedby id';
141 my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
142 $suggestion->acceptedby( $acceptor->borrowernumber )->store;
144 $suggestion = $suggestion->get_from_storage;
145 is( $suggestion->acceptedby, undef,
146 "The suggestion is not deleted when the related acceptor is deleted" );
149 throws_ok { $suggestion->rejectedby(1029384756)->store; }
150 'Koha::Exceptions::Object::FKConstraint',
151 'store raises an exception on invalid rejectedby id';
152 my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
153 $suggestion->rejectedby( $rejecter->borrowernumber )->store;
155 $suggestion = $suggestion->get_from_storage;
156 is( $suggestion->rejectedby, undef,
157 "The suggestion is not deleted when the related rejecter is deleted" );
160 throws_ok { $suggestion->budgetid(1029384756)->store; }
161 'Koha::Exceptions::Object::FKConstraint',
162 'store raises an exception on invalid budgetid';
163 my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
164 $suggestion->budgetid( $fund->id )->store;
166 $suggestion = $suggestion->get_from_storage;
167 is( $suggestion->budgetid, undef,
168 "The suggestion is not deleted when the related budget is deleted" );
170 $schema->storage->dbh->{PrintError} = $print_error;
171 $schema->storage->txn_rollback;