Bug 17249: Remove GetKohaAuthorisedValuesFromField - Add search_by_marc_field
[koha.git] / t / db_dependent / AuthorisedValues.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 15;
5
6 use C4::Context;
7 use Koha::AuthorisedValue;
8 use Koha::AuthorisedValues;
9 use Koha::AuthorisedValueCategories;
10 use Koha::MarcSubfieldStructures;
11
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
15
16 $dbh->do("DELETE FROM authorised_values");
17 $dbh->do("DELETE FROM authorised_value_categories");
18
19 # insert
20 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store;
21 Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store;
22 my $av1 = Koha::AuthorisedValue->new(
23     {
24         category         => 'av_for_testing',
25         authorised_value => 'value 1',
26         lib              => 'display value 1',
27         lib_opac         => 'opac display value 1',
28         imageurl         => 'image1.png',
29     }
30 )->store();
31
32 my $av2 = Koha::AuthorisedValue->new(
33     {
34         category         => 'av_for_testing',
35         authorised_value => 'value 2',
36         lib              => 'display value 2',
37         lib_opac         => 'opac display value 2',
38         imageurl         => 'image2.png',
39     }
40 )->store();
41
42 my $av3 = Koha::AuthorisedValue->new(
43     {
44         category         => 'av_for_testing',
45         authorised_value => 'value 3',
46         lib              => 'display value 3',
47         lib_opac         => 'opac display value 3',
48         imageurl         => 'image2.png',
49     }
50 )->store();
51
52 my $av4 = Koha::AuthorisedValue->new(
53     {
54         category         => 'aaav_for_testing',
55         authorised_value => 'value 4',
56         lib              => 'display value 4',
57         lib_opac         => 'opac display value 4',
58         imageurl         => 'image4.png',
59     }
60 )->store();
61
62 ok( $av1->id(), 'AV 1 is inserted' );
63 ok( $av2->id(), 'AV 2 is inserted' );
64 ok( $av3->id(), 'AV 3 is inserted' );
65 ok( $av4->id(), 'AV 4 is inserted' );
66
67 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
68 $av3->lib_opac('');
69 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
70
71 my @authorised_values =
72   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
73 is( @authorised_values, 3, "Get correct number of values" );
74
75 my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
76 my $branch1 = $branches_rs->next();
77 my $branchcode1 = $branch1->branchcode();
78 my $branch2 = $branches_rs->next();
79 my $branchcode2 = $branch2->branchcode();
80
81 $av1->add_branch_limitation( $branchcode1 );
82
83 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
84 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
85
86 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
87 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
88
89 $av1->del_branch_limitation( $branchcode1 );
90 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
91 is( @authorised_values, 3, "Branch limitation deleted successfully" );
92
93 $av1->add_branch_limitation( $branchcode1 );
94 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
95
96 my $limits = $av1->branch_limitations;
97 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
98
99 my @categories = Koha::AuthorisedValues->new->categories;
100 is( @categories, 2, 'There should have 2 categories inserted' );
101 is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
102 is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
103
104 subtest 'search_by_*_field' => sub {
105     plan tests => 1;
106     my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
107     $loc_cat->delete if $loc_cat;
108     my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
109     $mss->delete if $mss;
110     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
111     $mss->delete if $mss;
112     Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store;
113     Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
114     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
115     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
116     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
117     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store;
118     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store;
119     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store;
120     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
121     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
122     subtest 'search_by_marc_field' => sub {
123         plan tests => 4;
124         my $avs;
125         $avs = Koha::AuthorisedValues->search_by_marc_field();
126         is ( $avs, undef );
127         $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
128         is ( $avs, undef );
129         $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'});
130         is( $avs->count, 3, 'default fk');
131         is( $avs->next->authorised_value, 'location_1', );
132     };
133 };