Bug 14097 : Changing AddReserve prototype call
[koha.git] / t / db_dependent / AuthorisedValues.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 14;
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 my $av4 = Koha::AuthorisedValue->new(
48     {
49         category         => 'aaav_for_testing',
50         authorised_value => 'value 4',
51         lib              => 'display value 4',
52         lib_opac         => 'opac display value 4',
53         imageurl         => 'image4.png',
54     }
55 )->store();
56
57 ok( $av1->id(), 'AV 1 is inserted' );
58 ok( $av2->id(), 'AV 2 is inserted' );
59 ok( $av3->id(), 'AV 3 is inserted' );
60 ok( $av4->id(), 'AV 4 is inserted' );
61
62 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
63 $av3->lib_opac('');
64 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
65
66 my @authorised_values =
67   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
68 is( @authorised_values, 3, "Get correct number of values" );
69
70 my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
71 my $branch1 = $branches_rs->next();
72 my $branchcode1 = $branch1->branchcode();
73 my $branch2 = $branches_rs->next();
74 my $branchcode2 = $branch2->branchcode();
75
76 $av1->add_branch_limitation( $branchcode1 );
77
78 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
79 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
80
81 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
82 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
83
84 $av1->del_branch_limitation( $branchcode1 );
85 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
86 is( @authorised_values, 3, "Branch limitation deleted successfully" );
87
88 $av1->add_branch_limitation( $branchcode1 );
89 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
90
91 my $limits = $av1->branch_limitations;
92 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
93
94 my @categories = Koha::AuthorisedValues->new->categories;
95 is( @categories, 2, 'There should have 2 categories inserted' );
96 is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
97 is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );