Bugzilla – Attachment 141718 Details for
Bug 24606
Allow storing item values as a template for creating new items
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 24606: Add Koha Object(s) with unit tests
Bug-24606-Add-Koha-Objects-with-unit-tests.patch (text/plain), 8.96 KB, created by
Kyle M Hall (khall)
on 2022-10-12 10:58:12 UTC
(
hide
)
Description:
Bug 24606: Add Koha Object(s) with unit tests
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2022-10-12 10:58:12 UTC
Size:
8.96 KB
patch
obsolete
>From b0d8d40f29c6398a1ae58cceb8c161c0bb3bd55e Mon Sep 17 00:00:00 2001 >From: Kyle Hall <kyle@bywatersolutions.com> >Date: Thu, 29 Sep 2022 09:05:07 -0400 >Subject: [PATCH] Bug 24606: Add Koha Object(s) with unit tests > >--- > 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 <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; >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 <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; >diff --git a/t/db_dependent/Koha/Item/Template.t b/t/db_dependent/Koha/Item/Template.t >new file mode 100755 >index 0000000000..710c03f8df >--- /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 <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; >+}; >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 <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; >+}; >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 24606
:
141613
|
141614
|
141615
|
141616
|
141617
|
141717
|
141718
|
141719
|
141720
|
141721
|
141972
|
142003
|
142004
|
142005
|
142006
|
142007
|
142008
|
142833
|
142834
|
142835
|
142836
|
142837
|
142877
|
142878
|
142879
|
142880
|
142881
|
142882
|
142883
|
142884
|
142885
|
142886
|
142887
|
142888
|
143020
|
143569
|
143570
|
143571
|
143572
|
143573
|
143574
|
143575
|
143576
|
143577
|
143578
|
143579
|
143580
|
143581
|
143794
|
143795
|
143796
|
143989
|
143990
|
144161