blob: f6e588e6e2cdaaed22456c569b8653329e4a0eac [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
Akron0e1ed242018-10-11 13:22:00 +020028# Query passed
Akron8ea84292018-10-24 13:41:52 +020029$t->get_ok('/?q=baum')
Akron0e1ed242018-10-11 13:22:00 +020030 ->status_is(200)
31 ->text_is('#error','')
Akron8ea84292018-10-24 13:41:52 +020032
Akron32396632018-10-11 17:08:37 +020033 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
34 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
Akron0e1ed242018-10-11 13:22:00 +020035 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
Akrondffa9392018-10-12 16:26:09 +020036
37 # Total results
Akron32396632018-10-11 17:08:37 +020038 ->text_is('#total-results', 51)
Akrondffa9392018-10-12 16:26:09 +020039
40 # Total pages
Akron18a2a272020-06-16 11:47:38 +020041 ->element_count_is('#pagination > a', 5)
Akrondffa9392018-10-12 16:26:09 +020042
43 # api_response
44 ->content_like(qr/\"authorized\":null/)
45 ->content_like(qr/\"pubDate\",\"subTitle\",\"author\"/)
46
Akron8ea84292018-10-24 13:41:52 +020047 # No cutOff
48 ->content_unlike(qr!\"cutOff":true!)
49
Akrondffa9392018-10-12 16:26:09 +020050 ->element_exists('li[data-text-sigle=GOE/AGI/00000]')
51 ->element_exists('li:nth-of-type(1) div.flop')
52 ->element_exists('li[data-text-sigle=GOE/AGI/00001]')
53 ->element_exists('li:nth-of-type(2) div.flip')
54
55 # Match1
56 ->element_exists('li:nth-of-type(1)' .
57 '[data-match-id="p2030-2031"]' .
58 '[data-text-sigle="GOE/AGI/00000"]' .
59 '[id="GOE/AGI/00000#p2030-2031"]' .
60 '[data-available-info^="base/s=spans"]' .
61 '[data-info^="{"]')
62 ->text_is('li:nth-of-type(1) div.meta', 'GOE/AGI/00000')
63 ->element_exists('li:nth-of-type(1) div.match-main div.match-wrap div.snippet')
64 ->element_exists('li:nth-of-type(1) div.snippet.startMore.endMore')
65 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
66 ->text_like('li:nth-of-type(1) div.snippet span.context-left',qr!sie etwas bedeuten!)
67 ->text_is('li:nth-of-type(1) div.snippet span.match mark','Baum')
68 ->text_like('li:nth-of-type(1) div.snippet span.context-right',qr!es war!)
69 ->text_is('li:nth-of-type(1) p.ref strong', 'Italienische Reise')
70 ->text_like('li:nth-of-type(1) p.ref', qr!by Goethe, Johann Wolfgang!)
71 ->text_is('li:nth-of-type(1) p.ref time[datetime=1982]', 1982)
72 ->text_is('li:nth-of-type(1) p.ref span.sigle', '[GOE/AGI/00000]')
Akron8ea84292018-10-24 13:41:52 +020073 ->header_isnt('X-Kalamar-Cache', 'true')
Akron0e1ed242018-10-11 13:22:00 +020074 ;
75
Akron8ea84292018-10-24 13:41:52 +020076$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020077 ->status_is(400)
78 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010079 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020080 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
81 ;
82
Akron73f36082018-10-25 15:34:59 +020083# Check for query error with ql (from remote.t)
84$t->get_ok('/?q=[orth=das&ql=poliqarp')
85 ->element_exists('.notify-error')
86 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
87 ->content_like(qr!KorAP\.koralQuery =!)
Akron3c390c42020-03-30 09:06:21 +020088 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020089 ;
90
91
Akron8ea84292018-10-24 13:41:52 +020092# Query with partial cache (for total results)
93$t->get_ok('/?q=baum')
94 ->status_is(200)
95 ->text_is('#error','')
96 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
97 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
98 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
99 ->header_isnt('X-Kalamar-Cache', 'true')
100 ->content_like(qr!\"cutOff":true!)
101 ->text_is('#total-results', 51)
102 ;
103
Akron385249d2018-10-29 12:26:29 +0100104# Query without partial cache (unfortunately) (but no total results)
105$t->get_ok('/?q=baum&cutoff=true')
106 ->status_is(200)
107 ->text_is('#error','')
108 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
109 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
110 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
111 ->header_isnt('X-Kalamar-Cache', 'true')
112 ->content_like(qr!\"cutOff":true!)
113 ->element_exists_not('#total-results')
114 ;
115
116# Query with partial cache (but no total results)
117$t->get_ok('/?q=baum&cutoff=true')
118 ->status_is(200)
119 ->text_is('#error','')
120 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
121 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
122 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
123 ->header_is('X-Kalamar-Cache', 'true')
124 ->content_like(qr!\"cutOff":true!)
125 ->element_exists_not('#total-results')
126 ;
127
Akron8ea84292018-10-24 13:41:52 +0200128# Query with full cache
129$t->get_ok('/?q=baum')
130 ->status_is(200)
131 ->text_is('#error','')
132 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
133 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
134 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
135 ->header_is('X-Kalamar-Cache', 'true')
136 ->content_like(qr!\"cutOff":true!)
137 ->text_is('#total-results', 51)
138 ;
139
140
141# Query with page information
142$t->get_ok('/?q=der&p=1&count=2')
143 ->status_is(200)
144 ->text_is('#error','')
145 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
146
147 # Total results
148 ->text_is('#total-results', '14,581')
149
150 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200151 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200152 ->text_is('#pagination a:nth-of-type(6) span', 7291)
153 ->content_like(qr!\"count":2!)
154 ->content_like(qr!\"startIndex":0!)
155 ->content_like(qr!\"itemsPerPage":2!)
156
157 # No caching
158 ->header_isnt('X-Kalamar-Cache', 'true')
159
160 # Not searched for "der" before
161 ->content_unlike(qr!\"cutOff":true!)
162 ;
163
Akron4c7cf952019-08-29 09:31:35 +0200164# Check pagination repetion of page
165my $next_href = $t->get_ok('/?q=der&p=1&count=2')
166 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
167like($next_href, qr/p=2/);
168unlike($next_href, qr/p=1/);
169
Akron8ea84292018-10-24 13:41:52 +0200170# Query with page information - next page
171$t->get_ok('/?q=der&p=2&count=2')
172 ->status_is(200)
173 ->text_is('#error','')
174 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100175 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200176
177 # Total results
178 ->text_is('#total-results', '14,581')
179
180 # Total pages
Akron18a2a272020-06-16 11:47:38 +0200181 ->element_count_is('#pagination > a', 7)
Akron8ea84292018-10-24 13:41:52 +0200182 ->text_is('#pagination a:nth-of-type(6) span', 7291)
183 ->content_like(qr!\"count":2!)
184 ->content_like(qr!\"itemsPerPage":2!)
185 ->content_like(qr!\"startIndex":2!)
186
187 # No caching
188 ->header_isnt('X-Kalamar-Cache', 'true')
189 ->content_like(qr!\"cutOff":true!)
190 ;
191
Akronc4be8192018-10-25 16:07:53 +0200192# Query with failing parameters
193$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
194 ->status_is(400)
195 ->text_is('noscript div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100196 ->element_exists('#search')
Akronc4be8192018-10-25 16:07:53 +0200197 ->element_count_is('noscript div.notify-error', 1)
198 ;
199$t->get_ok('/?q=fantastisch&cutoff=no')
200 ->status_is(400)
201 ->text_is('noscript div.notify-error', 'Parameter "cutoff" invalid')
202 ->element_count_is('noscript div.notify-error', 1)
203 ;
204$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
205 ->status_is(400)
206 ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!)
207 ->element_count_is('noscript div.notify-error', 3)
208 ;
Akron8ea84292018-10-24 13:41:52 +0200209
Akrond1ff8d82018-11-08 13:16:55 +0100210# Query too long
211my $long_query = 'b' x 2000;
212$t->get_ok('/?q=' . $long_query)
213 ->status_is(400)
214 ->text_is('#error','')
215 ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!)
216 ;
217
Akrona3c353c2019-02-14 23:50:00 +0100218# Query with timeout
219$t->get_ok('/?q=timeout')
220 ->status_is(200)
221 ->text_like('noscript div.notify-warn', qr!Response time exceeded!)
222 ->text_is('#total-results', '> 4,274,841');
223;
224
225# Do not cache
226$t->get_ok('/?q=timeout')
227 ->status_is(200)
228 # ->text_like('noscript div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200229 ->element_exists("input#cq")
230 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100231 ->text_is('#total-results', '> 4,274,841');
232 ;
233
Akron91a76852019-08-28 12:35:37 +0200234$t->app->defaults(no_cache => 1);
235
Akroncd42a142019-07-12 18:55:37 +0200236# Query with collection
237$t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
238 ->status_is(200)
239 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akron91a76852019-08-28 12:35:37 +0200240 ->content_like(qr!\"availability\"!)
Akroncd42a142019-07-12 18:55:37 +0200241 ->text_is('#error','')
242 ;
243
Akron4cdc4fc2020-04-28 12:19:11 +0200244$t->app->hook(
Akron7c87c1a2020-04-28 12:35:55 +0200245 after_search => sub {
Akron4cdc4fc2020-04-28 12:19:11 +0200246 my $c = shift;
247 $c->content_for('after_search_results' => '<p id="special">Funny</p>');
248 }
249);
250
Akroncd42a142019-07-12 18:55:37 +0200251# Query with corpus query
252$t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
253 ->status_is(200)
254 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akron91a76852019-08-28 12:35:37 +0200255 ->content_like(qr!\"availability\"!)
Akroncd42a142019-07-12 18:55:37 +0200256 ->text_is('#error','')
Akron4cdc4fc2020-04-28 12:19:11 +0200257 ->text_is('#special', 'Funny')
Akroncd42a142019-07-12 18:55:37 +0200258 ;
Akron8ea84292018-10-24 13:41:52 +0200259
Akron4c7cf952019-08-29 09:31:35 +0200260
Akron909ed082019-12-11 21:38:27 +0100261my $match = {
262 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
263 textSigle => 'FOLK/00070-SE-01/T-04'
264};
265
266$match = Kalamar::Controller::Search::_map_match($match);
267
268is($match->{matchID}, 'p5441-5442');
269
270
Akron0e1ed242018-10-11 13:22:00 +0200271done_testing;
Akron8ea84292018-10-24 13:41:52 +0200272__END__