Bug 22709: Unit tests
[koha-equinox.git] / t / lib / Koha / Plugin / Test.pm
1 package Koha::Plugin::Test;
2
3 ## It's good practice to use Modern::Perl
4 use Modern::Perl;
5
6 use Koha::Exceptions::Exception;
7 use Mojo::JSON qw(decode_json);
8
9 ## Required for all plugins
10 use base qw(Koha::Plugins::Base);
11
12 our $VERSION = 1.01;
13 our $metadata = {
14     name            => 'Test Plugin',
15     author          => 'Kyle M Hall',
16     description     => 'Test plugin',
17     date_authored   => '2013-01-14',
18     date_updated    => '2013-01-14',
19     minimum_version => '3.11',
20     maximum_version => undef,
21     version         => $VERSION,
22     my_example_tag  => 'find_me',
23 };
24
25 ## This is the minimum code required for a plugin's 'new' method
26 ## More can be added, but none should be removed
27 sub new {
28     my ( $class, $args ) = @_;
29     $args->{'metadata'} = $metadata;
30     my $self = $class->SUPER::new($args);
31     return $self;
32 }
33
34 sub report {
35     my ( $self, $args ) = @_;
36     return "Koha::Plugin::Test::report";
37 }
38
39 sub tool {
40     my ( $self, $args ) = @_;
41     return "Koha::Plugin::Test::tool";
42 }
43
44 sub to_marc {
45     my ( $self, $args ) = @_;
46     return "Koha::Plugin::Test::to_marc";
47 }
48
49 sub intranet_catalog_biblio_enhancements_toolbar_button {
50     my ( $self, $args ) = @_;
51     return "Koha::Plugin::Test::intranet_catalog_biblio_enhancements_toolbar_button";
52 }
53
54 sub intranet_catalog_biblio_enhancements {
55     my ( $self, $args ) = @_;
56     return "Koha::Plugin::Test::intranet_catalog_biblio_enhancements";
57 }
58
59 sub opac_online_payment {
60     my ( $self, $args ) = @_;
61     return "Koha::Plugin::Test::opac_online_payment";
62 }
63
64 sub opac_online_payment_begin {
65     my ( $self, $args ) = @_;
66     return "Koha::Plugin::Test::opac_online_payment_begin";
67 }
68
69 sub opac_online_payment_end {
70     my ( $self, $args ) = @_;
71     return "Koha::Plugin::Test::opac_online_payment_end";
72 }
73
74 sub opac_head {
75     my ( $self, $args ) = @_;
76     return "Koha::Plugin::Test::opac_head";
77 }
78
79 sub opac_js {
80     my ( $self, $args ) = @_;
81     return "Koha::Plugin::Test::opac_js";
82 }
83
84 sub intranet_head {
85     my ( $self, $args ) = @_;
86     return "Koha::Plugin::Test::intranet_head";
87 }
88
89 sub intranet_js {
90     my ( $self, $args ) = @_;
91     return "Koha::Plugin::Test::intranet_js";
92 }
93
94 sub configure {
95     my ( $self, $args ) = @_;
96     return "Koha::Plugin::Test::configure";;
97 }
98
99 sub install {
100     my ( $self, $args ) = @_;
101     return "Koha::Plugin::Test::install";
102 }
103
104 sub upgrade {
105     my ( $self, $args ) = @_;
106     return "Koha::Plugin::Test::upgrade";
107 }
108
109 sub uninstall {
110     my ( $self, $args ) = @_;
111     return "Koha::Plugin::Test::uninstall";
112 }
113
114 sub test_output {
115     my ( $self ) = @_;
116     $self->output( '¡Hola output!', 'json' );
117 }
118
119 sub test_output_html {
120     my ( $self ) = @_;
121     $self->output_html( '¡Hola output_html!' );
122 }
123
124 sub api_namespace {
125     return "testplugin";
126 }
127
128 sub after_biblio_action {
129     my ( $self, $params ) = @_;
130     my $action    = $params->{action} // '';
131     my $biblio    = $params->{biblio};
132     my $biblio_id = $params->{biblio_id};
133
134     if ( $action ne 'delete' ) {
135         Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action, ref: " . ref($biblio) );
136     }
137     else {
138         Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action") if $biblio_id;
139     }
140 }
141
142
143 sub after_item_action {
144     my ( $self, $params ) = @_;
145     my $action  = $params->{action} // '';
146     my $item    = $params->{item};
147     my $item_id = $params->{item_id};
148
149     if ( $action ne 'delete' ) {
150         Koha::Exceptions::Exception->throw("after_item_action called with action: $action, ref: " . ref($item) );
151     }
152     else {
153         Koha::Exceptions::Exception->throw("after_item_action called with action: $action" ) if $item_id;
154     }
155 }
156
157 sub api_routes {
158     my ( $self, $args ) = @_;
159
160     my $spec = qq{
161 {
162   "/patrons/{patron_id}/bother": {
163     "put": {
164       "x-mojo-to": "Koha::Plugin::Test#bother",
165       "operationId": "BotherPatron",
166       "tags": ["patrons"],
167       "parameters": [{
168         "name": "patron_id",
169         "in": "path",
170         "description": "Internal patron identifier",
171         "required": true,
172         "type": "integer"
173       }],
174       "produces": [
175         "application/json"
176       ],
177       "responses": {
178         "200": {
179           "description": "A bothered patron",
180           "schema": {
181               "type": "object",
182                 "properties": {
183                   "bothered": {
184                     "description": "If the patron has been bothered",
185                     "type": "boolean"
186                   }
187                 }
188           }
189         },
190         "404": {
191           "description": "An error occurred",
192           "schema": {
193               "type": "object",
194                 "properties": {
195                   "error": {
196                     "description": "An explanation for the error",
197                     "type": "string"
198                   }
199                 }
200           }
201         }
202       },
203       "x-koha-authorization": {
204         "permissions": {
205           "borrowers": "1"
206         }
207       }
208     }
209   }
210 }
211     };
212
213     return decode_json($spec);
214 }
215
216 sub _private_sub {
217     return "";
218 }