blob: 50d81b15e7ca2ab2ac7b97a0e05fe86635c01e33 [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
46 # api_response
Akronbc6b3f22021-01-13 14:53:12 +010047 ->content_like(qr/${q}authorized${q}:null/)
48 ->content_like(qr/${q}pubDate${q},${q}subTitle${q},${q}author${q}/)
Akrondffa9392018-10-12 16:26:09 +020049
Akron8ea84292018-10-24 13:41:52 +020050 # No cutOff
Akronbc6b3f22021-01-13 14:53:12 +010051 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +020052
Akrondffa9392018-10-12 16:26:09 +020053 ->element_exists('li[data-text-sigle=GOE/AGI/00000]')
54 ->element_exists('li:nth-of-type(1) div.flop')
55 ->element_exists('li[data-text-sigle=GOE/AGI/00001]')
56 ->element_exists('li:nth-of-type(2) div.flip')
57
58 # Match1
59 ->element_exists('li:nth-of-type(1)' .
60 '[data-match-id="p2030-2031"]' .
61 '[data-text-sigle="GOE/AGI/00000"]' .
62 '[id="GOE/AGI/00000#p2030-2031"]' .
63 '[data-available-info^="base/s=spans"]' .
64 '[data-info^="{"]')
65 ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000')
66 ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet')
67 ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore')
68 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
69 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
70 ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum')
71 ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!)
72 ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise')
73 ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!)
74 ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982)
75 ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]')
Akron8ea84292018-10-24 13:41:52 +020076 ->header_isnt('X-Kalamar-Cache', 'true')
Akron0e1ed242018-10-11 13:22:00 +020077 ;
78
Akron8ea84292018-10-24 13:41:52 +020079$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020080 ->status_is(400)
81 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010082 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020083 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
84 ;
85
Akron73f36082018-10-25 15:34:59 +020086# Check for query error with ql (from remote.t)
87$t->get_ok('/?q=[orth=das&ql=poliqarp')
88 ->element_exists('.notify-error')
89 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
Akronbc6b3f22021-01-13 14:53:12 +010090 ->content_like(qr!data-koralquery=!)
Akron3c390c42020-03-30 09:06:21 +020091 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020092 ;
93
94
Akron8ea84292018-10-24 13:41:52 +020095# Query with partial cache (for total results)
96$t->get_ok('/?q=baum')
97 ->status_is(200)
98 ->text_is('#error','')
99 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
100 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
101 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
102 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100103 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200104 ->text_is('#total-results', 51)
105 ;
106
Akron385249d2018-10-29 12:26:29 +0100107# Query without partial cache (unfortunately) (but no total results)
108$t->get_ok('/?q=baum&cutoff=true')
109 ->status_is(200)
110 ->text_is('#error','')
111 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
112 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
113 ->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!)
Akron385249d2018-10-29 12:26:29 +0100116 ->element_exists_not('#total-results')
117 ;
118
119# Query with partial cache (but no total results)
120$t->get_ok('/?q=baum&cutoff=true')
121 ->status_is(200)
122 ->text_is('#error','')
123 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
124 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
125 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
126 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100127 ->content_like(qr!${q}cutOff${q}:true!)
Akron385249d2018-10-29 12:26:29 +0100128 ->element_exists_not('#total-results')
129 ;
130
Akron8ea84292018-10-24 13:41:52 +0200131# Query with full cache
132$t->get_ok('/?q=baum')
133 ->status_is(200)
134 ->text_is('#error','')
135 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
136 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
137 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
138 ->header_is('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100139 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200140 ->text_is('#total-results', 51)
141 ;
142
143
144# Query with page information
145$t->get_ok('/?q=der&p=1&count=2')
146 ->status_is(200)
147 ->text_is('#error','')
148 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
149
150 # Total results
151 ->text_is('#total-results', '14,581')
152
153 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200154 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200155 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100156 ->content_like(qr!${q}count${q}:2!)
157 ->content_like(qr!${q}startIndex${q}:0!)
158 ->content_like(qr!${q}itemsPerPage${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200159
160 # No caching
161 ->header_isnt('X-Kalamar-Cache', 'true')
162
163 # Not searched for "der" before
Akronbc6b3f22021-01-13 14:53:12 +0100164 ->content_unlike(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200165 ;
166
Akron4c7cf952019-08-29 09:31:35 +0200167# Check pagination repetion of page
168my $next_href = $t->get_ok('/?q=der&p=1&count=2')
169 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
170like($next_href, qr/p=2/);
171unlike($next_href, qr/p=1/);
172
Akron8ea84292018-10-24 13:41:52 +0200173# Query with page information - next page
174$t->get_ok('/?q=der&p=2&count=2')
175 ->status_is(200)
176 ->text_is('#error','')
177 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100178 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200179
180 # Total results
181 ->text_is('#total-results', '14,581')
182
183 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200184 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200185 ->text_is('#pagination a:nth-of-type(6) span', 7291)
Akronbc6b3f22021-01-13 14:53:12 +0100186 ->content_like(qr!${q}count${q}:2!)
187 ->content_like(qr!${q}itemsPerPage${q}:2!)
188 ->content_like(qr!${q}startIndex${q}:2!)
Akron8ea84292018-10-24 13:41:52 +0200189
190 # No caching
191 ->header_isnt('X-Kalamar-Cache', 'true')
Akronbc6b3f22021-01-13 14:53:12 +0100192 ->content_like(qr!${q}cutOff${q}:true!)
Akron8ea84292018-10-24 13:41:52 +0200193 ;
194
Akronc4be8192018-10-25 16:07:53 +0200195# Query with failing parameters
196$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
197 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100198 ->text_is('#notifications div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100199 ->element_exists('#search')
Akroncb5c1712021-01-26 18:01:04 +0100200 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200201 ;
202$t->get_ok('/?q=fantastisch&cutoff=no')
203 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100204 ->text_is('#notifications div.notify-error', 'Parameter "cutoff" invalid')
205 ->element_count_is('#notifications div.notify-error', 1)
Akronc4be8192018-10-25 16:07:53 +0200206 ;
207$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
208 ->status_is(400)
Akroncb5c1712021-01-26 18:01:04 +0100209 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
210 ->element_count_is('#notifications div.notify-error', 3)
Akronc4be8192018-10-25 16:07:53 +0200211 ;
Akron8ea84292018-10-24 13:41:52 +0200212
Akrond1ff8d82018-11-08 13:16:55 +0100213# Query too long
214my $long_query = 'b' x 2000;
215$t->get_ok('/?q=' . $long_query)
216 ->status_is(400)
217 ->text_is('#error','')
Akroncb5c1712021-01-26 18:01:04 +0100218 ->text_like('#notifications div.notify-error', qr!Parameter ".+?" invalid!)
Akrond1ff8d82018-11-08 13:16:55 +0100219 ;
220
Akrona3c353c2019-02-14 23:50:00 +0100221# Query with timeout
222$t->get_ok('/?q=timeout')
223 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100224 ->text_like('#notifications div.notify-warn', qr!Response time exceeded!)
Akrona3c353c2019-02-14 23:50:00 +0100225 ->text_is('#total-results', '> 4,274,841');
226;
227
Akroncce055c2021-07-02 12:18:03 +0200228# Query with error
229$t->get_ok('/?q=error')
230 ->status_is(400)
231 ->text_is('#notifications .notify-error','500: Internal Server Error')
232;
233
Akrona3c353c2019-02-14 23:50:00 +0100234# Do not cache
235$t->get_ok('/?q=timeout')
236 ->status_is(200)
Akroncb5c1712021-01-26 18:01:04 +0100237 # ->text_like('#notifications div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200238 ->element_exists("input#cq")
239 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100240 ->text_is('#total-results', '> 4,274,841');
241 ;
242
Akron91a76852019-08-28 12:35:37 +0200243$t->app->defaults(no_cache => 1);
244
Akroncd42a142019-07-12 18:55:37 +0200245# Query with collection
246$t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
247 ->status_is(200)
248 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100249 ->content_like(qr!${q}availability${q}!)
Akroncd42a142019-07-12 18:55:37 +0200250 ->text_is('#error','')
251 ;
252
Akron4cdc4fc2020-04-28 12:19:11 +0200253$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200254 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200255 my $c = shift;
256 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
257 }
258);
259
Akroncd42a142019-07-12 18:55:37 +0200260# Query with corpus query
261$t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
262 ->status_is(200)
263 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akronbc6b3f22021-01-13 14:53:12 +0100264 ->content_like(qr!${q}availability${q}!)
Akroncd42a142019-07-12 18:55:37 +0200265 ->text_is('#error','')
Akron4cdc4fc2020-04-28 12:19:11 +0200266 ->text_is('#special', 'Funny')
Akroncd42a142019-07-12 18:55:37 +0200267 ;
Akron8ea84292018-10-24 13:41:52 +0200268
Akron909ed082019-12-11 21:38:27 +0100269my $match = {
270 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
271 textSigle => 'FOLK/00070-SE-01/T-04'
272};
273
274$match = Kalamar::Controller::Search::_map_match($match);
275
276is($match->{matchID}, 'p5441-5442');
277
Akron7b9a1962020-07-02 09:52:53 +0200278# Query with pipe
279$t->get_ok('/?q=baum&pipe=glemm')
280 ->status_is(200)
281 ->text_is('#error','')
Akronbc6b3f22021-01-13 14:53:12 +0100282 ->content_like(qr/${q}pipes${q}:${q}glemm${q}/)
Akron7b9a1962020-07-02 09:52:53 +0200283 ;
284
Akron909ed082019-12-11 21:38:27 +0100285
Akron0e1ed242018-10-11 13:22:00 +0200286done_testing;
Akron8ea84292018-10-24 13:41:52 +0200287__END__