Bug 11430: UT: add unit tests to delete only selected lines
[koha.git] / t / db_dependent / Search / History.t
blob5cd0c7784fc90735353c7496af4f765a7427e549
1 #!/usr/bin/env perl
3 use Modern::Perl;
5 use Test::More tests => 18;
6 use URI::Escape;
7 use List::Util qw( shuffle );
9 use C4::Context;
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');
17 my $userid = 123;
18 my $previous_sessionid = "PREVIOUS_SESSIONID";
19 my $current_sessionid = "CURRENT_SESSIONID";
20 my $total = 42;
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};
24 # add
25 my $added = add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
26 is ( $added, 9, '9 searches are added' );
28 # get
29 my $searches_for_userid = C4::Search::History::get({
30 userid => $userid,
31 });
32 is( scalar(@$searches_for_userid), 9, 'There are 9 searches in all' );
34 my $searches_for_current_session = C4::Search::History::get({
35 userid => $userid,
36 sessionid => $current_sessionid,
37 });
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({
41 userid => $userid,
42 sessionid => $current_sessionid,
43 previous => 1,
44 });
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({
48 userid => $userid,
49 sessionid => $current_sessionid,
50 type => 'authority',
51 });
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({
55 userid => $userid,
56 sessionid => $current_sessionid,
57 type => 'authority',
58 previous => 1,
59 });
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({
63 userid => $userid,
64 type => 'biblio',
65 });
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({
69 userid => $userid,
70 type => 'authority',
71 });
72 is( scalar(@$authority_searches_for_userid), 5, 'There are 4 searches for previous sessions' );
74 delete_all( $userid );
76 # delete
77 add( $userid, $current_sessionid, $previous_sessionid, $total, $query_cgi_b, $query_cgi_a );
78 C4::Search::History::delete({
79 userid => $userid,
80 sessionid => $current_sessionid,
81 type => 'authority',
82 });
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({
89 userid => $userid,
90 sessionid => $current_sessionid,
91 type => 'biblio',
92 previous => 1,
93 });
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({
100 userid => $userid,
101 sessionid => $current_sessionid,
102 previous => 1,
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({
110 userid => $userid,
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({
119 userid => $userid,
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 });
134 # Delete 5 searches
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 });
145 # Delete 5 searches
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 );
152 sub add {
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 = {
157 userid => $userid,
158 sessionid => $previous_sessionid,
159 query_desc => $query_desc_b1_p,
160 query_cgi => $query_cgi_b,
161 total => $total,
162 type => 'biblio',
165 my $query_desc_a1_p = q{first previous authority search};
166 my $first_previous_authority_search = {
167 userid => $userid,
168 sessionid => $previous_sessionid,
169 query_desc => $query_desc_a1_p,
170 query_cgi => $query_cgi_a,
171 total => $total,
172 type => 'authority',
175 my $query_desc_b2_p = q{second previous biblio search};
176 my $second_previous_biblio_search = {
177 userid => $userid,
178 sessionid => $previous_sessionid,
179 query_desc => $query_desc_b2_p,
180 query_cgi => $query_cgi_b,
181 total => $total,
182 type => 'biblio',
185 my $query_desc_a2_p = q{second previous authority search};
186 my $second_previous_authority_search = {
187 userid => $userid,
188 sessionid => $previous_sessionid,
189 query_desc => $query_desc_a2_p,
190 query_cgi => $query_cgi_a,
191 total => $total,
192 type => 'authority',
196 my $query_desc_b1_c = q{first current biblio search};
198 my $first_current_biblio_search = {
199 userid => $userid,
200 sessionid => $current_sessionid,
201 query_desc => $query_desc_b1_c,
202 query_cgi => $query_cgi_b,
203 total => $total,
204 type => 'biblio',
207 my $query_desc_a1_c = q{first current authority search};
208 my $first_current_authority_search = {
209 userid => $userid,
210 sessionid => $current_sessionid,
211 query_desc => $query_desc_a1_c,
212 query_cgi => $query_cgi_a,
213 total => $total,
214 type => 'authority',
217 my $query_desc_b2_c = q{second current biblio search};
218 my $second_current_biblio_search = {
219 userid => $userid,
220 sessionid => $current_sessionid,
221 query_desc => $query_desc_b2_c,
222 query_cgi => $query_cgi_b,
223 total => $total,
224 type => 'biblio',
227 my $query_desc_a2_c = q{second current authority search};
228 my $second_current_authority_search = {
229 userid => $userid,
230 sessionid => $current_sessionid,
231 query_desc => $query_desc_a2_c,
232 query_cgi => $query_cgi_a,
233 total => $total,
234 type => 'authority',
237 my $query_desc_a3_c = q{third current authority search};
238 my $third_current_authority_search = {
239 userid => $userid,
240 sessionid => $current_sessionid,
241 query_desc => $query_desc_a3_c,
242 query_cgi => $query_cgi_a,
243 total => $total,
244 type => 'authority',
248 my $r = 0;
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 );
258 return $r;
261 sub delete_all {
262 my $userid = shift;
263 C4::Search::History::delete({
264 userid => $userid,
268 $dbh->rollback;
270 done_testing;