Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

195 lines
7.9 KiB

  1. #!/usr/bin/perl
  2. # This file is part of Koha.
  3. #
  4. # Koha is free software; you can redistribute it and/or modify it under the
  5. # terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
  10. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along
  14. # with Koha; if not, see <http://www.gnu.org/licenses>.
  15. use Modern::Perl;
  16. use Test::More tests => 3;
  17. use Test::MockModule;
  18. use C4::Context;
  19. use C4::Biblio qw(AddBiblio);
  20. use Koha::Database;
  21. use Clone qw(clone);
  22. use List::MoreUtils qw(any);
  23. use t::lib::TestBuilder;
  24. use t::lib::Mocks;
  25. BEGIN {
  26. use_ok('Koha::Template::Plugin::Branches');
  27. }
  28. my $schema = Koha::Database->schema;
  29. my $builder = t::lib::TestBuilder->new;
  30. subtest 'all() tests' => sub {
  31. plan tests => 18;
  32. $schema->storage->txn_begin;
  33. my $library = $builder->build({
  34. source => 'Branch',
  35. value => {
  36. branchcode => 'MYLIBRARY',
  37. branchname => 'My sweet library'
  38. }
  39. });
  40. my $another_library = $builder->build({
  41. source => 'Branch',
  42. value => {
  43. branchcode => 'ANOTHERLIB',
  44. }
  45. });
  46. my $plugin = Koha::Template::Plugin::Branches->new();
  47. ok($plugin, "initialized Branches plugin");
  48. my $name = $plugin->GetName($library->{branchcode});
  49. is($name, $library->{branchname}, 'retrieved expected name for library');
  50. $name = $plugin->GetName('__ANY__');
  51. is($name, '', 'received empty string as name of the "__ANY__" placeholder library code');
  52. $name = $plugin->GetName(undef);
  53. is($name, '', 'received empty string as name of NULL/undefined library code');
  54. is($plugin->GetLoggedInBranchcode(), '', 'no active library code if there is no active user session');
  55. is($plugin->GetLoggedInBranchname(), '', 'no active library name if there is no active user session');
  56. t::lib::Mocks::mock_userenv({ branchcode => 'MYLIBRARY', branchname => 'My sweet library' });
  57. is($plugin->GetLoggedInBranchcode(), 'MYLIBRARY', 'GetLoggedInBranchcode() returns active library code');
  58. is($plugin->GetLoggedInBranchname(), 'My sweet library', 'GetLoggedInBranchname() returns active library name');
  59. t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
  60. my $libraries = $plugin->all();
  61. ok( scalar(@$libraries) > 1, 'If IndependentBranches is not set, all libraries should be returned' );
  62. is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and $_->{selected} == 1 } @$libraries ), 1, 'Without selected parameter, my library should be preselected' );
  63. is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and not exists $_->{selected} } @$libraries ), 1, 'Without selected parameter, other library should not be preselected' );
  64. $libraries = $plugin->all( { selected => 'ANOTHERLIB' } );
  65. is( grep ( { $_->{branchcode} eq 'MYLIBRARY' and not exists $_->{selected} } @$libraries ), 1, 'With selected parameter, my library should not be preselected' );
  66. is( grep ( { $_->{branchcode} eq 'ANOTHERLIB' and $_->{selected} == 1 } @$libraries ), 1, 'With selected parameter, other library should be preselected' );
  67. $libraries = $plugin->all( { selected => '' } );
  68. is( grep ( { exists $_->{selected} } @$libraries ), 0, 'With selected parameter set to an empty string, no library should be preselected' );
  69. my $total = @{$plugin->all};
  70. my $pickupable = @{$plugin->all( { search_params => { pickup_location => 1 } }) };
  71. my $yet_another_library = $builder->build({
  72. source => 'Branch',
  73. value => {
  74. branchcode => 'CANTPICKUP',
  75. pickup_location => 0,
  76. }
  77. });
  78. is(@{$plugin->all( { search_params => { pickup_location => 1 } }) }, $pickupable,
  79. 'Adding a new library with pickups'
  80. .' disabled does not increase the amount returned by ->pickup_locations');
  81. is(@{$plugin->all}, $total+1, 'However, adding a new library increases'
  82. .' the total amount gotten with ->all');
  83. t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
  84. $libraries = $plugin->all();
  85. is( scalar(@$libraries), 1, 'If IndependentBranches is set, only 1 library should be returned' );
  86. $libraries = $plugin->all( { unfiltered => 1 } );
  87. ok( scalar(@$libraries) > 1, 'If IndependentBranches is set, all libraries should be returned if the unfiltered flag is set' );
  88. $schema->storage->txn_rollback;
  89. };
  90. subtest 'pickup_locations() tests' => sub {
  91. plan tests => 8;
  92. $schema->storage->txn_begin;
  93. Koha::Libraries->search->update({ pickup_location => 0 });
  94. my $library_1 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
  95. my $library_2 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
  96. my $library_3 = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
  97. my $plugin = Koha::Template::Plugin::Branches->new();
  98. my $pickup_locations = $plugin->pickup_locations();
  99. is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
  100. $pickup_locations = $plugin->pickup_locations({ search_params => { item => undef }});
  101. is( scalar @{$pickup_locations}, 3, 'item parameter not a ref, fallback to general search' );
  102. $pickup_locations = $plugin->pickup_locations({ search_params => { biblio => undef }});
  103. is( scalar @{$pickup_locations}, 3, 'biblio parameter not a ref, fallback to general search' );
  104. my $item_class = Test::MockModule->new('Koha::Item');
  105. $item_class->mock(
  106. 'pickup_locations',
  107. sub {
  108. return Koha::Libraries->search(
  109. { branchcode => $library_1->branchcode } );
  110. }
  111. );
  112. my $item = $builder->build_sample_item();
  113. my $patron = $builder->build_object({ class => 'Koha::Patrons' });
  114. $pickup_locations = $plugin->pickup_locations(
  115. { search_params => { item => $item, patron => Koha::Patron->new } } );
  116. is( scalar @{$pickup_locations}, 1, 'Only the library returned by $item->pickup_locations is returned' );
  117. is( $pickup_locations->[0]->{branchcode}, $library_1->branchcode, 'Not cheating' );
  118. my $biblio_class = Test::MockModule->new('Koha::Biblio');
  119. $biblio_class->mock(
  120. 'pickup_locations',
  121. sub {
  122. return [$library_2];
  123. }
  124. );
  125. my $biblio = $builder->build_sample_biblio();
  126. $pickup_locations = $plugin->pickup_locations(
  127. { search_params => { biblio => $biblio, patron => Koha::Patron->new } } );
  128. is( scalar @{$pickup_locations}, 1, 'Only the library returned by $biblio->pickup_locations is returned' );
  129. is( $pickup_locations->[0]->{branchcode}, $library_2->branchcode, 'Not cheating' );
  130. subtest 'selected tests' => sub {
  131. plan tests => 4;
  132. t::lib::Mocks::mock_userenv({ branchcode => $library_2->branchcode });
  133. $pickup_locations = $plugin->pickup_locations();
  134. is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
  135. foreach my $pickup_location (@{ $pickup_locations }) {
  136. next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
  137. is( $pickup_location->{branchcode}, $library_2->branchcode, 'The right library is marked as selected' );
  138. }
  139. $pickup_locations = $plugin->pickup_locations({ selected => $library_3->branchcode });
  140. is( scalar @{$pickup_locations}, 3, 'Libraries count is correct' );
  141. foreach my $pickup_location (@{ $pickup_locations }) {
  142. next unless exists $pickup_location->{selected} and $pickup_location->{selected} == 1;
  143. is( $pickup_location->{branchcode}, $library_3->branchcode, 'The right library is marked as selected' );
  144. }
  145. };
  146. $schema->storage->txn_rollback;
  147. };