Bug 27272: Remove GetItemsInfo, GetItemsLocationInfo and GetHostItemsInfo
[koha-ffzg.git] / t / db_dependent / AuthorisedValues.t
old mode 100644 (file)
new mode 100755 (executable)
index ad5ec60..e9f64ed
@@ -1,7 +1,8 @@
 #!/usr/bin/perl
 
 use Modern::Perl;
-use Test::More tests => 15;
+use Test::More tests => 16;
+use Try::Tiny;
 
 use t::lib::TestBuilder;
 
@@ -16,11 +17,10 @@ my $schema  = Koha::Database->new->schema;
 $schema->storage->txn_begin;
 my $builder = t::lib::TestBuilder->new;
 
-Koha::AuthorisedValues->delete;
-Koha::AuthorisedValueCategories->delete;
+my @existing_categories = Koha::AuthorisedValues->new->categories;
 
 # insert
-Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store;
+Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing', is_system => 1 })->store;
 Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store;
 Koha::AuthorisedValueCategory->new({ category_name => 'restricted_for_testing' })->store;
 my $av1 = Koha::AuthorisedValue->new(
@@ -84,59 +84,87 @@ ok( $av2->id(), 'AV 2 is inserted' );
 ok( $av3->id(), 'AV 3 is inserted' );
 ok( $av4->id(), 'AV 4 is inserted' );
 
+{ # delete is_system AV categories
+    try {
+        Koha::AuthorisedValueCategories->find('av_for_testing')->delete
+    }
+    catch {
+        ok(
+            $_->isa('Koha::Exceptions::CannotDeleteDefault'),
+            'A system AV category cannot be deleted'
+        );
+    };
+
+    try {
+        Koha::AuthorisedValueCategories->search->delete
+    }
+    catch {
+        ok(
+            $_->isa('Koha::Exceptions::CannotDeleteDefault'),
+            'system AV categories cannot be deleted'
+        );
+    };
+}
+
 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
 $av3->lib_opac('');
 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
 
 my @authorised_values =
-  Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
+  Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } )->as_list;
 is( @authorised_values, 3, "Get correct number of values" );
 
 my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
 
-$av1->add_branch_limitation( $branchcode1 );
+$av1->add_library_limit( $branchcode1 );
 
-@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
+@authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode1 )->as_list;
 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
 
-@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
+@authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode2 )->as_list;
 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
 
-$av1->del_branch_limitation( $branchcode1 );
-@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
+$av1->del_library_limit( $branchcode1 );
+@authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode2 )->as_list;
 is( @authorised_values, 3, "Branch limitation deleted successfully" );
 
-$av1->add_branch_limitation( $branchcode1 );
-$av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
+$av1->add_library_limit( $branchcode1 );
+$av1->library_limits( [ $branchcode1, $branchcode2 ] );
 
-my $limits = $av1->branch_limitations;
-is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
+my $limits = $av1->library_limits->as_list;
+is( @$limits, 2, 'library_limits functions correctly both as setter and getter' );
 
 my @categories = Koha::AuthorisedValues->new->categories;
-is( @categories, 3, 'There should have 2 categories inserted' );
-is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
-is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
+is( @categories, @existing_categories+3, 'There should have 3 categories inserted' );
+is_deeply(
+    \@categories,
+    [ sort { uc $a cmp uc $b } @categories ],
+    'categories must be ordered by category names'
+);
 
 subtest 'search_by_*_field + find_by_koha_field + get_description' => sub {
     plan tests => 5;
-    my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
-    $loc_cat->delete if $loc_cat;
+
+    my $test_cat = Koha::AuthorisedValueCategories->find('TEST');
+    $test_cat->delete if $test_cat;
     my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
     $mss->delete if $mss;
+    $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ' } );
+    $mss->delete if $mss;
     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
     $mss->delete if $mss;
     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => '5', frameworkcode => '' } );
     $mss->delete if $mss;
-    Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store;
+    Koha::AuthorisedValueCategory->new( { category_name => 'TEST' } )->store;
     Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
-    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
-    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
+    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'TEST', kohafield => 'items.location' } )->store;
+    Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'TEST', kohafield => 'items.location' } )->store;
     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => '5', frameworkcode => '', authorised_value => 'restricted_for_testing', kohafield => 'items.restricted' } )->store;
-    Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store;
-    Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store;
-    Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store;
+    Koha::AuthorisedValue->new( { category => 'TEST', authorised_value => 'location_1', lib => 'location_1' } )->store;
+    Koha::AuthorisedValue->new( { category => 'TEST', authorised_value => 'location_2', lib => 'location_2' } )->store;
+    Koha::AuthorisedValue->new( { category => 'TEST', authorised_value => 'location_3', lib => 'location_3' } )->store;
     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
     subtest 'search_by_marc_field' => sub {
@@ -155,7 +183,7 @@ subtest 'search_by_*_field + find_by_koha_field + get_description' => sub {
         my $avs;
         $avs = Koha::AuthorisedValues->search_by_koha_field();
         is ( $avs, undef );
-        $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location', tagfield => 952, tagsubfield => 'c' } );
+        $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location' } );
         is( $avs->count,                  3, );
         is( $avs->next->authorised_value, 'location_1', );
 
@@ -212,14 +240,14 @@ subtest 'search_by_*_field + find_by_koha_field + get_description' => sub {
             \@descriptions,
             [
                 {
-                    authorised_value => '',
-                    lib              => $av_empty_string->lib,
-                    opac_description => $av_empty_string->lib_opac
-                },
-                {
                     authorised_value => $av_0->authorised_value,
                     lib              => $av_0->lib,
                     opac_description => $av_0->lib_opac
+                },
+                {
+                    authorised_value => '',
+                    lib              => $av_empty_string->lib,
+                    opac_description => $av_empty_string->lib_opac
                 }
             ],
         );