Bug 17091: In a first phase, restrict the usage of AUTOLOAD in Koha::Objects
[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 => 6;
23 use Test::Warn;
24
25 use Koha::Authority::Types;
26 use Koha::Cities;
27 use Koha::Patrons;
28 use Koha::Database;
29
30 use t::lib::TestBuilder;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
36
37 my @columns = Koha::Patrons->columns;
38 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
39 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
40
41 subtest 'update' => sub {
42     plan tests => 2;
43     my $builder = t::lib::TestBuilder->new;
44     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
45     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
46     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
47     $builder->build( { source => 'City', value => { city_country => 'France' } } );
48     $builder->build( { source => 'City', value => { city_country => 'France' } } );
49     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
50     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
51     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
52     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
53 };
54
55 subtest 'pager' => sub {
56     plan tests => 1;
57     my $pager = Koha::Patrons->search( {}, { page => 1, rows => 2 } )->pager;
58     is( ref($pager), 'DBIx::Class::ResultSet::Pager', 'Koha::Objects->pager returns a valid DBIx::Class object' );
59 };
60
61 subtest 'reset' => sub {
62     plan tests => 1;
63     my $builder   = t::lib::TestBuilder->new;
64     my $patrons = Koha::Patrons->search;
65     my $first_borrowernumber = $patrons->next->borrowernumber;
66     my $second_borrowernumber = $patrons->next->borrowernumber;
67     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
68 };
69
70 subtest 'not_covered_yet' => sub {
71     plan tests => 1;
72     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";
73 };
74
75 $schema->storage->txn_rollback;
76 1;