d9533f52f92a08396145b26390422c6c52ec6e48
[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 => 4;
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     # Test sending undef to find; should not generate a warning
57     warning_is { $patron = Koha::Patrons->find( undef ); }
58         "", "Sending undef does not trigger a DBIx warning";
59     warning_is { $patron = Koha::Patrons->find( undef, undef ); }
60         "", "Sending two undefs does not trigger a DBIx warning too";
61 };
62
63 subtest 'update' => sub {
64     plan tests => 2;
65
66     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
67     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
68     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
69     $builder->build( { source => 'City', value => { city_country => 'France' } } );
70     $builder->build( { source => 'City', value => { city_country => 'France' } } );
71     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
72     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
73     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
74     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
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 'new' => sub {
98     plan tests => 2;
99     my $a_cat_code = 'A_CAT_CODE';
100     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
101     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
102     Koha::Patron::Categories->find($a_cat_code)->delete;
103     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
104     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' );
105     Koha::Patron::Categories->find($a_cat_code)->delete;
106 };
107
108 subtest 'find' => sub {
109     plan tests => 5;
110
111     # check find on a single PK
112     my $patron = $builder->build({ source => 'Borrower' });
113     is( Koha::Patrons->find($patron->{borrowernumber})->surname,
114         $patron->{surname}, "Checking an arbitrary patron column after find"
115     );
116     # check find with unique column
117     my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
118     is( $obj->borrowernumber, $patron->{borrowernumber},
119         'Find with unique column and key specified' );
120     # check find with an additional where clause in the attrs hash
121     # we do not expect to find something now
122     is( Koha::Patrons->find(
123         $patron->{borrowernumber},
124         { where => { surname => { '!=', $patron->{surname} }}},
125     ), undef, 'Additional where clause in find call' );
126
127     # check find with a composite FK
128     my $rule = $builder->build({ source => 'Issuingrule' });
129     my @pk = ( $rule->{branchcode}, $rule->{categorycode}, $rule->{itemtype} );
130     is( ref(Koha::IssuingRules->find(@pk)), "Koha::IssuingRule",
131         'Find returned a Koha object for composite primary key' );
132
133     is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
134 };
135
136 subtest 'search_related' => sub {
137     plan tests => 8;
138     my $builder   = t::lib::TestBuilder->new;
139     my $patron_1  = $builder->build( { source => 'Borrower' } );
140     my $patron_2  = $builder->build( { source => 'Borrower' } );
141     my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
142     is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
143     is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
144     is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
145     is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
146
147     my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
148     is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
149     is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
150     is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
151     is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
152 };
153
154 subtest 'single' => sub {
155     plan tests => 2;
156     my $builder   = t::lib::TestBuilder->new;
157     my $patron_1  = $builder->build( { source => 'Borrower' } );
158     my $patron_2  = $builder->build( { source => 'Borrower' } );
159     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
160     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
161     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
162     "Warning is presented if single is used for a result with multiple rows.";
163 };
164
165 subtest 'last' => sub {
166     plan tests => 3;
167     my $builder = t::lib::TestBuilder->new;
168     my $patron_1  = $builder->build( { source => 'Borrower' } );
169     my $patron_2  = $builder->build( { source => 'Borrower' } );
170     my $last_patron = Koha::Patrons->search->last;
171     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
172     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
173     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
174     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
175     is( $last_patron, undef, '->last should return undef if search does not return any results' );
176 };
177
178 subtest 'get_column' => sub {
179     plan tests => 1;
180     my @cities = Koha::Cities->search;
181     my @city_names = map { $_->city_name } @cities;
182     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
183 };
184
185 subtest 'Exceptions' => sub {
186     plan tests => 7;
187
188     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
189     my $patron = Koha::Patrons->find( $patron_borrowernumber );
190
191     # Koha::Object
192     try {
193         $patron->blah('blah');
194     } catch {
195         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
196             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
197         is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
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     # Koha::Objects
208     try {
209         Koha::Patrons->search->not_covered_yet;
210     } catch {
211         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
212             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
213         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
214     };
215
216     try {
217         Koha::Patrons->not_covered_yet;
218     } catch {
219         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
220             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
221         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
222     };
223 };
224
225 $schema->storage->txn_rollback;
226
227 subtest '->is_paged and ->pager tests' => sub {
228
229     plan tests => 5;
230
231     $schema->storage->txn_begin;
232
233     # Delete existing patrons
234     Koha::Checkouts->delete;
235     Koha::Patrons->delete;
236     # Create 10 patrons
237     foreach (1..10) {
238         $builder->build_object({ class => 'Koha::Patrons' });
239     }
240
241     # Non-paginated search
242     my $patrons = Koha::Patrons->search();
243     is( $patrons->count, 10, 'Search returns all patrons' );
244     ok( !$patrons->is_paged, 'Search is not paged' );
245
246     # Paginated search
247     $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
248     is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
249     ok( $patrons->is_paged, 'Search is paged' );
250     my $pager = $patrons->pager;
251     is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
252        'Koha::Objects->pager returns a valid DBIx::Class object' );
253
254     $schema->storage->txn_rollback;
255 };
256
257 subtest '->search() tests' => sub {
258
259     plan tests => 12;
260
261     $schema->storage->txn_begin;
262
263     Koha::Patrons->delete;
264
265     # Create 10 patrons
266     foreach (1..10) {
267         $builder->build_object({ class => 'Koha::Patrons' });
268     }
269
270     my $patrons = Koha::Patrons->search();
271     is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
272     my @patrons = Koha::Patrons->search();
273     is( scalar @patrons, 10, 'search in list context returns a list of objects' );
274     my $i = 0;
275     foreach (1..10) {
276         is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
277         $i++;
278     }
279
280     $schema->storage->txn_rollback;
281 };