View | Details | Raw Unified | Return to bug 39522
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Plugins/Valuebuilder_hooks.t (+217 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
16
17
use Modern::Perl;
18
19
use Test::NoWarnings;
20
use Test::More tests => 9;
21
22
use File::Basename;
23
24
use C4::Context;
25
26
use Koha::FrameworkPlugin;
27
28
use t::lib::Mocks;
29
use t::lib::TestBuilder;
30
31
BEGIN {
32
    # Mock pluginsdir before loading Plugins module
33
    my $path = dirname(__FILE__) . '/../../../lib/plugins';
34
    t::lib::Mocks::mock_config( 'pluginsdir', $path );
35
36
    use_ok('Koha::Plugins');
37
    use_ok('Koha::Plugins::Handler');
38
    use_ok('Koha::Plugin::Test');
39
    use_ok('Koha::Plugin::TestValuebuilder');
40
}
41
42
my $schema  = Koha::Database->new->schema;
43
my $builder = t::lib::TestBuilder->new;
44
45
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
46
47
subtest 'get_valuebuilders_installed() tests' => sub {
48
49
    plan tests => 6;
50
51
    $schema->storage->txn_begin;
52
53
    # Temporarily remove any installed plugins data
54
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
55
56
    my $plugins = Koha::Plugins->new;
57
    $plugins->InstallPlugins;
58
59
    # Test 1: No valuebuilders when no plugins have get_valuebuilder method
60
    # Only enable the regular Test plugin (which doesn't have get_valuebuilder)
61
    my $regular_plugin = Koha::Plugin::Test->new->enable;
62
    my @valuebuilders  = $plugins->get_valuebuilders_installed();
63
    is( scalar @valuebuilders, 0, 'No valuebuilders when enabled plugin lacks get_valuebuilder method' );
64
65
    # Test 2: Enable the TestValuebuilder plugin which provides a valuebuilder
66
    my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable;
67
    @valuebuilders = $plugins->get_valuebuilders_installed();
68
    is( scalar @valuebuilders, 1, 'One valuebuilder found when TestValuebuilder plugin is enabled' );
69
70
    # Test 3: Check the structure of returned valuebuilder
71
    my $valuebuilder = $valuebuilders[0];
72
    is( ref($valuebuilder),    'HASH',                        'Valuebuilder is returned as hashref' );
73
    is( $valuebuilder->{name}, 'test_plugin_valuebuilder.pl', 'Valuebuilder name matches TestValuebuilder plugin' );
74
    isa_ok( $valuebuilder->{plugin}, 'Koha::Plugin::TestValuebuilder', 'Valuebuilder plugin is correct object type' );
75
76
    # Test 4: Disable the valuebuilder plugin and verify it's not found
77
    $valuebuilder_plugin->disable;
78
    @valuebuilders = $plugins->get_valuebuilders_installed();
79
    is( scalar @valuebuilders, 0, 'No valuebuilders when TestValuebuilder plugin is disabled' );
80
81
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
82
    $schema->storage->txn_rollback;
83
};
84
85
subtest 'FrameworkPlugin valuebuilder integration tests' => sub {
86
87
    plan tests => 6;
88
89
    $schema->storage->txn_begin;
90
91
    # Temporarily remove any installed plugins data
92
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
93
94
    my $plugins = Koha::Plugins->new;
95
    $plugins->InstallPlugins;
96
97
    # Enable the TestValuebuilder plugin
98
    my $valuebuilder_plugin    = Koha::Plugin::TestValuebuilder->new->enable;
99
    my $test_valuebuilder_name = $valuebuilder_plugin->get_valuebuilder();
100
101
    # Test 1: FrameworkPlugin can find plugin-based valuebuilder
102
    my $framework_plugin = Koha::FrameworkPlugin->new( { name => $test_valuebuilder_name } );
103
    ok( $framework_plugin, 'FrameworkPlugin object created successfully' );
104
    is( $framework_plugin->name, $test_valuebuilder_name, 'FrameworkPlugin has correct name' );
105
106
    # Test 2: FrameworkPlugin can load plugin-based valuebuilder
107
    my $build_result = $framework_plugin->build( { id => 'test_field_001' } );
108
    ok( $build_result, 'FrameworkPlugin build method succeeds with plugin valuebuilder' );
109
110
    # Test 3: FrameworkPlugin generates javascript from plugin
111
    my $javascript = $framework_plugin->javascript;
112
    ok( $javascript, 'FrameworkPlugin generates javascript' );
113
    like(
114
        $javascript, qr/test_focus_test_field_001/,
115
        'Generated javascript contains expected field-specific function'
116
    );
117
118
    # Test 4: FrameworkPlugin can launch plugin-based valuebuilder (would need CGI mock for full test)
119
    ok( $framework_plugin->{launcher}, 'FrameworkPlugin has launcher method loaded from plugin' );
120
121
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
122
    $schema->storage->txn_rollback;
123
};
124
125
subtest 'Plugin Base valuebuilder methods tests' => sub {
126
127
    plan tests => 6;
128
129
    $schema->storage->txn_begin;
130
131
    # Temporarily remove any installed plugins data
132
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
133
134
    my $plugins = Koha::Plugins->new;
135
    $plugins->InstallPlugins;
136
137
    # Test 1: get_valuebuilders() with plugin that doesn't have get_valuebuilder method
138
    my $regular_plugin = Koha::Plugin::Test->new->enable;
139
    my $valuebuilders  = $regular_plugin->get_valuebuilders();
140
    is( ref($valuebuilders),    'ARRAY', 'get_valuebuilders returns array reference' );
141
    is( scalar @$valuebuilders, 0, 'get_valuebuilders returns empty array when plugin lacks get_valuebuilder method' );
142
143
    # Test 2: get_valuebuilders() with plugin that has get_valuebuilder method
144
    my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable;
145
    $valuebuilders = $valuebuilder_plugin->get_valuebuilders();
146
    is( scalar @$valuebuilders, 1, 'get_valuebuilders returns one item when plugin has get_valuebuilder method' );
147
    is( $valuebuilders->[0],    'test_plugin_valuebuilder.pl', 'get_valuebuilders returns correct valuebuilder name' );
148
149
    # Test 3: get_valuebuilder_url() method
150
    my $url            = $valuebuilder_plugin->get_valuebuilder_url();
151
    my $expected_class = ref($valuebuilder_plugin);
152
    like(
153
        $url, qr|/cgi-bin/koha/plugins/run\.pl\?class=$expected_class&method=launcher|,
154
        'get_valuebuilder_url returns correct URL format'
155
    );
156
157
    # Test 4: get_valuebuilder() method directly
158
    my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder();
159
    is( $valuebuilder_name, 'test_plugin_valuebuilder.pl', 'get_valuebuilder returns expected name' );
160
161
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
162
    $schema->storage->txn_rollback;
163
};
164
165
subtest 'Real FrameworkPlugin integration with valuebuilder plugins' => sub {
166
167
    plan tests => 12;
168
169
    $schema->storage->txn_begin;
170
171
    # Temporarily remove any installed plugins data
172
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
173
174
    my $plugins = Koha::Plugins->new;
175
    $plugins->InstallPlugins;
176
177
    # Enable our test valuebuilder plugin
178
    my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable;
179
    ok( $valuebuilder_plugin->is_enabled, 'TestValuebuilder plugin is enabled' );
180
181
    # Test 1: Verify the plugin provides a valuebuilder
182
    my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder();
183
    is(
184
        $valuebuilder_name, 'test_plugin_valuebuilder.pl',
185
        'TestValuebuilder plugin returns correct valuebuilder name'
186
    );
187
188
    # Test 2: Verify get_valuebuilders_installed finds our plugin
189
    my @valuebuilders = $plugins->get_valuebuilders_installed();
190
    is( scalar @valuebuilders,     1,                  'get_valuebuilders_installed finds one valuebuilder' );
191
    is( $valuebuilders[0]->{name}, $valuebuilder_name, 'Found valuebuilder has correct name' );
192
    isa_ok(
193
        $valuebuilders[0]->{plugin}, 'Koha::Plugin::TestValuebuilder',
194
        'Found valuebuilder has correct plugin type'
195
    );
196
197
    # Test 3: FrameworkPlugin can load our plugin-based valuebuilder
198
    my $framework_plugin = Koha::FrameworkPlugin->new( { name => $valuebuilder_name } );
199
    ok( $framework_plugin, 'FrameworkPlugin created successfully with plugin valuebuilder name' );
200
    is( $framework_plugin->name, $valuebuilder_name, 'FrameworkPlugin has correct name' );
201
    ok( !$framework_plugin->errstr, 'FrameworkPlugin has no errors' );
202
203
    # Test 4: FrameworkPlugin can build JavaScript from our plugin
204
    my $build_result = $framework_plugin->build( { id => 'test_field_123' } );
205
    ok( $build_result, 'FrameworkPlugin build succeeds with real plugin valuebuilder' );
206
207
    my $javascript = $framework_plugin->javascript;
208
    ok( $javascript, 'FrameworkPlugin generates JavaScript from plugin' );
209
    like( $javascript, qr/test_focus_test_field_123/, 'Generated JavaScript contains expected function for field ID' );
210
    like(
211
        $javascript, qr/test_click_test_field_123/,
212
        'Generated JavaScript contains expected click function for field ID'
213
    );
214
215
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
216
    $schema->storage->txn_rollback;
217
};
(-)a/t/lib/plugins/Koha/Plugin/TestValuebuilder.pm (+80 lines)
Line 0 Link Here
1
package Koha::Plugin::TestValuebuilder;
2
3
## It's good practice to use Modern::Perl
4
use Modern::Perl;
5
6
## Required for all plugins
7
use base qw(Koha::Plugins::Base);
8
9
our $VERSION  = "v1.01";
10
our $metadata = {
11
    name             => 'Test Valuebuilder Plugin',
12
    author           => 'Koha Development Team',
13
    description      => 'Test plugin for valuebuilder functionality',
14
    date_authored    => '2025-01-01',
15
    date_updated     => '2025-01-01',
16
    minimum_version  => '3.11',
17
    maximum_version  => undef,
18
    version          => $VERSION,
19
    namespace        => 'test_valuebuilder',
20
    valuebuilder_tag => 'valuebuilder_test',
21
};
22
23
## This is the minimum code required for a plugin's 'new' method
24
## More can be added, but none should be removed
25
sub new {
26
    my ( $class, $args ) = @_;
27
    $args->{'metadata'} = $metadata;
28
    my $self = $class->SUPER::new($args);
29
    return $self;
30
}
31
32
## This method returns the name of the valuebuilder this plugin provides
33
sub get_valuebuilder {
34
    my $self = shift;
35
    return 'test_plugin_valuebuilder.pl';
36
}
37
38
## This method provides the builder code (JavaScript) for the valuebuilder
39
sub builder_code {
40
    my ( $self, $params ) = @_;
41
42
    my $id = $params->{id} || 'default_id';
43
44
    return qq{
45
        <script>
46
        function test_focus_$id() {
47
            var field = document.getElementById('$id');
48
            if (field && field.value === '') {
49
                field.value = 'Plugin Generated Value';
50
            }
51
        }
52
53
        function test_click_$id() {
54
            window.open('/cgi-bin/koha/plugins/run.pl?class=Koha::Plugin::TestValuebuilder&method=launcher&id=$id',
55
                       'valuebuilder',
56
                       'width=500,height=400,toolbar=false,scrollbars=yes');
57
        }
58
        </script>
59
    };
60
}
61
62
## This method provides the launcher (popup) functionality for the valuebuilder
63
sub launcher {
64
    my ( $self, $params ) = @_;
65
66
    my $input = $self->{cgi};
67
    my $id    = $input->param('id') || 'default_id';
68
69
    my $template = $self->get_template( { file => 'test_valuebuilder_popup.tt' } );
70
71
    $template->param(
72
        field_id    => $id,
73
        plugin_name => 'Test Valuebuilder Plugin',
74
    );
75
76
    print $input->header();
77
    print $template->output();
78
}
79
80
1;
(-)a/t/lib/plugins/Koha/Plugin/TestValuebuilder/test_valuebuilder_popup.tt (-1 / +29 lines)
Line 0 Link Here
0
- 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>[% plugin_name | html %] - Valuebuilder</title>
5
    <meta charset="UTF-8">
6
</head>
7
<body>
8
    <h1>[% plugin_name | html %]</h1>
9
    <p>This is a test valuebuilder popup for field: [% field_id | html %]</p>
10
11
    <form>
12
        <label for="test_value">Enter a value:</label>
13
        <input type="text" id="test_value" value="Test Value from Plugin">
14
        <br><br>
15
        <button type="button" onclick="setValueAndClose()">Set Value</button>
16
        <button type="button" onclick="window.close()">Cancel</button>
17
    </form>
18
19
    <script>
20
    function setValueAndClose() {
21
        var value = document.getElementById('test_value').value;
22
        if (window.opener && window.opener.document.getElementById('[% field_id | html %]')) {
23
            window.opener.document.getElementById('[% field_id | html %]').value = value;
24
        }
25
        window.close();
26
    }
27
    </script>
28
</body>
29
</html>

Return to bug 39522