blob: 1dc3d92ea27e99777a61f4c45549bb80c42b774a [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')
Akron58c60992021-09-07 13:11:43 +020078 ->tx->res->dom->at('#error')
Akron0e1ed242018-10-11 13:22:00 +020079 ;
Akron58c60992021-09-07 13:11:43 +020080is(defined $err ? $err->text : '', '');
81
Akron0e1ed242018-10-11 13:22:00 +020082
Akron8ea84292018-10-24 13:41:52 +020083$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020084 ->status_is(400)
85 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010086 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020087 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
88 ;
89
Akron73f36082018-10-25 15:34:59 +020090# Check for query error with ql (from remote.t)
91$t->get_ok('/?q=[orth=das&ql=poliqarp')
92 ->element_exists('.notify-error')
93 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
Akronbc6b3f22021-01-13 14:53:12 +010094 ->content_like(qr!data-koralquery=!)
Akron3c390c42020-03-30 09:06:21 +020095 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020096 ;
97
98
Akron8ea84292018-10-24 13:41:52 +020099# Query with partial cache (for total results)
Akron58c60992021-09-07 13:11:43 +0200100$err = $t->get_ok('/?q=baum')
Akron8ea84292018-10-24 13:41:52 +0200101 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200102 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
103 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
104 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
105 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100106 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200107 ->text_is('#total-results', 51)
Akron58c60992021-09-07 13:11:43 +0200108 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200109 ;
Akron58c60992021-09-07 13:11:43 +0200110is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200111
Akron385249d2018-10-29 12:26:29 +0100112# Query without partial cache (unfortunately) (but no total results)
Akron58c60992021-09-07 13:11:43 +0200113$err = $t->get_ok('/?q=baum&cutoff=true')
Akron385249d2018-10-29 12:26:29 +0100114 ->status_is(200)
Akron385249d2018-10-29 12:26:29 +0100115 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
116 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
117 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
118 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100119 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100120 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200121 ->tx->res->dom->at('#error')
Akron385249d2018-10-29 12:26:29 +0100122 ;
Akron58c60992021-09-07 13:11:43 +0200123is(defined $err ? $err->text : '', '');
Akron385249d2018-10-29 12:26:29 +0100124
125# Query with partial cache (but no total results)
Akron58c60992021-09-07 13:11:43 +0200126$err = $t->get_ok('/?q=baum&cutoff=true')
Akron385249d2018-10-29 12:26:29 +0100127 ->status_is(200)
Akron385249d2018-10-29 12:26:29 +0100128 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
129 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
130 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
131 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100132 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100133 ->element_exists_not('#total-results')
Akron58c60992021-09-07 13:11:43 +0200134 ->tx->res->dom->at('#error')
Akron385249d2018-10-29 12:26:29 +0100135 ;
Akron58c60992021-09-07 13:11:43 +0200136is(defined $err ? $err->text : '', '');
137
Akron385249d2018-10-29 12:26:29 +0100138
Akron8ea84292018-10-24 13:41:52 +0200139# Query with full cache
Akron58c60992021-09-07 13:11:43 +0200140$err = $t->get_ok('/?q=baum')
Akron8ea84292018-10-24 13:41:52 +0200141 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200142 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
143 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
144 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
145 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100146 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200147 ->text_is('#total-results', 51)
Akron58c60992021-09-07 13:11:43 +0200148 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200149 ;
Akron58c60992021-09-07 13:11:43 +0200150is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200151
152
153# Query with page information
Akron58c60992021-09-07 13:11:43 +0200154$err = $t->get_ok('/?q=der&p=1&count=2')
Akron8ea84292018-10-24 13:41:52 +0200155 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200156 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
157
158 # Total results
159 ->text_is('#total-results', '14,581')
160
161 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200162 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200163 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100164 ->content_like(qr!${q}count${q}:2!)
165 ->content_like(qr!${q}startIndex${q}:0!)
166 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200167
168 # No caching
169 ->header_isnt('X-Kalamar-Cache', 'true')
170
171 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100172 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron58c60992021-09-07 13:11:43 +0200173 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200174 ;
Akron58c60992021-09-07 13:11:43 +0200175is(defined $err ? $err->text : '', '');
176
Akron8ea84292018-10-24 13:41:52 +0200177
Akron4c7cf952019-08-29 09:31:35 +0200178# Check pagination repetion of page
179my $next_href = $t->get_ok('/?q=der&p=1&count=2')
180 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
181like($next_href, qr/p=2/);
182unlike($next_href, qr/p=1/);
183
Akron8ea84292018-10-24 13:41:52 +0200184# Query with page information - next page
Akron58c60992021-09-07 13:11:43 +0200185$err = $t->get_ok('/?q=der&p=2&count=2')
Akron8ea84292018-10-24 13:41:52 +0200186 ->status_is(200)
Akron8ea84292018-10-24 13:41:52 +0200187 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100188 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200189
190 # Total results
191 ->text_is('#total-results', '14,581')
192
193 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200194 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200195 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100196 ->content_like(qr!${q}count${q}:2!)
197 ->content_like(qr!${q}itemsPerPage${q}:2!)
198 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200199
200 # No caching
201 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100202 ->content_like(qr!${q}cutOff${q}:true!)
Akron58c60992021-09-07 13:11:43 +0200203 ->tx->res->dom->at('#error')
Akron8ea84292018-10-24 13:41:52 +0200204 ;
Akron58c60992021-09-07 13:11:43 +0200205is(defined $err ? $err->text : '', '');
206
Akron8ea84292018-10-24 13:41:52 +0200207
Akronc4be8192018-10-25 16:07:53 +0200208# Query with failing parameters
209$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
210 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100211 ->text_is('#notifications div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100212 ->element_exists('#search')
Akroncb5c1712021-01-26 18:01:04 +0100213 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200214 ;
215$t->get_ok('/?q=fantastisch&cutoff=no')
216 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100217 ->text_is('#notifications div.notify-error', 'Parameter "cutoff" invalid')
218 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200219 ;
220$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
221 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100222 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
223 ->element_count_is('#notifications div.notify-error', 3)
Akronc4be8192018-10-25 16:07:53 +0200224 ;
Akron8ea84292018-10-24 13:41:52 +0200225
Akrond1ff8d82018-11-08 13:16:55 +0100226# Query too long
227my $long_query = 'b' x 2000;
Akron58c60992021-09-07 13:11:43 +0200228$err = $t->get_ok('/?q=' . $long_query)
Akrond1ff8d82018-11-08 13:16:55 +0100229 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100230 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
Akron58c60992021-09-07 13:11:43 +0200231 ->tx->res->dom->at('#error')
Akrond1ff8d82018-11-08 13:16:55 +0100232 ;
Akron58c60992021-09-07 13:11:43 +0200233is(defined $err ? $err->text : '', '');
Akrond1ff8d82018-11-08 13:16:55 +0100234
Akrona3c353c2019-02-14 23:50:00 +0100235# Query with timeout
236$t->get_ok('/?q=timeout')
237 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100238 ->text_like('#notifications div.notify-warn', qr!Response time exceeded!)
Akrona3c353c2019-02-14 23:50:00 +0100239 ->text_is('#total-results', '> 4,274,841');
240;
241
Akroncce055c2021-07-02 12:18:03 +0200242# Query with error
243$t->get_ok('/?q=error')
244 ->status_is(400)
245 ->text_is('#notifications .notify-error','500: Internal Server Error')
246;
247
Akrona3c353c2019-02-14 23:50:00 +0100248# Do not cache
249$t->get_ok('/?q=timeout')
250 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100251 # ->text_like('#notifications div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200252 ->element_exists("input#cq")
253 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100254 ->text_is('#total-results', '> 4,274,841');
255 ;
256
Akron91a76852019-08-28 12:35:37 +0200257$t->app->defaults(no_cache => 1);
258
Akroncd42a142019-07-12 18:55:37 +0200259# Query with collection
Akron58c60992021-09-07 13:11:43 +0200260$err = $t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200261 ->status_is(200)
262 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100263 ->content_like(qr!${q}availability${q}!)
Akron58c60992021-09-07 13:11:43 +0200264 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200265 ;
Akron58c60992021-09-07 13:11:43 +0200266is(defined $err ? $err->text : '', '');
267
Akroncd42a142019-07-12 18:55:37 +0200268
Akron4cdc4fc2020-04-28 12:19:11 +0200269$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200270 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200271 my $c = shift;
272 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
273 }
274);
275
Akroncd42a142019-07-12 18:55:37 +0200276# Query with corpus query
Akron58c60992021-09-07 13:11:43 +0200277$err = $t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
Akroncd42a142019-07-12 18:55:37 +0200278 ->status_is(200)
279 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100280 ->content_like(qr!${q}availability${q}!)
Akron4cdc4fc2020-04-28 12:19:11 +0200281 ->text_is('#special', 'Funny')
Akron58c60992021-09-07 13:11:43 +0200282 ->tx->res->dom->at('#error')
Akroncd42a142019-07-12 18:55:37 +0200283 ;
Akron58c60992021-09-07 13:11:43 +0200284is(defined $err ? $err->text : '', '');
Akron8ea84292018-10-24 13:41:52 +0200285
Akron909ed082019-12-11 21:38:27 +0100286my $match = {
287 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
288 textSigle => 'FOLK/00070-SE-01/T-04'
289};
290
291$match = Kalamar::Controller::Search::_map_match($match);
292
293is($match->{matchID}, 'p5441-5442');
294
Akron7b9a1962020-07-02 09:52:53 +0200295# Query with pipe
Akron58c60992021-09-07 13:11:43 +0200296$err = $t->get_ok('/?q=baum&pipe=glemm')
Akron7b9a1962020-07-02 09:52:53 +0200297 ->status_is(200)
Akronbc6b3f22021-01-13 14:53:12 +0100298 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron58c60992021-09-07 13:11:43 +0200299 ->tx->res->dom->at('#error')
Akron7b9a1962020-07-02 09:52:53 +0200300 ;
Akron58c60992021-09-07 13:11:43 +0200301is(defined $err ? $err->text : '', '');
Akron7b9a1962020-07-02 09:52:53 +0200302
Akron909ed082019-12-11 21:38:27 +0100303
Akron0e1ed242018-10-11 13:22:00 +0200304done_testing;
Akron8ea84292018-10-24 13:41:52 +0200305__END__