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 => 10;
25 use Koha::Authority::Types;
27 use Koha::Patron::Category;
28 use Koha::Patron::Categories;
32 use t::lib::TestBuilder;
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
38 my $builder = t::lib::TestBuilder->new;
40 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
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' );
46 subtest 'update' => sub {
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' );
60 subtest 'pager' => sub {
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' );
66 subtest 'reset' => sub {
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');
77 subtest 'delete' => sub {
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, '');
86 subtest 'not_covered_yet' => sub {
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";
90 subtest 'new' => sub {
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;
101 subtest 'search_related' => sub {
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' );
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' );
119 subtest 'Exceptions' => sub {
122 my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
123 my $patron = Koha::Patrons->find( $patron_borrowernumber );
126 $patron->blah('blah');
128 ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
129 'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
133 $patron->set({ blah => 'blah' });
135 ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
136 'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
140 $schema->storage->txn_rollback;