blob: a2e2f447f9bcede14a1124ffc56fecdc59bf3f41 [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
Akron8ea84292018-10-24 13:41:52 +020031$t->get_ok('/?q=baum')
Akron0e1ed242018-10-11 13:22:00 +020032 ->status_is(200)
33 ->text_is('#error','')
Akron69481a42021-03-22 10:31:16 +010034 ->content_type_is('text/html;charset=UTF-8')
Akron8ea84292018-10-24 13:41:52 +020035
Akron32396632018-10-11 17:08:37 +020036 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
37 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
Akron0e1ed242018-10-11 13:22:00 +020038 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
Akrondffa9392018-10-12 16:26:09 +020039
40 # Total results
Akron32396632018-10-11 17:08:37 +020041 ->text_is('#total-results', 51)
Akrondffa9392018-10-12 16:26:09 +020042
43 # Total pages
Akron18a2a272020-06-16 11:47:38 +020044 ->element_count_is('#pagination > a', 5)
Akrondffa9392018-10-12 16:26:09 +020045
Akron9bd140e2021-07-27 16:20:03 +020046 ->element_exists_not('#resultinfo > #pagination')
47
Akrondffa9392018-10-12 16:26:09 +020048 # api_response
Akronbc6b3f22021-01-13 14:53:12 +010049 ->content_like(qr/${q}authorized${q}:null/)
50 ->content_like(qr/${q}pubDate${q},${q}subTitle${q},${q}author${q}/)
Akrondffa9392018-10-12 16:26:09 +020051
Akron8ea84292018-10-24 13:41:52 +020052 # No cutOff
Akronbc6b3f22021-01-13 14:53:12 +010053 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +020054
Akrondffa9392018-10-12 16:26:09 +020055 ->element_exists('li[data-text-sigle=GOE/AGI/00000]')
56 ->element_exists('li:nth-of-type(1) div.flop')
57 ->element_exists('li[data-text-sigle=GOE/AGI/00001]')
58 ->element_exists('li:nth-of-type(2) div.flip')
59
60 # Match1
61 ->element_exists('li:nth-of-type(1)' .
62 '[data-match-id="p2030-2031"]' .
63 '[data-text-sigle="GOE/AGI/00000"]' .
64 '[id="GOE/AGI/00000#p2030-2031"]' .
65 '[data-available-info^="base/s=spans"]' .
66 '[data-info^="{"]')
67 ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000')
68 ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet')
69 ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore')
70 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
71 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
72 ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum')
73 ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!)
74 ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise')
75 ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!)
76 ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982)
77 ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]')
Akron8ea84292018-10-24 13:41:52 +020078 ->header_isnt('X-Kalamar-Cache', 'true')
Akron0e1ed242018-10-11 13:22:00 +020079 ;
80
Akron8ea84292018-10-24 13:41:52 +020081$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020082 ->status_is(400)
83 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010084 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020085 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
86 ;
87
Akron73f36082018-10-25 15:34:59 +020088# Check for query error with ql (from remote.t)
89$t->get_ok('/?q=[orth=das&ql=poliqarp')
90 ->element_exists('.notify-error')
91 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
Akronbc6b3f22021-01-13 14:53:12 +010092 ->content_like(qr!data-koralquery=!)
Akron3c390c42020-03-30 09:06:21 +020093 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020094 ;
95
96
Akron8ea84292018-10-24 13:41:52 +020097# Query with partial cache (for total results)
98$t->get_ok('/?q=baum')
99 ->status_is(200)
100 ->text_is('#error','')
101 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
102 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
103 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
104 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100105 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200106 ->text_is('#total-results', 51)
107 ;
108
Akron385249d2018-10-29 12:26:29 +0100109# Query without partial cache (unfortunately) (but no total results)
110$t->get_ok('/?q=baum&cutoff=true')
111 ->status_is(200)
112 ->text_is('#error','')
113 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
114 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
115 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
116 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100117 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100118 ->element_exists_not('#total-results')
119 ;
120
121# Query with partial cache (but no total results)
122$t->get_ok('/?q=baum&cutoff=true')
123 ->status_is(200)
124 ->text_is('#error','')
125 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
126 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
127 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
128 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100129 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100130 ->element_exists_not('#total-results')
131 ;
132
Akron8ea84292018-10-24 13:41:52 +0200133# Query with full cache
134$t->get_ok('/?q=baum')
135 ->status_is(200)
136 ->text_is('#error','')
137 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
138 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
139 ->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!)
Akron8ea84292018-10-24 13:41:52 +0200142 ->text_is('#total-results', 51)
143 ;
144
145
146# Query with page information
147$t->get_ok('/?q=der&p=1&count=2')
148 ->status_is(200)
149 ->text_is('#error','')
150 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
151
152 # Total results
153 ->text_is('#total-results', '14,581')
154
155 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200156 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200157 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100158 ->content_like(qr!${q}count${q}:2!)
159 ->content_like(qr!${q}startIndex${q}:0!)
160 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200161
162 # No caching
163 ->header_isnt('X-Kalamar-Cache', 'true')
164
165 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100166 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200167 ;
168
Akron4c7cf952019-08-29 09:31:35 +0200169# Check pagination repetion of page
170my $next_href = $t->get_ok('/?q=der&p=1&count=2')
171 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
172like($next_href, qr/p=2/);
173unlike($next_href, qr/p=1/);
174
Akron8ea84292018-10-24 13:41:52 +0200175# Query with page information - next page
176$t->get_ok('/?q=der&p=2&count=2')
177 ->status_is(200)
178 ->text_is('#error','')
179 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100180 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200181
182 # Total results
183 ->text_is('#total-results', '14,581')
184
185 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200186 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200187 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100188 ->content_like(qr!${q}count${q}:2!)
189 ->content_like(qr!${q}itemsPerPage${q}:2!)
190 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200191
192 # No caching
193 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100194 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200195 ;
196
Akronc4be8192018-10-25 16:07:53 +0200197# Query with failing parameters
198$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
199 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100200 ->text_is('#notifications div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100201 ->element_exists('#search')
Akroncb5c1712021-01-26 18:01:04 +0100202 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200203 ;
204$t->get_ok('/?q=fantastisch&cutoff=no')
205 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100206 ->text_is('#notifications div.notify-error', 'Parameter "cutoff" invalid')
207 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200208 ;
209$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
210 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100211 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
212 ->element_count_is('#notifications div.notify-error', 3)
Akronc4be8192018-10-25 16:07:53 +0200213 ;
Akron8ea84292018-10-24 13:41:52 +0200214
Akrond1ff8d82018-11-08 13:16:55 +0100215# Query too long
216my $long_query = 'b' x 2000;
217$t->get_ok('/?q=' . $long_query)
218 ->status_is(400)
219 ->text_is('#error','')
Akroncb5c1712021-01-26 18:01:04 +0100220 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
Akrond1ff8d82018-11-08 13:16:55 +0100221 ;
222
Akrona3c353c2019-02-14 23:50:00 +0100223# Query with timeout
224$t->get_ok('/?q=timeout')
225 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100226 ->text_like('#notifications div.notify-warn', qr!Response time exceeded!)
Akrona3c353c2019-02-14 23:50:00 +0100227 ->text_is('#total-results', '> 4,274,841');
228;
229
Akroncce055c2021-07-02 12:18:03 +0200230# Query with error
231$t->get_ok('/?q=error')
232 ->status_is(400)
233 ->text_is('#notifications .notify-error','500: Internal Server Error')
234;
235
Akrona3c353c2019-02-14 23:50:00 +0100236# Do not cache
237$t->get_ok('/?q=timeout')
238 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100239 # ->text_like('#notifications div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200240 ->element_exists("input#cq")
241 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100242 ->text_is('#total-results', '> 4,274,841');
243 ;
244
Akron91a76852019-08-28 12:35:37 +0200245$t->app->defaults(no_cache => 1);
246
Akroncd42a142019-07-12 18:55:37 +0200247# Query with collection
248$t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
249 ->status_is(200)
250 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100251 ->content_like(qr!${q}availability${q}!)
Akroncd42a142019-07-12 18:55:37 +0200252 ->text_is('#error','')
253 ;
254
Akron4cdc4fc2020-04-28 12:19:11 +0200255$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200256 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200257 my $c = shift;
258 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
259 }
260);
261
Akroncd42a142019-07-12 18:55:37 +0200262# Query with corpus query
263$t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
264 ->status_is(200)
265 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100266 ->content_like(qr!${q}availability${q}!)
Akroncd42a142019-07-12 18:55:37 +0200267 ->text_is('#error','')
Akron4cdc4fc2020-04-28 12:19:11 +0200268 ->text_is('#special', 'Funny')
Akroncd42a142019-07-12 18:55:37 +0200269 ;
Akron8ea84292018-10-24 13:41:52 +0200270
Akron909ed082019-12-11 21:38:27 +0100271my $match = {
272 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
273 textSigle => 'FOLK/00070-SE-01/T-04'
274};
275
276$match = Kalamar::Controller::Search::_map_match($match);
277
278is($match->{matchID}, 'p5441-5442');
279
Akron7b9a1962020-07-02 09:52:53 +0200280# Query with pipe
281$t->get_ok('/?q=baum&pipe=glemm')
282 ->status_is(200)
283 ->text_is('#error','')
Akronbc6b3f22021-01-13 14:53:12 +0100284 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron7b9a1962020-07-02 09:52:53 +0200285 ;
286
Akron909ed082019-12-11 21:38:27 +0100287
Akron0e1ed242018-10-11 13:22:00 +0200288done_testing;
Akron8ea84292018-10-24 13:41:52 +0200289__END__