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

(-)a/t/db_dependent/Koha/Plugins/Valuebuilder_hooks.t (+203 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( $javascript, qr/test_focus_test_field_001/, 'Generated javascript contains expected field-specific function' );
114
115
    # Test 4: FrameworkPlugin can launch plugin-based valuebuilder (would need CGI mock for full test)
116
    ok( $framework_plugin->{launcher}, 'FrameworkPlugin has launcher method loaded from plugin' );
117
118
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
119
    $schema->storage->txn_rollback;
120
};
121
122
subtest 'Plugin Base valuebuilder methods tests' => sub {
123
124
    plan tests => 6;
125
126
    $schema->storage->txn_begin;
127
128
    # Temporarily remove any installed plugins data
129
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
130
131
    my $plugins = Koha::Plugins->new;
132
    $plugins->InstallPlugins;
133
134
    # Test 1: get_valuebuilders() with plugin that doesn't have get_valuebuilder method
135
    my $regular_plugin = Koha::Plugin::Test->new->enable;
136
    my $valuebuilders = $regular_plugin->get_valuebuilders();
137
    is( ref($valuebuilders), 'ARRAY', 'get_valuebuilders returns array reference' );
138
    is( scalar @$valuebuilders, 0, 'get_valuebuilders returns empty array when plugin lacks get_valuebuilder method' );
139
140
    # Test 2: get_valuebuilders() with plugin that has get_valuebuilder method
141
    my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable;
142
    $valuebuilders = $valuebuilder_plugin->get_valuebuilders();
143
    is( scalar @$valuebuilders, 1, 'get_valuebuilders returns one item when plugin has get_valuebuilder method' );
144
    is( $valuebuilders->[0], 'test_plugin_valuebuilder.pl', 'get_valuebuilders returns correct valuebuilder name' );
145
146
    # Test 3: get_valuebuilder_url() method
147
    my $url = $valuebuilder_plugin->get_valuebuilder_url();
148
    my $expected_class = ref($valuebuilder_plugin);
149
    like( $url, qr|/cgi-bin/koha/plugins/run\.pl\?class=$expected_class&method=launcher|, 
150
          'get_valuebuilder_url returns correct URL format' );
151
152
    # Test 4: get_valuebuilder() method directly
153
    my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder();
154
    is( $valuebuilder_name, 'test_plugin_valuebuilder.pl', 'get_valuebuilder returns expected name' );
155
156
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
157
    $schema->storage->txn_rollback;
158
};
159
160
subtest 'Real FrameworkPlugin integration with valuebuilder plugins' => sub {
161
162
    plan tests => 12;
163
164
    $schema->storage->txn_begin;
165
166
    # Temporarily remove any installed plugins data
167
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
168
169
    my $plugins = Koha::Plugins->new;
170
    $plugins->InstallPlugins;
171
172
    # Enable our test valuebuilder plugin
173
    my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable;
174
    ok( $valuebuilder_plugin->is_enabled, 'TestValuebuilder plugin is enabled' );
175
176
    # Test 1: Verify the plugin provides a valuebuilder
177
    my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder();
178
    is( $valuebuilder_name, 'test_plugin_valuebuilder.pl', 'TestValuebuilder plugin returns correct valuebuilder name' );
179
180
    # Test 2: Verify get_valuebuilders_installed finds our plugin
181
    my @valuebuilders = $plugins->get_valuebuilders_installed();
182
    is( scalar @valuebuilders, 1, 'get_valuebuilders_installed finds one valuebuilder' );
183
    is( $valuebuilders[0]->{name}, $valuebuilder_name, 'Found valuebuilder has correct name' );
184
    isa_ok( $valuebuilders[0]->{plugin}, 'Koha::Plugin::TestValuebuilder', 'Found valuebuilder has correct plugin type' );
185
186
    # Test 3: FrameworkPlugin can load our plugin-based valuebuilder
187
    my $framework_plugin = Koha::FrameworkPlugin->new({ name => $valuebuilder_name });
188
    ok( $framework_plugin, 'FrameworkPlugin created successfully with plugin valuebuilder name' );
189
    is( $framework_plugin->name, $valuebuilder_name, 'FrameworkPlugin has correct name' );
190
    ok( !$framework_plugin->errstr, 'FrameworkPlugin has no errors' );
191
192
    # Test 4: FrameworkPlugin can build JavaScript from our plugin
193
    my $build_result = $framework_plugin->build({ id => 'test_field_123' });
194
    ok( $build_result, 'FrameworkPlugin build succeeds with real plugin valuebuilder' );
195
196
    my $javascript = $framework_plugin->javascript;
197
    ok( $javascript, 'FrameworkPlugin generates JavaScript from plugin' );
198
    like( $javascript, qr/test_focus_test_field_123/, 'Generated JavaScript contains expected function for field ID' );
199
    like( $javascript, qr/test_click_test_field_123/, 'Generated JavaScript contains expected click function for field ID' );
200
201
    Koha::Plugins->RemovePlugins( { destructive => 1 } );
202
    $schema->storage->txn_rollback;
203
};
(-)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 %] - Valuebuilder</title>
5
    <meta charset="UTF-8">
6
</head>
7
<body>
8
    <h1>[% plugin_name %]</h1>
9
    <p>This is a test valuebuilder popup for field: [% field_id %]</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 %]')) {
23
            window.opener.document.getElementById('[% field_id %]').value = value;
24
        }
25
        window.close();
26
    }
27
    </script>
28
</body>
29
</html>

Return to bug 39522