1<?php
2namespace dokuwiki\plugin\dbquery\test;
3
4use DokuWikiTest;
5
6/**
7 * General tests for the dbquery plugin
8 *
9 * @group plugin_dbquery
10 * @group plugins
11 */
12class HelperTest extends DokuWikiTest
13{
14    protected $pluginsEnabled = array('dbquery');
15    /** @var \helper_plugin_dbquery $hlp */
16    protected $hlp;
17
18    /** @inheritDoc */
19    public function setUp(): void
20    {
21        parent::setUp();
22        $this->hlp = plugin_load('helper', 'dbquery');
23    }
24
25    public function testGatherVariables()
26    {
27        global $INFO;
28        $INFO['id'] = 'foo:bar:baz';
29
30        $expected = [
31            ':user' => '',
32            ':mail' => '',
33            ':groups' => [],
34            ':id' => ':foo:bar:baz',
35            ':page' => 'baz',
36            ':ns' => ':foo:bar',
37        ];
38        $actual = $this->hlp->gatherVariables();
39        $this->assertEquals($expected, $actual);
40    }
41
42    public function testGatherVariablesUser()
43    {
44        global $INFO;
45        /** @var \auth_plugin_authplain $auth */
46        global $auth;
47
48        $INFO['id'] = 'foo:bar:baz';
49        $auth->createUser('foo', 'bar', 'My Test User', 'foo@example.com', ['one', 'two', 'three']);
50        auth_login('foo', 'bar');
51
52        $expected = [
53            ':user' => 'foo',
54            ':mail' => 'foo@example.com',
55            ':groups' => ['one', 'two', 'three'],
56            ':id' => ':foo:bar:baz',
57            ':page' => 'baz',
58            ':ns' => ':foo:bar',
59        ];
60        $actual = $this->hlp->gatherVariables();
61        $this->assertEquals($expected, $actual);
62    }
63
64    public function testPrepareStatement()
65    {
66        $pdo = $this->hlp->getPDO('sqlite:', '', '');
67
68        $parameters = [
69            ':user' => 'foo',
70            ':mail' => 'foo@example.com',
71            ':groups' => ['one', 'two', 'three'],
72            ':id' => ':foo:bar:baz',
73            ':page' => 'baz',
74            ':ns' => ':foo:bar',
75        ];
76        $sql = 'SELECT :user, :mail, :id, :page, :ns WHERE \'foo\' in (:groups)';
77        $sth = $this->hlp->prepareStatement($pdo, $sql, $parameters);
78
79        $actual = $sth->queryString;
80        $expected = 'SELECT :user, :mail, :id, :page, :ns WHERE \'foo\' in (:group0,:group1,:group2)';
81        $this->assertEquals($expected, $actual);
82    }
83
84    public function testGetDsnAliases()
85    {
86        $conf = "nouser mysql:host=localhost;port=3306;dbname=testdb1\n\n".
87                "nopass mysql:host=localhost;port=3306;dbname=testdb2 user\n".
88                "both mysql:host=localhost;port=3306;dbname=testdb3 user pass\n";
89
90        $expect = [
91            '_' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb1', 'user' => 'dfu', 'pass' => 'dfp'],
92            'nouser' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb1', 'user' => 'dfu', 'pass' => 'dfp'],
93            'nopass' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb2', 'user' => 'user', 'pass' => 'dfp'],
94            'both' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb3', 'user' => 'user', 'pass' => 'pass'],
95        ];
96
97        $actual = $this->callInaccessibleMethod($this->hlp, 'getDsnAliases', [$conf, 'dfu', 'dfp']);
98        $this->assertEquals($expect, $actual);
99    }
100
101    public function testGetDsnAliasesLegacy()
102    {
103        $conf = 'mysql:host=localhost;port=3306;dbname=testdb1';
104
105        $expect = [
106            '_' => ['dsn' => 'mysql:host=localhost;port=3306;dbname=testdb1', 'user' => 'dfu', 'pass' => 'dfp'],
107        ];
108
109        $actual = $this->callInaccessibleMethod($this->hlp, 'getDsnAliases', [$conf, 'dfu', 'dfp']);
110        $this->assertEquals($expect, $actual);
111    }
112}
113