From d00842a44bf936c8029bdea74f0b04c7ebd47d88 Mon Sep 17 00:00:00 2001 From: Kyle Hall Date: Thu, 29 Sep 2022 09:05:07 -0400 Subject: [PATCH] Bug 24606: Add Koha Object(s) with unit tests Signed-off-by: David Nind Signed-off-by: Katrin Fischer --- Koha/Item/Template.pm | 69 +++++++++++++++++++++ Koha/Item/Templates.pm | 79 ++++++++++++++++++++++++ t/db_dependent/Koha/Item/Template.t | 48 +++++++++++++++ t/db_dependent/Koha/Item/Templates.t | 91 ++++++++++++++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 Koha/Item/Template.pm create mode 100644 Koha/Item/Templates.pm create mode 100755 t/db_dependent/Koha/Item/Template.t create mode 100755 t/db_dependent/Koha/Item/Templates.t diff --git a/Koha/Item/Template.pm b/Koha/Item/Template.pm new file mode 100644 index 0000000000..b2e04a57d2 --- /dev/null +++ b/Koha/Item/Template.pm @@ -0,0 +1,69 @@ +package Koha::Item::Template; + +# 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 JSON qw( encode_json decode_json ); + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Item::Template - Koha Item Template Object class + +=head1 API + +=head2 Class Methods + +=head3 store + +Override base store method +to serialize the template contents as JSON + +=cut + +sub store { + my ($self) = @_; + + if ( ref( $self->contents ) eq 'HASH' ) { + $self->contents( encode_json( $self->contents ) ); + } + + $self = $self->SUPER::store; +} + +=head3 decoded_contents + +Returns a deserilized perl structure of the JSON formatted contents + +=cut + +sub decoded_contents { + my ($self) = @_; + + return decode_json( $self->contents ) if $self->contents; +} + +=head3 type + +=cut + +sub _type { + return 'ItemEditorTemplate'; +} + +1; diff --git a/Koha/Item/Templates.pm b/Koha/Item/Templates.pm new file mode 100644 index 0000000000..2ebe3c8e28 --- /dev/null +++ b/Koha/Item/Templates.pm @@ -0,0 +1,79 @@ +package Koha::Item::Templates; + +# 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 Koha::Database; + +use Koha::Item::Template; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Item::Templates - Koha Item Template Object set class + +=head3 get_available + +Returns a hashref with keys 'owned' and 'shared' pointing to Koha::Item::Templats objects +representing templates owned by the user or shared to the user respectivetly. + +=cut + +sub get_available { + my ( $class, $borrowernumber ) = @_; + + my $params = { + order_by => 'name', + columns => [ 'id', 'borrowernumber', 'name', 'is_shared' ], + }; + + return { + owned => Koha::Item::Templates->search( + { + borrowernumber => $borrowernumber + }, + $params + ), + shared => Koha::Item::Templates->search( + { + borrowernumber => { "!=" => $borrowernumber }, + is_shared => 1 + }, + $params + ), + }; +} + +=head3 _type + +=cut + +sub _type { + return 'ItemEditorTemplate'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Item::Template'; +} + +1; diff --git a/t/db_dependent/Koha/Item/Template.t b/t/db_dependent/Koha/Item/Template.t new file mode 100755 index 0000000000..20eb6269b9 --- /dev/null +++ b/t/db_dependent/Koha/Item/Template.t @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +# Copyright 2022 Koha Development team +# +# 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 Koha::Database; + +use t::lib::TestBuilder; + +use Test::More tests => 2; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +use_ok("Koha::Item::Template"); + +subtest 'Serializing and deserializing contents' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + my $data = { location => 'test' }; + my $template = Koha::Item::Template->new({ + name => 'My template', + contents => $data, + })->store(); + + is( $template->contents, '{"location":"test"}', 'Contents serialized correctly' ); + is( $template->decoded_contents->{location}, 'test', 'Contents deserialized correctly' ); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/Koha/Item/Templates.t b/t/db_dependent/Koha/Item/Templates.t new file mode 100755 index 0000000000..5e8956395b --- /dev/null +++ b/t/db_dependent/Koha/Item/Templates.t @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +# Copyright 2022 Koha Development team +# +# 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 Koha::Database; + +use t::lib::TestBuilder; + +use Test::More tests => 2; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +use_ok("Koha::Item::Templates"); + +subtest 'get_available' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + Koha::Item::Templates->search->delete; + + my $library = $builder->build( { source => 'Branch' } ); + my $category = $builder->build( { source => 'Category' } ); + my $patron_1 = Koha::Patron->new( + { + branchcode => $library->{branchcode}, + categorycode => $category->{categorycode}, + surname => 'surname for patron1', + firstname => 'firstname for patron1', + } + )->store; + my $patron_2 = Koha::Patron->new( + { + branchcode => $library->{branchcode}, + categorycode => $category->{categorycode}, + surname => 'surname for patron2', + firstname => 'firstname for patron2', + } + )->store; + + my $owner_template = Koha::Item::Template->new( + { + borrowernumber => $patron_1->id, + name => 'My template', + contents => { location => 'test' }, + is_shared => 0, + } + )->store(); + + my $shared_template = Koha::Item::Template->new( + { + borrowernumber => $patron_2->id, + name => 'My template', + contents => { location => 'test' }, + is_shared => 1, + } + )->store(); + + my $unshared_template = Koha::Item::Template->new( + { + borrowernumber => $patron_2->id, + name => 'My template', + contents => { location => 'test' }, + is_shared => 0, + } + )->store(); + + my $templates = Koha::Item::Templates->get_available( $patron_1->id ); + is( $templates->{owned}->count, 1, "Got back one owned template" ); + is( $templates->{shared}->count, 1, "Got back one shared templated" ); + + $schema->storage->txn_rollback; +}; -- 2.30.2