4 use Test::More tests => 15;
7 use Koha::AuthorisedValue;
8 use Koha::AuthorisedValues;
9 use Koha::AuthorisedValueCategories;
10 use Koha::MarcSubfieldStructures;
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
16 $dbh->do("DELETE FROM authorised_values");
17 $dbh->do("DELETE FROM authorised_value_categories");
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(
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',
32 my $av2 = Koha::AuthorisedValue->new(
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',
42 my $av3 = Koha::AuthorisedValue->new(
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',
52 my $av4 = Koha::AuthorisedValue->new(
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',
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' );
67 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
69 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
71 my @authorised_values =
72 Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
73 is( @authorised_values, 3, "Get correct number of values" );
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();
81 $av1->add_branch_limitation( $branchcode1 );
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" );
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" );
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" );
93 $av1->add_branch_limitation( $branchcode1 );
94 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
96 my $limits = $av1->branch_limitations;
97 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
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)' );
104 subtest 'search_by_*_field' => sub {
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 {
125 $avs = Koha::AuthorisedValues->search_by_marc_field();
127 $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
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', );
133 subtest 'search_by_koha_field' => sub {
136 $avs = Koha::AuthorisedValues->search_by_koha_field();
138 $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location', tagfield => 952, tagsubfield => 'c' } );
139 is( $avs->count, 3, );
140 is( $avs->next->authorised_value, 'location_1', );