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

(-)a/Koha/Schema/Result/AuthorisedValue.pm (-2 / +30 lines)
Lines 90-95 __PACKAGE__->add_columns( Link Here
90
90
91
__PACKAGE__->set_primary_key("id");
91
__PACKAGE__->set_primary_key("id");
92
92
93
=head1 UNIQUE CONSTRAINTS
94
95
=head2 C<av_uniq>
96
97
=over 4
98
99
=item * L</category>
100
101
=item * L</authorised_value>
102
103
=back
104
105
=cut
106
107
__PACKAGE__->add_unique_constraint("av_uniq", ["category", "authorised_value"]);
108
93
=head1 RELATIONS
109
=head1 RELATIONS
94
110
95
=head2 authorised_values_branches
111
=head2 authorised_values_branches
Lines 108-116 __PACKAGE__->has_many( Link Here
108
);
124
);
109
125
110
126
111
# Created by DBIx::Class::Schema::Loader v0.07025 @ 2013-10-14 20:56:21
127
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-08-05 16:05:45
112
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:N6Q7Y4sHL03X170zJ3APUA
128
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:9LllSeL5DPjwR2ucEOqU8w
129
130
__PACKAGE__->has_many(
131
  "branch_limitations",
132
  "Koha::Schema::Result::AuthorisedValuesBranch",
133
  { "foreign.av_id" => "self.id" },
134
  { cascade_copy => 0, cascade_delete => 0 },
135
);
136
137
sub add_branch_limitation {
138
    my ( $self, $branchcode ) = @_;
113
139
140
    return $self->update_or_create_related('branch_limitations', { branchcode => $branchcode } );
141
}
114
142
115
# You can replace this text with custom content, and it will be preserved on regeneration
143
# You can replace this text with custom content, and it will be preserved on regeneration
116
1;
144
1;
(-)a/Koha/Schema/ResultSet/AuthorisedValue.pm (+41 lines)
Line 0 Link Here
1
package Koha::Schema::ResultSet::AuthorisedValue;
2
3
use Modern::Perl;
4
5
use List::MoreUtils qw(uniq);
6
7
use base 'DBIx::Class::ResultSet';
8
9
sub categories {
10
    my ($self) = @_;
11
12
    return [ uniq( $self->get_column('category')->all() ) ];
13
}
14
15
sub in_category {
16
    my ( $self, $category ) = @_;
17
18
    return $self->search( { category => $category } );
19
}
20
21
sub for_branch {
22
    my ( $self, $branchcode ) = @_;
23
24
    return $self->search(
25
        {
26
            -or => [
27
                'authorised_values_branches.branchcode' => $branchcode,
28
                'authorised_values_branches.branchcode'  => undef
29
            ]
30
        },
31
        { join => 'authorised_values_branches' }
32
    );
33
}
34
35
sub by_category_and_branch {
36
    my ( $self, $category, $branchcode ) = @_;
37
38
    return $self->in_category( $category )->for_branch( $branchcode );
39
}
40
41
1;
(-)a/installer/data/mysql/kohastructure.sql (+1 lines)
Lines 104-109 CREATE TABLE `authorised_values` ( -- stores values for authorized values catego Link Here
104
  `lib_opac` varchar(200) default NULL, -- authorized value description as printed in the OPAC
104
  `lib_opac` varchar(200) default NULL, -- authorized value description as printed in the OPAC
105
  `imageurl` varchar(200) default NULL, -- authorized value URL
105
  `imageurl` varchar(200) default NULL, -- authorized value URL
106
  PRIMARY KEY  (`id`),
106
  PRIMARY KEY  (`id`),
107
  UNIQUE KEY `av_uniq` (`category`,`authorised_value`),
107
  KEY `name` (`category`),
108
  KEY `name` (`category`),
108
  KEY `lib` (`lib`),
109
  KEY `lib` (`lib`),
109
  KEY `auth_value_idx` (`authorised_value`)
110
  KEY `auth_value_idx` (`authorised_value`)
(-)a/installer/data/mysql/updatedatabase.pl (+9 lines)
Lines 8751-8756 if ( CheckVersion($DBversion) ) { Link Here
8751
    SetVersion($DBversion);
8751
    SetVersion($DBversion);
8752
}
8752
}
8753
8753
8754
$DBversion = "3.17.00.XXX";
8755
if ( CheckVersion($DBversion) ) {
8756
    $dbh->do(q{
8757
        ALTER TABLE authorised_values ADD UNIQUE KEY av_uniq(category, authorised_value);
8758
    });
8759
    print "Upgrade to $DBversion done (Bug XXXXX: Add unique key on the authorised_values table)\n";
8760
    SetVersion($DBversion);
8761
}
8762
8754
=head1 FUNCTIONS
8763
=head1 FUNCTIONS
8755
8764
8756
=head2 TableExists($table)
8765
=head2 TableExists($table)
(-)a/t/db_dependent/AuthorisedValues.t (-1 / +137 lines)
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

Return to bug 10363