Bug 26132: Remove raw sql query
[koha-ffzg.git] / t / db_dependent / Circulation / TooMany.t
index 863eeef..0f4843b 100644 (file)
@@ -15,7 +15,7 @@
 # with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 9;
+use Test::More tests => 10;
 use C4::Context;
 
 use C4::Members;
@@ -43,7 +43,6 @@ $dbh->do(q|DELETE FROM branches|);
 $dbh->do(q|DELETE FROM categories|);
 $dbh->do(q|DELETE FROM accountlines|);
 $dbh->do(q|DELETE FROM itemtypes|);
-$dbh->do(q|DELETE FROM issuingrules|);
 Koha::CirculationRules->search()->delete();
 
 my $builder = t::lib::TestBuilder->new();
@@ -65,19 +64,11 @@ my $patron = $builder->build({
     },
 });
 
-my $biblio = $builder->build({
-    source => 'Biblio',
-    value => {
-        branchcode => $branch->{branchcode},
-    },
-});
-my $item = $builder->build({
-    source => 'Item',
-    value => {
-        biblionumber => $biblio->{biblionumber},
-        homebranch => $branch->{branchcode},
-        holdingbranch => $branch->{branchcode},
-    },
+my $biblio = $builder->build_sample_biblio({ branchcode => $branch->{branchcode} });
+my $item = $builder->build_sample_item({
+    biblionumber => $biblio->biblionumber,
+    homebranch => $branch->{branchcode},
+    holdingbranch => $branch->{branchcode},
 });
 
 my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
@@ -90,12 +81,12 @@ t::lib::Mocks::mock_userenv( { patron => $patron_object });
 subtest 'no rules exist' => sub {
     plan tests => 2;
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         { reason => 'NO_RULE_DEFINED', max_allowed => 0 },
         'CO should not be allowed, in any cases'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         { reason => 'NO_RULE_DEFINED', max_allowed => 0 },
         'OSCO should not be allowed, in any cases'
     );
@@ -107,7 +98,7 @@ subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
         {
             branchcode   => $branch->{branchcode},
             categorycode => $category->{categorycode},
-            itemtype     => '*',
+            itemtype     => undef,
             rules        => {
                 maxissueqty       => 0,
                 maxonsiteissueqty => 0,
@@ -116,7 +107,7 @@ subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
     );
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 0,
@@ -125,7 +116,7 @@ subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_ONSITE_CHECKOUTS',
             count => 0,
@@ -136,7 +127,7 @@ subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 0,
@@ -145,7 +136,7 @@ subtest '1 Issuingrule exist 0 0: no issue allowed' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_ONSITE_CHECKOUTS',
             count => 0,
@@ -164,7 +155,7 @@ subtest '1 Issuingrule exist with onsiteissueqty=unlimited' => sub {
         {
             branchcode   => $branch->{branchcode},
             categorycode => $category->{categorycode},
-            itemtype     => '*',
+            itemtype     => undef,
             rules        => {
                 maxissueqty       => 1,
                 maxonsiteissueqty => undef,
@@ -172,10 +163,10 @@ subtest '1 Issuingrule exist with onsiteissueqty=unlimited' => sub {
         },
     );
 
-    my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string() );
+    my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, dt_from_string() );
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -184,14 +175,14 @@ subtest '1 Issuingrule exist with onsiteissueqty=unlimited' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         undef,
         'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -200,7 +191,7 @@ subtest '1 Issuingrule exist with onsiteissueqty=unlimited' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -219,7 +210,7 @@ subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
         {
             branchcode   => $branch->{branchcode},
             categorycode => $category->{categorycode},
-            itemtype     => '*',
+            itemtype     => undef,
             rules        => {
                 maxissueqty       => 1,
                 maxonsiteissueqty => 1,
@@ -228,24 +219,24 @@ subtest '1 Issuingrule exist 1 1: issue is allowed' => sub {
     );
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         undef,
         'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         undef,
         'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         undef,
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         undef,
         'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
@@ -259,7 +250,7 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
         {
             branchcode   => $branch->{branchcode},
             categorycode => $category->{categorycode},
-            itemtype     => '*',
+            itemtype     => undef,
             rules        => {
                 maxissueqty       => 1,
                 maxonsiteissueqty => 1,
@@ -267,12 +258,12 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
         }
     );
 
-    my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string() );
+    my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, dt_from_string() );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -281,14 +272,14 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         undef,
         'OSCO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -297,7 +288,7 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed. Do a CO' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -315,7 +306,7 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
         {
             branchcode   => $branch->{branchcode},
             categorycode => $category->{categorycode},
-            itemtype     => '*',
+            itemtype     => undef,
             rules        => {
                 maxissueqty       => 1,
                 maxonsiteissueqty => 1,
@@ -323,17 +314,17 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
         }
     );
 
-    my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
+    my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         undef,
         'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_ONSITE_CHECKOUTS',
             count => 1,
@@ -344,7 +335,7 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -353,7 +344,7 @@ subtest '1 Issuingrule exist: 1 CO allowed, 1 OSCO allowed, Do a OSCO' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_ONSITE_CHECKOUTS',
             count => 1,
@@ -382,12 +373,12 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
         }
     );
 
-    my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef );
+    my $issue = C4::Circulation::AddIssue( $patron, $item->barcode, dt_from_string(), undef, undef, undef );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -396,14 +387,14 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
         'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         undef,
         'OSCO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -412,7 +403,7 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -422,18 +413,29 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
     );
 
     teardown();
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => $branch->{branchcode},
+            categorycode => $category->{categorycode},
+            itemtype     => undef,
+            rules        => {
+                maxissueqty       => 1,
+                maxonsiteissueqty => 1,
+            }
+        }
+    );
 
