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

(-)a/C4/ClassSource.pm (-134 / +1 lines)
Lines 54-60 sources and sorting rules. Link Here
54
54
55
    &GetSourcesForSortRule
55
    &GetSourcesForSortRule
56
    &GetClassSort
56
    &GetClassSort
57
    
57
58
);
58
);
59
59
60
=head2 GetClassSources
60
=head2 GetClassSources
Lines 95-125 sub GetClassSources { Link Here
95
95
96
}
96
}
97
97
98
=head2 AddClassSource
99
100
  DEPRECATED
101
102
  AddClassSource($cn_source, $description, $used, $class_sort_rule);
103
104
  Adds a class_sources row.
105
106
=cut
107
108
sub AddClassSource {
109
110
    my ($cn_source, $description, $used, $class_sort_rule) = @_;
111
    my $exists = GetClassSource($cn_source);
112
    if ($exists) {
113
        return 0;
114
    }
115
    my $dbh = C4::Context->dbh;
116
    my $sth = $dbh->prepare("INSERT INTO `class_sources`
117
                                           (`cn_source`, `description`, `used`, `class_sort_rule`)
118
                                           VALUES (?, ?, ?, ?)");
119
    $sth->execute($cn_source, $description, $used, $class_sort_rule);
120
    return 1;
121
}
122
123
=head2 GetClassSource
98
=head2 GetClassSource
124
99
125
  my $hashref = GetClassSource($cn_source);
100
  my $hashref = GetClassSource($cn_source);
Lines 138-185 sub GetClassSource { Link Here
138
    return $row;
113
    return $row;
139
}
114
}
140
115
141
=head2 ModClassSource 
142
143
  DEPRECATED
144
145
  ModClassSource($cn_source, $description, $used, $class_sort_rule);
146
147
  Updates a class_sources row.
148
149
=cut
150
151
sub ModClassSource {
152
153
    my ($cn_source, $description, $used, $class_sort_rule) = @_;
154
    my $dbh = C4::Context->dbh;
155
    my $sth = $dbh->prepare("UPDATE `class_sources`
156
                                    SET  `description` = ?,
157
                                         `used` = ?,
158
                                         `class_sort_rule` = ?
159
                                    WHERE `cn_source` = ?");
160
    $sth->execute($description, $used, $class_sort_rule, $cn_source);
161
162
}
163
164
=head2 DelClassSource 
165
166
  DEPRECATED
167
168
  DelClassSource($cn_source);
169
170
  Deletes class_sources row.
171
172
=cut
173
174
sub DelClassSource {
175
176
    my ($cn_source) = @_;
177
    my $dbh = C4::Context->dbh;
178
    my $sth = $dbh->prepare("DELETE FROM `class_sources` WHERE `cn_source` = ?");
179
    $sth->execute($cn_source);
180
181
}
182
183
=head2 GetClassSortRules
116
=head2 GetClassSortRules
184
117
185
  my $sort_rules = GetClassSortRules();
118
  my $sort_rules = GetClassSortRules();
Lines 217-247 sub GetClassSortRules { Link Here
217
150
218
}
151
}
219
152
220
=head2 AddClassSortRule
221
222
  DEPRECATED
223
224
  AddClassSortRule($class_sort_rule, $description, $sort_routine);
225
226
  Adds a class_sort_rules row.
227
228
=cut
229
230
sub AddClassSortRule {
231
232
    my ($class_sort_rule, $description, $sort_routine) = @_;
233
    my $exists = GetClassSortRule($class_sort_rule);
234
    if ($exists) {
235
        return 0;
236
    }
237
    my $dbh = C4::Context->dbh;
238
    my $sth = $dbh->prepare("INSERT INTO `class_sort_rules`
239
                                           (`class_sort_rule`, `description`, `sort_routine`)
240
                                           VALUES (?, ?, ?)");
241
    $sth->execute($class_sort_rule, $description, $sort_routine);
242
    return 1;
243
}
244
245
=head2 GetClassSortRule
153
=head2 GetClassSortRule
246
154
247
  my $hashref = GetClassSortRule($class_sort_rule);
155
  my $hashref = GetClassSortRule($class_sort_rule);
Lines 260-306 sub GetClassSortRule { Link Here
260
    return $row;
168
    return $row;
261
}
169
}
262
170
263
=head2 ModClassSortRule 
264
265
  DEPRECATED
