Bugzilla – Attachment 190563 Details for
Bug 16994
Import and export MARC modification templates
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 16994: Add unit tests
Bug-16994-Add-unit-tests.patch (text/plain), 10.00 KB, created by
Arthur Suzuki
on 2025-12-16 22:59:53 UTC
(
hide
)
Description:
Bug 16994: Add unit tests
Filename:
MIME Type:
Creator:
Arthur Suzuki
Created:
2025-12-16 22:59:53 UTC
Size:
10.00 KB
patch
obsolete
>From 9b0090554f5ba3a6cec36f83612e73c333e58e0d Mon Sep 17 00:00:00 2001 >From: Arthur Suzuki <arthur.suzuki@biblibre.com> >Date: Tue, 16 Dec 2025 22:17:06 +0000 >Subject: [PATCH] Bug 16994: Add unit tests > >--- > t/db_dependent/MarcModificationTemplates.t | 259 ++++++++++++++++++++- > 1 file changed, 257 insertions(+), 2 deletions(-) > >diff --git a/t/db_dependent/MarcModificationTemplates.t b/t/db_dependent/MarcModificationTemplates.t >index d162df355f8..fb79e92454d 100755 >--- a/t/db_dependent/MarcModificationTemplates.t >+++ b/t/db_dependent/MarcModificationTemplates.t >@@ -3,10 +3,11 @@ > use Modern::Perl; > > use Test::NoWarnings; >-use Test::More tests => 131; >+use Test::More tests => 134; > > use Koha::Database; > use Koha::SimpleMARC; >+use JSON qw( encode_json decode_json ); > > use t::lib::Mocks; > >@@ -14,7 +15,7 @@ use_ok("MARC::Field"); > use_ok("MARC::Record"); > use_ok( > 'C4::MarcModificationTemplates', >- qw( AddModificationTemplate AddModificationTemplateAction GetModificationTemplateAction GetModificationTemplateActions ModModificationTemplateAction MoveModificationTemplateAction DelModificationTemplate DelModificationTemplateAction ModifyRecordWithTemplate GetModificationTemplates ) >+ qw( AddModificationTemplate AddModificationTemplateAction GetModificationTemplateAction GetModificationTemplateActions ModModificationTemplateAction MoveModificationTemplateAction DelModificationTemplate DelModificationTemplateAction ModifyRecordWithTemplate GetModificationTemplates ExportModificationTemplates ImportModificationTemplates ) > ); > > my $schema = Koha::Database->new->schema; >@@ -1076,3 +1077,257 @@ subtest "Bug 32950: Moving subfield preserves values in repeatable fields" => su > > DelModificationTemplate($template_id); > }; >+ >+# Test export and import functionality >+subtest 'Export and Import MARC modification templates' => sub { >+ plan tests => 16; >+ >+ $dbh->do(q|DELETE FROM marc_modification_templates|); >+ >+ my $template_id = AddModificationTemplate("Test Template for Export/Import"); >+ ok( $template_id, "Template created successfully" ); >+ >+ ok( >+ AddModificationTemplateAction( >+ $template_id, >+ 'update_field', >+ '1', >+ '020', >+ 'a', >+ '__BRANCHCODE__-', >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ 'Add branch code prefix to ISBN' >+ ), >+ "First action added" >+ ); >+ >+ ok( >+ AddModificationTemplateAction( >+ $template_id, >+ 'add_field', >+ '1', >+ '952', >+ 'a', >+ 'EXPORTED', >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ 'Mark as exported' >+ ), >+ "Second action added" >+ ); >+ >+ my $json = ExportModificationTemplates( [$template_id] ); >+ ok( defined $json && length($json) > 0, "Template exported successfully" ); >+ >+ my $decoded = eval { decode_json($json) }; >+ ok( !$@ && $decoded, "JSON is valid" ); >+ ok( exists $decoded->{version}, "JSON has version" ); >+ ok( exists $decoded->{templates} && @{ $decoded->{templates} } == 1, "JSON has one template" ); >+ >+ my $skip_existing = 0; >+ my $result = ImportModificationTemplates( $json, $skip_existing ); >+ >+ ok( $result->{success} == 1, "Template imported successfully with skip_existing=0" ); >+ ok( $result->{skipped} == 0, "No templates skipped when skip_existing=0" ); >+ ok( scalar( @{ $result->{errors} } ) == 0, "No import errors" ); >+ >+ # Test skip_existing=1: Template should be skipped if it already exists >+ $result = ImportModificationTemplates( $json, 1 ); >+ ok( $result->{success} == 0, "Template not imported when skip_existing=1 and template exists" ); >+ ok( $result->{skipped} == 1, "Template skipped when skip_existing=1" ); >+ ok( scalar( @{ $result->{errors} } ) == 0, "No import errors with skip_existing=1" ); >+ >+ # Verify the original template actions are still intact after skip >+ my @actions = GetModificationTemplateActions($template_id); >+ is( scalar(@actions), 2, "Template still has 2 actions after skip" ); >+ is( $actions[0]->{action}, 'update_field', "First action still update_field" ); >+ is( $actions[1]->{action}, 'add_field', "Second action still add_field" ); >+ >+ DelModificationTemplate($template_id); >+}; >+ >+# Test skip_existing parameter with overwrite behavior >+subtest 'Import with skip_existing=0 overwrites existing templates' => sub { >+ plan tests => 12; >+ >+ $dbh->do(q|DELETE FROM marc_modification_templates|); >+ >+ # Create a template with specific actions >+ my $template_id = AddModificationTemplate("Overwrite Test Template"); >+ ok( $template_id, "Template created successfully" ); >+ >+ ok( >+ AddModificationTemplateAction( >+ $template_id, >+ 'update_field', >+ '1', >+ '020', >+ 'a', >+ 'ORIGINAL_VALUE', >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ 'Original action' >+ ), >+ "Original action added" >+ ); >+ >+ # Export the template >+ my $json = ExportModificationTemplates( [$template_id] ); >+ ok( defined $json && length($json) > 0, "Template exported successfully" ); >+ >+ # Modify the template in the database (add a new action) >+ ok( >+ AddModificationTemplateAction( >+ $template_id, >+ 'add_field', >+ '1', >+ '999', >+ 'a', >+ 'MODIFIED', >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ 'Modified action' >+ ), >+ "Modified action added to existing template" >+ ); >+ >+ # Verify template now has 2 actions >+ my @actions = GetModificationTemplateActions($template_id); >+ is( scalar(@actions), 2, "Template has 2 actions before import" ); >+ >+ # Import with skip_existing=0 (should overwrite) >+ my $result = ImportModificationTemplates( $json, 0 ); >+ ok( $result->{success} == 1, "Template imported successfully with skip_existing=0" ); >+ ok( $result->{skipped} == 0, "No templates skipped" ); >+ ok( $result->{overwrite} == 1, "One template overwritten" ); >+ ok( scalar( @{ $result->{errors} } ) == 0, "No import errors" ); >+ >+ # Verify the template was overwritten (should have only 1 action now) >+ @actions = GetModificationTemplateActions($template_id); >+ is( scalar(@actions), 1, "Template has 1 action after overwrite" ); >+ is( $actions[0]->{action}, 'update_field', "Action is update_field (original)" ); >+ is( $actions[0]->{field_value}, 'ORIGINAL_VALUE', "Field value is original value" ); >+ >+ # Clean up >+ DelModificationTemplate($template_id); >+}; >+ >+# Test skip_existing parameter with skip behavior >+subtest 'Import with skip_existing=1 skips existing templates' => sub { >+ plan tests => 11; >+ >+ $dbh->do(q|DELETE FROM marc_modification_templates|); >+ >+ # Create a template with specific actions >+ my $template_id = AddModificationTemplate("Skip Test Template"); >+ ok( $template_id, "Template created successfully" ); >+ >+ ok( >+ AddModificationTemplateAction( >+ $template_id, >+ 'update_field', >+ '1', >+ '020', >+ 'a', >+ 'ORIGINAL_VALUE', >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ 'Original action' >+ ), >+ "Original action added" >+ ); >+ >+ # Export the template >+ my $json = ExportModificationTemplates( [$template_id] ); >+ ok( defined $json && length($json) > 0, "Template exported successfully" ); >+ >+ # Modify the template in the database (add a new action) >+ ok( >+ AddModificationTemplateAction( >+ $template_id, >+ 'add_field', >+ '1', >+ '999', >+ 'a', >+ 'MODIFIED', >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ undef, >+ 'Modified action' >+ ), >+ "Modified action added to existing template" >+ ); >+ >+ # Verify template now has 2 actions >+ my @actions = GetModificationTemplateActions($template_id); >+ is( scalar(@actions), 2, "Template has 2 actions before import" ); >+ >+ # Import with skip_existing=1 (should skip) >+ my $result = ImportModificationTemplates( $json, 1 ); >+ ok( $result->{success} == 0, "Template not imported when skip_existing=1" ); >+ ok( $result->{skipped} == 1, "Template skipped when skip_existing=1" ); >+ ok( scalar( @{ $result->{errors} } ) == 0, "No import errors" ); >+ >+ # Verify the template was NOT modified (still has 2 actions) >+ @actions = GetModificationTemplateActions($template_id); >+ is( scalar(@actions), 2, "Template still has 2 actions after skip" ); >+ is( $actions[0]->{action}, 'update_field', "First action is still update_field" ); >+ is( $actions[1]->{action}, 'add_field', "Second action is still add_field" ); >+ >+ # Clean up >+ DelModificationTemplate($template_id); >+}; >-- >2.39.5
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 16994
:
190560
|
190561
|
190563
|
190647
|
190648