|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Test::More tests => 29; |
| 5 |
|
| 6 |
use C4::Context; |
| 7 |
use Koha::AuthorisedValue; |
| 8 |
|
| 9 |
my $dbh = C4::Context->dbh; |
| 10 |
$dbh->{AutoCommit} = 0; |
| 11 |
$dbh->{RaiseError} = 1; |
| 12 |
|
| 13 |
$dbh->do("DELETE FROM authorised_values"); |
| 14 |
|
| 15 |
# Tests for Koha::AuthorisedValue |
| 16 |
|
| 17 |
# insert |
| 18 |
my $av1 = Koha::AuthorisedValue->new({ |
| 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 |
|
| 26 |
eval {$av1->insert}; |
| 27 |
ok( ! $@, 'AV 1 is inserted without error'); |
| 28 |
|
| 29 |
my $av1_bis = Koha::AuthorisedValue->new({ |
| 30 |
category => 'av_for_testing', |
| 31 |
authorised_value => 'value 1', |
| 32 |
lib => 'display value 2', |
| 33 |
lib_opac => 'opac display value 2', |
| 34 |
imageurl => 'image2.png', |
| 35 |
}); |
| 36 |
|
| 37 |
eval {$av1_bis->insert}; |
| 38 |
ok( $@, 'AV 2 is not inserted (error raised) : Unique key category-authorised_value'); |
| 39 |
|
| 40 |
my $id = $av1->{id}; |
| 41 |
ok( $av1->{id}, 'AV 1 is inserted' ); |
| 42 |
|
| 43 |
my $new_av1 = Koha::AuthorisedValue->new( { id => $id } )->fetch; |
| 44 |
|
| 45 |
is( $new_av1->{id}, $id, "insert: id has been correctly get back" ); |
| 46 |
is( $new_av1->{authorised_value}, $av1->{authorised_value}, "insert: authorised_value has been correctly get back" ); |
| 47 |
is( $new_av1->{lib}, $av1->{lib}, "insert: lib has been correctly get back" ); |
| 48 |
is( $new_av1->{lib_opac}, $av1->{lib_opac}, "insert: lib_opac has been correctly get back" ); |
| 49 |
is( $new_av1->{imageurl}, $av1->{imageurl}, "insert: imageurl has been correctly get back" ); |
| 50 |
is( $new_av1->{branches_limitations}, undef, "insert: branches_limitations is not get back with fetch" ); |
| 51 |
is_deeply( $new_av1->branches_limitations, [], "insert: The branches_limitations method returns the branches limitations" ); |
| 52 |
is_deeply( $new_av1->{branches_limitations}, [], "insert: The branches_limitations method set the branches_limitations key" ); |
| 53 |
|
| 54 |
# update |
| 55 |
$av1->{authorised_value} = 'new value 1'; |
| 56 |
$av1->{lib} = 'new lib 1'; |
| 57 |
$av1->{lib_opac} = 'new lib opac 1'; |
| 58 |
$av1->{imageurl} = 'new_image2.png'; |
| 59 |
my $branches_limitations = [ |
| 60 |
{branchcode => 'CPL', branchname => 'Centerville'}, |
| 61 |
{branchcode => 'MPL', branchname => 'Midway'}, |
| 62 |
]; |
| 63 |
$av1->{branches_limitations} = $branches_limitations; |
| 64 |
eval{$av1->update}; |
| 65 |
ok( ! $@, 'AV 1 is updated without error'); |
| 66 |
|
| 67 |
$new_av1 = Koha::AuthorisedValue->new( { id => $id } )->fetch; |
| 68 |
is( $new_av1->{id}, $id, "update: id has been correctly get back" ); |
| 69 |
is( $new_av1->{authorised_value}, 'new value 1', "update: authorised_value has been correctly get back" ); |
| 70 |
is( $new_av1->{lib}, 'new lib 1', "update: lib has been correctly get back" ); |
| 71 |
is( $new_av1->{lib_opac}, 'new lib opac 1', "update: lib_opac has been correctly get back" ); |
| 72 |
is( $new_av1->{imageurl}, 'new_image2.png', "update: imageurl has been correctly get back" ); |
| 73 |
is( $new_av1->{branches_limitations}, undef, "update: branches_limitations is not get back with fetch" ); |
| 74 |
is_deeply( $new_av1->branches_limitations, $branches_limitations, "update: The branches_limitations method returns the branches limitations" ); |
| 75 |
is_deeply( $new_av1->{branches_limitations}, $branches_limitations, "update: The branches_limitations method set the branches_limitations key" ); |
| 76 |
|
| 77 |
# delete |
| 78 |
eval{$av1->delete}; |
| 79 |
ok( ! $@, 'AV 1 is deleted without error' ); |
| 80 |
|
| 81 |
eval{ |
| 82 |
$new_av1 = Koha::AuthorisedValue->new( { id => $id } )->fetch; |
| 83 |
}; |
| 84 |
ok( $@, 'AV 1 is removed from the database' ); |
| 85 |
|
| 86 |
|
| 87 |
# Tests for Koha::AuthorisedValues |
| 88 |
$av1 = Koha::AuthorisedValue->new({ |
| 89 |
category => 'av_for_testing_1', |
| 90 |
authorised_value => 'value 1', |
| 91 |
lib => 'display value 1', |
| 92 |
lib_opac => 'opac display value 1', |
| 93 |
imageurl => 'image1.png', |
| 94 |
}); |
| 95 |
my $av2 = Koha::AuthorisedValue->new({ |
| 96 |
category => 'av_for_testing_1', |
| 97 |
authorised_value => 'value 2', |
| 98 |
lib => 'display value 2', |
| 99 |
lib_opac => 'opac display value 2', |
| 100 |
imageurl => 'image2.png', |
| 101 |
}); |
| 102 |
my $av3 = Koha::AuthorisedValue->new({ |
| 103 |
category => 'av_for_testing_1', |
| 104 |
authorised_value => 'value 3', |
| 105 |
lib => 'display value 3', |
| 106 |
lib_opac => 'opac display value 3', |
| 107 |
imageurl => 'image3.png', |
| 108 |
}); |
| 109 |
my $av4 = Koha::AuthorisedValue->new({ |
| 110 |
category => 'av_for_testing_2', |
| 111 |
authorised_value => 'value 4', |
| 112 |
lib => 'display value 4', |
| 113 |
lib_opac => 'opac display value 4', |
| 114 |
imageurl => 'image4.png', |
| 115 |
}); |
| 116 |
my $av5 = Koha::AuthorisedValue->new({ |
| 117 |
category => 'av_for_testing_2', |
| 118 |
authorised_value => 'value 5', |
| 119 |
lib => 'display value 5', |
| 120 |
lib_opac => 'opac display value 5', |
| 121 |
imageurl => 'image5.png', |
| 122 |
}); |
| 123 |
eval { |
| 124 |
$av1->insert; |
| 125 |
$av2->insert; |
| 126 |
$av3->insert; |
| 127 |
$av4->insert; |
| 128 |
$av5->insert; |
| 129 |
}; |
| 130 |
ok( ! $@, "5 AV inserted with success" ); |
| 131 |
my $avs = Koha::AuthorisedValue->new; |
| 132 |
|
| 133 |
# get categories |
| 134 |
my $categories = $avs->categories; |
| 135 |
is( grep ({/av_for_testing_1/} @$categories), 1, "av_for_testing_1 is a category"); |
| 136 |
is( grep ({/av_for_testing_2/} @$categories), 1, "av_for_testing_2 is a category"); |
| 137 |
is( grep ({/av_for_testing_3/} @$categories), 0, "av_for_testing_3 is not a category"); |
| 138 |
|
| 139 |
# filter |
| 140 |
my $avs1 = $avs->search({category => 'av_for_testing_1'} ); |
| 141 |
my $avs2 = $avs->search({category => 'av_for_testing_2'} ); |
| 142 |
is( scalar(@$avs1), 3, 'There are 3 AVs for category 1' ); |
| 143 |
is( scalar(@$avs2), 2, 'There are 2 AVs for category 2' ); |
| 144 |
|
| 145 |
is( $av1->delete, 1, "Delete 1 AV" ); |
| 146 |
|
| 147 |
$dbh->rollback; |