blob: f3b83a63586d2f252d14b9b5fe4642844576529a [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
41 ->element_count_is('#pagination a', 5)
42
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
Akron7093b812018-10-19 17:28:21 +020076
Akron8ea84292018-10-24 13:41:52 +020077$t->get_ok('/?q=[orth=das')
Akron7093b812018-10-19 17:28:21 +020078 ->status_is(400)
79 ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.')
Akronbc33beb2018-11-30 13:46:08 +010080 ->element_exists('#search')
Akron7093b812018-10-19 17:28:21 +020081 ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!)
82 ;
83
Akron73f36082018-10-25 15:34:59 +020084# Check for query error with ql (from remote.t)
85$t->get_ok('/?q=[orth=das&ql=poliqarp')
86 ->element_exists('.notify-error')
87 ->text_is('.notify-error', '302: Parantheses/brackets unbalanced.')
88 ->content_like(qr!KorAP\.koralQuery =!)
Akron3c390c42020-03-30 09:06:21 +020089 ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.')
Akron73f36082018-10-25 15:34:59 +020090 ;
91
92
Akron8ea84292018-10-24 13:41:52 +020093# Query with partial cache (for total results)
94$t->get_ok('/?q=baum')
95 ->status_is(200)
96 ->text_is('#error','')
97 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
98 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
99 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
100 ->header_isnt('X-Kalamar-Cache', 'true')
101 ->content_like(qr!\"cutOff":true!)
102 ->text_is('#total-results', 51)
103 ;
104
Akron385249d2018-10-29 12:26:29 +0100105# Query without partial cache (unfortunately) (but no total results)
106$t->get_ok('/?q=baum&cutoff=true')
107 ->status_is(200)
108 ->text_is('#error','')
109 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
110 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
111 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
112 ->header_isnt('X-Kalamar-Cache', 'true')
113 ->content_like(qr!\"cutOff":true!)
114 ->element_exists_not('#total-results')
115 ;
116
117# Query with partial cache (but no total results)
118$t->get_ok('/?q=baum&cutoff=true')
119 ->status_is(200)
120 ->text_is('#error','')
121 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
122 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
123 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
124 ->header_is('X-Kalamar-Cache', 'true')
125 ->content_like(qr!\"cutOff":true!)
126 ->element_exists_not('#total-results')
127 ;
128
Akron8ea84292018-10-24 13:41:52 +0200129# Query with full cache
130$t->get_ok('/?q=baum')
131 ->status_is(200)
132 ->text_is('#error','')
133 ->text_is('title', 'KorAP: Find »baum« with Poliqarp')
134 ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]')
135 ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]')
136 ->header_is('X-Kalamar-Cache', 'true')
137 ->content_like(qr!\"cutOff":true!)
138 ->text_is('#total-results', 51)
139 ;
140
141
142# Query with page information
143$t->get_ok('/?q=der&p=1&count=2')
144 ->status_is(200)
145 ->text_is('#error','')
146 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
147
148 # Total results
149 ->text_is('#total-results', '14,581')
150
151 # Total pages
152 ->element_count_is('#pagination a', 7)
153 ->text_is('#pagination a:nth-of-type(6) span', 7291)
154 ->content_like(qr!\"count":2!)
155 ->content_like(qr!\"startIndex":0!)
156 ->content_like(qr!\"itemsPerPage":2!)
157
158 # No caching
159 ->header_isnt('X-Kalamar-Cache', 'true')
160
161 # Not searched for "der" before
162 ->content_unlike(qr!\"cutOff":true!)
163 ;
164
Akron4c7cf952019-08-29 09:31:35 +0200165# Check pagination repetion of page
166my $next_href = $t->get_ok('/?q=der&p=1&count=2')
167 ->tx->res->dom->at('#pagination a[rel=next]')->attr('href');
168like($next_href, qr/p=2/);
169unlike($next_href, qr/p=1/);
170
Akron8ea84292018-10-24 13:41:52 +0200171# Query with page information - next page
172$t->get_ok('/?q=der&p=2&count=2')
173 ->status_is(200)
174 ->text_is('#error','')
175 ->text_is('title', 'KorAP: Find »der« with Poliqarp')
Akronbc33beb2018-11-30 13:46:08 +0100176 ->element_exists('#search')
Akron8ea84292018-10-24 13:41:52 +0200177
178 # Total results
179 ->text_is('#total-results', '14,581')
180
181 # Total pages
182 ->element_count_is('#pagination a', 7)
183 ->text_is('#pagination a:nth-of-type(6) span', 7291)
184 ->content_like(qr!\"count":2!)
185 ->content_like(qr!\"itemsPerPage":2!)
186 ->content_like(qr!\"startIndex":2!)
187
188 # No caching
189 ->header_isnt('X-Kalamar-Cache', 'true')
190 ->content_like(qr!\"cutOff":true!)
191 ;
192
Akronc4be8192018-10-25 16:07:53 +0200193# Query with failing parameters
194$t->get_ok('/?q=fantastisch&ql=Fabelsprache')
195 ->status_is(400)
196 ->text_is('noscript div.notify-error', 'Parameter "ql" invalid')
Akronbc33beb2018-11-30 13:46:08 +0100197 ->element_exists('#search')
Akronc4be8192018-10-25 16:07:53 +0200198 ->element_count_is('noscript div.notify-error', 1)
199 ;
200$t->get_ok('/?q=fantastisch&cutoff=no')
201 ->status_is(400)
202 ->text_is('noscript div.notify-error', 'Parameter "cutoff" invalid')
203 ->element_count_is('noscript div.notify-error', 1)
204 ;
205$t->get_ok('/?q=fantastisch&p=hui&o=hui&count=-8')
206 ->status_is(400)
207 ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!)
208 ->element_count_is('noscript div.notify-error', 3)
209 ;
Akron8ea84292018-10-24 13:41:52 +0200210
Akrond1ff8d82018-11-08 13:16:55 +0100211# Query too long
212my $long_query = 'b' x 2000;
213$t->get_ok('/?q=' . $long_query)
214 ->status_is(400)
215 ->text_is('#error','')
216 ->text_like('noscript div.notify-error', qr!Parameter ".+?" invalid!)
217 ;
218
Akrona3c353c2019-02-14 23:50:00 +0100219# Query with timeout
220$t->get_ok('/?q=timeout')
221 ->status_is(200)
222 ->text_like('noscript div.notify-warn', qr!Response time exceeded!)
223 ->text_is('#total-results', '> 4,274,841');
224;
225
226# Do not cache
227$t->get_ok('/?q=timeout')
228 ->status_is(200)
229 # ->text_like('noscript div.notify-warning', qr!Response time exceeded!)
Akroncd42a142019-07-12 18:55:37 +0200230 ->element_exists("input#cq")
231 ->element_exists_not("input#cq[value]")
Akrona3c353c2019-02-14 23:50:00 +0100232 ->text_is('#total-results', '> 4,274,841');
233 ;
234
Akron91a76852019-08-28 12:35:37 +0200235$t->app->defaults(no_cache => 1);
236
Akroncd42a142019-07-12 18:55:37 +0200237# Query with collection
238$t->get_ok('/?q=baum&collection=availability+%3D+%2FCC-BY.*%2F')
239 ->status_is(200)
240 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akron91a76852019-08-28 12:35:37 +0200241 ->content_like(qr!\"availability\"!)
Akroncd42a142019-07-12 18:55:37 +0200242 ->text_is('#error','')
243 ;
244
245# Query with corpus query
246$t->get_ok('/?q=baum&cq=availability+%3D+%2FCC-BY.*%2F')
247 ->status_is(200)
248 ->element_exists("input#cq[value='availability = /CC-BY.*/']")
Akron91a76852019-08-28 12:35:37 +0200249 ->content_like(qr!\"availability\"!)
Akroncd42a142019-07-12 18:55:37 +0200250 ->text_is('#error','')
251 ;
Akron8ea84292018-10-24 13:41:52 +0200252
Akron4c7cf952019-08-29 09:31:35 +0200253
Akron909ed082019-12-11 21:38:27 +0100254
255my $match = {
256 matchID => 'match-FOLK/00070-SE-01/T-04-p5441-5442',
257 textSigle => 'FOLK/00070-SE-01/T-04'
258};
259
260$match = Kalamar::Controller::Search::_map_match($match);
261
262is($match->{matchID}, 'p5441-5442');
263
264
Akron0e1ed242018-10-11 13:22:00 +0200265done_testing;
Akron8ea84292018-10-24 13:41:52 +0200266__END__