From e55fb4686a0e9dbcc62dd2e88224d2357a7db51e Mon Sep 17 00:00:00 2001 From: Lucas Gass Date: Tue, 15 Jul 2025 16:59:50 +0000 Subject: [PATCH] Bug 37883: (follow-up) Add a test --- t/db_dependent/Search.t | 96 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Search.t b/t/db_dependent/Search.t index beaf6cb43ab..ddb8e01c1d0 100755 --- a/t/db_dependent/Search.t +++ b/t/db_dependent/Search.t @@ -27,7 +27,7 @@ require C4::Context; use open ':std', ':encoding(utf8)'; use Test::NoWarnings; -use Test::More tests => 4; +use Test::More tests => 5; use Test::MockModule; use Test::Warn; use t::lib::Mocks; @@ -235,6 +235,100 @@ sub mock_GetMarcSubfieldStructure { } } +subtest 'searchResults branch-specific counts for Bug 37883' => sub { + plan tests => 8; + + use C4::Search qw( searchResults ); + + my $record = MARC::Record->new(); + $record->add_fields( + [ '001', '123' ], + [ '245', ' ', ' ', a => 'Test record for branch counting' ], + [ '999', ' ', ' ', c => '123', d => '123' ], + + # Available item from CPL + [ + '952', ' ', ' ', + a => 'CPL', # homebranch + b => 'CPL', # holdingbranch + p => 'TEST001', # barcode + y => 'BK', # itemtype + '9' => '1001', # itemnumber + '0' => '0', # withdrawn + '1' => '0', # itemlost + '4' => '0', # damaged + '7' => '0', # notforloan + q => '', # onloan + ], + + # Withdrawn item from CPL + [ + '952', ' ', ' ', + a => 'CPL', # homebranch + b => 'CPL', # holdingbranch + p => 'TEST002', # barcode + y => 'BK', # itemtype + '9' => '1002', # itemnumber + '0' => '1', # withdrawn = yes + '1' => '0', # itemlost + '4' => '0', # damaged + '7' => '0', # notforloan + q => '', # onloan + ], + + # Available item from MPL + [ + '952', ' ', ' ', + a => 'MPL', # homebranch + b => 'MPL', # holdingbranch + p => 'TEST003', # barcode + y => 'BK', # itemtype + '9' => '1003', # itemnumber + '0' => '0', # withdrawn + '1' => '0', # itemlost + '4' => '0', # damaged + '7' => '0', # notforloan + q => '', # onloan + ], + ); + + # Mock userenv for CPL branch + my $context_mock = Test::MockModule->new('C4::Context'); + $context_mock->mock( + 'userenv', + sub { + return { + branchcode => 'CPL', + branch => 'CPL', + number => 123, + }; + } + ); + + my @results = searchResults( + { interface => 'intranet' }, + 'test query', + 1, # total hits + 10, # results per page + 0, # offset + 0, # scan + [ $record->as_xml() ] + ); + + # Test that new branch count fields exist in search results + ok( defined $results[0]->{branchavailablecount}, 'branchavailablecount exists' ); + ok( defined $results[0]->{branchonloancount}, 'branchonloancount exists' ); + ok( defined $results[0]->{branchothercount}, 'branchothercount exists' ); + ok( defined $results[0]->{branchtotalcount}, 'branchtotalcount exists' ); + + is( $results[0]->{branchavailablecount}, 1, 'Branch available count: 1 available item at CPL' ); + is( $results[0]->{branchothercount}, 1, 'Branch other count: 1 withdrawn item at CPL' ); + is( $results[0]->{branchtotalcount}, 2, 'Branch total count: 2 items total at CPL' ); + + # Test that global counts still include all items from all branches + is( $results[0]->{availablecount}, 2, 'Global available count: 2 items available at all branches' ); +}; + sub run_marc21_search_tests { $marcflavour = 'MARC21'; -- 2.39.5