Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::Mojo; |
| 3 | use Test::More; |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 4 | use Mojo::File qw/path/; |
Akron | 909ed08 | 2019-12-11 21:38:27 +0100 | [diff] [blame] | 5 | use Kalamar::Controller::Search; |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 6 | |
| 7 | |
| 8 | ##################### |
| 9 | # Start Fake server # |
| 10 | ##################### |
Akron | 63d963b | 2019-07-05 15:35:51 +0200 | [diff] [blame] | 11 | my $mount_point = '/realapi/'; |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 12 | $ENV{KALAMAR_API} = $mount_point; |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 13 | |
| 14 | my $t = Test::Mojo->new('Kalamar'); |
| 15 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 16 | # Mount fake backend |
| 17 | # Get the fixture path |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 18 | my $fixtures_path = path(Mojo::File->new(__FILE__)->dirname, 'server'); |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 19 | my $fake_backend = $t->app->plugin( |
| 20 | Mount => { |
| 21 | $mount_point => |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 22 | $fixtures_path->child('mock.pl') |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 23 | } |
| 24 | ); |
| 25 | # Configure fake backend |
| 26 | $fake_backend->pattern->defaults->{app}->log($t->app->log); |
| 27 | |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 28 | my $q = qr!(?:\"|")!; |
| 29 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 30 | # Query passed |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 31 | $t->get_ok('/?q=baum') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 32 | ->status_is(200) |
| 33 | ->text_is('#error','') |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 34 | |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 35 | ->text_is('title', 'KorAP: Find »baum« with Poliqarp') |
| 36 | ->element_exists('meta[name="DC.title"][content="KorAP: Find »baum« with Poliqarp"]') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 37 | ->element_exists('body[itemscope][itemtype="http://schema.org/SearchResultsPage"]') |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 38 | |
| 39 | # Total results |
Akron | 3239663 | 2018-10-11 17:08:37 +0200 | [diff] [blame] | 40 | ->text_is('#total-results', 51) |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 41 | |
| 42 | # Total pages |
Akron | 18a2a27 | 2020-06-16 11:47:38 +0200 | [diff] [blame] | 43 | ->element_count_is('#pagination > a', 5) |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 44 | |
| 45 | # api_response |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 46 | ->content_like(qr/${q}authorized${q}:null/) |
| 47 | ->content_like(qr/${q}pubDate${q},${q}subTitle${q},${q}author${q}/) |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 48 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 49 | # No cutOff |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 50 | ->content_unlike(qr!${q}cutOff${q}:true!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 51 | |
Akron | dffa939 | 2018-10-12 16:26:09 +0200 | [diff] [blame] | 52 | ->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]') |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 75 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 76 | ; |
| 77 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 78 | $t->get_ok('/?q=[orth=das') |
Akron | 7093b81 | 2018-10-19 17:28:21 +0200 | [diff] [blame] | 79 | ->status_is(400) |
| 80 | ->text_is('div.notify-error:nth-of-type(1)', '302: Parantheses/brackets unbalanced.') |
Akron | bc33beb | 2018-11-30 13:46:08 +0100 | [diff] [blame] | 81 | ->element_exists('#search') |
Akron | 7093b81 | 2018-10-19 17:28:21 +0200 | [diff] [blame] | 82 | ->text_like('div.notify-error:nth-of-type(2)', qr!302: Could not parse query .+? \[orth=das.+?!) |
| 83 | ; |
| 84 | |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 85 | # 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.') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 89 | ->content_like(qr!data-koralquery=!) |
Akron | 3c390c4 | 2020-03-30 09:06:21 +0200 | [diff] [blame] | 90 | ->text_is('.no-results:nth-of-type(1)', 'Unable to perform the action.') |
Akron | 73f3608 | 2018-10-25 15:34:59 +0200 | [diff] [blame] | 91 | ; |
| 92 | |
| 93 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 94 | # 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') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 102 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 103 | ->text_is('#total-results', 51) |
| 104 | ; |
| 105 | |
Akron | 385249d | 2018-10-29 12:26:29 +0100 | [diff] [blame] | 106 | # 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') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 114 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | 385249d | 2018-10-29 12:26:29 +0100 | [diff] [blame] | 115 | ->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') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 126 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | 385249d | 2018-10-29 12:26:29 +0100 | [diff] [blame] | 127 | ->element_exists_not('#total-results') |
| 128 | ; |
| 129 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 130 | # 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') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 138 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 139 | ->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 |
Akron | 18a2a27 | 2020-06-16 11:47:38 +0200 | [diff] [blame] | 153 | ->element_count_is('#pagination > a', 7) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 154 | ->text_is('#pagination a:nth-of-type(6) span', 7291) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 155 | ->content_like(qr!${q}count${q}:2!) |
| 156 | ->content_like(qr!${q}startIndex${q}:0!) |
| 157 | ->content_like(qr!${q}itemsPerPage${q}:2!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 158 | |
| 159 | # No caching |
| 160 | ->header_isnt('X-Kalamar-Cache', 'true') |
| 161 | |
| 162 | # Not searched for "der" before |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 163 | ->content_unlike(qr!${q}cutOff${q}:true!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 164 | ; |
| 165 | |
Akron | 4c7cf95 | 2019-08-29 09:31:35 +0200 | [diff] [blame] | 166 | # Check pagination repetion of page |
| 167 | my $next_href = $t->get_ok('/?q=der&p=1&count=2') |
| 168 | ->tx->res->dom->at('#pagination a[rel=next]')->attr('href'); |
| 169 | like($next_href, qr/p=2/); |
| 170 | unlike($next_href, qr/p=1/); |
| 171 | |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 172 | # 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') |
Akron | bc33beb | 2018-11-30 13:46:08 +0100 | [diff] [blame] | 177 | ->element_exists('#search') |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 178 | |
| 179 | # Total results |
| 180 | ->text_is('#total-results', '14,581') |
| 181 | |
| 182 | # Total pages |
Akron | 18a2a27 | 2020-06-16 11:47:38 +0200 | [diff] [blame] | 183 | ->element_count_is('#pagination > a', 7) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 184 | ->text_is('#pagination a:nth-of-type(6) span', 7291) |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 185 | ->content_like(qr!${q}count${q}:2!) |
| 186 | ->content_like(qr!${q}itemsPerPage${q}:2!) |
| 187 | ->content_like(qr!${q}startIndex${q}:2!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 188 | |
| 189 | # No caching |
| 190 | ->header_isnt('X-Kalamar-Cache', 'true') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 191 | ->content_like(qr!${q}cutOff${q}:true!) |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 192 | ; |
| 193 | |
Akron | c4be819 | 2018-10-25 16:07:53 +0200 | [diff] [blame] | 194 | # 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') |
Akron | bc33beb | 2018-11-30 13:46:08 +0100 | [diff] [blame] | 198 | ->element_exists('#search') |
Akron | c4be819 | 2018-10-25 16:07:53 +0200 | [diff] [blame] | 199 | ->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 | ; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 211 | |
Akron | d1ff8d8 | 2018-11-08 13:16:55 +0100 | [diff] [blame] | 212 | # Query too long |
| 213 | my $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 | |
Akron | a3c353c | 2019-02-14 23:50:00 +0100 | [diff] [blame] | 220 | # 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!) |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 231 | ->element_exists("input#cq") |
| 232 | ->element_exists_not("input#cq[value]") |
Akron | a3c353c | 2019-02-14 23:50:00 +0100 | [diff] [blame] | 233 | ->text_is('#total-results', '> 4,274,841'); |
| 234 | ; |
| 235 | |
Akron | 91a7685 | 2019-08-28 12:35:37 +0200 | [diff] [blame] | 236 | $t->app->defaults(no_cache => 1); |
| 237 | |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 238 | # 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.*/']") |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 242 | ->content_like(qr!${q}availability${q}!) |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 243 | ->text_is('#error','') |
| 244 | ; |
| 245 | |
Akron | 4cdc4fc | 2020-04-28 12:19:11 +0200 | [diff] [blame] | 246 | $t->app->hook( |
Akron | 7c87c1a | 2020-04-28 12:35:55 +0200 | [diff] [blame] | 247 | after_search => sub { |
Akron | 4cdc4fc | 2020-04-28 12:19:11 +0200 | [diff] [blame] | 248 | my $c = shift; |
| 249 | $c->content_for('after_search_results' => '<p id="special">Funny</p>'); |
| 250 | } |
| 251 | ); |
| 252 | |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 253 | # 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.*/']") |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 257 | ->content_like(qr!${q}availability${q}!) |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 258 | ->text_is('#error','') |
Akron | 4cdc4fc | 2020-04-28 12:19:11 +0200 | [diff] [blame] | 259 | ->text_is('#special', 'Funny') |
Akron | cd42a14 | 2019-07-12 18:55:37 +0200 | [diff] [blame] | 260 | ; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 261 | |
Akron | 909ed08 | 2019-12-11 21:38:27 +0100 | [diff] [blame] | 262 | my $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 | |
| 269 | is($match->{matchID}, 'p5441-5442'); |
| 270 | |
Akron | 7b9a196 | 2020-07-02 09:52:53 +0200 | [diff] [blame] | 271 | # Query with pipe |
| 272 | $t->get_ok('/?q=baum&pipe=glemm') |
| 273 | ->status_is(200) |
| 274 | ->text_is('#error','') |
Akron | bc6b3f2 | 2021-01-13 14:53:12 +0100 | [diff] [blame] | 275 | ->content_like(qr/${q}pipes${q}:${q}glemm${q}/) |
Akron | 7b9a196 | 2020-07-02 09:52:53 +0200 | [diff] [blame] | 276 | ; |
| 277 | |
Akron | 909ed08 | 2019-12-11 21:38:27 +0100 | [diff] [blame] | 278 | |
Akron | 0e1ed24 | 2018-10-11 13:22:00 +0200 | [diff] [blame] | 279 | done_testing; |
Akron | 8ea8429 | 2018-10-24 13:41:52 +0200 | [diff] [blame] | 280 | __END__ |