blob: d68730ee3e3d6b359d513f88a351f7c1a287eef6 [file] [log] [blame]
Akronccd9d7d2020-09-22 09:51:19 +02001use Mojo::Base -strict;
2use Test::More;
3use Test::Mojo;
4use Mojo::File qw/tempfile/;
5
6# Test the documentation
7my $t = Test::Mojo->new('Kalamar');
8
9my $temp = tempfile();
10
Akron889bc202024-03-15 17:16:55 +010011$temp->spew(<<SCRIPT);
Akronccd9d7d2020-09-22 09:51:19 +020012[{
Akron8dda1c62021-01-20 10:27:32 +010013 "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"
Akronccd9d7d2020-09-22 09:51:19 +020023 }
24 }]
25}]
26SCRIPT
27
28$t->app->plugin('Plugins' => {
Akronf412d442025-10-01 10:52:39 +020029 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 ]
Akronccd9d7d2020-09-22 09:51:19 +020051});
52
53$t->get_ok('/')
54 ->text_is('h1 span', 'KorAP - Corpus Analysis Platform')
Akron8dda1c62021-01-20 10:27:32 +010055 ->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=!)
Akronf412d442025-10-01 10:52:39 +020064 ->content_unlike(qr!STUB!)
Akron8dda1c62021-01-20 10:27:32 +010065 ->content_like(qr!button-icon!)
Akronf412d442025-10-01 10:52:39 +020066 ->content_like(qr!exports KWICs and snippets!)
67 ->content_like(qr!Get extended access from an external provider!)
Akron8dda1c62021-01-20 10:27:32 +010068 ->json_is('/0/embed/0/title','exports KWICs and snippets')
Akronf412d442025-10-01 10:52:39 +020069 ->json_is('/1/embed/0/title','Full Text')
Akronccd9d7d2020-09-22 09:51:19 +020070 ;
71
Akron83224302026-01-21 12:05:51 +010072# Test non-existing file
73my $t2 = Test::Mojo->new('Kalamar');
74my $log_output = '';
75open 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});
81like($log_output, qr/provided default_plugins file does not exist/, 'Non-existing file logged');
82
83# Test malformed JSON
84$log_output = '';
85my $t3 = Test::Mojo->new('Kalamar');
86my $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});
94like($log_output, qr/provided plugin file syntax invalid/, 'Malformed JSON logged');
95
Akronccd9d7d2020-09-22 09:51:19 +020096done_testing;