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