| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 1 | use Mojo::Base -strict; |
| 2 | use Test::More; |
| 3 | use Test::Mojo; |
| 4 | use Mojo::File qw/tempfile/; |
| 5 | |
| 6 | # Test the documentation |
| 7 | my $t = Test::Mojo->new('Kalamar'); |
| 8 | |
| 9 | my $temp = tempfile(); |
| 10 | |
| Akron | 889bc20 | 2024-03-15 17:16:55 +0100 | [diff] [blame] | 11 | $temp->spew(<<SCRIPT); |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 12 | [{ |
| Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 13 | "name" : "Export", |
| 14 | "desc" : "Exports Kalamar results", |
| 15 | "embed" : [{ |
| 16 | "panel" : "result", |
| 17 | "title" : "exports KWICs and snippets", |
| 18 | "icon" : "\uf019", |
| 19 | "classes" : ["button-icon","plugin"], |
| 20 | "onClick" : { |
| 21 | "action" : "addWidget", |
| 22 | "template" : "http://localhost:7777/res/export.html" |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 23 | } |
| 24 | }] |
| 25 | }] |
| 26 | SCRIPT |
| 27 | |
| 28 | $t->app->plugin('Plugins' => { |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 29 | default_file => $temp->to_string, |
| 30 | default => [ |
| 31 | 'PLUGIN_STUB', |
| 32 | { |
| 33 | "name" => "External Resources", |
| 34 | "desc" => "Get extended access from an external provider", |
| 35 | "embed" => [{ |
| 36 | "panel" => "match", |
| 37 | "title" => "Full Text", |
| 38 | "classes" => ["plugin","cart"], |
| 39 | "icon" => "\f07a", |
| 40 | "onClick" => { |
| 41 | "action" => "addWidget", |
| 42 | "template" => "https://korap.ids-mannheim.de/plugin/external/", |
| 43 | "permissions"=> [ |
| 44 | "scripts", |
| 45 | "popups" |
| 46 | ] |
| 47 | } |
| 48 | }] |
| 49 | } |
| 50 | ] |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | $t->get_ok('/') |
| 54 | ->text_is('h1 span', 'KorAP - Corpus Analysis Platform') |
| Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 55 | ->content_unlike(qr!KorAP\.Plugins\s*=\s*\[!) |
| 56 | ->content_unlike(qr!<script>\/\/<\!\[CDATA\[!) |
| 57 | ->content_like(qr!<span id="kalamar-plugins" data-plugins="/settings/plugin/list\.json"></span>!) |
| 58 | ; |
| 59 | |
| 60 | $t->get_ok('/settings/plugin/list.json') |
| 61 | ->status_is(200) |
| 62 | ->header_is('Content-Type','application/json;charset=UTF-8') |
| 63 | ->content_unlike(qr!KorAP\.Plugins=!) |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 64 | ->content_unlike(qr!STUB!) |
| Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 65 | ->content_like(qr!button-icon!) |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 66 | ->content_like(qr!exports KWICs and snippets!) |
| 67 | ->content_like(qr!Get extended access from an external provider!) |
| Akron | 8dda1c6 | 2021-01-20 10:27:32 +0100 | [diff] [blame] | 68 | ->json_is('/0/embed/0/title','exports KWICs and snippets') |
| Akron | f412d44 | 2025-10-01 10:52:39 +0200 | [diff] [blame] | 69 | ->json_is('/1/embed/0/title','Full Text') |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 70 | ; |
| 71 | |
| Akron | 8322430 | 2026-01-21 12:05:51 +0100 | [diff] [blame] | 72 | # Test non-existing file |
| 73 | my $t2 = Test::Mojo->new('Kalamar'); |
| 74 | my $log_output = ''; |
| 75 | open my $log_fh, '>', \$log_output or die $!; |
| 76 | $t2->app->log->handle($log_fh); |
| 77 | $t2->app->log->level('error'); |
| 78 | $t2->app->plugin('Plugins' => { |
| 79 | default_plugins => '/nonexistent/file.json' |
| 80 | }); |
| 81 | like($log_output, qr/provided default_plugins file does not exist/, 'Non-existing file logged'); |
| 82 | |
| 83 | # Test malformed JSON |
| 84 | $log_output = ''; |
| 85 | my $t3 = Test::Mojo->new('Kalamar'); |
| 86 | my $bad_json = tempfile(); |
| 87 | $bad_json->spew('{invalid json}'); |
| 88 | #$t3->app->log->level('error'); |
| 89 | $t3->app->log->handle($log_fh); |
| 90 | $t3->app->log->level('error'); |
| 91 | $t3->app->plugin('Plugins' => { |
| 92 | default_plugins => $bad_json->to_string |
| 93 | }); |
| 94 | like($log_output, qr/provided plugin file syntax invalid/, 'Malformed JSON logged'); |
| 95 | |
| Akron | ccd9d7d | 2020-09-22 09:51:19 +0200 | [diff] [blame] | 96 | done_testing; |