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