Revert "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 => 13;
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 'update' => sub {
47     plan tests => 2;
48
49     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
50     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
51     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
52     $builder->build( { source => 'City', value => { city_country => 'France' } } );
53     $builder->build( { source => 'City', value => { city_country => 'France' } } );
54     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
55     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
56     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
57     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
58 };
59
60 subtest 'pager' => sub {
61     plan tests => 1;
62     my $pager = Koha::Patrons->search( {}, { page => 1, rows => 2 } )->pager;
63     is( ref($pager), 'DBIx::Class::ResultSet::Pager', 'Koha::Objects->pager returns a valid DBIx::Class object' );
64 };
65
66 subtest 'reset' => sub {
67     plan tests => 3;
68
69     my $patrons = Koha::Patrons->search;
70     my $first_borrowernumber = $patrons->next->borrowernumber;
71     my $second_borrowernumber = $patrons->next->borrowernumber;
72     is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
73     is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
74     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
75 };
76
77 subtest 'delete' => sub {
78     plan tests => 2;
79
80     my $patron_1 = $builder->build({source => 'Borrower'});
81     my $patron_2 = $builder->build({source => 'Borrower'});
82     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
83     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
84 };
85
86 subtest 'not_covered_yet' => sub {
87     plan tests => 1;
88     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";
89 };
90 subtest 'new' => sub {
91     plan tests => 2;
92     my $a_cat_code = 'A_CAT_CODE';
93     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
94     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
95     Koha::Patron::Categories->find($a_cat_code)->delete;
96     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
97     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' );
98     Koha::Patron::Categories->find($a_cat_code)->delete;
99 };
100
101 subtest 'search_related' => sub {
102     plan tests => 8;
103     my $builder   = t::lib::TestBuilder->new;
104     my $patron_1  = $builder->build( { source => 'Borrower' } );
105     my $patron_2  = $builder->build( { source => 'Borrower' } );
106     my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
107     is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
108     is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
109     is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
110     is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
111
112     my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
113     is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
114     is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
115     is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
116     is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
117 };
118
119 subtest 'single' => sub {
120     plan tests => 2;
121     my $builder   = t::lib::TestBuilder->new;
122     my $patron_1  = $builder->build( { source => 'Borrower' } );
123     my $patron_2  = $builder->build( { source => 'Borrower' } );
124     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
125     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
126     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
127     "Warning is presented if single is used for a result with multiple rows.";
128 };
129
130 subtest 'last' => sub {
131     plan tests => 3;
132     my $builder = t::lib::TestBuilder->new;
133     my $patron_1  = $builder->build( { source => 'Borrower' } );
134     my $patron_2  = $builder->build( { source => 'Borrower' } );
135     my $last_patron = Koha::Patrons->search->last;
136     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
137     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
138     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
139     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
140     is( $last_patron, undef, '->last should return undef if search does not return any results' );
141 };
142
143 subtest 'get_column' => sub {
144     plan tests => 1;
145     my @cities = Koha::Cities->search;
146     my @city_names = map { $_->city_name } @cities;
147     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
148 };
149
150 subtest 'Exceptions' => sub {
151     plan tests => 2;
152
153     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
154     my $patron = Koha::Patrons->find( $patron_borrowernumber );
155
156     try {
157         $patron->blah('blah');
158     } catch {
159         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
160             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
161     };
162
163     try {
164         $patron->set({ blah => 'blah' });
165     } catch {
166         ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
167             'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
168     };
169 };
170
171 $schema->storage->txn_rollback;
172 1;