Koha-clms/t/db_dependent/Koha/Database.t

114 lines
4.5 KiB
Perl
Executable File

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <https://www.gnu.org/licenses>.
use Modern::Perl;
use Test::NoWarnings;
use Test::More tests => 6;
use C4::Context;
use t::lib::Mocks;
my $dbh = C4::Context->dbh;
my $sql_mode = $dbh->selectrow_array(q|SELECT @@SQL_MODE|);
like( $sql_mode, qr{STRICT_TRANS_TABLES}, 'Strict SQL modes must be turned on for tests' );
is( $dbh->{RaiseError}, 1, 'RaiseError must be turned on for tests' );
subtest 'db_scheme2dbi' => sub {
plan tests => 4;
is( Koha::Database::db_scheme2dbi('mysql'), 'mysql', 'ask for mysql, get mysql' );
is( Koha::Database::db_scheme2dbi('Pg'), 'Pg', 'ask for Pg, get Pg' );
is( Koha::Database::db_scheme2dbi('xxx'), 'mysql', 'ask for unsupported DBMS, get mysql' );
is( Koha::Database::db_scheme2dbi(), 'mysql', 'ask for nothing, get mysql' );
};
subtest 'generate_dsn' => sub {
plan tests => 6;
my $config = Koha::Config->get_instance;
t::lib::Mocks::mock_config( 'database', 'koha' );
t::lib::Mocks::mock_config( 'hostname', 'localhost' );
t::lib::Mocks::mock_config( 'port', '3306' );
t::lib::Mocks::mock_config( 'db_scheme', 'mysql' );
t::lib::Mocks::mock_config( 'tls', 'no' );
is(
Koha::Database::generate_dsn($config),
'dbi:mysql:database=koha;host=localhost;port=3306',
'DSN string for MySQL configuration without TLS.'
);
t::lib::Mocks::mock_config( 'tls', 'yes' );
is(
Koha::Database::generate_dsn($config),
'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1',
'DSN string for MySQL configuration with TLS without client key, client cert and ca file.'
);
t::lib::Mocks::mock_config( 'key', '/path/to/client-key.pem' );
is(
Koha::Database::generate_dsn($config),
'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_client_key=/path/to/client-key.pem',
'DSN string for MySQL configuration with TLS and client key.'
);
t::lib::Mocks::mock_config( 'cert', '/path/to/client-cert.pem' );
is(
Koha::Database::generate_dsn($config),
'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_client_key=/path/to/client-key.pem;mysql_ssl_client_cert=/path/to/client-cert.pem',
'DSN string for MySQL configuration with TLS, client key and client cert.'
);
t::lib::Mocks::mock_config( 'ca', '/path/to/ca.pem' );
is(
Koha::Database::generate_dsn($config),
'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_client_key=/path/to/client-key.pem;mysql_ssl_client_cert=/path/to/client-cert.pem;mysql_ssl_ca_file=/path/to/ca.pem',
'DSN string for MySQL configuration with TLS with client key, client cert and ca file.'
);
t::lib::Mocks::mock_config( 'key', '__DB_TLS_CLIENT_KEY__' );
t::lib::Mocks::mock_config( 'cert', '__DB_TLS_CLIENT_CERTIFICATE__' );
is(
Koha::Database::generate_dsn($config),
'dbi:mysql:database=koha;host=localhost;port=3306;mysql_ssl=1;mysql_ssl_ca_file=/path/to/ca.pem',
'DSN string for MySQL configuration with TLS with ca file.'
);
};
subtest 'table_exists' => sub {
plan tests => 5;
# Use the existing dbh established at the top of the test
my $dbh = C4::Context->dbh;
# Test with a table that definitely exists
ok( Koha::Database->table_exists( $dbh, 'borrowers' ), 'borrowers table exists (class method with dbh)' );
# Test with a table that doesn't exist
ok(
!Koha::Database->table_exists( $dbh, 'nonexistent_table_xyz' ),
'nonexistent table returns false (class method with dbh)'
);
# Test with various standard Koha tables
ok( Koha::Database->table_exists( $dbh, 'items' ), 'items table exists' );
ok( Koha::Database->table_exists( $dbh, 'biblioitems' ), 'biblioitems table exists' );
ok( Koha::Database->table_exists( $dbh, 'plugin_data' ), 'plugin_data table exists' );
};