blob: 73e488c913095b87d7f6df5476c0108d6cc6d9bf [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','')
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
45 # api_response
Akronbc6b3f22021-01-13 14:53:12 +010046 ->content_like(qr/${q}authorized${q}:null/)
47 ->content_like(qr/${q}pubDate${q},${q}subTitle${q},${q}author${q}/)
Akrondffa9392018-10-12 16:26:09 +020048
Akron8ea84292018-10-24 13:41:52 +020049 # No cutOff
Akronbc6b3f22021-01-13 14:53:12 +010050 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +020051
Akrondffa9392018-10-12 16:26:09 +020052 ->element_exists('li[data-text-sigle=GOE/AGI/00000]')
53 ->element_exists('li:nth-of-type(1) div.flop')
54 ->element_exists('li[data-text-sigle=GOE/AGI/00001]')
55 ->element_exists('li:nth-of-type(2) div.flip')
56
57 # Match1
58 ->element_exists('li:nth-of-type(1)' .
59 '[data-match-id="p2030-2031"]' .
60 '[data-text-sigle="GOE/AGI/00000"]' .
61 '[id="GOE/AGI/00000#p2030-2031"]' .
62 '[data-available-info^="base/s=spans"]' .
63 '[data-info^="{"]')
64 ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000')
65 ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet')
66 ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore')
67 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
68 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
69 ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum')
70 ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!)
71 ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise')
72 ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!)
73 ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982)
74 ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]')
Akron8ea84292018-10-24 13:41:52 +020075 ->header_isnt('X-Kalamar-Cache', 'true')
Akron0e1ed242018-10-11 13:22:00 +020076 ;
77
Akron8ea84292018-10-24 13:41:52 +020078$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020079 ->status_is(400)
80 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010081 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020082 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
83 ;
84
Akron73f36082018-10-25 15:34:59 +020085# Check for query error with ql (from remote.t)
86$t->get_ok('/?q=[orth=das&ql=poliqarp')
87 ->element_exists('.notify-error')
88 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
Akronbc6b3f22021-01-13 14:53:12 +010089 ->content_like(qr!data-koralquery=!)
Akron3c390c42020-03-30 09:06:21 +020090 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020091 ;
92
93
Akron8ea84292018-10-24 13:41:52 +020094# Query with partial cache (for total results)
95$t->get_ok('/?q=baum')
96 ->status_is(200)
97 ->text_is('#error','')
98 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
99 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
100 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
101 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100102 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200103 ->text_is('#total-results', 51)
104 ;
105
Akron385249d2018-10-29 12:26:29 +0100106# Query without partial cache (unfortunately) (but no total results)
107$t->get_ok('/?q=baum&cutoff=true')
108 ->status_is(200)
109 ->text_is('#error','')
110 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
111 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
112 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
113 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100114 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100115 ->element_exists_not('#total-results')
116 ;
117
118# Query with partial cache (but no total results)
119$t->get_ok('/?q=baum&cutoff=true')
120 ->status_is(200)
121 ->text_is('#error','')
122 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
123 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
124 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
125 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100126 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100127 ->element_exists_not('#total-results')
128 ;
129
Akron8ea84292018-10-24 13:41:52 +0200130# Query with full cache
131$t->get_ok('/?q=baum')
132 ->status_is(200)
133 ->text_is('#error','')
134 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
135 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
136 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
137 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100138 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200139 ->text_is('#total-results', 51)
140 ;
141
142
143# Query with page information
144$t->get_ok('/?q=der&p=1&count=2')
145 ->status_is(200)
146 ->text_is('#error','')
147 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
148
149 # Total results
150 ->text_is('#total-results', '14,581')
151
152 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200153 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200154 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100155 ->content_like(qr!${q}count${q}:2!)
156 ->content_like(qr!${q}startIndex${q}:0!)
157 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200158
159 # No caching
160 ->header_isnt('X-Kalamar-Cache', 'true')
161
162 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100163 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200164 ;
165
Akron4c7cf952019-08-29 09:31:35 +0200166# Check pagination repetion of page
167my $next_href = $t->get_ok('/?q=der&p=1&count=2')
168 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
169like($next_href, qr/p=2/);
170unlike($next_href, qr/p=1/);
171
Akron8ea84292018-10-24 13:41:52 +0200172# Query with page information - next page
173$t->get_ok('/?q=der&p=2&count=2')
174 ->status_is(200)
175 ->text_is('#error','')
176 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100177 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200178
179 # Total results
180 ->text_is('#total-results', '14,581')
181
182 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200183 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200184 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100185 ->content_like(qr!${q}count${q}:2!)
186 ->content_like(qr!${q}itemsPerPage${q}:2!)
187 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200188
189 # No caching
190 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100191 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200192 ;
193
Akronc4be8192018-10-25 16:07:53 +0200194# Query with failing parameters
195$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
196 ->status_is(400)
197 ->text_is('noscript div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100198 ->element_exists('#search')
Akronc4be8192018-10-25 16:07:53 +0200199 ->element_count_is('noscript div.notify-error', 1)
200 ;
201$t->get_ok('/?q=fantastisch&cutoff=no')
202 ->status_is(400)
203 ->text_is('noscript div.notify-error', 'Parameter "cutoff" invalid')
204 ->element_count_is('noscript div.notify-error', 1)
205 ;
206$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
207 ->status_is(400)
208 ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!)
209 ->element_count_is('noscript div.notify-error', 3)
210 ;
Akron8ea84292018-10-24 13:41:52 +0200211
Akrond1ff8d82018-11-08 13:16:55 +0100212# Query too long
213my $long_query = 'b' x 2000;
214$t->get_ok('/?q=' . $long_query)
215 ->status_is(400)
216 ->text_is('#error','')
217 ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!)
218 ;
219
Akrona3c353c2019-02-14 23:50:00 +0100220# Query with timeout
221$t->get_ok('/?q=timeout')
222 ->status_is(200)
223 ->text_like('noscript div.notify-warn', qr!Response time exceeded!)
224 ->text_is('#total-results', '> 4,274,841');
225;
226
227# Do not cache
228$t->get_ok('/?q=timeout')
229 ->status_is(200)
230 # ->text_like('noscript div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200231 ->element_exists("input#cq")
232 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100233 ->text_is('#total-results', '> 4,274,841');
234 ;
235
Akron91a76852019-08-28 12:35:37 +0200236$t->app->defaults(no_cache => 1);
237
Akroncd42a142019-07-12 18:55:37 +0200238# Query with collection
239$t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
240 ->status_is(200)
241 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100242 ->content_like(qr!${q}availability${q}!)
Akroncd42a142019-07-12 18:55:37 +0200243 ->text_is('#error','')
244 ;
245
Akron4cdc4fc2020-04-28 12:19:11 +0200246$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200247 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200248 my $c = shift;
249 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
250 }
251);
252
Akroncd42a142019-07-12 18:55:37 +0200253# Query with corpus query
254$t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
255 ->status_is(200)
256 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100257 ->content_like(qr!${q}availability${q}!)
Akroncd42a142019-07-12 18:55:37 +0200258 ->text_is('#error','')
Akron4cdc4fc2020-04-28 12:19:11 +0200259 ->text_is('#special', 'Funny')
Akroncd42a142019-07-12 18:55:37 +0200260 ;
Akron8ea84292018-10-24 13:41:52 +0200261
Akron909ed082019-12-11 21:38:27 +0100262my $match = {
263 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
264 textSigle => 'FOLK/00070-SE-01/T-04'
265};
266
267$match = Kalamar::Controller::Search::_map_match($match);
268
269is($match->{matchID}, 'p5441-5442');
270
Akron7b9a1962020-07-02 09:52:53 +0200271# Query with pipe
272$t->get_ok('/?q=baum&pipe=glemm')
273 ->status_is(200)
274 ->text_is('#error','')
Akronbc6b3f22021-01-13 14:53:12 +0100275 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron7b9a1962020-07-02 09:52:53 +0200276 ;
277
Akron909ed082019-12-11 21:38:27 +0100278
Akron0e1ed242018-10-11 13:22:00 +0200279done_testing;
Akron8ea84292018-10-24 13:41:52 +0200280__END__