blob: b82d769bcd5b8580983e6f61f314ce7034f044c2 [file] [log] [blame]
Akron0e1ed242018-10-11 13:22:00 +02001use Mojo::Base -strict;
2use Test::Mojo;
3use Test::More;
Akron32396632018-10-11 17:08:37 +02004use Mojo::File qw/path/;
Akron909ed082019-12-11 21:38:27 +01005use Kalamar::Controller::Search;
Akron32396632018-10-11 17:08:37 +02006
7
8#####################
9# Start Fake server #
10#####################
Akron63d963b2019-07-05 15:35:51 +020011my $mount_point = '/realapi/';
Akron32396632018-10-11 17:08:37 +020012$ENV{KALAMAR_API} = $mount_point;
Akron0e1ed242018-10-11 13:22:00 +020013
14my $t = Test::Mojo->new('Kalamar');
15
Akron32396632018-10-11 17:08:37 +020016# Mount fake backend
17# Get the fixture path
Akron73f36082018-10-25 15:34:59 +020018my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
Akron32396632018-10-11 17:08:37 +020019my $fake_backend = $t->app->plugin(
20 Mount => {
21 $mount_point =>
Akron73f36082018-10-25 15:34:59 +020022 $fixtures_path->child('mock.pl')
Akron32396632018-10-11 17:08:37 +020023 }
24);
25# Configure fake backend
26$fake_backend->pattern->defaults->{app}->log($t->app->log);
27
Akronbc6b3f22021-01-13 14:53:12 +010028my $q = qr!(?:\"|")!;
29
Akron0e1ed242018-10-11 13:22:00 +020030# Query passed
Akron58c60992021-09-07 13:11:43 +020031my $err = $t->get_ok('/?q=baum')
Akron0e1ed242018-10-11 13:22:00 +020032 ->status_is(200)
Akron69481a42021-03-22 10:31:16 +010033 ->content_type_is('text/html;charset=UTF-8')
Akron8ea84292018-10-24 13:41:52 +020034
Akron32396632018-10-11 17:08:37 +020035 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
36 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
Akron0e1ed242018-10-11 13:22:00 +020037 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
Akrondffa9392018-10-12 16:26:09 +020038
39 # Total results
Akron32396632018-10-11 17:08:37 +020040 ->text_is('#total-results', 51)
Akrondffa9392018-10-12 16:26:09 +020041
42 # Total pages
Akron18a2a272020-06-16 11:47:38 +020043 ->element_count_is('#pagination > a', 5)
Akrondffa9392018-10-12 16:26:09 +020044
Akron9bd140e2021-07-27 16:20:03 +020045 ->element_exists_not('#resultinfo > #pagination')
46
Akrondffa9392018-10-12 16:26:09 +020047 # api_response
Akronbc6b3f22021-01-13 14:53:12 +010048 ->content_like(qr/${q}authorized${q}:null/)
49 ->content_like(qr/${q}pubDate${q},${q}subTitle${q},${q}author${q}/)
Akrondffa9392018-10-12 16:26:09 +020050
Akron8ea84292018-10-24 13:41:52 +020051 # No cutOff
Akronbc6b3f22021-01-13 14:53:12 +010052 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +020053
Akrondffa9392018-10-12 16:26:09 +020054 ->element_exists('li[data-text-sigle=GOE/AGI/00000]')
55 ->element_exists('li:nth-of-type(1) div.flop')
56 ->element_exists('li[data-text-sigle=GOE/AGI/00001]')
57 ->element_exists('li:nth-of-type(2) div.flip')
58
59 # Match1
60 ->element_exists('li:nth-of-type(1)' .
61 '[data-match-id="p2030-2031"]' .
62 '[data-text-sigle="GOE/AGI/00000"]' .
63 '[id="GOE/AGI/00000#p2030-2031"]' .
64 '[data-available-info^="base/s=spans"]' .
65 '[data-info^="{"]')
66 ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000')
67 ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet')
68 ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore')
69 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
70 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
71 ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum')
72 ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!)
73 ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise')
74 ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!)
75 ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982)
76 ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]')
Akron8ea84292018-10-24 13:41:52 +020077 ->header_isnt('X-Kalamar-Cache', 'true')
Akron26d57f22021-09-10 16:48:57 +020078 ->attr_is('#pagination','data-page','1')
79 ->attr_is('#pagination','data-total','3')
80 ->attr_is('#pagination','data-count','25')
Akron58c60992021-09-07 13:11:43 +020081 ->tx->res->dom->at('#error')
Akron0e1ed242018-10-11 13:22:00 +020082 ;
Akron58c60992021-09-07 13:11:43 +020083is(defined $err ? $err->text : '', '');
84
Akron0e1ed242018-10-11 13:22:00 +020085
Akron8ea84292018-10-24 13:41:52 +020086$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020087 ->status_is(400)
88 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010089 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020090 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
91 ;
92
Akron73f36082018-10-25 15:34:59 +020093# Check for query error with ql (from remote.t)
94$t->get_ok('/?q=[orth=das&ql=poliqarp')
95 ->element_exists('.notify-error')
96 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
Akronbc6b3f22021-01-13 14:53:12 +010097 ->content_like(qr!data-koralquery=!)
Akron3c390c42020-03-30 09:06:21 +020098 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020099 ;
100
101
Akron8ea84292018-10-24 13:41:52 +0200102# Query with partial cache (for total results)
Akron58c60992021-09-07 13:11:43 +0200103$err = $t->get_ok('/?q=baum')
Akron8ea84292018-10-24 13:41:52 +0200104 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200105 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
106 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
107 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
108 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100109 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200110 ->text_is('#total-results', 51)
Akron58c60992021-09-07 13:11:43 +0200111 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200112 ;
Akron58c60992021-09-07 13:11:43 +0200113is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200114
Akron385249d2018-10-29 12:26:29 +0100115# Query without partial cache (unfortunately) (but no total results)
Akron58c60992021-09-07 13:11:43 +0200116$err = $t->get_ok('/?q=baum&cutoff=true')
Akron385249d2018-10-29 12:26:29 +0100117 ->status_is(200)
Akron385249d2018-10-29 12:26:29 +0100118 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
119 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
120 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
121 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100122 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100123 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200124 ->tx->res->dom->at('#error')
Akron385249d2018-10-29 12:26:29 +0100125 ;
Akron58c60992021-09-07 13:11:43 +0200126is(defined $err ? $err->text : '', '');
Akron385249d2018-10-29 12:26:29 +0100127
128# Query with partial cache (but no total results)
Akron58c60992021-09-07 13:11:43 +0200129$err = $t->get_ok('/?q=baum&cutoff=true')
Akron385249d2018-10-29 12:26:29 +0100130 ->status_is(200)
Akron385249d2018-10-29 12:26:29 +0100131 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
132 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
133 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
134 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100135 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100136 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200137 ->tx->res->dom->at('#error')
Akron385249d2018-10-29 12:26:29 +0100138 ;
Akron58c60992021-09-07 13:11:43 +0200139is(defined $err ? $err->text : '', '');
140
Akron385249d2018-10-29 12:26:29 +0100141
Akron8ea84292018-10-24 13:41:52 +0200142# Query with full cache
Akron58c60992021-09-07 13:11:43 +0200143$err = $t->get_ok('/?q=baum')
Akron8ea84292018-10-24 13:41:52 +0200144 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200145 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
146 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
147 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
148 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100149 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200150 ->text_is('#total-results', 51)
Akron58c60992021-09-07 13:11:43 +0200151 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200152 ;
Akron58c60992021-09-07 13:11:43 +0200153is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200154
155
156# Query with page information
Akron58c60992021-09-07 13:11:43 +0200157$err = $t->get_ok('/?q=der&p=1&count=2')
Akron8ea84292018-10-24 13:41:52 +0200158 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200159 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
160
161 # Total results
162 ->text_is('#total-results', '14,581')
163
164 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200165 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200166 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100167 ->content_like(qr!${q}count${q}:2!)
168 ->content_like(qr!${q}startIndex${q}:0!)
169 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200170
171 # No caching
172 ->header_isnt('X-Kalamar-Cache', 'true')
173
174 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100175 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron26d57f22021-09-10 16:48:57 +0200176
177 ->attr_is('#pagination','data-page','1')
178 ->attr_is('#pagination','data-total','7291')
179 ->attr_is('#pagination','data-count','2')
180
Akron58c60992021-09-07 13:11:43 +0200181 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200182 ;
Akron58c60992021-09-07 13:11:43 +0200183is(defined $err ? $err->text : '', '');
184
Akron8ea84292018-10-24 13:41:52 +0200185
Akron4c7cf952019-08-29 09:31:35 +0200186# Check pagination repetion of page
187my $next_href = $t->get_ok('/?q=der&p=1&count=2')
188 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
189like($next_href, qr/p=2/);
190unlike($next_href, qr/p=1/);
191
Akron8ea84292018-10-24 13:41:52 +0200192# Query with page information - next page
Akron58c60992021-09-07 13:11:43 +0200193$err = $t->get_ok('/?q=der&p=2&count=2')
Akron8ea84292018-10-24 13:41:52 +0200194 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200195 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100196 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200197
198 # Total results
199 ->text_is('#total-results', '14,581')
200
201 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200202 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200203 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100204 ->content_like(qr!${q}count${q}:2!)
205 ->content_like(qr!${q}itemsPerPage${q}:2!)
206 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200207
Akron26d57f22021-09-10 16:48:57 +0200208 ->attr_is('#pagination','data-page','2')
209 ->attr_is('#pagination','data-total','7291')
210 ->attr_is('#pagination','data-count','2')
211
Akron8ea84292018-10-24 13:41:52 +0200212 # No caching
213 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100214 ->content_like(qr!${q}cutOff${q}:true!)
Akron58c60992021-09-07 13:11:43 +0200215 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200216 ;
Akron58c60992021-09-07 13:11:43 +0200217is(defined $err ? $err->text : '', '');
218
Akron8ea84292018-10-24 13:41:52 +0200219
Akronc4be8192018-10-25 16:07:53 +0200220# Query with failing parameters
221$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
222 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100223 ->text_is('#notifications div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100224 ->element_exists('#search')
Akroncb5c1712021-01-26 18:01:04 +0100225 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200226 ;
227$t->get_ok('/?q=fantastisch&cutoff=no')
228 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100229 ->text_is('#notifications div.notify-error', 'Parameter "cutoff" invalid')
230 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200231 ;
232$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
233 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100234 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
235 ->element_count_is('#notifications div.notify-error', 3)
Akronc4be8192018-10-25 16:07:53 +0200236 ;
Akron8ea84292018-10-24 13:41:52 +0200237
Akrond1ff8d82018-11-08 13:16:55 +0100238# Query too long
239my $long_query = 'b' x 2000;
Akron58c60992021-09-07 13:11:43 +0200240$err = $t->get_ok('/?q=' . $long_query)
Akrond1ff8d82018-11-08 13:16:55 +0100241 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100242 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
Akron58c60992021-09-07 13:11:43 +0200243 ->tx->res->dom->at('#error')
Akrond1ff8d82018-11-08 13:16:55 +0100244 ;
Akron58c60992021-09-07 13:11:43 +0200245is(defined $err ? $err->text : '', '');
Akrond1ff8d82018-11-08 13:16:55 +0100246
Akrona3c353c2019-02-14 23:50:00 +0100247# Query with timeout
248$t->get_ok('/?q=timeout')
249 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100250 ->text_like('#notifications div.notify-warn', qr!Response time exceeded!)
Akrona3c353c2019-02-14 23:50:00 +0100251 ->text_is('#total-results', '> 4,274,841');
252;
253
Akroncce055c2021-07-02 12:18:03 +0200254# Query with error
255$t->get_ok('/?q=error')
256 ->status_is(400)
257 ->text_is('#notifications .notify-error','500: Internal Server Error')
258;
259
Akrona3c353c2019-02-14 23:50:00 +0100260# Do not cache
261$t->get_ok('/?q=timeout')
262 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100263 # ->text_like('#notifications div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200264 ->element_exists("input#cq")
265 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100266 ->text_is('#total-results', '> 4,274,841');
267 ;
268
Akron91a76852019-08-28 12:35:37 +0200269$t->app->defaults(no_cache => 1);
270
Akroncd42a142019-07-12 18:55:37 +0200271# Query with collection
Akron58c60992021-09-07 13:11:43 +0200272$err = $t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200273 ->status_is(200)
274 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100275 ->content_like(qr!${q}availability${q}!)
Akron58c60992021-09-07 13:11:43 +0200276 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200277 ;
Akron58c60992021-09-07 13:11:43 +0200278is(defined $err ? $err->text : '', '');
279
Akroncd42a142019-07-12 18:55:37 +0200280
Akron4cdc4fc2020-04-28 12:19:11 +0200281$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200282 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200283 my $c = shift;
284 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
285 }
286);
287
Akroncd42a142019-07-12 18:55:37 +0200288# Query with corpus query
Akron58c60992021-09-07 13:11:43 +0200289$err = $t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200290 ->status_is(200)
291 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100292 ->content_like(qr!${q}availability${q}!)
Akron4cdc4fc2020-04-28 12:19:11 +0200293 ->text_is('#special', 'Funny')
Akron58c60992021-09-07 13:11:43 +0200294 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200295 ;
Akron58c60992021-09-07 13:11:43 +0200296is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200297
Akron909ed082019-12-11 21:38:27 +0100298my $match = {
299 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
300 textSigle => 'FOLK/00070-SE-01/T-04'
301};
302
303$match = Kalamar::Controller::Search::_map_match($match);
304
305is($match->{matchID}, 'p5441-5442');
306
Akron7b9a1962020-07-02 09:52:53 +0200307# Query with pipe
Akron58c60992021-09-07 13:11:43 +0200308$err = $t->get_ok('/?q=baum&pipe=glemm')
Akron7b9a1962020-07-02 09:52:53 +0200309 ->status_is(200)
Akronbc6b3f22021-01-13 14:53:12 +0100310 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron58c60992021-09-07 13:11:43 +0200311 ->tx->res->dom->at('#error')
Akron7b9a1962020-07-02 09:52:53 +0200312 ;
Akron58c60992021-09-07 13:11:43 +0200313is(defined $err ? $err->text : '', '');
Akron7b9a1962020-07-02 09:52:53 +0200314
Akron909ed082019-12-11 21:38:27 +0100315
Akron0e1ed242018-10-11 13:22:00 +0200316done_testing;
Akron8ea84292018-10-24 13:41:52 +0200317__END__