From 544c90f638e5e25390616370246afc406df230e6 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 15 Sep 2021 14:59:21 +0000 Subject: [PATCH] Bug 29029: Add tests Content-Type: text/plain; charset=utf-8 --- t/db_dependent/Koha/Object/ColumnSet.t | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 t/db_dependent/Koha/Object/ColumnSet.t diff --git a/t/db_dependent/Koha/Object/ColumnSet.t b/t/db_dependent/Koha/Object/ColumnSet.t new file mode 100755 index 0000000000..c289f1f6f5 --- /dev/null +++ b/t/db_dependent/Koha/Object/ColumnSet.t @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Data::Dumper qw/Dumper/; +use Test::MockModule; +use Test::More tests => 1; + +use t::lib::TestBuilder; + +use Koha::City; +use Koha::Logger; +use Koha::Object::ColumnSet; +use Koha::Schema::Result::City; + +our $builder = t::lib::TestBuilder->new; +our $column_sets = {}; +our $city_module = Test::MockModule->new( 'Koha::Schema::Result::City' ); +our $logger_module = Test::MockModule->new( 'Koha::Logger' ); +our $log_test; + +subtest 'Test ColumnSet on City' => sub { + plan tests => 14; + + my $city = $builder->build_object( { class => 'Koha::Cities' } ); + my $dump = {}; + + # First test: feature not supported + # This ASSUMES that City does not yet contain a get_column_set routine. + my $count = keys %{$city->unblessed}; + is_deeply( $city->filter({ column_set => 'whatever' }), {}, 'No data expected' ); + is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, [], + 'No columns listed when feature not supported in schema' ); + + # Now test bad supported feature (wrong return value) + $city_module->mock( 'get_column_set', sub {} ); + is_deeply( $city->filter({ column_set => 'whatever' }), {}, 'No data for bad support' ); + is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, [], + 'list_columns should still be empty' ); + + # What about empty column sets? + $city_module->mock( 'get_column_set', sub { return $column_sets->{$_[1]} // []; } ); + is_deeply( $city->filter({ column_set => 'whatever' }), {}, 'Expected no fields' ); + is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, [], + 'list_columns should still be empty again' ); + + # Now make it return some data + $column_sets->{test} = [ 'aap', 'noot', 'mies' ]; + $column_sets->{opac_test} = [ 'city_name', 'city_state' ]; + is( keys %{$city->filter({ column_set => 'opac_test' })}, 2, 'Expected two fields' ); + is( keys %{$city->filter({ column_set => 'test' })}, 0, 'No data for unknown fields' ); + is_deeply( Koha::Object::ColumnSet->new({ name => 'opac_test', rs => $city->_result })->list_columns, $column_sets->{opac_test}, + 'list_columns should return some columns now' ); + + # Test input parameter, deny_mode + my $input = { city_id => 20, city_name => 'Rotterdam', aap => 2 }; + my $result = $city->filter({ input => $input, column_set => 'test' }); + is_deeply( $result, { aap => 2 }, 'Only one key got through' ); + $result = $city->filter({ input => $input, column_set => 'test', deny_mode => 1 }); + is_deeply( $result, { city_id => 20, city_name => 'Rotterdam' }, 'Two keys with deny_mode' ); + + # Test dump parameter + $result = $city->filter({ input => $input, column_set => 'test', dump => $dump }); + is_deeply( $dump, { city_id => 20, city_name => 'Rotterdam' }, 'Check dump variable' ); + + # Test logging + $log_test = 0; + $logger_module->mock( 'warn', sub { $log_test++; } ); + $result = $city->filter({ input => $input, column_set => 'test', log_level => 'warn' }); + is( $log_test, 1, 'Filter triggered warn' ); + $result = $city->filter({ input => {}, column_set => 'test', log_level => 'warn' }); + is( $log_test, 1, 'Empty hash did not trigger warn again' ); + +}; -- 2.20.1