From b6f9d71490fd278fb7001c664a3c73690c28ec92 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Fri, 1 Dec 2023 22:23:49 +0000 Subject: [PATCH] Bug 35471: Add Koha::Template::Plugin::Frameworks->GetChildren() To test: 1. prove t/db_dependent/Koha/Template/Plugin/Frameworks.t Sponsored-by: Hypernova Oy --- Koha/Template/Plugin/Frameworks.pm | 21 +++++ .../Koha/Template/Plugin/Frameworks.t | 81 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 t/db_dependent/Koha/Template/Plugin/Frameworks.t diff --git a/Koha/Template/Plugin/Frameworks.pm b/Koha/Template/Plugin/Frameworks.pm index 01305455be..49c8c92b55 100644 --- a/Koha/Template/Plugin/Frameworks.pm +++ b/Koha/Template/Plugin/Frameworks.pm @@ -56,4 +56,25 @@ sub GetName { return $frameworktext; } +=head3 GetChildren + +[% Frameworks.GetChildren( frameworkcode ) %] + +Return the child frameworks ordered by frameworktext + +=cut + +sub GetChildren { + my ( $self, $frameworkcode ) = @_; + return [] unless defined $frameworkcode; + return [] if $frameworkcode eq q{}; + + my $child_frameworks = Koha::BiblioFrameworks->search( + { parent_frameworkcode => $frameworkcode }, + { order_by => ['frameworktext'], } + )->unblessed; + + return $child_frameworks; +} + 1; diff --git a/t/db_dependent/Koha/Template/Plugin/Frameworks.t b/t/db_dependent/Koha/Template/Plugin/Frameworks.t new file mode 100755 index 0000000000..e5d1e62842 --- /dev/null +++ b/t/db_dependent/Koha/Template/Plugin/Frameworks.t @@ -0,0 +1,81 @@ +#!/usr/bin/env perl + +# Copyright 2023 Hypernova Oy +# +# 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 Test::More tests => 2; + +use t::lib::TestBuilder; + +use Koha::Database; +use Koha::Template::Plugin::Frameworks; + +BEGIN { + use_ok('Koha::Template::Plugin::Frameworks'); +} + +my $builder = t::lib::TestBuilder->new; + +my $schema = Koha::Database->new->schema; + +subtest 'GetChildren() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $parent_framework = $builder->build_object( { class => 'Koha::BiblioFrameworks' } ); + + my $plugin = Koha::Template::Plugin::Frameworks->new(); + + is( scalar @{ $plugin->GetChildren() }, 0, 'No child frameworks' ); + is( scalar @{ $plugin->GetChildren( $parent_framework->frameworkcode ) }, 0, 'No child frameworks' ); + + my $child_framework = $builder->build_object( + { + class => 'Koha::BiblioFrameworks', + value => { parent_frameworkcode => $parent_framework->frameworkcode, frameworktext => 'afirst' } + } + ); + is( scalar @{ $plugin->GetChildren( $parent_framework->frameworkcode ) }, 1, 'One child framework' ); + is( + $plugin->GetChildren( $parent_framework->frameworkcode )->[0]->{'frameworkcode'}, + $child_framework->frameworkcode, 'No child frameworks' + ); + my $child_framework2 = $builder->build_object( + { + class => 'Koha::BiblioFrameworks', + value => { parent_frameworkcode => $parent_framework->frameworkcode, frameworktext => 'bsecond' } + } + ); + is( scalar @{ $plugin->GetChildren( $parent_framework->frameworkcode ) }, 2, 'Two child frameworks' ); + + # note: elements returned by GetChildren() are ordered by frameworktext + # the two following tests depend on this fact + is( + $plugin->GetChildren( $parent_framework->frameworkcode )->[0]->{'frameworkcode'}, + $child_framework->frameworkcode, 'First child framework found' + ); + is( + $plugin->GetChildren( $parent_framework->frameworkcode )->[1]->{'frameworkcode'}, + $child_framework2->frameworkcode, 'Second child framework found' + ); + + $schema->storage->txn_rollback; +}; -- 2.34.1