Bug 18361: Additional tests for Koha::Objects->find
[koha.git] / t / db_dependent / Koha / Objects.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 => 15;
23 use Test::Warn;
24
25 use Koha::Authority::Types;
26 use Koha::Cities;
27 use Koha::IssuingRules;
28 use Koha::Patron::Category;
29 use Koha::Patron::Categories;
30 use Koha::Patrons;
31 use Koha::Database;
32
33 use t::lib::TestBuilder;
34
35 use Try::Tiny;
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new;
40
41 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
42
43 my @columns = Koha::Patrons->columns;
44 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
45 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
46
47 subtest 'find' => sub {
48     plan tests => 2;
49     my $patron = $builder->build({source => 'Borrower'});
50     my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
51     is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
52
53     eval { my @patrons = Koha::Patrons->find( $patron->{borrowernumber} ); };
54     like( $@, qr|^Cannot use "->find" in list context|, "->find should not be called in list context to avoid side-effects" );
55 };
56
57 subtest 'update' => sub {
58     plan tests => 2;
59
60     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
61     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
62     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
63     $builder->build( { source => 'City', value => { city_country => 'France' } } );
64     $builder->build( { source => 'City', value => { city_country => 'France' } } );
65     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
66     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
67     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
68     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
69 };
70
71 subtest 'pager' => sub {
72     plan tests => 1;
73     my $pager = Koha::Patrons->search( {}, { page => 1, rows => 2 } )->pager;
74     is( ref($pager), 'DBIx::Class::ResultSet::Pager', 'Koha::Objects->pager returns a valid DBIx::Class object' );
75 };
76
77 subtest 'reset' => sub {
78     plan tests => 3;
79
80     my $patrons = Koha::Patrons->search;
81     my $first_borrowernumber = $patrons->next->borrowernumber;
82     my $second_borrowernumber = $patrons->next->borrowernumber;
83     is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
84     is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
85     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
86 };
87
88 subtest 'delete' => sub {
89     plan tests => 2;
90
91     my $patron_1 = $builder->build({source => 'Borrower'});
92     my $patron_2 = $builder->build({source => 'Borrower'});
93     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
94     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
95 };
96
97 subtest 'not_covered_yet' => sub {
98     plan tests => 1;
99     warning_is { Koha::Patrons->search->not_covered_yet } { carped => 'The method not_covered_yet is not covered by tests' }, "If a method is not covered by tests, the AUTOLOAD method won't execute the method";
100 };
101 subtest 'new' => sub {
102     plan tests => 2;
103     my $a_cat_code = 'A_CAT_CODE';
104     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
105     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
106     Koha::Patron::Categories->find($a_cat_code)->delete;
107     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
108     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' );
109     Koha::Patron::Categories->find($a_cat_code)->delete;
110 };
111
112 subtest 'find' => sub {
113     plan tests => 4;
114
115     # check find on a single PK
116     my $patron = $builder->build({ source => 'Borrower' });
117     is( Koha::Patrons->find($patron->{borrowernumber})->surname,
118         $patron->{surname}, "Checking an arbitrary patron column after find"
119     );
120     # check find with unique column
121     my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
122     is( $obj->borrowernumber, $patron->{borrowernumber},
123         'Find with unique column and key specified' );
124     # check find with an additional where clause in the attrs hash
125     # we do not expect to find something now
126     is( Koha::Patrons->find(
127         $patron->{borrowernumber},
128         { where => { surname => { '!=', $patron->{surname} }}},
129     ), undef, 'Additional where clause in find call' );
130
131     # check find with a composite FK
132     my $rule = $builder->build({ source => 'Issuingrule' });
133     my @pk = ( $rule->{branchcode}, $rule->{categorycode}, $rule->{itemtype} );
134     is( ref(Koha::IssuingRules->find(@pk)), "Koha::IssuingRule",
135         'Find returned a Koha object for composite primary key' );
136 };
137
138 subtest 'search_related' => sub {
139     plan tests => 8;
140     my $builder   = t::lib::TestBuilder->new;
141     my $patron_1  = $builder->build( { source => 'Borrower' } );
142     my $patron_2  = $builder->build( { source => 'Borrower' } );
143     my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
144     is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
145     is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
146     is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
147     is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
148
149     my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
150     is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
151     is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
152     is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
153     is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
154 };
155
156 subtest 'single' => sub {
157     plan tests => 2;
158     my $builder   = t::lib::TestBuilder->new;
159     my $patron_1  = $builder->build( { source => 'Borrower' } );
160     my $patron_2  = $builder->build( { source => 'Borrower' } );
161     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
162     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
163     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
164     "Warning is presented if single is used for a result with multiple rows.";
165 };
166
167 subtest 'last' => sub {
168     plan tests => 3;
169     my $builder = t::lib::TestBuilder->new;
170     my $patron_1  = $builder->build( { source => 'Borrower' } );
171     my $patron_2  = $builder->build( { source => 'Borrower' } );
172     my $last_patron = Koha::Patrons->search->last;
173     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
174     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
175     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
176     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
177     is( $last_patron, undef, '->last should return undef if search does not return any results' );
178 };
179
180 subtest 'get_column' => sub {
181     plan tests => 1;
182     my @cities = Koha::Cities->search;
183     my @city_names = map { $_->city_name } @cities;
184     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
185 };
186
187 subtest 'Exceptions' => sub {
188     plan tests => 2;
189
190     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
191     my $patron = Koha::Patrons->find( $patron_borrowernumber );
192
193     try {
194         $patron->blah('blah');
195     } catch {
196         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
197             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
198     };
199
200     try {
201         $patron->set({ blah => 'blah' });
202     } catch {
203         ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
204             'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
205     };
206 };
207
208 $schema->storage->txn_rollback;
209 1;