Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
use Test::More tests => 25; |
5 |
|
6 |
use C4::Context; |
7 |
use Koha::Database; |
8 |
|
9 |
my $dbh = C4::Context->dbh; |
10 |
$dbh->{AutoCommit} = 0; |
11 |
$dbh->{RaiseError} = 1; |
12 |
|
13 |
my $schema = Koha::Database->new()->schema(); |
14 |
|
15 |
my $rs = $schema->resultset('AuthorisedValue'); |
16 |
|
17 |
$dbh->do("DELETE FROM authorised_values"); |
18 |
|
19 |
# Tests for Koha::AuthorisedValue |
20 |
|
21 |
|
22 |
# insert |
23 |
my $av1 = $rs->create({ |
24 |
category => 'av_for_testing', |
25 |
authorised_value => 'value 1', |
26 |
lib => 'display value 1', |
27 |
lib_opac => 'opac display value 1', |
28 |
imageurl => 'image1.png', |
29 |
}); |
30 |
|
31 |
my $av1_bis = $rs->create({ |
32 |
category => 'av_for_testing', |
33 |
authorised_value => 'value 2', |
34 |
lib => 'display value 2', |
35 |
lib_opac => 'opac display value 2', |
36 |
imageurl => 'image2.png', |
37 |
}); |
38 |
|
39 |
my $id = $av1->id(); |
40 |
|
41 |
ok( $id, 'AV 1 is inserted' ); |
42 |
|
43 |
my $new_av1 = $rs->find( $id ); |
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->branch_limitations()->count(), 0 , "insert: branches_limitations is not get back with fetch" ); |
51 |
|
52 |
# update |
53 |
$av1->authorised_value( 'new value 1' ); |
54 |
$av1->lib( 'new lib 1' ); |
55 |
$av1->lib_opac( 'new lib opac 1' ); |
56 |
$av1->imageurl( 'new_image2.png' ); |
57 |
$av1->update(); |
58 |
|
59 |
$av1->add_branch_limitation('CPL'); |
60 |
$av1->add_branch_limitation('MPL'); |
61 |
$av1->add_branch_limitation('MPL'); |
62 |
|
63 |
$new_av1 = $rs->find( $id ); |
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->branch_limitations()->count(), 2, "Authorised value now has two branch limitations" ); |
70 |
|
71 |
# delete |
72 |
$av1->delete(); |
73 |
is( $av1->in_storage, 0, 'AV 1 is deleted without error' ); |
74 |
|
75 |
$new_av1 = $rs->find( $id ); |
76 |
ok( ! $new_av1, 'AV 1 is removed from the database' ); |
77 |
|
78 |
# Tests for Koha::AuthorisedValues |
79 |
$av1 = $rs->create({ |
80 |
category => 'av_for_testing_1', |
81 |
authorised_value => 'value 1', |
82 |
lib => 'display value 1', |
83 |
lib_opac => 'opac display value 1', |
84 |
imageurl => 'image1.png', |
85 |
}); |
86 |
my $av2 = $rs->create({ |
87 |
category => 'av_for_testing_1', |
88 |
authorised_value => 'value 2', |
89 |
lib => 'display value 2', |
90 |
lib_opac => 'opac display value 2', |
91 |
imageurl => 'image2.png', |
92 |
}); |
93 |
my $av3 = $rs->create({ |
94 |
category => 'av_for_testing_1', |
95 |
authorised_value => 'value 3', |
96 |
lib => 'display value 3', |
97 |
lib_opac => 'opac display value 3', |
98 |
imageurl => 'image3.png', |
99 |
}); |
100 |
my $av4 = $rs->create({ |
101 |
category => 'av_for_testing_2', |
102 |
authorised_value => 'value 4', |
103 |
lib => 'display value 4', |
104 |
lib_opac => 'opac display value 4', |
105 |
imageurl => 'image4.png', |
106 |
}); |
107 |
my $av5 = $rs->create({ |
108 |
category => 'av_for_testing_2', |
109 |
authorised_value => 'value 5', |
110 |
lib => 'display value 5', |
111 |
lib_opac => 'opac display value 5', |
112 |
imageurl => 'image5.png', |
113 |
}); |
114 |
|
115 |
ok( $av1 && $av2 && $av3 && $av4 && $av5, "5 AV inserted with success" ); |
116 |
|
117 |
# get categories |
118 |
my $categories = $rs->categories; |
119 |
is( grep ({/av_for_testing_1/} @$categories), 1, "av_for_testing_1 is a category"); |
120 |
is( grep ({/av_for_testing_2/} @$categories), 1, "av_for_testing_2 is a category"); |
121 |
is( grep ({/av_for_testing_3/} @$categories), 0, "av_for_testing_3 is not a category"); |
122 |
|
123 |
# filter |
124 |
my $avs1 = $rs->in_category( 'av_for_testing_1' ); |
125 |
my $avs2 = $rs->in_category('av_for_testing_2' ); |
126 |
is( $avs1->count() , 3, 'There are 3 AVs for category 1' ); |
127 |
is( $avs2->count(), 2, 'There are 2 AVs for category 2' ); |
128 |
|
129 |
is( $rs->in_category('av_for_testing_1')->for_branch('MPL'), 3, "MPL can access 3 values" ); |
130 |
my $av = $rs->in_category('av_for_testing_1')->first(); |
131 |
$av->add_branch_limitation( 'CPL' ); |
132 |
is( $rs->in_category('av_for_testing_1')->for_branch('MPL'), 2, "MPL can access 2 values" ); |
133 |
$av->add_branch_limitation( 'MPL' ); |
134 |
is( $rs->in_category('av_for_testing_1')->for_branch('MPL'), 3, "MPL can access 3 values again" ); |
135 |
|
136 |
is( $rs->by_category_and_branch('av_for_testing_1', 'MPL'), 3, "MPL can access 3 values" ); |
137 |
|