From 40fc2a99c19de69feb8e10a49441d701c9e6e0e8 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 3 Sep 2015 10:13:18 -0300 Subject: [PATCH] Bug 14639: (QA followup) make schema mandatory This patch makes the 'schema' param mandatory. It is passed in every call on the current codebase, so it makes no harm now, but makes the code less error-prone. Tests for this situation are added to t/Koha_MetadataRecord.t (schema param is omitted and new() returns undef and a carped warning). Signed-off-by: Tomas Cohen Arazi --- Koha/MetadataRecord.pm | 7 ++++++- t/Koha_MetadataRecord.t | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Koha/MetadataRecord.pm b/Koha/MetadataRecord.pm index cec2487eb1..8b0d8a45eb 100644 --- a/Koha/MetadataRecord.pm +++ b/Koha/MetadataRecord.pm @@ -82,8 +82,13 @@ sub new { return; } + if (!defined $params->{ schema }) { + carp 'No schema passed'; + return; + } + my $record = $params->{ record }; - my $schema = $params->{ schema } // 'marc21'; + my $schema = $params->{ schema }; my $format = $params->{ format } // 'MARC'; my $id = $params->{ id }; diff --git a/t/Koha_MetadataRecord.t b/t/Koha_MetadataRecord.t index a92484353a..f53e13dd4f 100755 --- a/t/Koha_MetadataRecord.t +++ b/t/Koha_MetadataRecord.t @@ -100,17 +100,26 @@ is($dupkeys, 0, 'No duplicate keys'); subtest "new() tests" => sub { - plan tests => 12; + plan tests => 14; # Test default values with a MARC::Record record my $record = MARC::Record->new(); - my $metadata_record = new Koha::MetadataRecord({ - record => $record + my $metadata_record; + + warning_is { $metadata_record = new Koha::MetadataRecord({ + record => $record }) } + { carped => 'No schema passed' }, + "Metadata schema is mandatory, raise a carped warning if omitted"; + is( $metadata_record, undef, "Metadata schema is mandatory, return undef if omitted"); + + $metadata_record = new Koha::MetadataRecord({ + record => $record, + schema => 'marc21' }); is( ref($metadata_record), 'Koha::MetadataRecord', 'Type correct'); is( ref($metadata_record->record), 'MARC::Record', 'Record type preserved'); - is( $metadata_record->schema, 'marc21', 'Metadata schema defaults to marc21'); + is( $metadata_record->schema, 'marc21', 'Metadata schema is set to marc21'); is( $metadata_record->format, 'MARC', 'Serializacion format defaults to marc'); is( $metadata_record->id, undef, 'id is optional, undef if unspecifid'); -- 2.39.5