Bug 22368: Add tests
[koha.git] / t / db_dependent / Koha / Suggestions.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 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 => 6;
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 $patron = $builder->build_object( { class => "Koha::Patrons" } );
87     my $biblio = $builder->build_sample_biblio();
88     my $branch = $builder->build_object( { class => "Koha::Libraries" } );
89
90     my $suggestion = $builder->build_object(
91         {
92             class => "Koha::Suggestions",
93             value => {
94                 suggestedby  => $patron->borrowernumber,
95                 biblionumber => $biblio->biblionumber,
96                 branchcode   => $branch->branchcode,
97                 managedby    => undef,
98                 acceptedby   => undef,
99                 rejectedby   => undef,
100                 budgetid     => undef,
101             }
102         }
103     );
104
105     # suggestedby
106     $patron->delete;
107     $suggestion = $suggestion->get_from_storage;
108     is( $suggestion->suggestedby, undef,
109         "The suggestion is not deleted when the related patron is deleted" );
110
111     # biblionumber
112     $biblio->delete;
113     $suggestion = $suggestion->get_from_storage;
114     is( $suggestion->biblionumber, undef,
115         "The suggestion is not deleted when the related biblio is deleted" );
116
117     # branchcode
118     $branch->delete;
119     $suggestion = $suggestion->get_from_storage;
120     is( $suggestion->branchcode, undef,
121         "The suggestion is not deleted when the related branch is deleted" );
122
123     # managerid
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;
129     $manager->delete;
130     $suggestion = $suggestion->get_from_storage;
131     is( $suggestion->managedby, undef,
132         "The suggestion is not deleted when the related manager is deleted" );
133
134     # acceptedby
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;
140     $acceptor->delete;
141     $suggestion = $suggestion->get_from_storage;
142     is( $suggestion->acceptedby, undef,
143         "The suggestion is not deleted when the related acceptor is deleted" );
144
145     # rejectedby
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;
151     $rejecter->delete;
152     $suggestion = $suggestion->get_from_storage;
153     is( $suggestion->rejectedby, undef,
154         "The suggestion is not deleted when the related rejecter is deleted" );
155
156     # budgetid
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;
162     $fund->delete;
163     $suggestion = $suggestion->get_from_storage;
164     is( $suggestion->budgetid, undef,
165         "The suggestion is not deleted when the related budget is deleted" );
166
167     $schema->storage->txn_rollback;
168 };