5 use Test
::More tests
=> 18;
7 use List
::Util
qw( shuffle );
10 my $dbh = C4
::Context
->dbh;
11 $dbh->{AutoCommit
} = 0;
12 $dbh->{RaiseError
} = 1;
14 use_ok
('Koha::DateUtils');
15 use_ok
('C4::Search::History');
18 my $previous_sessionid = "PREVIOUS_SESSIONID";
19 my $current_sessionid = "CURRENT_SESSIONID";
21 my $query_cgi_b = q{idx=kw&idx=ti&idx=au%2Cwrdl&q=word1é&q=word2è&q=word3à&do=Search&sort_by=author_az};
22 my $query_cgi_a = q{op=do_search&type=opac&authtypecode=NP&operator=start&value=Harry&marclist=match&and_or=and&orderby=HeadingAsc};
25 my $added = add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
26 is
( $added, 9, '9 searches are added' );
29 my $searches_for_userid = C4
::Search
::History
::get
({
32 is
( scalar(@
$searches_for_userid), 9, 'There are 9 searches in all' );
34 my $searches_for_current_session = C4
::Search
::History
::get
({
36 sessionid
=> $current_sessionid,
38 is
( scalar(@
$searches_for_current_session), 5, 'There are 5 searches for the current session' );
40 my $searches_for_previous_sessions = C4
::Search
::History
::get
({
42 sessionid
=> $current_sessionid,
45 is
( scalar(@
$searches_for_previous_sessions), 4, 'There are 4 searches for previous sessions' );
47 my $authority_searches_for_current_session = C4
::Search
::History
::get
({
49 sessionid
=> $current_sessionid,
52 is
( scalar(@
$authority_searches_for_current_session), 3, 'There are 3 authority searches for the current session' );
54 my $authority_searches_for_previous_session = C4
::Search
::History
::get
({
56 sessionid
=> $current_sessionid,
60 is
( scalar(@
$authority_searches_for_previous_session), 2, 'There are 2 authority searches for previous sessions' );
62 my $biblio_searches_for_userid = C4
::Search
::History
::get
({
66 is
( scalar(@
$biblio_searches_for_userid), 4, 'There are 5 searches for the current session' );
68 my $authority_searches_for_userid = C4
::Search
::History
::get
({
72 is
( scalar(@
$authority_searches_for_userid), 5, 'There are 4 searches for previous sessions' );
74 delete_all
( $userid );
77 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
78 C4
::Search
::History
::delete({
80 sessionid
=> $current_sessionid,
83 my $all = C4
::Search
::History
::get
({userid
=> $userid});
84 is
( scalar(@
$all), 6, 'There are 6 searches in all after deleting current biblio searches' );
85 delete_all
( $userid );
87 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
88 C4
::Search
::History
::delete({
90 sessionid
=> $current_sessionid,
94 $all = C4
::Search
::History
::get
({userid
=> $userid});
95 is
( scalar(@
$all), 7, 'There are 7 searches in all after deleting previous authority searches' );
96 delete_all
( $userid );
98 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
99 C4
::Search
::History
::delete({
101 sessionid
=> $current_sessionid,
104 $all = C4
::Search
::History
::get
({userid
=> $userid});
105 is
( scalar(@
$all), 5, 'There are 5 searches in all after deleting all previous searches' );
106 delete_all
( $userid );
108 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
109 C4
::Search
::History
::delete({
111 sessionid
=> $current_sessionid,
113 $all = C4
::Search
::History
::get
({userid
=> $userid});
114 is
( scalar(@
$all), 4, 'There are 5 searches in all after deleting all searches for a sessionid' );
115 delete_all
( $userid );
117 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
118 C4
::Search
::History
::delete({
121 $all = C4
::Search
::History
::get
({userid
=> $userid});
122 is
( scalar(@
$all), 0, 'There are 0 search after deleting all searches for a userid' );
123 delete_all
( $userid );
125 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
126 C4
::Search
::History
::delete({});
127 $all = C4
::Search
::History
::get
({userid
=> $userid});
128 is
( scalar(@
$all), 9, 'There are still 9 searches after calling delete without userid' );
129 delete_all
( $userid );
131 # Delete (with a given id)
132 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
133 $all = C4
::Search
::History
::get
({ userid
=> $userid });
135 my $ids = [ shuffle
map { $_->{id
} } @
$all ];
136 for my $id ( @
$ids[ 0 .. 4 ] ) {
137 C4
::Search
::History
::delete({ id
=> $id });
139 $all = C4
::Search
::History
::get
({ userid
=> $userid });
140 is
( scalar(@
$all), 4, 'There are 4 searches after calling 5 times delete with id' );
141 delete_all
( $userid );
143 add
( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
144 $all = C4
::Search
::History
::get
({ userid
=> $userid });
146 $ids = [ shuffle
map { $_->{id
} } @
$all ];
147 C4
::Search
::History
::delete({ id
=> [ @
$ids[0..4] ] });
148 $all = C4
::Search
::History
::get
({ userid
=> $userid });
149 is
( scalar(@
$all), 4, 'There are 4 searches after calling delete with 5 ids' );
150 delete_all
( $userid );
153 my ( $userid, $current_session_id, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a ) = @_;
155 my $query_desc_b1_p = q{first previous biblio search};
156 my $first_previous_biblio_search = {
158 sessionid
=> $previous_sessionid,
159 query_desc
=> $query_desc_b1_p,
160 query_cgi
=> $query_cgi_b,
165 my $query_desc_a1_p = q{first previous authority search};
166 my $first_previous_authority_search = {
168 sessionid
=> $previous_sessionid,
169 query_desc
=> $query_desc_a1_p,
170 query_cgi
=> $query_cgi_a,
175 my $query_desc_b2_p = q{second previous biblio search};
176 my $second_previous_biblio_search = {
178 sessionid
=> $previous_sessionid,
179 query_desc
=> $query_desc_b2_p,
180 query_cgi
=> $query_cgi_b,
185 my $query_desc_a2_p = q{second previous authority search};
186 my $second_previous_authority_search = {
188 sessionid
=> $previous_sessionid,
189 query_desc
=> $query_desc_a2_p,
190 query_cgi
=> $query_cgi_a,
196 my $query_desc_b1_c = q{first current biblio search};
198 my $first_current_biblio_search = {
200 sessionid
=> $current_sessionid,
201 query_desc
=> $query_desc_b1_c,
202 query_cgi
=> $query_cgi_b,
207 my $query_desc_a1_c = q{first current authority search};
208 my $first_current_authority_search = {
210 sessionid
=> $current_sessionid,
211 query_desc
=> $query_desc_a1_c,
212 query_cgi
=> $query_cgi_a,
217 my $query_desc_b2_c = q{second current biblio search};
218 my $second_current_biblio_search = {
220 sessionid
=> $current_sessionid,
221 query_desc
=> $query_desc_b2_c,
222 query_cgi
=> $query_cgi_b,
227 my $query_desc_a2_c = q{second current authority search};
228 my $second_current_authority_search = {
230 sessionid
=> $current_sessionid,
231 query_desc
=> $query_desc_a2_c,
232 query_cgi
=> $query_cgi_a,
237 my $query_desc_a3_c = q{third current authority search};
238 my $third_current_authority_search = {
240 sessionid
=> $current_sessionid,
241 query_desc
=> $query_desc_a3_c,
242 query_cgi
=> $query_cgi_a,
249 $r += C4
::Search
::History
::add
( $first_current_biblio_search );
250 $r += C4
::Search
::History
::add
( $first_current_authority_search );
251 $r += C4
::Search
::History
::add
( $second_current_biblio_search );
252 $r += C4
::Search
::History
::add
( $second_current_authority_search );
253 $r += C4
::Search
::History
::add
( $first_previous_biblio_search );
254 $r += C4
::Search
::History
::add
( $first_previous_authority_search );
255 $r += C4
::Search
::History
::add
( $second_previous_biblio_search );
256 $r += C4
::Search
::History
::add
( $second_previous_authority_search );
257 $r += C4
::Search
::History
::add
( $third_current_authority_search );
263 C4
::Search
::History
::delete({