| 
      
            Line 0
          
      
      
        Link Here
      
     | 
  
          
            
              | 0 | 
              -   | 
              1 | 
              #!/usr/bin/perl  | 
            
            
               | 
               | 
              2 | 
               | 
            
            
              | 3 | 
              # This file is part of Koha.  | 
            
            
              | 4 | 
              #  | 
            
            
              | 5 | 
              # Copyright PTFS Europe 2024  | 
            
            
              | 6 | 
              #  | 
            
            
              | 7 | 
              # Koha is free software; you can redistribute it and/or modify it  | 
            
            
              | 8 | 
              # under the terms of the GNU General Public License as published by  | 
            
            
              | 9 | 
              # the Free Software Foundation; either version 3 of the License, or  | 
            
            
              | 10 | 
              # (at your option) any later version.  | 
            
            
              | 11 | 
              #  | 
            
            
              | 12 | 
              # Koha is distributed in the hope that it will be useful, but  | 
            
            
              | 13 | 
              # WITHOUT ANY WARRANTY; without even the implied warranty of  | 
            
            
              | 14 | 
              # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  | 
            
            
              | 15 | 
              # GNU General Public License for more details.  | 
            
            
              | 16 | 
              #  | 
            
            
              | 17 | 
              # You should have received a copy of the GNU General Public License  | 
            
            
              | 18 | 
              # along with Koha; if not, see <http://www.gnu.org/licenses>.  | 
            
            
              | 19 | 
               | 
            
            
              | 20 | 
              use Modern::Perl;  | 
            
            
              | 21 | 
              use Test::More tests => 1;  | 
            
            
              | 22 | 
              use t::lib::TestBuilder;  | 
            
            
              | 23 | 
              use t::lib::Mocks;  | 
            
            
              | 24 | 
               | 
            
            
              | 25 | 
              my $builder = t::lib::TestBuilder->new;  | 
            
            
              | 26 | 
              my $schema  = Koha::Database->schema;  | 
            
            
              | 27 | 
               | 
            
            
              | 28 | 
              subtest 'define_library_group_limits' => sub { | 
            
            
              | 29 | 
                  plan tests => 8;  | 
            
            
              | 30 | 
               | 
            
            
              | 31 | 
                  $schema->txn_begin;  | 
            
            
              | 32 | 
               | 
            
            
              | 33 | 
                  Koha::Acquisition::Baskets->delete;  | 
            
            
              | 34 | 
                  Koha::Acquisition::Booksellers->delete;  | 
            
            
              | 35 | 
               | 
            
            
              | 36 | 
                  my $library1 = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 37 | 
                  my $library2 = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 38 | 
                  my $library3 = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 39 | 
                  my $library4 = $builder->build_object( { class => 'Koha::Libraries' } ); | 
            
            
              | 40 | 
               | 
            
            
              | 41 | 
                  # Group 1 - two sub groups: A, B. Sub group A then has two sub groups: A1, A2  | 
            
            
              | 42 | 
                  my $root_group1 = Koha::Library::Group->new(  | 
            
            
              | 43 | 
                      { | 
            
            
              | 44 | 
                          title => "LibGroup1",  | 
            
            
              | 45 | 
                      }  | 
            
            
              | 46 | 
                  )->store();  | 
            
            
              | 47 | 
               | 
            
            
              | 48 | 
                  my $lg1groupA =  | 
            
            
              | 49 | 
                      Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupA' } )->store(); | 
            
            
              | 50 | 
                  my $lg1groupA1 =  | 
            
            
              | 51 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup1' } )->store(); | 
            
            
              | 52 | 
                  my $lg1groupA2 =  | 
            
            
              | 53 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupA->id, title => 'LibGroup1 SubGroupA SubGroup2' } )->store(); | 
            
            
              | 54 | 
                  my $lg1groupB =  | 
            
            
              | 55 | 
                      Koha::Library::Group->new( { parent_id => $root_group1->id, title => 'LibGroup1 SubGroupB' } )->store(); | 
            
            
              | 56 | 
               | 
            
            
              | 57 | 
                  my $lg1groupA_library1 =  | 
            
            
              | 58 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupA->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 59 | 
                  my $lg1groupA_library2 =  | 
            
            
              | 60 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupA->id, branchcode => $library2->branchcode } )->store(); | 
            
            
              | 61 | 
                  my $lg1groupB_library1 =  | 
            
            
              | 62 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupB->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 63 | 
                  my $lg1groupA1_library1 =  | 
            
            
              | 64 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupA1->id, branchcode => $library2->branchcode } )->store(); | 
            
            
              | 65 | 
                  my $lg1groupA2_library1 =  | 
            
            
              | 66 | 
                      Koha::Library::Group->new( { parent_id => $lg1groupA2->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 67 | 
               | 
            
            
              | 68 | 
                  # Group 2 - three sub groups: A, B, C. Sub group A then has two sub groups: A1, A2 and sub group C has two: C1, C2  | 
            
            
              | 69 | 
                  my $root_group2 = Koha::Library::Group->new(  | 
            
            
              | 70 | 
                      { | 
            
            
              | 71 | 
                          title => "LibGroup2",  | 
            
            
              | 72 | 
                      }  | 
            
            
              | 73 | 
                  )->store();  | 
            
            
              | 74 | 
               | 
            
            
              | 75 | 
                  my $lg2groupA =  | 
            
            
              | 76 | 
                      Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupA' } )->store(); | 
            
            
              | 77 | 
                  my $lg2groupA1 =  | 
            
            
              | 78 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupA->id, title => 'LibGroup2 SubGroupA SubGroup1' } )->store(); | 
            
            
              | 79 | 
                  my $lg2groupA2 =  | 
            
            
              | 80 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupA->id, title => 'LibGroup2 SubGroupA SubGroup2' } )->store(); | 
            
            
              | 81 | 
                  my $lg2groupB =  | 
            
            
              | 82 | 
                      Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupB' } )->store(); | 
            
            
              | 83 | 
                  my $lg2groupC =  | 
            
            
              | 84 | 
                      Koha::Library::Group->new( { parent_id => $root_group2->id, title => 'LibGroup2 SubGroupC' } )->store(); | 
            
            
              | 85 | 
                  my $lg2groupC1 =  | 
            
            
              | 86 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC->id, title => 'LibGroup2 SubGroupC SubGroup1' } )->store(); | 
            
            
              | 87 | 
                  my $lg2groupC2 =  | 
            
            
              | 88 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC->id, title => 'LibGroup2 SubGroupC SubGroup2' } )->store(); | 
            
            
              | 89 | 
               | 
            
            
              | 90 | 
                  my $lg2groupA_library1 =  | 
            
            
              | 91 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupA->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 92 | 
                  my $lg2groupA_library2 =  | 
            
            
              | 93 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupA->id, branchcode => $library3->branchcode } )->store(); | 
            
            
              | 94 | 
                  my $lg2groupB_library1 =  | 
            
            
              | 95 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupB->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 96 | 
                  my $lg2groupC_library1 =  | 
            
            
              | 97 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 98 | 
                  my $lg2groupC_library2 =  | 
            
            
              | 99 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library3->branchcode } )->store(); | 
            
            
              | 100 | 
                  my $lg2groupC_library3 =  | 
            
            
              | 101 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC->id, branchcode => $library4->branchcode } )->store(); | 
            
            
              | 102 | 
                  my $lg2groupA1_library1 =  | 
            
            
              | 103 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupA1->id, branchcode => $library3->branchcode } )->store(); | 
            
            
              | 104 | 
                  my $lg2groupA2_library1 =  | 
            
            
              | 105 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupA2->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 106 | 
                  my $lg2groupC1_library1 =  | 
            
            
              | 107 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC1->id, branchcode => $library3->branchcode } )->store(); | 
            
            
              | 108 | 
                  my $lg2groupC2_library1 =  | 
            
            
              | 109 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC2->id, branchcode => $library1->branchcode } )->store(); | 
            
            
              | 110 | 
                  my $lg2groupC2_library2 =  | 
            
            
              | 111 | 
                      Koha::Library::Group->new( { parent_id => $lg2groupC2->id, branchcode => $library4->branchcode } )->store(); | 
            
            
              | 112 | 
               | 
            
            
              | 113 | 
                  my $vendor1 = $builder->build_object(  | 
            
            
              | 114 | 
                      { | 
            
            
              | 115 | 
                          class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $root_group1->id . "|" } | 
            
            
              | 116 | 
                      }  | 
            
            
              | 117 | 
                  );  | 
            
            
              | 118 | 
                  my $vendor2 = $builder->build_object(  | 
            
            
              | 119 | 
                      { | 
            
            
              | 120 | 
                          class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $root_group2->id . "|" } | 
            
            
              | 121 | 
                      }  | 
            
            
              | 122 | 
                  );  | 
            
            
              | 123 | 
               | 
            
            
              | 124 | 
                  t::lib::Mocks::mock_userenv( { branchcode => $library1->branchcode } ); | 
            
            
              | 125 | 
                  my $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 126 | 
                  is( scalar(@$vendors), 2, "Two vendors returned" );  | 
            
            
              | 127 | 
               | 
            
            
              | 128 | 
                  t::lib::Mocks::mock_userenv( { branchcode => $library2->branchcode } ); | 
            
            
              | 129 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 130 | 
                  is( scalar(@$vendors), 1, "One vendor returned as library2 is not in root_group2" );  | 
            
            
              | 131 | 
               | 
            
            
              | 132 | 
                  my $vendor3 = $builder->build_object(  | 
            
            
              | 133 | 
                      { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $lg2groupA->id . "|" } } | 
            
            
              | 134 | 
                  );  | 
            
            
              | 135 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 136 | 
                  is( scalar(@$vendors), 1, "Still only one vendor returned as library2 is not in root_group2 or lg2groupA" );  | 
            
            
              | 137 | 
               | 
            
            
              | 138 | 
                  t::lib::Mocks::mock_userenv( { branchcode => $library3->branchcode } ); | 
            
            
              | 139 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 140 | 
                  is( scalar(@$vendors), 2, "Two vendors now returned as library3 is in lg2groupA and root_group2" );  | 
            
            
              | 141 | 
               | 
            
            
              | 142 | 
                  my $vendor4 = $builder->build_object(  | 
            
            
              | 143 | 
                      { class => 'Koha::Acquisition::Booksellers', value => { lib_group_visibility => "|" . $lg2groupB->id . "|" } } | 
            
            
              | 144 | 
                  );  | 
            
            
              | 145 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 146 | 
                  is(  | 
            
            
              | 147 | 
                      scalar(@$vendors), 2,  | 
            
            
              | 148 | 
                      "Still two vendors returned as library3 is in lg2groupA and root_group2 but not lg2groupB"  | 
            
            
              | 149 | 
                  );  | 
            
            
              | 150 | 
               | 
            
            
              | 151 | 
                  t::lib::Mocks::mock_userenv( { branchcode => $library4->branchcode } ); | 
            
            
              | 152 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 153 | 
                  is(  | 
            
            
              | 154 | 
                      scalar(@$vendors), 1,  | 
            
            
              | 155 | 
                      "One vendor returned as library2 is only in root_group2 but not the previously assigned sub groups"  | 
            
            
              | 156 | 
                  );  | 
            
            
              | 157 | 
                  $vendor2->lib_group_visibility( "|" . $root_group1->id . "|" )->store;  | 
            
            
              | 158 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 159 | 
                  is( scalar(@$vendors), 0, "No vendors returned as none are visible to this branch code" );  | 
            
            
              | 160 | 
               | 
            
            
              | 161 | 
                  $vendor2->lib_group_visibility(undef)->store;  | 
            
            
              | 162 | 
                  $vendors = Koha::Acquisition::Booksellers->search()->as_list;  | 
            
            
              | 163 | 
                  is(  | 
            
            
              | 164 | 
                      scalar(@$vendors), 1,  | 
            
            
              | 165 | 
                      "One vendor returned as its lib_group_visibility is now undefined and so is visible to all"  | 
            
            
              | 166 | 
                  );  | 
            
            
              | 167 | 
               | 
            
            
              | 168 | 
                  $schema->txn_rollback;  | 
            
            
              | 169 | 
              };  |