266
267
  ModClassSortRule($class_sort_rule, $description, $sort_routine);
268
269
  Updates a class_sort_rules row.
270
271
=cut
272
273
sub ModClassSortRule {
274
275
    my ($class_sort_rule, $description, $sort_routine) = @_;
276
    my $dbh = C4::Context->dbh;
277
    my $sth = $dbh->prepare("UPDATE `class_sort_rules`
278
                                    SET  `description` = ?,
279
                                         `sort_routine` = ?
280
                                    WHERE `class_sort_rule` = ?");
281
    $sth->execute($description, $sort_routine, $class_sort_rule);
282
283
}
284
285
=head2 DelClassSortRule 
286
287
  DEPRECATED
288
289
  DelClassSortRule($class_sort_rule);
290
291
  Deletes class_sort_rules row.
292
293
=cut
294
295
sub DelClassSortRule {
296
297
    my ($class_sort_rule) = @_;
298
    my $dbh = C4::Context->dbh;
299
    my $sth = $dbh->prepare("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
300
    $sth->execute($class_sort_rule);
301
302
}
303
304
=head2 GetSourcesForSortRule
171
=head2 GetSourcesForSortRule
305
172
306
  my @source = GetSourcesForSortRule($class_sort_rule);
173
  my @source = GetSourcesForSortRule($class_sort_rule);
(-)a/t/db_dependent/ClassSource.t (-202 lines)
Lines 1-201 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 25;
6
use Test::Warn;
7
8
use C4::Context;
9
10
BEGIN {
11
    use_ok('C4::ClassSource');
12
}
13
can_ok( 'C4::ClassSource',
14
    qw( AddClassSortRule
15
        AddClassSource
16
        GetClassSort
17
        GetClassSortRule
18
        GetClassSortRules
19
        GetClassSource
20
        GetClassSources
21
        DelClassSortRule
22
        DelClassSource
23
        GetSourcesForSortRule
24
        ModClassSortRule
25
        ModClassSource)
26
);
27
28
#Start transaction
29
my $dbh = C4::Context->dbh;
30
$dbh->{RaiseError} = 1;
31
$dbh->{AutoCommit} = 0;
32
33
#Start tests
34
$dbh->do(q|DELETE FROM class_sources|);
35
$dbh->do(q|DELETE FROM class_sort_rules|);
36
37
#Test AddClassSortRule
38
my $countSources  = scalar( keys(%{ GetClassSources() }) );
39
my $countSources2 = scalar( keys(%{ GetClassSortRules() }) );
40
AddClassSortRule( 'sortrule1', 'description1', 'routine1' );
41
AddClassSortRule( 'sortrule2', 'description2', 'routine2' );
42
is(
43
    scalar( keys(%{ GetClassSortRules() }) ),
44
    $countSources + 2,
45
    "SortRule1 and SortRules2 have been added"
46
);
47
48
#Test AddClassSource
49
AddClassSource( 'source1', 'Description_source1', 1, 'sortrule1' );
50
AddClassSource( 'source2', 'Description_source2', 0, 'sortrule1' );
51
is(
52
    scalar( keys(%{ GetClassSources() }) ),
53
    $countSources2 + 2,
54
    "Source1 and source2 have been added"
55
);
56
57
#Test GetClassSortRule
58
is_deeply(
59
    GetClassSortRule('sortrule1'),
60
    {
61
        class_sort_rule => 'sortrule1',
62
        description     => 'description1',
63
        sort_routine    => 'routine1'
64
    },
65
    "GetClassSort gives sortrule1's information"
66
);
67
is_deeply( GetClassSortRule(), undef,
68
    "GetClassSort without params returns undef" );
69
is_deeply( GetClassSortRule('doesnt_exist'),
70
    undef, "GetClassSort with an id which doesn't exist returns undef" );
71
72
#Test GetClassSortRules
73
my $getsortrules = GetClassSortRules();
74
is_deeply(
75
    $getsortrules,
76
    {
77
        sortrule1 => {
78
            class_sort_rule => 'sortrule1',
79
            description     => 'description1',
80
            sort_routine    => 'routine1'
81
        },
82
        sortrule2 => {
83
            class_sort_rule => 'sortrule2',
84
            description     => 'description2',
85
            sort_routine    => 'routine2'
86
        }
87
    },
88
    "GetClassSortRules returns the id off all SortRule and their information"
89
);
90
91
#Test GetClassSource
92
my $getsource1 = GetClassSource('source1');
93
is_deeply(
94
    $getsource1,
95
    {
96
        cn_source       => 'source1',
97
        description     => 'Description_source1',
98
        used            => 1,
99
        class_sort_rule => 'sortrule1'
100
    },
101
    "GetClassSource gives source1's information"
102
);
103
is_deeply( GetClassSource(), undef,
104
    "GetClassSource without params returns undef" );
105
is_deeply( GetClassSource('doesnt_exist'),
106
    undef, "GetClassSource with an id which doesn't exist returns undef" );
107
108
#Test GetClassSources
109
my $getsources = GetClassSources();
110
is_deeply(
111
    $getsources,
112
    {
113
        source1 => {
114
            cn_source       => 'source1',
115
            description     => 'Description_source1',
116
            used            => 1,
117
            class_sort_rule => 'sortrule1'
118
        },
119
        source2 => {
120
            cn_source       => 'source2',
121
            description     => 'Description_source2',
122
            used            => 0,
123
            class_sort_rule => 'sortrule1'
124
        }
125
    },
126
    "GetClassSources returns the id off all sources and their information"
127
);
128
129
#Test GetClassSort
130
my $getclassSort;
131
warning_like
132
    { $getclassSort = GetClassSort( 'source1', 'sortrule1', 'item1' ) }
133
    qr/attempting to use non-existent class sorting routine/,
134
    'Non-existent class warning caught';
135
is( $getclassSort, "SORTRULE1_ITEM1",
136
" the sort key corresponding to Source1 and sortrule1 and item1 is SORTRULE1_ITEM1"
137
);
138
139
#Test GetSourcesForSorSortRule
140
my @sources = GetSourcesForSortRule('sortrule1');
141
is_deeply(
142
    \@sources,
143
    [ 'source1', 'source2' ],
144
    "Sortrule1 has source1 and source2"
145
);
146
@sources = GetSourcesForSortRule();
147
is_deeply( \@sources, [],
148
    "Without params GetSourcesForSortRule returns an empty array" );
149
@sources = GetSourcesForSortRule('doesnt_exist');
150
is_deeply( \@sources, [],
151
    "With a wrong params GetSourcesForSortRule returns an empty array" );
152
153
#Test DelClassSortRule
154
#DelClassSortRule ('sortrule1');
155
#is(scalar (keys (%{ GetClassSortRules() })),1,"SortRule1 has been deleted");#FIXME : impossible if some sources exist
156
DelClassSortRule('sortrule2');
157
is( scalar( keys(%{ GetClassSortRules() }) ), 1, "SortRule2 has been deleted" );
158
DelClassSortRule();
159
is( scalar( keys(%{ GetClassSortRules() }) ),
160
    1, "Without params DelClassSortRule doesn't do anything" );
161
DelClassSortRule('doesnt_exist');
162
is( scalar( keys(%{ GetClassSortRules() }) ),
163
    1, "With wrong id, DelClassSortRule doesn't do anything" );
164
165
#Test DelClassSource
166
DelClassSource('source2');
167
is( scalar( keys(%{ GetClassSources() }) ), 1, "Source2 has been deleted" );
168
DelClassSource();
169
is( scalar( keys(%{ GetClassSources() }) ),
170
    1, "Without params DelClassSource doesn't do anything" );
171
DelClassSource('doesnt_exist');
172
is( scalar( keys(%{ GetClassSources() }) ),
173
    1, "With wrong id, DelClassSource doesn't do anything" );
174
175
#Test ModClassSortRule
176
ModClassSortRule( 'sortrule1', 'description1_modified', 'routine1_modified' );
177
is_deeply(
178
    GetClassSortRule('sortrule1'),
179
    {
180
        class_sort_rule => 'sortrule1',
181
        description     => 'description1_modified',
182
        sort_routine    => 'routine1_modified'
183
    },
184
    "Sortrule1 has been modified"
185
);
186
187
#Test ModClassSource
188
ModClassSource( 'source1', 'Description_source1_modified', 0, 'sortrule1' );
189
is_deeply(
190
    GetClassSource('source1'),
191
    {
192
        cn_source       => 'source1',
193
        description     => 'Description_source1_modified',
194
        used            => 0,
195
        class_sort_rule => 'sortrule1'
196
    },
197
    "Source1 has been modified"
198
);
199
200
#End transaction
201
$dbh->rollback;
202
- 

Return to bug 15836