4 use Test::More tests => 14;
7 use Koha::AuthorisedValue;
8 use Koha::AuthorisedValues;
9 use Koha::AuthorisedValueCategories;
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
15 $dbh->do("DELETE FROM authorised_values");
16 $dbh->do("DELETE FROM authorised_value_categories");
19 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store;
20 Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store;
21 my $av1 = Koha::AuthorisedValue->new(
23 category => 'av_for_testing',
24 authorised_value => 'value 1',
25 lib => 'display value 1',
26 lib_opac => 'opac display value 1',
27 imageurl => 'image1.png',
31 my $av2 = Koha::AuthorisedValue->new(
33 category => 'av_for_testing',
34 authorised_value => 'value 2',
35 lib => 'display value 2',
36 lib_opac => 'opac display value 2',
37 imageurl => 'image2.png',
41 my $av3 = Koha::AuthorisedValue->new(
43 category => 'av_for_testing',
44 authorised_value => 'value 3',
45 lib => 'display value 3',
46 lib_opac => 'opac display value 3',
47 imageurl => 'image2.png',
51 my $av4 = Koha::AuthorisedValue->new(
53 category => 'aaav_for_testing',
54 authorised_value => 'value 4',
55 lib => 'display value 4',
56 lib_opac => 'opac display value 4',
57 imageurl => 'image4.png',
61 ok( $av1->id(), 'AV 1 is inserted' );
62 ok( $av2->id(), 'AV 2 is inserted' );
63 ok( $av3->id(), 'AV 3 is inserted' );
64 ok( $av4->id(), 'AV 4 is inserted' );
66 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
68 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
70 my @authorised_values =
71 Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
72 is( @authorised_values, 3, "Get correct number of values" );
74 my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
75 my $branch1 = $branches_rs->next();
76 my $branchcode1 = $branch1->branchcode();
77 my $branch2 = $branches_rs->next();
78 my $branchcode2 = $branch2->branchcode();
80 $av1->add_branch_limitation( $branchcode1 );
82 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
83 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
85 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
86 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
88 $av1->del_branch_limitation( $branchcode1 );
89 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
90 is( @authorised_values, 3, "Branch limitation deleted successfully" );
92 $av1->add_branch_limitation( $branchcode1 );
93 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
95 my $limits = $av1->branch_limitations;
96 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
98 my @categories = Koha::AuthorisedValues->new->categories;
99 is( @categories, 2, 'There should have 2 categories inserted' );
100 is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
101 is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );