3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 16;
26 use Koha::Authority::Types;
28 use Koha::IssuingRules;
29 use Koha::Patron::Category;
30 use Koha::Patron::Categories;
34 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40 my $builder = t::lib::TestBuilder->new;
42 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
44 my @columns = Koha::Patrons->columns;
45 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
46 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
48 subtest 'find' => sub {
50 my $patron = $builder->build({source => 'Borrower'});
51 my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
52 is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
54 eval { my @patrons = Koha::Patrons->find( $patron->{borrowernumber} ); };
55 like( $@, qr|^Cannot use "->find" in list context|, "->find should not be called in list context to avoid side-effects" );
57 # Test sending undef to find; should not generate a warning
58 warning_is { $patron = Koha::Patrons->find( undef ); }
59 "", "Sending undef does not trigger a DBIx warning";
60 warning_is { $patron = Koha::Patrons->find( undef, undef ); }
61 "", "Sending two undefs does not trigger a DBIx warning too";
64 subtest 'update' => sub {
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 => 'UK' } } );
70 $builder->build( { source => 'City', value => { city_country => 'France' } } );
71 $builder->build( { source => 'City', value => { city_country => 'France' } } );
72 $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
73 Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
74 is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
75 is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
78 subtest 'reset' => sub {
81 my $patrons = Koha::Patrons->search;
82 my $first_borrowernumber = $patrons->next->borrowernumber;
83 my $second_borrowernumber = $patrons->next->borrowernumber;
84 is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
85 is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
86 is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
89 subtest 'delete' => sub {
92 my $patron_1 = $builder->build({source => 'Borrower'});
93 my $patron_2 = $builder->build({source => 'Borrower'});
94 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
95 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
98 subtest 'new' => sub {
100 my $a_cat_code = 'A_CAT_CODE';
101 my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
102 is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
103 Koha::Patron::Categories->find($a_cat_code)->delete;
104 $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
105 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' );
106 Koha::Patron::Categories->find($a_cat_code)->delete;
109 subtest 'find' => sub {
112 # check find on a single PK
113 my $patron = $builder->build({ source => 'Borrower' });
114 is( Koha::Patrons->find($patron->{borrowernumber})->surname,
115 $patron->{surname}, "Checking an arbitrary patron column after find"
117 # check find with unique column
118 my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
119 is( $obj->borrowernumber, $patron->{borrowernumber},
120 'Find with unique column and key specified' );
121 # check find with an additional where clause in the attrs hash
122 # we do not expect to find something now
123 is( Koha::Patrons->find(
124 $patron->{borrowernumber},
125 { where => { surname => { '!=', $patron->{surname} }}},
126 ), undef, 'Additional where clause in find call' );
128 # check find with a composite FK
129 my $rule = $builder->build({ source => 'Issuingrule' });
130 my @pk = ( $rule->{branchcode}, $rule->{categorycode}, $rule->{itemtype} );
131 is( ref(Koha::IssuingRules->find(@pk)), "Koha::IssuingRule",
132 'Find returned a Koha object for composite primary key' );
134 is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
137 subtest 'search_related' => sub {
139 my $builder = t::lib::TestBuilder->new;
140 my $patron_1 = $builder->build( { source => 'Borrower' } );
141 my $patron_2 = $builder->build( { source => 'Borrower' } );
142 my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
143 is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
144 is( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
145 is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
146 is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
148 my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
149 is( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
150 is( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
151 is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
152 is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
155 subtest 'single' => sub {
157 my $builder = t::lib::TestBuilder->new;
158 my $patron_1 = $builder->build( { source => 'Borrower' } );
159 my $patron_2 = $builder->build( { source => 'Borrower' } );
160 my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
161 is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
162 warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
163 "Warning is presented if single is used for a result with multiple rows.";
166 subtest 'last' => sub {
168 my $builder = t::lib::TestBuilder->new;
169 my $patron_1 = $builder->build( { source => 'Borrower' } );
170 my $patron_2 = $builder->build( { source => 'Borrower' } );
171 my $last_patron = Koha::Patrons->search->last;
172 is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
173 $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
174 is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
175 $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
176 is( $last_patron, undef, '->last should return undef if search does not return any results' );
179 subtest 'get_column' => sub {
181 my @cities = Koha::Cities->search;
182 my @city_names = map { $_->city_name } @cities;
183 is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
186 subtest 'Exceptions' => sub {
189 my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
190 my $patron = Koha::Patrons->find( $patron_borrowernumber );
194 $patron->blah('blah');
196 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
197 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
198 is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
202 $patron->set({ blah => 'blah' });
204 ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
205 'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
210 Koha::Patrons->search->not_covered_yet;
212 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
213 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
214 is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
218 Koha::Patrons->not_covered_yet;
220 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
221 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
222 is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
226 $schema->storage->txn_rollback;
228 subtest '->is_paged and ->pager tests' => sub {
232 $schema->storage->txn_begin;
234 # Delete existing patrons
235 Koha::Checkouts->delete;
236 Koha::Patrons->delete;
239 $builder->build_object({ class => 'Koha::Patrons' });
242 # Non-paginated search
243 my $patrons = Koha::Patrons->search();
244 is( $patrons->count, 10, 'Search returns all patrons' );
245 ok( !$patrons->is_paged, 'Search is not paged' );
248 $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
249 is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
250 ok( $patrons->is_paged, 'Search is paged' );
251 my $pager = $patrons->pager;
252 is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
253 'Koha::Objects->pager returns a valid DBIx::Class object' );
255 $schema->storage->txn_rollback;
258 subtest '->search() tests' => sub {
262 $schema->storage->txn_begin;
264 my $count = Koha::Patrons->search->count;
268 $builder->build_object({ class => 'Koha::Patrons' });
271 my $patrons = Koha::Patrons->search();
272 is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
273 my @patrons = Koha::Patrons->search();
274 is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
277 is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
281 $schema->storage->txn_rollback;
284 subtest "to_api() tests" => sub {
288 $schema->storage->txn_begin;
290 my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
291 my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
293 my $cities = Koha::Cities->search(
295 cityid => [ $city_1->cityid, $city_2->cityid ]
297 { -orderby => { -desc => 'cityid' } }
300 is( $cities->count, 2, 'Count is correct' );
301 my $cities_api = $cities->to_api;
302 is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
303 is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
304 is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
306 $schema->storage->txn_rollback;