DatabaseFunction.php 5.13 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
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
<?php
/**
 * @author Carsten Rose <carsten.rose@math.uzh.ch>
 */

namespace IMATHUZH\Qfq\Tests\Unit\Core\Database;

require_once(__DIR__ . '/AbstractDatabaseTest.php');

/**
 * Class DatabaseTest
 */
class DatabaseFunction extends AbstractDatabaseTest {

    /**
     * @var \IMATHUZH\Qfq\Core\Database\Database
     */
    protected $db = null;

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testQMore() {

        $this->assertEquals('', $this->db->sql('SELECT QMORE("",10)', ROW_IMPLODE_ALL));
        $this->assertEquals('', $this->db->sql('SELECT QMORE("",0)', ROW_IMPLODE_ALL));
        $this->assertEquals('test', $this->db->sql('SELECT QMORE("test",10)', ROW_IMPLODE_ALL));
        $this->assertEquals('te<span class="qfq-more-text">st</span>', $this->db->sql('SELECT QMORE("test",3)', ROW_IMPLODE_ALL));
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testQBar() {

        $this->assertEquals('', $this->db->sql('SELECT QBAR("")', ROW_IMPLODE_ALL));
        $this->assertEquals('test', $this->db->sql('SELECT QBAR("test")', ROW_IMPLODE_ALL));
        $this->assertEquals('te\|st', $this->db->sql('SELECT QBAR("te|st")', ROW_IMPLODE_ALL));
        $this->assertEquals('\|', $this->db->sql('SELECT QBAR("|")', ROW_IMPLODE_ALL));
        $this->assertEquals('\|\|\|', $this->db->sql('SELECT QBAR("|||")', ROW_IMPLODE_ALL));
        $this->assertEquals('a\|b\|', $this->db->sql('SELECT QBAR("a|b|")', ROW_IMPLODE_ALL));
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testQNl2Br() {

        $this->assertEquals('', $this->db->sql('SELECT QNL2BR("")', ROW_IMPLODE_ALL));
        $this->assertEquals('test<br>newline', $this->db->sql('SELECT QNL2BR("test<br>newline")', ROW_IMPLODE_ALL));
        $this->assertEquals("test<br>newline", $this->db->sql('SELECT QNL2BR("test' . PHP_EOL . 'newline")', ROW_IMPLODE_ALL));
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testQIfEmpty() {

        $this->assertEquals('test', $this->db->sql('SELECT QIFEMPTY("test","-")', ROW_IMPLODE_ALL));
68
69
70
        $this->assertEquals('<', $this->db->sql('SELECT QIFEMPTY("<","-")', ROW_IMPLODE_ALL));

        $this->assertEquals('-', $this->db->sql('SELECT QIFEMPTY(null,"-")', ROW_IMPLODE_ALL));
Carsten  Rose's avatar
Carsten Rose committed
71
        $this->assertEquals('-', $this->db->sql('SELECT QIFEMPTY("","-")', ROW_IMPLODE_ALL));
72

Carsten  Rose's avatar
Carsten Rose committed
73
        $this->assertEquals('-', $this->db->sql('SELECT QIFEMPTY("0","-")', ROW_IMPLODE_ALL));
74
75
76
77
78
        $this->assertEquals('-', $this->db->sql('SELECT QIFEMPTY(0,"-")', ROW_IMPLODE_ALL));

        $this->assertEquals('1', $this->db->sql('SELECT QIFEMPTY("1","-")', ROW_IMPLODE_ALL));
        $this->assertEquals('1', $this->db->sql('SELECT QIFEMPTY(1,"-")', ROW_IMPLODE_ALL));

Carsten  Rose's avatar
Carsten Rose committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        $this->assertEquals('-', $this->db->sql('SELECT QIFEMPTY("0000-00-00","-")', ROW_IMPLODE_ALL));
        $this->assertEquals('-', $this->db->sql('SELECT QIFEMPTY("0000-00-00 00:00:00","-")', ROW_IMPLODE_ALL));
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testStrip_Tags() {

        $this->assertEquals('', $this->db->sql('SELECT strip_tags("")', ROW_IMPLODE_ALL));
        $this->assertEquals('test', $this->db->sql('SELECT strip_tags("test")', ROW_IMPLODE_ALL));
        $this->assertEquals('test', $this->db->sql('SELECT strip_tags("te<a>st")', ROW_IMPLODE_ALL));
        $this->assertEquals('test', $this->db->sql('SELECT strip_tags("te<a class=\"exp\">st")', ROW_IMPLODE_ALL));
        $this->assertEquals('test', $this->db->sql('SELECT strip_tags("te<a class=\"exp\">s</a>t")', ROW_IMPLODE_ALL));
        $this->assertEquals('world', $this->db->sql('SELECT strip_tags("<a class=\"exp\">world</a>")', ROW_IMPLODE_ALL));
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     */
    public function testQDate_Format() {

        $this->assertEquals('-', $this->db->sql('SELECT QDATE_FORMAT("")', ROW_IMPLODE_ALL));
        $this->assertEquals('-', $this->db->sql('SELECT QDATE_FORMAT("0000-00-00")', ROW_IMPLODE_ALL));
        $this->assertEquals('-', $this->db->sql('SELECT QDATE_FORMAT("0000-00-00 00:00:00")', ROW_IMPLODE_ALL));
        $this->assertEquals('02.11.2019 00:00', $this->db->sql('SELECT QDATE_FORMAT("2019-11-02")', ROW_IMPLODE_ALL));
        $this->assertEquals('02.11.2019 01:23', $this->db->sql('SELECT QDATE_FORMAT("2019-11-02 01:23")', ROW_IMPLODE_ALL));
        $this->assertEquals('02.11.2019 01:23', $this->db->sql('SELECT QDATE_FORMAT("2019-11-02 01:23:45")', ROW_IMPLODE_ALL));
    }

    /**
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
118
     * @throws \Exception
Carsten  Rose's avatar
Carsten Rose committed
119
120
121
122
123
124
125
126
127
128
     */
    protected function setUp() {
        parent::setUp();

        $this->db = $this->dbArray[DB_INDEX_DEFAULT];

        $this->executeSQLFile(__DIR__ . '../../../../../Classes/Sql/function.sql', true);

    }
}