From 5f904824bb85c1de9cb60c6ca6bb33c084347f97 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 22 Jul 2025 13:54:18 +0100 Subject: [PATCH] Bug 40383: Add unit tests for Koha::Edifact to_json method This patch adds unit tests for the new to_json method being introduced in Koha::Edifact module in the next patch. The tests verify: - JSON output generation and validity - Proper structure with header, messages, and trailer fields - Message array structure with correct number of messages - Individual message structure (header, segments, trailer) - Segment data integrity including raw data and elements arrays - Line ID functionality for related segments (LIN, QTY, PRI, PIA) Test plan: 1. Apply this patch and the next one 2. Run: prove t/Edifact.t 3. Verify all 63 tests pass 4. Confirm the new JSON-related tests (22 tests) execute successfully Sponsored-by: Martin Renvoize --- t/Edifact.t | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/t/Edifact.t b/t/Edifact.t index 55d35d85dfa..1dc5bd7d714 100755 --- a/t/Edifact.t +++ b/t/Edifact.t @@ -3,7 +3,8 @@ use strict; use warnings; use FindBin qw( $Bin ); -use Test::More tests => 41; +use Test::More tests => 63; +use JSON qw( decode_json ); use Koha::EDI; BEGIN { use_ok('Koha::Edifact') } @@ -138,3 +139,61 @@ is( $dp, 9.0, 'Discount calculated with discount = 0' ); $dp = Koha::EDI::_discounted_price( 0.0, 9, 8.0 ); is( $dp, 8.0, 'Discount overridden by incoming calculated value' ); + +# Test JSON output functionality +my $json_output = $quote->to_json(); +ok( $json_output, 'JSON output generated' ); + +my $parsed_json = decode_json($json_output); +ok( $parsed_json, 'JSON output is valid JSON' ); + +# Test structure +ok( exists $parsed_json->{header}, 'JSON has header field' ); +ok( exists $parsed_json->{messages}, 'JSON has messages field' ); +ok( exists $parsed_json->{trailer}, 'JSON has trailer field' ); + +# Test header content (should be UNB segment) +like( $parsed_json->{header}, qr/^UNB\+/, 'Header starts with UNB' ); + +# Test messages array +is( ref $parsed_json->{messages}, 'ARRAY', 'Messages is an array' ); +is( scalar @{ $parsed_json->{messages} }, 1, 'Correct number of messages' ); + +# Test first message structure +my $first_msg = $parsed_json->{messages}->[0]; +ok( exists $first_msg->{header}, 'Message has header' ); +ok( exists $first_msg->{segments}, 'Message has segments' ); +ok( exists $first_msg->{trailer}, 'Message has trailer' ); + +# Test message header/trailer format +like( $first_msg->{header}, qr/^UNH\+/, 'Message header starts with UNH' ); +like( $first_msg->{trailer}, qr/^UNT\+/, 'Message trailer starts with UNT' ); + +# Test segments array +is( ref $first_msg->{segments}, 'ARRAY', 'Segments is an array' ); +ok( scalar @{ $first_msg->{segments} } > 0, 'Message has segments' ); + +# Find a LIN segment to test line_id functionality +my $lin_segment; +my $lin_related_segment; +for my $seg ( @{ $first_msg->{segments} } ) { + if ( $seg->{tag} eq 'LIN' ) { + $lin_segment = $seg; + } elsif ( $lin_segment && $seg->{tag} =~ /^(QTY|PRI|PIA)$/ && $seg->{line_id} ) { + $lin_related_segment = $seg; + last; + } +} + +ok( $lin_segment, 'Found LIN segment in JSON' ); +if ($lin_segment) { + ok( exists $lin_segment->{line_id}, 'LIN segment has line_id' ); + ok( exists $lin_segment->{raw}, 'LIN segment has raw data' ); + ok( exists $lin_segment->{elements}, 'LIN segment has elements array' ); + like( $lin_segment->{raw}, qr/^LIN\+/, 'LIN raw data starts correctly' ); +} + +ok( $lin_related_segment, 'Found line-related segment with line_id' ); +if ( $lin_related_segment && $lin_segment ) { + is( $lin_related_segment->{line_id}, $lin_segment->{line_id}, 'Line-related segment has matching line_id' ); +} -- 2.50.1