Bug 20568: (QA follow-up) Get rid of the id column
[koha.git] / t / db_dependent / ClassSource.t
blob6d4ab7ec4bdecdb52d7403ea1329b3c6954a8a16
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Test::More tests => 25;
6 use Test::Warn;
8 use C4::Context;
10 BEGIN {
11 use_ok('C4::ClassSource');
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)
28 #Start transaction
29 my $dbh = C4::Context->dbh;
30 $dbh->{RaiseError} = 1;
31 $dbh->{AutoCommit} = 0;
33 #Start tests
34 $dbh->do(q|DELETE FROM class_sources|);
35 $dbh->do(q|DELETE FROM class_sort_rules|);
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"
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"
57 #Test GetClassSortRule
58 is_deeply(
59 GetClassSortRule('sortrule1'),
61 class_sort_rule => 'sortrule1',
62 description => 'description1',
63 sort_routine => 'routine1'
65 "GetClassSort gives sortrule1's information"
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" );
72 #Test GetClassSortRules
73 my $getsortrules = GetClassSortRules();
74 is_deeply(
75 $getsortrules,
77 sortrule1 => {
78 class_sort_rule => 'sortrule1',
79 description => 'description1',
80 sort_routine => 'routine1'
82 sortrule2 => {
83 class_sort_rule => 'sortrule2',
84 description => 'description2',
85 sort_routine => 'routine2'
88 "GetClassSortRules returns the id off all SortRule and their information"
91 #Test GetClassSource
92 my $getsource1 = GetClassSource('source1');
93 is_deeply(
94 $getsource1,
96 cn_source => 'source1',
97 description => 'Description_source1',
98 used => 1,
99 class_sort_rule => 'sortrule1'
101 "GetClassSource gives source1's information"
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" );
108 #Test GetClassSources
109 my $getsources = GetClassSources();
110 is_deeply(
111 $getsources,
113 source1 => {
114 cn_source => 'source1',
115 description => 'Description_source1',
116 used => 1,
117 class_sort_rule => 'sortrule1'
119 source2 => {
120 cn_source => 'source2',
121 description => 'Description_source2',
122 used => 0,
123 class_sort_rule => 'sortrule1'
126 "GetClassSources returns the id off all sources and their information"
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"
139 #Test GetSourcesForSorSortRule
140 my @sources = GetSourcesForSortRule('sortrule1');
141 is_deeply(
142 \@sources,
143 [ 'source1', 'source2' ],
144 "Sortrule1 has source1 and source2"
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" );
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" );
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" );
175 #Test ModClassSortRule
176 ModClassSortRule( 'sortrule1', 'description1_modified', 'routine1_modified' );
177 is_deeply(
178 GetClassSortRule('sortrule1'),
180 class_sort_rule => 'sortrule1',
181 description => 'description1_modified',
182 sort_routine => 'routine1_modified'
184 "Sortrule1 has been modified"
187 #Test ModClassSource
188 ModClassSource( 'source1', 'Description_source1_modified', 0, 'sortrule1' );
189 is_deeply(
190 GetClassSource('source1'),
192 cn_source => 'source1',
193 description => 'Description_source1_modified',
194 used => 0,
195 class_sort_rule => 'sortrule1'
197 "Source1 has been modified"
200 #End transaction
201 $dbh->rollback;