View | Details | Raw Unified | Return to bug 10363
Collapse All | Expand All

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

Return to bug 10363