Bug 15407: Koha::Patron::Categories - replace C4::Category->all
[koha.git] / t / db_dependent / Koha / Patron / Categories.t
index df0327c..2a12fdb 100644 (file)
@@ -19,8 +19,9 @@
 
 use Modern::Perl;
 
-use Test::More tests => 7;
+use Test::More tests => 11;
 
+use C4::Context;
 use Koha::Database;
 use Koha::Patron::Category;
 use Koha::Patron::Categories;
@@ -54,8 +55,24 @@ my $retrieved_category_2 = Koha::Patron::Categories->find( $new_category_2->cate
 is( $retrieved_category_1->checkprevcheckout, 'inherit', 'Koha::Patron::Category->store should default checkprevcheckout to inherit' );
 is( $retrieved_category_2->checkprevcheckout, 'inherit', 'Koha::Patron::Category->store should default checkprevcheckout to inherit' );
 
+my $another_branch = $builder->build( { source => 'Branch', } );
+C4::Context->_new_userenv('my_new_userenv');
+C4::Context->set_userenv( 0, 0, 'usercnum', 'firstname', 'surname', $another_branch->{branchcode}, 'My wonderful library', '', '', '' );
+my $new_category_3 = Koha::Patron::Category->new(
+    {   categorycode => 'mycatcodeZ',
+        description  => 'mycatdescZ',
+    }
+)->store;
+$new_category_3->add_branch_limitation( $another_branch->{branchcode} );
+is( Koha::Patron::Categories->search->count, $nb_of_categories + 3, 'The 3rd patron category should have been added' );
+my @limited_categories = Koha::Patron::Categories->search_limited;
+my @limited_category_codes = map { $_->categorycode } @limited_categories;
+is( scalar( grep { $_ eq $new_category_1->categorycode } @limited_category_codes ), 0, 'The first category is limited to another branch' );
+is( scalar( grep { $_ eq $new_category_2->categorycode } @limited_category_codes ), 1, 'The second category is not limited' );
+is( scalar( grep { $_ eq $new_category_3->categorycode } @limited_category_codes ), 1, 'The third category is limited to my branch ' );
+
 $retrieved_category_1->delete;
-is( Koha::Patron::Categories->search->count, $nb_of_categories + 1, 'Delete should have deleted the patron category' );
+is( Koha::Patron::Categories->search->count, $nb_of_categories + 2, 'Delete should have deleted the patron category' );
 
 $schema->storage->txn_rollback;