From 190205f603a5fa0614de43ddb669427e94518be2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 19 Aug 2024 11:35:13 -0300 Subject: [PATCH] Bug 37513: Add Koha::RecordSource->usage_count This patch adds the `usage_count` method to be used for embedding from the API. Tests cover all use cases. 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: David Nind Signed-off-by: Matt Blenkinsop Signed-off-by: Katrin Fischer --- Koha/RecordSource.pm | 15 ++++++++++ t/db_dependent/Koha/RecordSource.t | 47 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 t/db_dependent/Koha/RecordSource.t diff --git a/Koha/RecordSource.pm b/Koha/RecordSource.pm index 8b4fecbe25..54f7921b6d 100644 --- a/Koha/RecordSource.pm +++ b/Koha/RecordSource.pm @@ -31,6 +31,21 @@ Koha::RecordSource - Koha RecordSource Object class =head1 API +=head2 Class methods + +=head3 usage_count + + my $count = $source->usage_count(); + +This method returns the count for records using this record source. + +=cut + +sub usage_count { + my ($self) = @_; + return $self->_result->biblio_metadatas->count(); +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/RecordSource.t b/t/db_dependent/Koha/RecordSource.t new file mode 100755 index 0000000000..5a0bba3212 --- /dev/null +++ b/t/db_dependent/Koha/RecordSource.t @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# 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 => 1; + +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'usage_count() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $source = $builder->build_object( { class => 'Koha::RecordSources' } ); + + is( $source->usage_count, 0, q{Unused record source has a count of 0} ); + + foreach ( 1 .. 3 ) { + my $biblio = $builder->build_sample_biblio(); + $biblio->metadata->record_source_id( $source->id )->store(); + } + + is( $source->usage_count, 3, q{3 records linked, count is 3} ); + + $schema->storage->txn_rollback; +}; -- 2.39.5