Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Test::More tests => 16; |
6 |
use URI::Escape; |
7 |
|
8 |
use C4::Context; |
9 |
my $dbh = C4::Context->dbh; |
10 |
$dbh->{AutoCommit} = 0; |
11 |
$dbh->{RaiseError} = 1; |
12 |
|
13 |
use_ok('Koha::DateUtils'); |
14 |
use_ok('C4::Search::History'); |
15 |
|
16 |
my $userid = 123; |
17 |
my $previous_sessionid = "PREVIOUS_SESSIONID"; |
18 |
my $current_sessionid = "CURRENT_SESSIONID"; |
19 |
my $total = 42; |
20 |
my $query_cgi_b = q{idx=kw&idx=ti&idx=au%2Cwrdl&q=word1é&q=word2è&q=word3à&do=Search&sort_by=author_az}; |
21 |
my $query_cgi_a = q{op=do_search&type=opac&authtypecode=NP&operator=start&value=Harry&marclist=match&and_or=and&orderby=HeadingAsc}; |
22 |
|
23 |
# add |
24 |
my $added = add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
25 |
is ( $added, 9, '9 searches are added' ); |
26 |
|
27 |
# get |
28 |
my $searches_for_userid = C4::Search::History::get({ |
29 |
userid => $userid, |
30 |
}); |
31 |
is( scalar(@$searches_for_userid), 9, 'There are 9 searches in all' ); |
32 |
|
33 |
my $searches_for_current_session = C4::Search::History::get({ |
34 |
userid => $userid, |
35 |
sessionid => $current_sessionid, |
36 |
}); |
37 |
is( scalar(@$searches_for_current_session), 5, 'There are 5 searches for the current session' ); |
38 |
|
39 |
my $searches_for_previous_sessions = C4::Search::History::get({ |
40 |
userid => $userid, |
41 |
sessionid => $current_sessionid, |
42 |
previous => 1, |
43 |
}); |
44 |
is( scalar(@$searches_for_previous_sessions), 4, 'There are 4 searches for previous sessions' ); |
45 |
|
46 |
my $authority_searches_for_current_session = C4::Search::History::get({ |
47 |
userid => $userid, |
48 |
sessionid => $current_sessionid, |
49 |
type => 'authority', |
50 |
}); |
51 |
is( scalar(@$authority_searches_for_current_session), 3, 'There are 3 authority searches for the current session' ); |
52 |
|
53 |
my $authority_searches_for_previous_session = C4::Search::History::get({ |
54 |
userid => $userid, |
55 |
sessionid => $current_sessionid, |
56 |
type => 'authority', |
57 |
previous => 1, |
58 |
}); |
59 |
is( scalar(@$authority_searches_for_previous_session), 2, 'There are 2 authority searches for previous sessions' ); |
60 |
|
61 |
my $biblio_searches_for_userid = C4::Search::History::get({ |
62 |
userid => $userid, |
63 |
type => 'biblio', |
64 |
}); |
65 |
is( scalar(@$biblio_searches_for_userid), 4, 'There are 5 searches for the current session' ); |
66 |
|
67 |
my $authority_searches_for_userid = C4::Search::History::get({ |
68 |
userid => $userid, |
69 |
type => 'authority', |
70 |
}); |
71 |
is( scalar(@$authority_searches_for_userid), 5, 'There are 4 searches for previous sessions' ); |
72 |
|
73 |
delete_all( $userid ); |
74 |
|
75 |
# delete |
76 |
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
77 |
C4::Search::History::delete({ |
78 |
userid => $userid, |
79 |
sessionid => $current_sessionid, |
80 |
type => 'authority', |
81 |
}); |
82 |
my $all = C4::Search::History::get({userid => $userid}); |
83 |
is( scalar(@$all), 6, 'There are 6 searches in all after deleting current biblio searches' ); |
84 |
delete_all( $userid ); |
85 |
|
86 |
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
87 |
C4::Search::History::delete({ |
88 |
userid => $userid, |
89 |
sessionid => $current_sessionid, |
90 |
type => 'biblio', |
91 |
previous => 1, |
92 |
}); |
93 |
$all = C4::Search::History::get({userid => $userid}); |
94 |
is( scalar(@$all), 7, 'There are 7 searches in all after deleting previous authority searches' ); |
95 |
delete_all( $userid ); |
96 |
|
97 |
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
98 |
C4::Search::History::delete({ |
99 |
userid => $userid, |
100 |
sessionid => $current_sessionid, |
101 |
previous => 1, |
102 |
}); |
103 |
$all = C4::Search::History::get({userid => $userid}); |
104 |
is( scalar(@$all), 5, 'There are 5 searches in all after deleting all previous searches' ); |
105 |
delete_all( $userid ); |
106 |
|
107 |
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
108 |
C4::Search::History::delete({ |
109 |
userid => $userid, |
110 |
sessionid => $current_sessionid, |
111 |
}); |
112 |
$all = C4::Search::History::get({userid => $userid}); |
113 |
is( scalar(@$all), 4, 'There are 5 searches in all after deleting all searches for a sessionid' ); |
114 |
delete_all( $userid ); |
115 |
|
116 |
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
117 |
C4::Search::History::delete({ |
118 |
userid => $userid, |
119 |
}); |
120 |
$all = C4::Search::History::get({userid => $userid}); |
121 |
is( scalar(@$all), 0, 'There are 0 search after deleting all searches for a userid' ); |
122 |
delete_all( $userid ); |
123 |
|
124 |
add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ); |
125 |
C4::Search::History::delete({}); |
126 |
$all = C4::Search::History::get({userid => $userid}); |
127 |
is( scalar(@$all), 9, 'There are still 9 searches after calling delete without userid' ); |
128 |
delete_all( $userid ); |
129 |
|
130 |
sub add { |
131 |
my ( $userid, $current_session_id, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ) = @_; |
132 |
|
133 |
my $query_desc_b1_p = q{first previous biblio search}; |
134 |
my $first_previous_biblio_search = { |
135 |
userid => $userid, |
136 |
sessionid => $previous_sessionid, |
137 |
query_desc => $query_desc_b1_p, |
138 |
query_cgi => $query_cgi_b, |
139 |
total => $total, |
140 |
type => 'biblio', |
141 |
}; |
142 |
|
143 |
my $query_desc_a1_p = q{first previous authority search}; |
144 |
my $first_previous_authority_search = { |
145 |
userid => $userid, |
146 |
sessionid => $previous_sessionid, |
147 |
query_desc => $query_desc_a1_p, |
148 |
query_cgi => $query_cgi_a, |
149 |
total => $total, |
150 |
type => 'authority', |
151 |
}; |
152 |
|
153 |
my $query_desc_b2_p = q{second previous biblio search}; |
154 |
my $second_previous_biblio_search = { |
155 |
userid => $userid, |
156 |
sessionid => $previous_sessionid, |
157 |
query_desc => $query_desc_b2_p, |
158 |
query_cgi => $query_cgi_b, |
159 |
total => $total, |
160 |
type => 'biblio', |
161 |
}; |
162 |
|
163 |
my $query_desc_a2_p = q{second previous authority search}; |
164 |
my $second_previous_authority_search = { |
165 |
userid => $userid, |
166 |
sessionid => $previous_sessionid, |
167 |
query_desc => $query_desc_a2_p, |
168 |
query_cgi => $query_cgi_a, |
169 |
total => $total, |
170 |
type => 'authority', |
171 |
}; |
172 |
|
173 |
|
174 |
my $query_desc_b1_c = q{first current biblio search}; |
175 |
|
176 |
my $first_current_biblio_search = { |
177 |
userid => $userid, |
178 |
sessionid => $current_sessionid, |
179 |
query_desc => $query_desc_b1_c, |
180 |
query_cgi => $query_cgi_b, |
181 |
total => $total, |
182 |
type => 'biblio', |
183 |
}; |
184 |
|
185 |
my $query_desc_a1_c = q{first current authority search}; |
186 |
my $first_current_authority_search = { |
187 |
userid => $userid, |
188 |
sessionid => $current_sessionid, |
189 |
query_desc => $query_desc_a1_c, |
190 |
query_cgi => $query_cgi_a, |
191 |
total => $total, |
192 |
type => 'authority', |
193 |
}; |
194 |
|
195 |
my $query_desc_b2_c = q{second current biblio search}; |
196 |
my $second_current_biblio_search = { |
197 |
userid => $userid, |
198 |
sessionid => $current_sessionid, |
199 |
query_desc => $query_desc_b2_c, |
200 |
query_cgi => $query_cgi_b, |
201 |
total => $total, |
202 |
type => 'biblio', |
203 |
}; |
204 |
|
205 |
my $query_desc_a2_c = q{second current authority search}; |
206 |
my $second_current_authority_search = { |
207 |
userid => $userid, |
208 |
sessionid => $current_sessionid, |
209 |
query_desc => $query_desc_a2_c, |
210 |
query_cgi => $query_cgi_a, |
211 |
total => $total, |
212 |
type => 'authority', |
213 |
}; |
214 |
|
215 |
my $query_desc_a3_c = q{third current authority search}; |
216 |
my $third_current_authority_search = { |
217 |
userid => $userid, |
218 |
sessionid => $current_sessionid, |
219 |
query_desc => $query_desc_a3_c, |
220 |
query_cgi => $query_cgi_a, |
221 |
total => $total, |
222 |
type => 'authority', |
223 |
}; |
224 |
|
225 |
|
226 |
my $r = 0; |
227 |
$r += C4::Search::History::add( $first_current_biblio_search ); |
228 |
$r += C4::Search::History::add( $first_current_authority_search ); |
229 |
$r += C4::Search::History::add( $second_current_biblio_search ); |
230 |
$r += C4::Search::History::add( $second_current_authority_search ); |
231 |
$r += C4::Search::History::add( $first_previous_biblio_search ); |
232 |
$r += C4::Search::History::add( $first_previous_authority_search ); |
233 |
$r += C4::Search::History::add( $second_previous_biblio_search ); |
234 |
$r += C4::Search::History::add( $second_previous_authority_search ); |
235 |
$r += C4::Search::History::add( $third_current_authority_search ); |
236 |
return $r; |
237 |
} |
238 |
|
239 |
sub delete_all { |
240 |
my $userid = shift; |
241 |
C4::Search::History::delete({ |
242 |
userid => $userid, |
243 |
}); |
244 |
} |
245 |
|
246 |
$dbh->rollback; |
247 |
|
248 |
done_testing; |