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 $patron = $builder->build_object( { class => "Koha::Patrons" } );
87 my $biblio = $builder->build_sample_biblio();
88 my $branch = $builder->build_object( { class => "Koha::Libraries" } );
90 my $suggestion = $builder->build_object(
92 class => "Koha::Suggestions",
94 suggestedby => $patron->borrowernumber,
95 biblionumber => $biblio->biblionumber,
96 branchcode => $branch->branchcode,
107 $suggestion = $suggestion->get_from_storage;
108 is( $suggestion->suggestedby, undef,
109 "The suggestion is not deleted when the related patron is deleted" );
113 $suggestion = $suggestion->get_from_storage;
114 is( $suggestion->biblionumber, undef,
115 "The suggestion is not deleted when the related biblio is deleted" );
119 $suggestion = $suggestion->get_from_storage;
120 is( $suggestion->branchcode, undef,
121 "The suggestion is not deleted when the related branch is deleted" );
124 throws_ok { $suggestion->managedby(1029384756)->store; }
125 'Koha::Exceptions::Object::FKConstraint',
126 'store raises an exception on invalid managerid';
127 my $manager = $builder->build_object( { class => "Koha::Patrons" } );
128 $suggestion->managedby( $manager->borrowernumber )->store;
130 $suggestion = $suggestion->get_from_storage;
131 is( $suggestion->managedby, undef,
132 "The suggestion is not deleted when the related manager is deleted" );
135 throws_ok { $suggestion->acceptedby(1029384756)->store; }
136 'Koha::Exceptions::Object::FKConstraint',
137 'store raises an exception on invalid acceptedby id';
138 my $acceptor = $builder->build_object( { class => "Koha::Patrons" } );
139 $suggestion->acceptedby( $acceptor->borrowernumber )->store;
141 $suggestion = $suggestion->get_from_storage;
142 is( $suggestion->acceptedby, undef,
143 "The suggestion is not deleted when the related acceptor is deleted" );
146 throws_ok { $suggestion->rejectedby(1029384756)->store; }
147 'Koha::Exceptions::Object::FKConstraint',
148 'store raises an exception on invalid rejectedby id';
149 my $rejecter = $builder->build_object( { class => "Koha::Patrons" } );
150 $suggestion->rejectedby( $rejecter->borrowernumber )->store;
152 $suggestion = $suggestion->get_from_storage;
153 is( $suggestion->rejectedby, undef,
154 "The suggestion is not deleted when the related rejecter is deleted" );
157 throws_ok { $suggestion->budgetid(1029384756)->store; }
158 'Koha::Exceptions::Object::FKConstraint',
159 'store raises an exception on invalid budgetid';
160 my $fund = $builder->build_object( { class => "Koha::Acquisition::Funds" } );
161 $suggestion->budgetid( $fund->id )->store;
163 $suggestion = $suggestion->get_from_storage;
164 is( $suggestion->budgetid, undef,
165 "The suggestion is not deleted when the related budget is deleted" );
167 $schema->storage->txn_rollback;