View | Details | Raw Unified | Return to bug 29029
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Object/ColumnSet.t (-1 / +88 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use Data::Dumper qw/Dumper/;
20
use Test::MockModule;
21
use Test::More tests => 1;
22
23
use t::lib::TestBuilder;
24
25
use Koha::City;
26
use Koha::Logger;
27
use Koha::Object::ColumnSet;
28
use Koha::Schema::Result::City;
29
30
our $builder = t::lib::TestBuilder->new;
31
our $column_sets = {};
32
our $city_module = Test::MockModule->new( 'Koha::Schema::Result::City' );
33
our $logger_module = Test::MockModule->new( 'Koha::Logger' );
34
our $log_test;
35
36
subtest 'Test ColumnSet on City' => sub {
37
    plan tests => 14;
38
39
    my $city = $builder->build_object( { class => 'Koha::Cities' } );
40
    my $dump = {};
41
42
    # First test: feature not supported
43
    # This ASSUMES that City does not yet contain a get_column_set routine.
44
    my $count = keys %{$city->unblessed};
45
    is_deeply( $city->filter({ column_set => 'whatever' }), {}, 'No data expected' );
46
    is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, [],
47
        'No columns listed when feature not supported in schema' );
48
49
    # Now test bad supported feature (wrong return value)
50
    $city_module->mock( 'get_column_set', sub {} );
51
    is_deeply( $city->filter({ column_set => 'whatever' }), {}, 'No data for bad support' );
52
    is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, [],
53
        'list_columns should still be empty' );
54
55
    # What about empty column sets?
56
    $city_module->mock( 'get_column_set', sub { return $column_sets->{$_[1]} // []; } );
57
    is_deeply( $city->filter({ column_set => 'whatever' }), {}, 'Expected no fields' );
58
    is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, [],
59
        'list_columns should still be empty again' );
60
61
    # Now make it return some data
62
    $column_sets->{test} = [ 'aap', 'noot', 'mies' ];
63
    $column_sets->{opac_test} = [ 'city_name', 'city_state' ];
64
    is( keys %{$city->filter({ column_set => 'opac_test' })}, 2, 'Expected two fields' );
65
    is( keys %{$city->filter({ column_set => 'test' })}, 0, 'No data for unknown fields' );
66
    is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, $column_sets->{opac_test},
67
        'list_columns should return some columns now' );
68
69
    # Test input parameter, deny_mode 
70
    my $input = { city_id => 20, city_name => 'Rotterdam', aap => 2 };
71
    my $result = $city->filter({ input => $input, column_set => 'test' });
72
    is_deeply( $result, { aap => 2 }, 'Only one key got through' );
73
    $result = $city->filter({ input => $input, column_set => 'test', deny_mode => 1 });
74
    is_deeply( $result, { city_id => 20, city_name => 'Rotterdam' }, 'Two keys with deny_mode' );
75
76
    # Test dump parameter
77
    $result = $city->filter({ input => $input, column_set => 'test', dump => $dump });
78
    is_deeply( $dump, { city_id => 20, city_name => 'Rotterdam' }, 'Check dump variable' );
79
80
    # Test logging
81
    $log_test = 0;
82
    $logger_module->mock( 'warn', sub { $log_test++; } );
83
    $result = $city->filter({ input => $input, column_set => 'test', log_level => 'warn' });
84
    is( $log_test, 1, 'Filter triggered warn' );
85
    $result = $city->filter({ input => {}, column_set => 'test', log_level => 'warn' });
86
    is( $log_test, 1, 'Empty hash did not trigger warn again' );
87
88
};

Return to bug 29029