blob: c7d0a55729dd81a6d43bd84982adf86aac0107a5 [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
Akronc23ce662021-12-14 12:39:42 +0100157$err = $t->get_ok('/?q=der&p=1&count=2' => { 'Accept-Language' => 'en-US, en, de-DE' })
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)
Akronc23ce662021-12-14 12:39:42 +0100166 ->text_is('#pagination a:nth-of-type(6) span', '7,291')
Akrona4b17f72021-11-04 15:37:02 +0100167 ->element_exists('#pagination a.ellipsis.inactive')
Akronbc6b3f22021-01-13 14:53:12 +0100168 ->content_like(qr!${q}count${q}:2!)
169 ->content_like(qr!${q}startIndex${q}:0!)
170 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200171
172 # No caching
173 ->header_isnt('X-Kalamar-Cache', 'true')
174
175 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100176 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron26d57f22021-09-10 16:48:57 +0200177
178 ->attr_is('#pagination','data-page','1')
179 ->attr_is('#pagination','data-total','7291')
180 ->attr_is('#pagination','data-count','2')
181
Akron58c60992021-09-07 13:11:43 +0200182 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200183 ;
Akron58c60992021-09-07 13:11:43 +0200184is(defined $err ? $err->text : '', '');
185
Akron8ea84292018-10-24 13:41:52 +0200186
Akron4c7cf952019-08-29 09:31:35 +0200187# Check pagination repetion of page
188my $next_href = $t->get_ok('/?q=der&p=1&count=2')
189 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
190like($next_href, qr/p=2/);
191unlike($next_href, qr/p=1/);
192
Akron8ea84292018-10-24 13:41:52 +0200193# Query with page information - next page
Akronc23ce662021-12-14 12:39:42 +0100194$err = $t->get_ok('/?q=der&p=2&count=2' => { 'Accept-Language' => 'de-DE, en-US, en' })
Akron8ea84292018-10-24 13:41:52 +0200195 ->status_is(200)
Akronc23ce662021-12-14 12:39:42 +0100196 ->text_is('title', 'KorAP: Finde »der« mit Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100197 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200198
199 # Total results
Akronc23ce662021-12-14 12:39:42 +0100200 ->text_is('#total-results', '14.581')
Akron8ea84292018-10-24 13:41:52 +0200201
202 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200203 ->element_count_is('#pagination > a', 7)
Akronc23ce662021-12-14 12:39:42 +0100204 ->text_is('#pagination a:nth-of-type(6) span', '7.291')
Akronbc6b3f22021-01-13 14:53:12 +0100205 ->content_like(qr!${q}count${q}:2!)
206 ->content_like(qr!${q}itemsPerPage${q}:2!)
207 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200208
Akron26d57f22021-09-10 16:48:57 +0200209 ->attr_is('#pagination','data-page','2')
210 ->attr_is('#pagination','data-total','7291')
211 ->attr_is('#pagination','data-count','2')
212
Akron8ea84292018-10-24 13:41:52 +0200213 # No caching
214 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100215 ->content_like(qr!${q}cutOff${q}:true!)
Akron58c60992021-09-07 13:11:43 +0200216 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200217 ;
Akron58c60992021-09-07 13:11:43 +0200218is(defined $err ? $err->text : '', '');
219
Akron8ea84292018-10-24 13:41:52 +0200220
Akronc4be8192018-10-25 16:07:53 +0200221# Query with failing parameters
222$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
223 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100224 ->text_is('#notifications div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100225 ->element_exists('#search')
Akroncb5c1712021-01-26 18:01:04 +0100226 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200227 ;
228$t->get_ok('/?q=fantastisch&cutoff=no')
229 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100230 ->text_is('#notifications div.notify-error', 'Parameter "cutoff" invalid')
231 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200232 ;
233$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
234 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100235 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
236 ->element_count_is('#notifications div.notify-error', 3)
Akronc4be8192018-10-25 16:07:53 +0200237 ;
Akron8ea84292018-10-24 13:41:52 +0200238
Marc Kupietz4dc3d502022-07-06 16:50:14 +0200239# Long, but not too long query
240my $long_query = 'b' x 4096;
Akron58c60992021-09-07 13:11:43 +0200241$err = $t->get_ok('/?q=' . $long_query)
Akrond1ff8d82018-11-08 13:16:55 +0100242 ->status_is(400)
Marc Kupietz4dc3d502022-07-06 16:50:14 +0200243 ->text_like('#notifications div.notify-error', qr!Unable to load query response from!)
244 ->tx->res->dom->at('#error')
245 ;
246
247# Query too long
248my $too_long_query = 'b' x 4097;
249$err = $t->get_ok('/?q=' . $too_long_query)
250 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100251 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
Akron58c60992021-09-07 13:11:43 +0200252 ->tx->res->dom->at('#error')
Akrond1ff8d82018-11-08 13:16:55 +0100253 ;
Akron58c60992021-09-07 13:11:43 +0200254is(defined $err ? $err->text : '', '');
Akrond1ff8d82018-11-08 13:16:55 +0100255
Akrona3c353c2019-02-14 23:50:00 +0100256# Query with timeout
257$t->get_ok('/?q=timeout')
258 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100259 ->text_like('#notifications div.notify-warn', qr!Response time exceeded!)
Akrona3c353c2019-02-14 23:50:00 +0100260 ->text_is('#total-results', '> 4,274,841');
261;
262
Akroncce055c2021-07-02 12:18:03 +0200263# Query with error
264$t->get_ok('/?q=error')
265 ->status_is(400)
266 ->text_is('#notifications .notify-error','500: Internal Server Error')
267;
268
Akrona3c353c2019-02-14 23:50:00 +0100269# Do not cache
270$t->get_ok('/?q=timeout')
271 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100272 # ->text_like('#notifications div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200273 ->element_exists("input#cq")
274 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100275 ->text_is('#total-results', '> 4,274,841');
276 ;
277
Akron91a76852019-08-28 12:35:37 +0200278$t->app->defaults(no_cache => 1);
279
Akroncd42a142019-07-12 18:55:37 +0200280# Query with collection
Akron58c60992021-09-07 13:11:43 +0200281$err = $t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200282 ->status_is(200)
283 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100284 ->content_like(qr!${q}availability${q}!)
Akron58c60992021-09-07 13:11:43 +0200285 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200286 ;
Akron58c60992021-09-07 13:11:43 +0200287is(defined $err ? $err->text : '', '');
288
Akroncd42a142019-07-12 18:55:37 +0200289
Akron4cdc4fc2020-04-28 12:19:11 +0200290$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200291 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200292 my $c = shift;
293 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
294 }
295);
296
Akroncd42a142019-07-12 18:55:37 +0200297# Query with corpus query
Akron58c60992021-09-07 13:11:43 +0200298$err = $t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200299 ->status_is(200)
300 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100301 ->content_like(qr!${q}availability${q}!)
Akron4cdc4fc2020-04-28 12:19:11 +0200302 ->text_is('#special', 'Funny')
Akron58c60992021-09-07 13:11:43 +0200303 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200304 ;
Akron58c60992021-09-07 13:11:43 +0200305is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200306
Akron909ed082019-12-11 21:38:27 +0100307my $match = {
308 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
309 textSigle => 'FOLK/00070-SE-01/T-04'
310};
311
312$match = Kalamar::Controller::Search::_map_match($match);
313
314is($match->{matchID}, 'p5441-5442');
315
Akron7b9a1962020-07-02 09:52:53 +0200316# Query with pipe
Akron58c60992021-09-07 13:11:43 +0200317$err = $t->get_ok('/?q=baum&pipe=glemm')
Akron7b9a1962020-07-02 09:52:53 +0200318 ->status_is(200)
Akronbc6b3f22021-01-13 14:53:12 +0100319 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron58c60992021-09-07 13:11:43 +0200320 ->tx->res->dom->at('#error')
Akron7b9a1962020-07-02 09:52:53 +0200321 ;
Akron58c60992021-09-07 13:11:43 +0200322is(defined $err ? $err->text : '', '');
Akron7b9a1962020-07-02 09:52:53 +0200323
Akron909ed082019-12-11 21:38:27 +0100324
Akron0e1ed242018-10-11 13:22:00 +0200325done_testing;
Akron8ea84292018-10-24 13:41:52 +0200326__END__