-    $issue = C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
+    $issue = C4::Circulation::AddIssue( $patron, $item->barcode, dt_from_string(), undef, undef, undef, { onsite_checkout => 1 } );
     like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 0);
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         undef,
         'CO should be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 0'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_ONSITE_CHECKOUTS',
             count => 1,
@@ -444,7 +446,7 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
 
     t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -453,7 +455,7 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub {
         'CO should not be allowed if ConsiderOnSiteCheckoutsAsNormalCheckouts == 1'
     );
     is_deeply(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         {
             reason => 'TOO_MANY_ONSITE_CHECKOUTS',
             count => 1,
@@ -489,18 +491,19 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     });
 
     # Set up an issuing rule
-    my $rule = $builder->build({
-        source => 'Issuingrule',
-        value => {
+    Koha::CirculationRules->set_rules(
+        {
             categorycode => '*',
             itemtype     => $itemtype->{itemtype},
             branchcode   => '*',
-            issuelength  => 1,
-            firstremind  => 1,        # 1 day of grace
-            finedays     => 2,        # 2 days of fine per day of overdue
-            lengthunit   => 'days',
+            rules        => {
+                issuelength => 1,
+                firstremind => 1,        # 1 day of grace
+                finedays    => 2,        # 2 days of fine per day of overdue
+                lengthunit  => 'days',
+            }
         }
-    });
+    );
 
     # Set an All->All for an itemtype
     Koha::CirculationRules->set_rules(
@@ -530,7 +533,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     my $issue = C4::Circulation::AddIssue( $patron, $issue_item->barcode, dt_from_string() );
     # We checkout one item
     is_deeply(
-        C4::Circulation::TooMany( $patron, $branch_item->biblionumber, $branch_item->unblessed ),
+        C4::Circulation::TooMany( $patron, $branch_item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -545,7 +548,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     $branch_item->biblio->biblioitem->itemtype($itemtype->{itemtype})->store;
     # We checkout one item
     is_deeply(
-        C4::Circulation::TooMany( $patron, $branch_item->biblionumber, $branch_item->unblessed ),
+        C4::Circulation::TooMany( $patron, $branch_item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -569,7 +572,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     );
 
     is(
-        C4::Circulation::TooMany( $patron, $branch_item->biblionumber, $branch_item->unblessed ),
+        C4::Circulation::TooMany( $patron, $branch_item ),
         undef,
         'We are allowed one from the branch specifically now'
     );
@@ -577,7 +580,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     # If circcontrol is PatronLibrary we count all the patron's loan, regardless of branch
     t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
     is_deeply(
-        C4::Circulation::TooMany( $patron, $branch_item->biblionumber, $branch_item->unblessed ),
+        C4::Circulation::TooMany( $patron, $branch_item ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -596,7 +599,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
         holdingbranch => $branch->{branchcode}
     });
     is_deeply(
-        C4::Circulation::TooMany( $patron, $branch_item_2->biblionumber, $branch_item_2->unblessed ),
+        C4::Circulation::TooMany( $patron, $branch_item_2 ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 1,
@@ -610,7 +613,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
         itype => $itemtype->{itemtype},
     });
     is_deeply(
-        C4::Circulation::TooMany( $patron, $item_2->biblionumber, $item_2->unblessed ),
+        C4::Circulation::TooMany( $patron, $item_2 ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 2,
@@ -620,7 +623,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     );
     t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
     is_deeply(
-        C4::Circulation::TooMany( $patron, $item_2->biblionumber, $item_2->unblessed ),
+        C4::Circulation::TooMany( $patron, $item_2 ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 2,
@@ -631,7 +634,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
 
     t::lib::Mocks::mock_preference('CircControl', 'PickupLibrary');
     is_deeply(
-        C4::Circulation::TooMany( $patron, $item_2->biblionumber, $item_2->unblessed ),
+        C4::Circulation::TooMany( $patron, $item_2 ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 2,
@@ -643,7 +646,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     my $branch2   = $builder->build({source => 'Branch',});
     t::lib::Mocks::mock_userenv({ branchcode => $branch2->{branchcode} });
     is_deeply(
-        C4::Circulation::TooMany( $patron, $item_2->biblionumber, $item_2->unblessed ),
+        C4::Circulation::TooMany( $patron, $item_2 ),
         {
             reason => 'TOO_MANY_CHECKOUTS',
             count => 2,
@@ -665,7 +668,7 @@ subtest 'General vs specific rules limit quantity correctly' => sub {
     );
 
     is(
-        C4::Circulation::TooMany( $patron, $branch_item->biblionumber, $branch_item->unblessed ),
+        C4::Circulation::TooMany( $patron, $branch_item ),
         undef,
         'We are allowed one from the branch specifically now'
     );
@@ -686,15 +689,277 @@ subtest 'empty string means unlimited' => sub {
         },
     );
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item ),
+        C4::Circulation::TooMany( $patron, $item ),
         undef,
         'maxissueqty="" should mean unlimited'
     );
 
     is(
-        C4::Circulation::TooMany( $patron, $biblio->{biblionumber}, $item, { onsite_checkout => 1 } ),
+        C4::Circulation::TooMany( $patron, $item, { onsite_checkout => 1 } ),
         undef,
         'maxonsiteissueqty="" should mean unlimited'
+      );
+};
+
+subtest 'itemtype group tests' => sub {
+    plan tests => 13;
+
+    t::lib::Mocks::mock_preference( 'CircControl', 'ItemHomeLibrary' );
+    Koha::CirculationRules->set_rules(
+        {
+            branchcode   => '*',
+            categorycode => '*',
+            itemtype     => '*',
+            rules        => {
+                maxissueqty       => '',
+                maxonsiteissueqty => '',
+                issuelength       => 1,
+                firstremind       => 1,      # 1 day of grace
+                finedays          => 2,      # 2 days of fine per day of overdue
+                lengthunit        => 'days',
+            }
+        },
+    );
+
+    my $parent_itype = $builder->build(
+        {
+            source => 'Itemtype',
+            value  => {
+                parent_type         => undef,
+                rentalcharge        => undef,
+                rentalcharge_daily  => undef,
+                rentalcharge_hourly => undef,
+                notforloan          => 0,
+            }
+        }
+    );
+    my $child_itype_1 = $builder->build(
+        {
+            source => 'Itemtype',
+            value  => {
+                parent_type         => $parent_itype->{itemtype},
+                rentalcharge        => 0,
+                rentalcharge_daily  => 0,
+                rentalcharge_hourly => 0,
+                notforloan          => 0,
+            }
+        }
+    );
+    my $child_itype_2 = $builder->build(
+        {
+            source => 'Itemtype',
+            value  => {
+                parent_type         => $parent_itype->{itemtype},
+                rentalcharge        => 0,
+                rentalcharge_daily  => 0,
+                rentalcharge_hourly => 0,
+                notforloan          => 0,
+            }
+        }
+    );
+
+    my $branch   = $builder->build( { source => 'Branch', } );
+    my $category = $builder->build( { source => 'Category', } );
+    my $patron   = $builder->build(
+        {
+            source => 'Borrower',
+            value  => {
+                categorycode => $category->{categorycode},
+                branchcode   => $branch->{branchcode},
+            },
+        }
+    );
+    my $item = $builder->build_sample_item(
+        {
+            homebranch    => $branch->{branchcode},
+            holdingbranch => $branch->{branchcode},
+            itype         => $child_itype_1->{itemtype}
+        }
+    );
+
+    my $all_iq_rule = $builder->build(
+        {
+            source => 'CirculationRule',
+            value  => {
+                branchcode   => $branch->{branchcode},
+                categorycode => $category->{categorycode},
+                itemtype     => undef,
+                rule_name    => 'maxissueqty',
+                rule_value   => 1
+            }
+        }
+    );
+    is( C4::Circulation::TooMany( $patron, $item ),
+        undef, 'Checkout allowed, using all rule of 1' );
+
+    #Checkout an item
+    my $issue =
+      C4::Circulation::AddIssue( $patron, $item->barcode, dt_from_string() );
+    like( $issue->issue_id, qr|^\d+$|, 'The issue should have been inserted' );
+
+    #Patron has 1 checkout of child itype1
+
+    my $parent_iq_rule = $builder->build(
+        {
+            source => 'CirculationRule',
+            value  => {
+                branchcode   => $branch->{branchcode},
+                categorycode => $category->{categorycode},
+                itemtype     => $parent_itype->{itemtype},
+                rule_name    => 'maxissueqty',
+                rule_value   => 2
+            }
+        }
+    );
+
+    is( C4::Circulation::TooMany( $patron, $item ),
+        undef, 'Checkout allowed, using parent type rule of 2' );
+
+    my $child1_iq_rule = $builder->build_object(
+        {
+            class => 'Koha::CirculationRules',
+            value => {
+                branchcode   => $branch->{branchcode},
+                categorycode => $category->{categorycode},
+                itemtype     => $child_itype_1->{itemtype},
+                rule_name    => 'maxissueqty',
+                rule_value   => 1
+            }
+        }
+    );
+
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $item ),
+        {
+            reason      => 'TOO_MANY_CHECKOUTS',
+            count       => 1,
+            max_allowed => 1,
+        },
+        'Checkout not allowed, using specific type rule of 1'
+    );
+
+    my $item_1 = $builder->build_sample_item(
+        {
+            homebranch    => $branch->{branchcode},
+            holdingbranch => $branch->{branchcode},
+            itype         => $child_itype_2->{itemtype}
+        }
+    );
+
+    my $child2_iq_rule = $builder->build(
+        {
+            source => 'CirculationRule',
+            value  => {
+                branchcode   => $branch->{branchcode},
+                categorycode => $category->{categorycode},
+                itemtype     => $child_itype_2->{itemtype},
+                rule_name    => 'maxissueqty',
+                rule_value   => 3
+            }
+        }
+    );
+
+    is( C4::Circulation::TooMany( $patron, $item_1 ),
+        undef, 'Checkout allowed' );
+
+    #checkout an item
+    $issue =
+      C4::Circulation::AddIssue( $patron, $item_1->barcode, dt_from_string() );
+    like( $issue->issue_id, qr|^\d+$|, 'the issue should have been inserted' );
+
+    #patron has 1 checkout of childitype1 and 1 checkout of childitype2
+
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $item ),
+        {
+            reason      => 'TOO_MANY_CHECKOUTS',
+            count       => 2,
+            max_allowed => 2,
+        },
+'Checkout not allowed, using parent type rule of 2, checkout of sibling itemtype counted'
+    );
+
+    my $parent_item = $builder->build_sample_item(
+        {
+            homebranch    => $branch->{branchcode},
+            holdingbranch => $branch->{branchcode},
+            itype         => $parent_itype->{itemtype}
+        }
+    );
+
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $parent_item ),
+        {
+            reason      => 'TOO_MANY_CHECKOUTS',
+            count       => 2,
+            max_allowed => 2,
+        },
+'Checkout not allowed, using parent type rule of 2, checkout of child itemtypes counted'
+    );
+
+    #increase parent type to greater than specific
+    my $circ_rule_object =
+      Koha::CirculationRules->find( $parent_iq_rule->{id} );
+    $circ_rule_object->rule_value(4)->store();
+
+    is( C4::Circulation::TooMany( $patron, $item_1 ),
+        undef, 'Checkout allowed, using specific type rule of 3' );
+
+    my $item_2 = $builder->build_sample_item(
+        {
+            homebranch    => $branch->{branchcode},
+            holdingbranch => $branch->{branchcode},
+            itype         => $child_itype_2->{itemtype}
+        }
+    );
+
+    #checkout an item
+    $issue =
+      C4::Circulation::AddIssue( $patron, $item_2->barcode, dt_from_string(),
+        undef, undef, undef );
+    like( $issue->issue_id, qr|^\d+$|, 'the issue should have been inserted' );
+
+    #patron has 1 checkout of childitype1 and 2 of childitype2
+
+    is(
+        C4::Circulation::TooMany( $patron, $item_2 ),
+        undef,
+'Checkout allowed, using specific type rule of 3, checkout of sibling itemtype not counted'
+    );
+
+    $child1_iq_rule->rule_value(2)->store(); #Allow 2 checkouts for child type 1
+
+    my $item_3 = $builder->build_sample_item(
+        {
+            homebranch    => $branch->{branchcode},
+            holdingbranch => $branch->{branchcode},
+            itype         => $child_itype_1->{itemtype}
+        }
+    );
+    my $item_4 = $builder->build_sample_item(
+        {
+            homebranch    => $branch->{branchcode},
+            holdingbranch => $branch->{branchcode},
+            itype         => $child_itype_2->{itemtype}
+        }
+    );
+
+    #checkout an item
+    $issue =
+      C4::Circulation::AddIssue( $patron, $item_4->barcode, dt_from_string(),
+        undef, undef, undef );
+    like( $issue->issue_id, qr|^\d+$|, 'the issue should have been inserted' );
+
+    #patron has 1 checkout of childitype 1 and 3 of childitype2
+
+    is_deeply(
+        C4::Circulation::TooMany( $patron, $item_3 ),
+        {
+            reason      => 'TOO_MANY_CHECKOUTS',
+            max_allowed => 4,
+            count       => 4,
+        },
+'Checkout not allowed, using specific type rule of 2, checkout of sibling itemtype not counted, but parent rule (4) prevents another'
     );
 
     teardown();
@@ -704,6 +969,6 @@ $schema->storage->txn_rollback;
 
 sub teardown {
     $dbh->do(q|DELETE FROM issues|);
-    $dbh->do(q|DELETE FROM issuingrules|);
+    $dbh->do(q|DELETE FROM circulation_rules|);
 }