Bug 17216: Fix tests
[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 use Koha::AuthorisedValueCategories;
10
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
14
15 $dbh->do("DELETE FROM authorised_values");
16 $dbh->do("DELETE FROM authorised_value_categories");
17
18 # insert
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(
22     {
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',
28     }
29 )->store();
30
31 my $av2 = Koha::AuthorisedValue->new(
32     {
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',
38     }
39 )->store();
40
41 my $av3 = Koha::AuthorisedValue->new(
42     {
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',
48     }
49 )->store();
50
51 my $av4 = Koha::AuthorisedValue->new(
52     {
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',
58     }
59 )->store();
60
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' );
65
66 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
67 $av3->lib_opac('');
68 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
69
70 my @authorised_values =
71   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
72 is( @authorised_values, 3, "Get correct number of values" );
73
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();
79
80 $av1->add_branch_limitation( $branchcode1 );
81
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" );
84
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" );
87
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" );
91
92 $av1->add_branch_limitation( $branchcode1 );
93 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
94
95 my $limits = $av1->branch_limitations;
96 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
97
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)' );