blob: 376a8795fb024f38a1b899fd3645d333f8c428e4 [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
Akronc4177492023-11-29 08:35:37 +010014my $t = Test::Mojo->new('Kalamar' => {
15 Localize => {
16 dict => {
17 title_addon => 'Test'
18 }
19 }
20});
Akron0e1ed242018-10-11 13:22:00 +020021
Akron32396632018-10-11 17:08:37 +020022# Mount fake backend
23# Get the fixture path
Akron73f36082018-10-25 15:34:59 +020024my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server');
Akron32396632018-10-11 17:08:37 +020025my $fake_backend = $t->app->plugin(
26 Mount => {
27 $mount_point =>
Akron73f36082018-10-25 15:34:59 +020028 $fixtures_path->child('mock.pl')
Akron32396632018-10-11 17:08:37 +020029 }
30);
31# Configure fake backend
32$fake_backend->pattern->defaults->{app}->log($t->app->log);
33
Akronbc6b3f22021-01-13 14:53:12 +010034my $q = qr!(?:\"|")!;
35
Akron0e1ed242018-10-11 13:22:00 +020036# Query passed
Akron58c60992021-09-07 13:11:43 +020037my $err = $t->get_ok('/?q=baum')
Akron0e1ed242018-10-11 13:22:00 +020038 ->status_is(200)
Akron69481a42021-03-22 10:31:16 +010039 ->content_type_is('text/html;charset=UTF-8')
Akron8ea84292018-10-24 13:41:52 +020040
Akronc4177492023-11-29 08:35:37 +010041 ->text_is('title', 'KorAP-Test: Find »baum« with Poliqarp')
42 ->element_exists('meta[name="DC.title"][content="KorAP-Test: Find »baum« with Poliqarp"]')
Akron0e1ed242018-10-11 13:22:00 +020043 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
Akrondffa9392018-10-12 16:26:09 +020044
45 # Total results
Akron32396632018-10-11 17:08:37 +020046 ->text_is('#total-results', 51)
Akrondffa9392018-10-12 16:26:09 +020047
48 # Total pages
Akron18a2a272020-06-16 11:47:38 +020049 ->element_count_is('#pagination > a', 5)
Akrondffa9392018-10-12 16:26:09 +020050
Akron9bd140e2021-07-27 16:20:03 +020051 ->element_exists_not('#resultinfo > #pagination')
52
Akrondffa9392018-10-12 16:26:09 +020053 # api_response
Akronbc6b3f22021-01-13 14:53:12 +010054 ->content_like(qr/${q}authorized${q}:null/)
55 ->content_like(qr/${q}pubDate${q},${q}subTitle${q},${q}author${q}/)
Akrondffa9392018-10-12 16:26:09 +020056
Akron8ea84292018-10-24 13:41:52 +020057 # No cutOff
Akronbc6b3f22021-01-13 14:53:12 +010058 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +020059
Akrondffa9392018-10-12 16:26:09 +020060 ->element_exists('li[data-text-sigle=GOE/AGI/00000]')
61 ->element_exists('li:nth-of-type(1) div.flop')
62 ->element_exists('li[data-text-sigle=GOE/AGI/00001]')
63 ->element_exists('li:nth-of-type(2) div.flip')
64
65 # Match1
66 ->element_exists('li:nth-of-type(1)' .
67 '[data-match-id="p2030-2031"]' .
68 '[data-text-sigle="GOE/AGI/00000"]' .
69 '[id="GOE/AGI/00000#p2030-2031"]' .
70 '[data-available-info^="base/s=spans"]' .
71 '[data-info^="{"]')
72 ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000')
73 ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet')
74 ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore')
75 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
76 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
77 ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum')
78 ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!)
79 ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise')
80 ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!)
81 ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982)
82 ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]')
Akron8ea84292018-10-24 13:41:52 +020083 ->header_isnt('X-Kalamar-Cache', 'true')
Akron26d57f22021-09-10 16:48:57 +020084 ->attr_is('#pagination','data-page','1')
85 ->attr_is('#pagination','data-total','3')
86 ->attr_is('#pagination','data-count','25')
Akron58c60992021-09-07 13:11:43 +020087 ->tx->res->dom->at('#error')
Akron0e1ed242018-10-11 13:22:00 +020088 ;
Akron58c60992021-09-07 13:11:43 +020089is(defined $err ? $err->text : '', '');
90
Akron0e1ed242018-10-11 13:22:00 +020091
Akron8ea84292018-10-24 13:41:52 +020092$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020093 ->status_is(400)
94 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010095 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020096 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
97 ;
98
Akron73f36082018-10-25 15:34:59 +020099# Check for query error with ql (from remote.t)
100$t->get_ok('/?q=[orth=das&ql=poliqarp')
101 ->element_exists('.notify-error')
102 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
Akronbc6b3f22021-01-13 14:53:12 +0100103 ->content_like(qr!data-koralquery=!)
Akron3c390c42020-03-30 09:06:21 +0200104 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +0200105 ;
106
107
Akron8ea84292018-10-24 13:41:52 +0200108# Query with partial cache (for total results)
Akron58c60992021-09-07 13:11:43 +0200109$err = $t->get_ok('/?q=baum')
Akron8ea84292018-10-24 13:41:52 +0200110 ->status_is(200)
Akronc4177492023-11-29 08:35:37 +0100111 ->text_is('title', 'KorAP-Test: Find »baum« with Poliqarp')
112 ->element_exists('meta[name="DC.title"][content="KorAP-Test: Find »baum« with Poliqarp"]')
Akron8ea84292018-10-24 13:41:52 +0200113 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
114 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100115 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200116 ->text_is('#total-results', 51)
Akron58c60992021-09-07 13:11:43 +0200117 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200118 ;
Akron58c60992021-09-07 13:11:43 +0200119is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200120
Akron385249d2018-10-29 12:26:29 +0100121# Query without partial cache (unfortunately) (but no total results)
Akron58c60992021-09-07 13:11:43 +0200122$err = $t->get_ok('/?q=baum&cutoff=true')
Akron385249d2018-10-29 12:26:29 +0100123 ->status_is(200)
Akronc4177492023-11-29 08:35:37 +0100124 ->text_is('title', 'KorAP-Test: Find »baum« with Poliqarp')
125 ->element_exists('meta[name="DC.title"][content="KorAP-Test: Find »baum« with Poliqarp"]')
Akron385249d2018-10-29 12:26:29 +0100126 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
127 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100128 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100129 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200130 ->tx->res->dom->at('#error')
Akron385249d2018-10-29 12:26:29 +0100131 ;
Akron58c60992021-09-07 13:11:43 +0200132is(defined $err ? $err->text : '', '');
Akron385249d2018-10-29 12:26:29 +0100133
134# Query with partial cache (but no total results)
Akron58c60992021-09-07 13:11:43 +0200135$err = $t->get_ok('/?q=baum&cutoff=true')
Akron385249d2018-10-29 12:26:29 +0100136 ->status_is(200)
Akronc4177492023-11-29 08:35:37 +0100137 ->text_is('title', 'KorAP-Test: Find »baum« with Poliqarp')
138 ->element_exists('meta[name="DC.title"][content="KorAP-Test: Find »baum« with Poliqarp"]')
Akron385249d2018-10-29 12:26:29 +0100139 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
140 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100141 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100142 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200143 ->tx->res->dom->at('#error')
Akron385249d2018-10-29 12:26:29 +0100144 ;
Akron58c60992021-09-07 13:11:43 +0200145is(defined $err ? $err->text : '', '');
146
Akron385249d2018-10-29 12:26:29 +0100147
Akron8ea84292018-10-24 13:41:52 +0200148# Query with full cache
Akron58c60992021-09-07 13:11:43 +0200149$err = $t->get_ok('/?q=baum')
Akron8ea84292018-10-24 13:41:52 +0200150 ->status_is(200)
Akronc4177492023-11-29 08:35:37 +0100151 ->text_is('title', 'KorAP-Test: Find »baum« with Poliqarp')
152 ->element_exists('meta[name="DC.title"][content="KorAP-Test: Find »baum« with Poliqarp"]')
Akron8ea84292018-10-24 13:41:52 +0200153 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
154 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100155 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200156 ->text_is('#total-results', 51)
Akron58c60992021-09-07 13:11:43 +0200157 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200158 ;
Akron58c60992021-09-07 13:11:43 +0200159is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200160
161
162# Query with page information
Akronc23ce662021-12-14 12:39:42 +0100163$err = $t->get_ok('/?q=der&p=1&count=2' => { 'Accept-Language' => 'en-US, en, de-DE' })
Akron8ea84292018-10-24 13:41:52 +0200164 ->status_is(200)
Akronc4177492023-11-29 08:35:37 +0100165 ->text_is('title', 'KorAP-Test: Find »der« with Poliqarp')
Akron8ea84292018-10-24 13:41:52 +0200166
167 # Total results
168 ->text_is('#total-results', '14,581')
169
170 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200171 ->element_count_is('#pagination > a', 7)
Akronc23ce662021-12-14 12:39:42 +0100172 ->text_is('#pagination a:nth-of-type(6) span', '7,291')
Akrona4b17f72021-11-04 15:37:02 +0100173 ->element_exists('#pagination a.ellipsis.inactive')
Akronbc6b3f22021-01-13 14:53:12 +0100174 ->content_like(qr!${q}count${q}:2!)
175 ->content_like(qr!${q}startIndex${q}:0!)
176 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200177
178 # No caching
179 ->header_isnt('X-Kalamar-Cache', 'true')
180
181 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100182 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron26d57f22021-09-10 16:48:57 +0200183
184 ->attr_is('#pagination','data-page','1')
185 ->attr_is('#pagination','data-total','7291')
186 ->attr_is('#pagination','data-count','2')
187
Akron58c60992021-09-07 13:11:43 +0200188 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200189 ;
Akron58c60992021-09-07 13:11:43 +0200190is(defined $err ? $err->text : '', '');
191
Akron8ea84292018-10-24 13:41:52 +0200192
Akron4c7cf952019-08-29 09:31:35 +0200193# Check pagination repetion of page
194my $next_href = $t->get_ok('/?q=der&p=1&count=2')
195 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
196like($next_href, qr/p=2/);
197unlike($next_href, qr/p=1/);
198
Akron8ea84292018-10-24 13:41:52 +0200199# Query with page information - next page
Akronc23ce662021-12-14 12:39:42 +0100200$err = $t->get_ok('/?q=der&p=2&count=2' => { 'Accept-Language' => 'de-DE, en-US, en' })
Akron8ea84292018-10-24 13:41:52 +0200201 ->status_is(200)
Akronc4177492023-11-29 08:35:37 +0100202 ->text_is('div.logoaddon', 'Test')
203 ->text_is('title', 'KorAP-Test: Finde »der« mit Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100204 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200205
206 # Total results
Akronc23ce662021-12-14 12:39:42 +0100207 ->text_is('#total-results', '14.581')
Akron8ea84292018-10-24 13:41:52 +0200208
209 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200210 ->element_count_is('#pagination > a', 7)
Akronc23ce662021-12-14 12:39:42 +0100211 ->text_is('#pagination a:nth-of-type(6) span', '7.291')
Akronbc6b3f22021-01-13 14:53:12 +0100212 ->content_like(qr!${q}count${q}:2!)
213 ->content_like(qr!${q}itemsPerPage${q}:2!)
214 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200215
Akron26d57f22021-09-10 16:48:57 +0200216 ->attr_is('#pagination','data-page','2')
217 ->attr_is('#pagination','data-total','7291')
218 ->attr_is('#pagination','data-count','2')
219
Akron8ea84292018-10-24 13:41:52 +0200220 # No caching
221 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100222 ->content_like(qr!${q}cutOff${q}:true!)
Akron58c60992021-09-07 13:11:43 +0200223 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200224 ;
Akron58c60992021-09-07 13:11:43 +0200225is(defined $err ? $err->text : '', '');
226
Akron8ea84292018-10-24 13:41:52 +0200227
Akronc4be8192018-10-25 16:07:53 +0200228# Query with failing parameters
229$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
230 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100231 ->text_is('#notifications div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100232 ->element_exists('#search')
Akroncb5c1712021-01-26 18:01:04 +0100233 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200234 ;
235$t->get_ok('/?q=fantastisch&cutoff=no')
236 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100237 ->text_is('#notifications div.notify-error', 'Parameter "cutoff" invalid')
238 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200239 ;
240$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
241 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100242 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
243 ->element_count_is('#notifications div.notify-error', 3)
Akronc4be8192018-10-25 16:07:53 +0200244 ;
Akron8ea84292018-10-24 13:41:52 +0200245
Marc Kupietz4dc3d502022-07-06 16:50:14 +0200246# Long, but not too long query
247my $long_query = 'b' x 4096;
Akron58c60992021-09-07 13:11:43 +0200248$err = $t->get_ok('/?q=' . $long_query)
Akrond1ff8d82018-11-08 13:16:55 +0100249 ->status_is(400)
Marc Kupietz4dc3d502022-07-06 16:50:14 +0200250 ->text_like('#notifications div.notify-error', qr!Unable to load query response from!)
251 ->tx->res->dom->at('#error')
252 ;
253
254# Query too long
255my $too_long_query = 'b' x 4097;
256$err = $t->get_ok('/?q=' . $too_long_query)
257 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100258 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
Akron58c60992021-09-07 13:11:43 +0200259 ->tx->res->dom->at('#error')
Akrond1ff8d82018-11-08 13:16:55 +0100260 ;
Akron58c60992021-09-07 13:11:43 +0200261is(defined $err ? $err->text : '', '');
Akrond1ff8d82018-11-08 13:16:55 +0100262
Akrona3c353c2019-02-14 23:50:00 +0100263# Query with timeout
264$t->get_ok('/?q=timeout')
265 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100266 ->text_like('#notifications div.notify-warn', qr!Response time exceeded!)
Akrona3c353c2019-02-14 23:50:00 +0100267 ->text_is('#total-results', '> 4,274,841');
268;
269
Akroncce055c2021-07-02 12:18:03 +0200270# Query with error
271$t->get_ok('/?q=error')
272 ->status_is(400)
273 ->text_is('#notifications .notify-error','500: Internal Server Error')
274;
275
Akrona3c353c2019-02-14 23:50:00 +0100276# Do not cache
277$t->get_ok('/?q=timeout')
278 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100279 # ->text_like('#notifications div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200280 ->element_exists("input#cq")
281 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100282 ->text_is('#total-results', '> 4,274,841');
283 ;
284
Akron91a76852019-08-28 12:35:37 +0200285$t->app->defaults(no_cache => 1);
286
Akroncd42a142019-07-12 18:55:37 +0200287# Query with collection
Akron58c60992021-09-07 13:11:43 +0200288$err = $t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200289 ->status_is(200)
290 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100291 ->content_like(qr!${q}availability${q}!)
Akron58c60992021-09-07 13:11:43 +0200292 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200293 ;
Akron58c60992021-09-07 13:11:43 +0200294is(defined $err ? $err->text : '', '');
295
Akroncd42a142019-07-12 18:55:37 +0200296
Akron4cdc4fc2020-04-28 12:19:11 +0200297$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200298 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200299 my $c = shift;
300 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
301 }
302);
303
Akroncd42a142019-07-12 18:55:37 +0200304# Query with corpus query
Akron58c60992021-09-07 13:11:43 +0200305$err = $t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200306 ->status_is(200)
307 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100308 ->content_like(qr!${q}availability${q}!)
Akron4cdc4fc2020-04-28 12:19:11 +0200309 ->text_is('#special', 'Funny')
Akron58c60992021-09-07 13:11:43 +0200310 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200311 ;
Akron58c60992021-09-07 13:11:43 +0200312is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200313
Akron909ed082019-12-11 21:38:27 +0100314my $match = {
315 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
316 textSigle => 'FOLK/00070-SE-01/T-04'
317};
318
319$match = Kalamar::Controller::Search::_map_match($match);
320
321is($match->{matchID}, 'p5441-5442');
322
Akron7b9a1962020-07-02 09:52:53 +0200323# Query with pipe
Akron58c60992021-09-07 13:11:43 +0200324$err = $t->get_ok('/?q=baum&pipe=glemm')
Akron7b9a1962020-07-02 09:52:53 +0200325 ->status_is(200)
Akronbc6b3f22021-01-13 14:53:12 +0100326 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron58c60992021-09-07 13:11:43 +0200327 ->tx->res->dom->at('#error')
Akron7b9a1962020-07-02 09:52:53 +0200328 ;
Akron58c60992021-09-07 13:11:43 +0200329is(defined $err ? $err->text : '', '');
Akron7b9a1962020-07-02 09:52:53 +0200330
Akron909ed082019-12-11 21:38:27 +0100331
Akron0e1ed242018-10-11 13:22:00 +0200332done_testing;
Akron8ea84292018-10-24 13:41:52 +0200333__END__