Bug 10363: There is no package for authorised values.
[koha.git] / t / db_dependent / AuthorisedValues.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More;    # tests => 25;
5
6 use C4::Context;
7 use Koha::AuthorisedValue;
8 use Koha::AuthorisedValues;
9
10 my $dbh = C4::Context->dbh;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
13
14 $dbh->do("DELETE FROM authorised_values");
15
16 # insert
17 my $av1 = Koha::AuthorisedValue->new(
18     {
19         category         => 'av_for_testing',
20         authorised_value => 'value 1',
21         lib              => 'display value 1',
22         lib_opac         => 'opac display value 1',
23         imageurl         => 'image1.png',
24     }
25 )->store();
26
27 my $av2 = Koha::AuthorisedValue->new(
28     {
29         category         => 'av_for_testing',
30         authorised_value => 'value 2',
31         lib              => 'display value 2',
32         lib_opac         => 'opac display value 2',
33         imageurl         => 'image2.png',
34     }
35 )->store();
36
37 my $av3 = Koha::AuthorisedValue->new(
38     {
39         category         => 'av_for_testing',
40         authorised_value => 'value 3',
41         lib              => 'display value 3',
42         lib_opac         => 'opac display value 3',
43         imageurl         => 'image2.png',
44     }
45 )->store();
46
47 ok( $av1->id(), 'AV 1 is inserted' );
48 ok( $av2->id(), 'AV 2 is inserted' );
49 ok( $av3->id(), 'AV 3 is inserted' );
50
51 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
52 $av3->lib_opac('');
53 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
54
55 my @authorised_values =
56   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
57 is( @authorised_values, 3, "Get correct number of values" );
58
59 my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
60 my $branch1 = $branches_rs->next();
61 my $branchcode1 = $branch1->branchcode();
62 my $branch2 = $branches_rs->next();
63 my $branchcode2 = $branch2->branchcode();
64
65 $av1->add_branch_limitation( $branchcode1 );
66
67 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
68 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
69
70 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
71 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
72
73 $av1->del_branch_limitation( $branchcode1 );
74 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
75 is( @authorised_values, 3, "Branch limitation deleted successfully" );
76
77 $av1->add_branch_limitation( $branchcode1 );
78 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
79
80 my $limits = $av1->branch_limitations;
81 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
82
83 done_testing;