1<?php
2
3/**
4 * Tests for the dbquery plugin helper plugin
5 *
6 * @group plugin_dbquery
7 * @group plugins
8 */
9class helper_plugin_dbquery_test extends DokuWikiTest
10{
11    protected $pluginsEnabled = array('dbquery');
12    /** @var helper_plugin_dbquery $hlp */
13    protected $hlp;
14
15    /** @inheritDoc */
16    public function setUp()
17    {
18        parent::setUp();
19        $this->hlp = plugin_load('helper', 'dbquery');
20    }
21
22    public function testGatherVariables()
23    {
24        global $INFO;
25        $INFO['id'] = 'foo:bar:baz';
26
27        $expected = [
28            ':user' => '',
29            ':mail' => '',
30            ':groups' => [],
31            ':id' => ':foo:bar:baz',
32            ':page' => 'baz',
33            ':ns' => ':foo:bar',
34        ];
35        $actual = $this->hlp->gatherVariables();
36        $this->assertEquals($expected, $actual);
37    }
38
39    public function testGatherVariablesUser()
40    {
41        global $INFO;
42        /** @var auth_plugin_authplain $auth */
43        global $auth;
44
45        $INFO['id'] = 'foo:bar:baz';
46        $auth->createUser('foo', 'bar', 'My Test User', 'foo@example.com', ['one', 'two', 'three']);
47        auth_login('foo', 'bar');
48
49        $expected = [
50            ':user' => 'foo',
51            ':mail' => 'foo@example.com',
52            ':groups' => ['one', 'two', 'three'],
53            ':id' => ':foo:bar:baz',
54            ':page' => 'baz',
55            ':ns' => ':foo:bar',
56        ];
57        $actual = $this->hlp->gatherVariables();
58        $this->assertEquals($expected, $actual);
59    }
60
61    public function testPrepareStatement()
62    {
63        $pdo = $this->hlp->getPDO('sqlite:', '', '');
64
65        $parameters = [
66            ':user' => 'foo',
67            ':mail' => 'foo@example.com',
68            ':groups' => ['one', 'two', 'three'],
69            ':id' => ':foo:bar:baz',
70            ':page' => 'baz',
71            ':ns' => ':foo:bar',
72        ];
73        $sql = 'SELECT :user, :mail, :id, :page, :ns WHERE \'foo\' in (:groups)';
74        $sth = $this->hlp->prepareStatement($pdo, $sql, $parameters);
75
76        $actual = $sth->queryString;
77        $expected = 'SELECT :user, :mail, :id, :page, :ns WHERE \'foo\' in (:group0,:group1,:group2)';
78        $this->assertEquals($expected, $actual);
79    }
80}
81