From bb080f14026e154988a7e665535c930dd860426c Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 12 Jul 2024 10:16:08 +0100 Subject: [PATCH] Bug 36641: Unit tests for effective=0 --- t/db_dependent/api/v1/circulation_rules.t | 69 ++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/circulation_rules.t b/t/db_dependent/api/v1/circulation_rules.t index f1ec45a56ec..712c9d459b4 100755 --- a/t/db_dependent/api/v1/circulation_rules.t +++ b/t/db_dependent/api/v1/circulation_rules.t @@ -33,7 +33,7 @@ my $t = Test::Mojo->new('Koha::REST::V1'); t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); subtest 'list_rules() tests' => sub { - plan tests => 32; + plan tests => 33; $schema->storage->txn_begin; @@ -202,5 +202,72 @@ subtest 'list_rules() tests' => sub { # Unauthorized access $t->get_ok("//$unauth_userid:$password@/api/v1/circulation_rules")->status_is(403); + subtest 'effective=false tests' => sub { + + my $expected_keys = [ keys %{ Koha::CirculationRules->rule_kinds } ]; + my $count = scalar( @{$expected_keys} ); + + plan tests => ( $count * 2 ) + $count + 10; + + # All rules + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?effective=0")->status_is(200); + + # Extract and decode the JSON response + my $json = $t->tx->res->json; + + # Check if the response is an array + is( ref $json, 'ARRAY', 'Response is an array' ); + is( scalar( @{$json} ), 2, 'Response contains 2 rule sets' ); + + # Iterate over each hash in the array + my $index = 0; + foreach my $hash ( @{$json} ) { + my $pointer = Mojo::JSON::Pointer->new($hash); + + # First rule set should march default, default, default + if ( $index == 0 ) { + ok( $pointer->get('/branchcode') eq "*" + && $pointer->get('/itemtype') eq '*' + && $pointer->get('/categorycode') eq '*', "Default rules returned first" ); + } + + # Iterate over the list of expected keys for each hash + foreach my $key ( @{$expected_keys} ) { + ok( $pointer->contains( '/' . $key ), "Hash contains key '$key'" ); + } + + $index++; + } + + # Filter on library + $t->get_ok("//$userid:$password@/api/v1/circulation_rules?effective=0&library_id=$branchcode")->status_is(200); + + # Extract and decode the JSON response + $json = $t->tx->res->json; + + # Check if the response is an array + is( ref $json, 'ARRAY', 'Response is an array' ); + is( scalar( @{$json} ), 1, 'Filtered response contains 1 rule set' ); + + $index = 0; + foreach my $hash ( @{$json} ) { + my $pointer = Mojo::JSON::Pointer->new($hash); + + # First (and only) rule set should match branchcode, default, default. + if ( $index == 0 ) { + ok( $pointer->get('/branchcode') eq $branchcode + && $pointer->get('/itemtype') eq '*' + && $pointer->get('/categorycode') eq '*', "Branchcode rule set returned when filtered" ); + } + + # Iterate over the list of expected keys for each hash + foreach my $key ( @{$expected_keys} ) { + ok( $pointer->contains( '/' . $key ), "Hash contains key '$key'" ); + } + + $index++; + } + + }; $schema->storage->txn_rollback; }; -- 2.45.2