From 0b95427184758db9c50bd4e55675d791a40e9067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Moyano?= Date: Fri, 15 Sep 2023 09:18:29 -0300 Subject: [PATCH] Bug 32607: Add Koha::RecordSource(s) classes This patch introduces classes for handling record sources. The Koha::RecordSource->store method takes care of generating the `api_token` attribute if required. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/RecordSource.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi --- Koha/RecordSource.pm | 93 ++++++++++++++++++++++++ Koha/RecordSources.pm | 51 +++++++++++++ Koha/Schema/Result/RecordSource.pm | 110 +++++++++++++++++++++++++++++ t/db_dependent/Koha/RecordSource.t | 82 +++++++++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 Koha/RecordSource.pm create mode 100644 Koha/RecordSources.pm create mode 100644 Koha/Schema/Result/RecordSource.pm create mode 100755 t/db_dependent/Koha/RecordSource.t diff --git a/Koha/RecordSource.pm b/Koha/RecordSource.pm new file mode 100644 index 00000000000..fcd933d5f4d --- /dev/null +++ b/Koha/RecordSource.pm @@ -0,0 +1,93 @@ +package Koha::RecordSource; + +# This file is part of Koha. +# +# Copyright 2020 Koha Development Team +# +# 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::Patrons; +use Koha::Token; + +=head1 NAME + +Koha::RecordSource - Koha RecordSource Object class + +=head1 API + +=head2 Class methods + +=head3 store + + my $record_source = Koha::RecordSource->new( { name => $name, patron_id => $patron->id } )->store; + +Overloaded I method that takes care of generating the assigned token. + +=cut + +sub store { + my ($self) = @_; + + my $needs_new_token = 1; + + if ( $self->in_storage ) { # update + if ( Koha::Token->new->check_jwt( { id => $self->id, token => $self->api_token } ) ) { + $needs_new_token = 0; + } + } else { # fresh + # store so we get a fresh id + $self->SUPER::store; + } + + if ($needs_new_token) { # new + my $token = Koha::Token->new->generate_jwt( { id => $self->id } ); + $self->api_token($token); + } + + return $self->SUPER::store; +} + +=head3 patron + + my $patron = $record_source->patron + + Return the patron for this record source + +=cut + +sub patron { + my ($self) = @_; + + my $patron_rs = $self->_result->patron; + return unless $patron_rs; + + return Koha::Patron->_new_from_dbic($patron_rs); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'RecordSource'; +} + +1; diff --git a/Koha/RecordSources.pm b/Koha/RecordSources.pm new file mode 100644 index 00000000000..b18d4791755 --- /dev/null +++ b/Koha/RecordSources.pm @@ -0,0 +1,51 @@ +package Koha::RecordSources; + +# This file is part of Koha. +# +# Copyright 2020 Koha Development Team +# +# 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); + +use Koha::RecordSource; + +=head1 NAME + +Koha::RecordSources - Koha RecordSources Object class + +=head1 API + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'RecordSource'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::RecordSource'; +} + +1; diff --git a/Koha/Schema/Result/RecordSource.pm b/Koha/Schema/Result/RecordSource.pm new file mode 100644 index 00000000000..f3985a88147 --- /dev/null +++ b/Koha/Schema/Result/RecordSource.pm @@ -0,0 +1,110 @@ +use utf8; +package Koha::Schema::Result::RecordSource; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +=head1 NAME + +Koha::Schema::Result::RecordSource + +=cut + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +=head1 TABLE: C + +=cut + +__PACKAGE__->table("record_sources"); + +=head1 ACCESSORS + +=head2 record_source_id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +Primary key for the `record_sources` table + +=head2 name + + data_type: 'text' + is_nullable: 0 + +User defined name for the record source + +=head2 patron_id + + data_type: 'integer' + is_foreign_key: 1 + is_nullable: 0 + +Internal patron identifier + +=head2 api_token + + data_type: 'varchar' + is_nullable: 1 + size: 191 + +Generated token for identifying the record source on API interactions + +=cut + +__PACKAGE__->add_columns( + "record_source_id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "name", + { data_type => "text", is_nullable => 0 }, + "patron_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, + "api_token", + { data_type => "varchar", is_nullable => 1, size => 191 }, +); + +=head1 PRIMARY KEY + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->set_primary_key("record_source_id"); + +=head1 RELATIONS + +=head2 patron + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "patron", + "Koha::Schema::Result::Borrower", + { borrowernumber => "patron_id" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07049 @ 2023-09-14 19:23:47 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:QW7VYQ0haYHIb7ryTPhgZQ + +sub koha_objects_class { + 'Koha::RecordSources'; +} +sub koha_object_class { + 'Koha::RecordSource'; +} + +1; diff --git a/t/db_dependent/Koha/RecordSource.t b/t/db_dependent/Koha/RecordSource.t new file mode 100755 index 00000000000..fedc3f16c8e --- /dev/null +++ b/t/db_dependent/Koha/RecordSource.t @@ -0,0 +1,82 @@ +#!/usr/bin/perl + +# Copyright 2023 Theke Solutions +# +# 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 Koha::RecordSources; +use Koha::Token; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'patron() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $source = $builder->build_object( + { + class => 'Koha::RecordSources', + value => { patron_id => $patron->id } + } + ); + + my $fetched_patron = $source->patron; + + is( ref($fetched_patron), 'Koha::Patron' ); + is( $fetched_patron->id, $patron->id ); + + $patron->delete; + $source->discard_changes; + + is( $source->patron, undef, 'Method returns undef if no linked patron' ); + + $schema->storage->txn_rollback; +}; + +subtest 'store() tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $source = Koha::RecordSource->new( { name => 'Sample' } )->store; + isnt( $source->api_token, undef, "'api_token' set on creation" ); + ok( Koha::Token->new->check_jwt( { id => $source->id, token => $source->api_token } ), 'Token is valid' ); + + my $api_token = $source->api_token; + $source->name('Sample2')->store->discard_changes; + + is( $api_token, $source->api_token, 'Token unchanged on name change' ); + + # change the id, token validation is based on id so should be regenerated + $source->set( { record_source_id => $source->record_source_id + 1 } )->store->discard_changes; + + isnt( $api_token, $source->api_token, 'Token changed on id change' ); + ok( Koha::Token->new->check_jwt( { id => $source->id, token => $source->api_token } ), 'New token is valid' ); + + $schema->storage->txn_rollback; +}; -- 2.42.0