Bug 24606: Add Koha Object(s) with unit tests

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Kyle Hall 2022-09-29 09:05:07 -04:00 committed by Tomas Cohen Arazi
parent 16f0117e55
commit c5a3b14bf8
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
4 changed files with 287 additions and 0 deletions

69
Koha/Item/Template.pm Normal file
View file

@ -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 <http://www.gnu.org/licenses>.
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;

79
Koha/Item/Templates.pm Normal file
View file

@ -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 <http://www.gnu.org/licenses>.
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;

View file

@ -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 <http://www.gnu.org/licenses>.
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;
};

View file

@ -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 <http://www.gnu.org/licenses>.
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;
};