File: DBPgsql.php

package info (click to toggle)
roundcube 1.6.11%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,868 kB
  • sloc: javascript: 195,588; php: 76,818; sql: 3,150; sh: 2,882; pascal: 1,079; makefile: 234; xml: 93; perl: 73; ansic: 48; python: 21
file content (102 lines) | stat: -rw-r--r-- 3,922 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

namespace Roundcube\Tests\Framework;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

use function Roundcube\Tests\invokeMethod;

/**
 * Test class to test rcube_db_pgsql class
 *
 * @group database
 * @group postgres
 */
#[Group('database')]
#[Group('postgres')]
class Framework_DBPgsql extends TestCase
{
    /**
     * Class constructor
     */
    function test_class()
    {
        $object = new \rcube_db_pgsql('test');

        $this->assertInstanceOf(\rcube_db_pgsql::class, $object, "Class constructor");
    }

    /**
     * Test script execution and table_prefix replacements when the prefix is a schema prefix
     */
    function test_exec_script_schema_prefix()
    {
        $db = \rcube_db::factory('pgsql:test');
        $db->set_option('table_prefix', 'prefix.');

        $script = [
            "CREATE TABLE `xxx` (test int, INDEX xxx (test))",
            "ALTER TABLE `xxx` CHANGE test test int",
            "TRUNCATE xxx",
            "TRUNCATE TABLE xxx",
            "DROP TABLE `vvv`",
            "CREATE TABLE `i` (test int CONSTRAINT `iii`
                FOREIGN KEY (`test`) REFERENCES `xxx`(`test`) ON DELETE CASCADE ON UPDATE CASCADE)",
            "CREATE TABLE `i` (`test` int, INDEX `testidx` (`test`))",
            "CREATE TABLE `i` (`test` int, UNIQUE `testidx` (`test`))",
            "CREATE TABLE `i` (`test` int, UNIQUE INDEX `testidx` (`test`))",
            "INSERT INTO xxx test = 1",
            "SELECT test FROM xxx",
            "CREATE SEQUENCE users_seq INCREMENT BY 1",
            "CREATE TABLE users ( user_id integer DEFAULT nextval('users_seq'::text) PRIMARY KEY )",
            "ALTER SEQUENCE user_ids RENAME TO users_seq",
        ];

        $output = [
            "CREATE TABLE `prefix`.`xxx` (test int, INDEX xxx (test))",
            "ALTER TABLE `prefix`.`xxx` CHANGE test test int",
            "TRUNCATE prefix.xxx",
            "TRUNCATE TABLE prefix.xxx",
            "DROP TABLE `prefix`.`vvv`",
            "CREATE TABLE `prefix`.`i` (test int CONSTRAINT `iii`
                FOREIGN KEY (`test`) REFERENCES `prefix`.`xxx`(`test`) ON DELETE CASCADE ON UPDATE CASCADE)",
            "CREATE TABLE `prefix`.`i` (`test` int, INDEX `testidx` (`test`))",
            "CREATE TABLE `prefix`.`i` (`test` int, UNIQUE `testidx` (`test`))",
            "CREATE TABLE `prefix`.`i` (`test` int, UNIQUE INDEX `testidx` (`test`))",
            "INSERT INTO prefix.xxx test = 1",
            "SELECT test FROM prefix.xxx",
            "CREATE SEQUENCE prefix.users_seq INCREMENT BY 1",
            "CREATE TABLE prefix.users ( user_id integer DEFAULT nextval('prefix.users_seq'::text) PRIMARY KEY )",
            "ALTER SEQUENCE prefix.user_ids RENAME TO prefix.users_seq",
        ];

        $method = new \ReflectionMethod('rcube_db_pgsql', 'fix_table_names');
        $method->setAccessible(true);

        foreach ($script as $idx => $query) {
            $res = $method->invoke($db, $query);
            $this->assertSame($output[$idx], $res, "Test case $idx");
        }
    }

    /**
     * Test converting config DSN string into PDO connection string
     */
    function test_dsn_string()
    {
        $db = new \rcube_db_pgsql('test');

        $dsn = $db->parse_dsn("pgsql://USERNAME:PASSWORD@HOST:5432/DATABASE");
        $result = invokeMethod($db, 'dsn_string', [$dsn]);
        $this->assertSame("pgsql:host=HOST;port=5432;dbname=DATABASE", $result);

        $dsn = $db->parse_dsn("pgsql:///DATABASE");
        $result = invokeMethod($db, 'dsn_string', [$dsn]);
        $this->assertSame("pgsql:dbname=DATABASE", $result);

        $dsn = $db->parse_dsn("pgsql://user@unix(/var/run/postgresql)/roundcubemail?sslmode=verify-full");
        $result = invokeMethod($db, 'dsn_string', [$dsn]);
        $this->assertSame("pgsql:host=/var/run/postgresql;dbname=roundcubemail;sslmode=verify-full", $result);
    }
}