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

(-)a/t/db_dependent/Koha/Installer.t (-1 / +160 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright 2024 Koha Development Team
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 4;
22
23
use Test::MockModule;
24
use File::Temp qw/tempdir/;
25
use File::Path qw/make_path/;
26
use File::Spec;
27
28
use t::lib::Mocks;
29
use t::lib::TestBuilder;
30
31
BEGIN { use_ok('Koha::Installer') }
32
33
my $schema = Koha::Database->new->schema;
34
35
subtest 'needs_update' => sub {
36
    plan tests => 5;
37
38
    my $dbh = $schema->storage->dbh;
39
40
    # Back up the original row where variable = 'Version'
41
    # Note: We can't do the usual transaction wrapping here for rollback due to not sharing a db handle
42
    my $sth = $dbh->prepare("SELECT * FROM systempreferences WHERE variable = 'Version'");
43
    $sth->execute();
44
    my $original_row = $sth->fetchrow_hashref();
45
46
    # Tests
47
    my $mock_koha      = Test::MockModule->new('Koha');
48
    my $mock_installer = Test::MockModule->new('Koha::Installer');
49
50
    $dbh->do("UPDATE systempreferences SET value = '20.0501000' WHERE variable = 'Version'");
51
    $mock_koha->mock( 'version', sub { '20.05.01.000' } );
52
    $mock_installer->mock( 'get_atomic_updates', sub { return [] } );
53
54
    my $result = Koha::Installer::needs_update();
55
    is( $result, 0, 'No update needed when DB version matches code version and no atomic updates' );
56
57
    # Test 2: DB version does not match code version, no atomic updates
58
    $dbh->do("UPDATE systempreferences SET value = '19.1102999' WHERE variable = 'Version'");
59
    $mock_koha->mock( 'version', sub { '20.05.01.000' } );
60
61
    $result = Koha::Installer::needs_update();
62
    is( $result, 1, 'Update needed when DB version does not match code version' );
63
64
    # Test 3: DB version matches code version, atomic updates present
65
    $dbh->do("UPDATE systempreferences SET value = '20.0501000' WHERE variable = 'Version'");
66
    $mock_installer->mock( 'get_atomic_updates', sub { return ['update_001.perl'] } );
67
68
    $result = Koha::Installer::needs_update();
69
    is( $result, 1, 'Update needed when atomic updates are present, even if versions match' );
70
71
    # Test 4: DB version does not match code version, atomic updates present
72
    $dbh->do("UPDATE systempreferences SET value = '18.0001000' WHERE variable = 'Version'");
73
    $mock_installer->mock( 'get_atomic_updates', sub { return ['update_001.perl'] } );
74
75
    $result = Koha::Installer::needs_update();
76
    is( $result, 1, 'Update needed when DB version does not match code version and atomic updates are present' );
77
78
    # Test 5: Edge case where DB version is undefined (should default to update needed)
79
    $dbh->do("DELETE FROM systempreferences WHERE variable = 'Version'");
80
    $mock_installer->mock( 'get_atomic_updates', sub { return [] } );
81
82
    $result = Koha::Installer::needs_update();
83
    is( $result, 1, 'Update needed when DB version is undefined' );
84
85
    # Restore the original row
86
    if ($original_row) {
87
        $dbh->do(
88
            "INSERT INTO systempreferences (variable, value, options, explanation, type) VALUES (?, ?, ?, ?, ?)",
89
            undef,
90
            $original_row->{variable},
91
            $original_row->{value},
92
            $original_row->{options},
93
            $original_row->{explanation},
94
            $original_row->{type}
95
        );
96
    } else {
97
        $dbh->do("DELETE FROM systempreferences WHERE variable = 'Version'");
98
    }
99
};
100
101
subtest 'TransformToNum' => sub {
102
    plan tests => 5;
103
104
    my %test_cases = (
105
        '20.05.01.000' => '20.0501000',
106
        '19.11.02.XXX' => '19.1102999',
107
        '21.06.00.003' => '21.0600003',
108
        '18.05.02.XXX' => '18.0502999',
109
        '22.11.01.123' => '22.1101123',
110
    );
111
112
    foreach my $input_version ( keys %test_cases ) {
113
        my $expected_output = $test_cases{$input_version};
114
        my $actual_output   = Koha::Installer::TransformToNum($input_version);
115
116
        is( $actual_output, $expected_output, "TransformToNum('$input_version') = '$expected_output'" );
117
    }
118
119
};
120
121
subtest 'get_atomic_updates method' => sub {
122
    plan tests => 1;
123
124
    # Create a temporary directory with some files
125
    my $temp_dir   = tempdir( CLEANUP => 1 );
126
    my $update_dir = File::Spec->catdir( $temp_dir, 'installer', 'data', 'mysql', 'atomicupdate' );
127
    make_path($update_dir) or die "Failed to create directory $update_dir: $!";
128
129
    my @test_files = (
130
        'update_001.perl',
131
        'update_002.pl',
132
        'update_003.sql',    # Should be ignored
133
        'skeleton.perl',     # Should be ignored
134
        'update_004.perl',
135
        'skeleton.pl',       # Should be ignored
136
        'README.txt',        # Should be ignored
137
    );
138
139
    foreach my $file (@test_files) {
140
        open my $fh, '>', File::Spec->catfile( $update_dir, $file ) or die $!;
141
        close $fh;
142
    }
143
144
    my $mock_koha_config = Test::MockModule->new('Koha::Config');
145
    $mock_koha_config->mock( 'guess_koha_conf', sub { '/fake/koha.conf' } );
146
    $mock_koha_config->mock(
147
        'get_instance',
148
        sub {
149
            return { config => { intranetdir => $temp_dir } };
150
        }
151
    );
152
153
    # Run the method under test
154
    my $result = Koha::Installer::get_atomic_updates();
155
156
    # Expected output (sorted)
157
    my @expected_files = sort ( 'update_001.perl', 'update_002.pl', 'update_004.perl' );
158
159
    is_deeply( $result, \@expected_files, 'get_atomic_updates returns correct files' );
160
};

Return to bug 34088