From ede066a469b6fa98b3293aee1faf12f2c8408a89 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 25 Oct 2022 13:07:44 +0100 Subject: [PATCH] Bug 31028: Add new Koha::Object(s) classes This patch adds new Koha::Object(s) for the newly introduced tables, including updateing existing Koha::Objects adding new relations as required. Signed-off-by: David Nind Signed-off-by: Helen Oliver Signed-off-by: Kyle M Hall --- Koha/Biblio.pm | 15 +++ Koha/Ticket.pm | 110 +++++++++++++++++++++ Koha/Ticket/Update.pm | 68 +++++++++++++ Koha/Ticket/Updates.pm | 48 +++++++++ Koha/Tickets.pm | 50 ++++++++++ t/db_dependent/Koha/Biblio.t | 30 +++++- t/db_dependent/Koha/Ticket.t | 147 ++++++++++++++++++++++++++++ t/db_dependent/Koha/Ticket/Update.t | 74 ++++++++++++++ 8 files changed, 541 insertions(+), 1 deletion(-) create mode 100644 Koha/Ticket.pm create mode 100644 Koha/Ticket/Update.pm create mode 100644 Koha/Ticket/Updates.pm create mode 100644 Koha/Tickets.pm create mode 100644 t/db_dependent/Koha/Ticket.t create mode 100755 t/db_dependent/Koha/Ticket/Update.t diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 5bf369cd6a..6f74ce9741 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -48,6 +48,7 @@ use Koha::Subscriptions; use Koha::SearchEngine; use Koha::SearchEngine::Search; use Koha::SearchEngine::QueryBuilder; +use Koha::Tickets; =head1 NAME @@ -119,6 +120,20 @@ sub active_orders { return $self->orders->search({ datecancellationprinted => undef }); } +=head3 tickets + + my $tickets = $biblio->tickets(); + +Returns all tickets linked to the biblio + +=cut + +sub tickets { + my ( $self ) = @_; + my $rs = $self->_result->tickets; + return Koha::Tickets->_new_from_dbic( $rs ); +} + =head3 item_groups my $item_groups = $biblio->item_groups(); diff --git a/Koha/Ticket.pm b/Koha/Ticket.pm new file mode 100644 index 0000000000..408c818319 --- /dev/null +++ b/Koha/Ticket.pm @@ -0,0 +1,110 @@ +package Koha::Ticket; + +# 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 base qw(Koha::Object); + +use Koha::Ticket::Update; +use Koha::Ticket::Updates; + +=head1 NAME + +Koha::Ticket - Koha Ticket Object class + +=head1 API + +=head2 Relations + +=cut + +=head3 reporter + +Return the patron who submitted this ticket + +=cut + +sub reporter { + my ($self) = @_; + my $rs = $self->_result->reporter; + return unless $rs; + return Koha::Patron->_new_from_dbic($rs); +} + +=head3 resolver + +Return the user who resolved this ticket + +=cut + +sub resolver { + my ($self) = @_; + my $rs = $self->_result->resolver; + return unless $rs; + return Koha::Patron->_new_from_dbic($rs) if $rs; +} + +=head3 biblio + +Return the biblio linked to this ticket + +=cut + +sub biblio { + my ($self) = @_; + my $rs = $self->_result->biblio; + return unless $rs; + return Koha::Biblio->_new_from_dbic($rs); +} + +=head3 updates + +Return any updates attached to this ticket + +=cut + +sub updates { + my ($self) = @_; + my $rs = $self->_result->ticket_updates; + return unless $rs; + return Koha::Ticket::Updates->_new_from_dbic($rs) if $rs; +} + +=head2 Actions + +=head3 add_update + +=cut + +sub add_update { + my ( $self, $params ) = @_; + + my $rs = $self->_result->add_to_ticket_updates($params)->discard_changes; + return Koha::Ticket::Update->_new_from_dbic($rs); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'Ticket'; +} + +1; diff --git a/Koha/Ticket/Update.pm b/Koha/Ticket/Update.pm new file mode 100644 index 0000000000..b954d59f03 --- /dev/null +++ b/Koha/Ticket/Update.pm @@ -0,0 +1,68 @@ +package Koha::Ticket::Update; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::Ticket::Update - Koha Ticket Update Object class + +=head1 API + +=head2 Relations + +=cut + +=head3 ticket + +Return the ticket this update relates to + +=cut + +sub ticket { + my ($self) = @_; + my $rs = $self->_result->ticket; + return unless $rs; + return Koha::Ticket->_new_from_dbic($rs); +} + +=head3 user + +Return the patron who submitted this update + +=cut + +sub user { + my ($self) = @_; + my $rs = $self->_result->user; + return unless $rs; + return Koha::Patron->_new_from_dbic($rs); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'TicketUpdate'; +} + +1; diff --git a/Koha/Ticket/Updates.pm b/Koha/Ticket/Updates.pm new file mode 100644 index 0000000000..d3e7433e76 --- /dev/null +++ b/Koha/Ticket/Updates.pm @@ -0,0 +1,48 @@ +package Koha::Ticket::Updates; + +# 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 base qw(Koha::Objects); + +=head1 NAME + +Koha::Ticket::Updates - Koha Ticket Update Objects class + +=head1 API + +=head2 Internal methods + +=cut + +=head3 _type + +=cut + +sub _type { + return 'TicketUpdate'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Ticket::Update'; +} + +1; diff --git a/Koha/Tickets.pm b/Koha/Tickets.pm new file mode 100644 index 0000000000..85a7de8a8a --- /dev/null +++ b/Koha/Tickets.pm @@ -0,0 +1,50 @@ +package Koha::Tickets; + +# 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::Ticket; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Tickets - Koha Ticket Objects class + +=head1 API + +=head2 Internal methods + +=cut + +=head3 _type + +=cut + +sub _type { + return 'Ticket'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Ticket'; +} + +1; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 67513cd09f..188625e113 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 22; # +1 +use Test::More tests => 23; # +1 use Test::Warn; use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc ); @@ -650,6 +650,34 @@ subtest 'orders() and active_orders() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'tickets() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $tickets = $biblio->tickets; + is( ref($tickets), 'Koha::Tickets', 'Koha::Biblio->tickets should return a Koha::Tickets object' ); + is( $tickets->count, 0, 'Koha::Biblio->tickets should return a count of 0 when there are no related tickets' ); + + # Add two tickets + foreach (1..2) { + $builder->build_object( + { + class => 'Koha::Tickets', + value => { biblio_id => $biblio->biblionumber } + } + ); + } + + $tickets = $biblio->tickets; + is( ref($tickets), 'Koha::Tickets', 'Koha::Biblio->tickets should return a Koha::Tickets object' ); + is( $tickets->count, 2, 'Koha::Biblio->tickets should return the correct number of tickets' ); + + $schema->storage->txn_rollback; +}; + subtest 'subscriptions() tests' => sub { plan tests => 4; diff --git a/t/db_dependent/Koha/Ticket.t b/t/db_dependent/Koha/Ticket.t new file mode 100644 index 0000000000..672d749350 --- /dev/null +++ b/t/db_dependent/Koha/Ticket.t @@ -0,0 +1,147 @@ +#!/usr/bin/perl + +# Copyright 2023 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 Test::More tests => 5; +use t::lib::TestBuilder; + +use Koha::Database; + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; + +subtest 'reporter() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $ticket = $builder->build_object( + { + class => 'Koha::Tickets', + value => { + reporter_id => $patron->id + } + } + ); + + my $reporter = $ticket->reporter; + is( ref($reporter), 'Koha::Patron', 'Koha::Ticket->reporter returns a Koha::Patron object' ); + is( $reporter->id, $patron->id, 'Koha::Ticket->reporter returns the right Koha::Patron' ); + + $schema->storage->txn_rollback; +}; + +subtest 'resolver() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $ticket = $builder->build_object( + { + class => 'Koha::Tickets', + value => { + resolver_id => $patron->id + } + } + ); + + my $resolver = $ticket->resolver; + is( ref($resolver), 'Koha::Patron', 'Koha::Ticket->resolver returns a Koha::Patron object' ); + is( $resolver->id, $patron->id, 'Koha::Ticket->resolver returns the right Koha::Patron' ); + + $schema->storage->txn_rollback; +}; + +subtest 'biblio() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_object({ class => 'Koha::Biblios' }); + my $ticket = $builder->build_object( + { + class => 'Koha::Tickets', + value => { + biblio_id => $biblio->id + } + } + ); + + my $related_biblio = $ticket->biblio; + is( ref($related_biblio), 'Koha::Biblio', 'Koha::Ticket->biblio returns a Koha::Biblio object' ); + is( $related_biblio->id, $biblio->id, 'Koha::Ticket->biblio returns the right Koha::Biblio' ); + + $schema->storage->txn_rollback; +}; + +subtest 'updates() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $ticket = $builder->build_object( { class => 'Koha::Tickets' } ); + my $updates = $ticket->updates; + is( ref($updates), 'Koha::Ticket::Updates', 'Koha::Ticket->updates should return a Koha::Ticket::Updates object' ); + is( $updates->count, 0, 'Koha::Ticket->updates should return a count of 0 when there are no related updates' ); + + # Add two updates + foreach (1..2) { + $builder->build_object( + { + class => 'Koha::Ticket::Updates', + value => { ticket_id => $ticket->id } + } + ); + } + + $updates = $ticket->updates; + is( ref($updates), 'Koha::Ticket::Updates', 'Koha::Ticket->updates should return a Koha::Ticket::Updates object' ); + is( $updates->count, 2, 'Koha::Ticket->updates should return the correct number of updates' ); + + $schema->storage->txn_rollback; +}; + +subtest 'add_update() tests' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $ticket = $builder->build_object( { class => 'Koha::Tickets' } ); + my $update = $ticket->add_update( + { user_id => $patron->id, public => 1, message => "Some message" } ); + is( ref($update), 'Koha::Ticket::Update', + 'Koha::Ticket->add_update should return a Koha::Ticket::Update object' + ); + + my $updates = $ticket->updates; + is( $updates->count, 1, + 'Koha::Ticket->add_update should have added 1 update linked to this ticket' + ); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/Koha/Ticket/Update.t b/t/db_dependent/Koha/Ticket/Update.t new file mode 100755 index 0000000000..8c357aef78 --- /dev/null +++ b/t/db_dependent/Koha/Ticket/Update.t @@ -0,0 +1,74 @@ +#!/usr/bin/perl + +# Copyright 2023 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 Test::More tests => 2; +use t::lib::TestBuilder; + +use Koha::Database; + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; + +subtest 'ticket() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $ticket = $builder->build_object({ class => 'Koha::Tickets' }); + my $update = $builder->build_object( + { + class => 'Koha::Ticket::Updates', + value => { + ticket_id => $ticket->id + } + } + ); + + my $linked_ticket = $update->ticket; + is( ref($linked_ticket), 'Koha::Ticket', 'Koha::Ticket::Update->ticket returns a Koha::Ticket object' ); + is( $linked_ticket->id, $ticket->id, 'Koha::Ticket::Update->ticket returns the right Koha::Ticket' ); + + $schema->storage->txn_rollback; +}; + +subtest 'user() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $user = $builder->build_object({ class => 'Koha::Patrons' }); + my $update = $builder->build_object( + { + class => 'Koha::Ticket::Updates', + value => { + user_id => $user->id + } + } + ); + + my $linked_user = $update->user; + is( ref($linked_user), 'Koha::Patron', 'Koha::Ticket::Update->user returns a Koha::Patron object' ); + is( $linked_user->id, $user->id, 'Koha::Ticket::Update->user returns the right Koha::Patron' ); + + $schema->storage->txn_rollback; +}; -- 2.30.2