OnStringTest.php 6.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 11/02/18
 * Time: 9:16 PM
 */

namespace qfq;

11
12
13
14
15
16
use PHPUnit\Framework\TestCase;

/**
 * Class OnStringTest
 * @package qfq
 */
Carsten  Rose's avatar
Carsten Rose committed
17
18
class OnStringTest extends TestCase
{
19

20
21
22
    /**
     *
     */
23
24
25
26
27
28
29
    public function testStrrstr() {

        $this->assertEquals('', OnString::strrstr('', ''));
        $this->assertEquals('', OnString::strrstr('hello world to the limit', ''));
        $this->assertEquals('limit', OnString::strrstr('hello world to the limit', ' '));
        $this->assertEquals('', OnString::strrstr('', ' '));
    }
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

    /**
     *
     */
    public function testStripFirstCharIf() {
        $this->assertEquals('', OnString::stripFirstCharIf('', ''));
        $this->assertEquals('', OnString::stripFirstCharIf('c', ''));
        $this->assertEquals('', OnString::stripFirstCharIf('c', 'c'));
        $this->assertEquals('def', OnString::stripFirstCharIf('c', 'cdef'));
        $this->assertEquals('def', OnString::stripFirstCharIf('c', 'def'));
        $this->assertEquals('def', OnString::stripFirstCharIf('cd', 'def'));

    }

    /**
     *
     */
    public function testTrimQuote() {
        $this->assertEquals('', OnString::trimQuote(''));
        $this->assertEquals('test', OnString::trimQuote('test'));
        $this->assertEquals(' test ', OnString::trimQuote(' test '));
        $this->assertEquals('" test ', OnString::trimQuote('" test '));
        $this->assertEquals('" test \'', OnString::trimQuote('" test \''));
        $this->assertEquals('\' test "', OnString::trimQuote('\' test "'));
        $this->assertEquals(' test ', OnString::trimQuote('" test "'));
        $this->assertEquals(' test ', OnString::trimQuote("' test '"));
        $this->assertEquals(' te"st ', OnString::trimQuote("' te\"st '"));
        $this->assertEquals(' te\'st ', OnString::trimQuote("' te'st '"));

    }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    /**
     *
     */
    public function testRemoveNewlinesInNestedExpression() {
        $this->assertEquals("", OnString::removeNewlinesInNestedExpression(""));
        $this->assertEquals("test", OnString::removeNewlinesInNestedExpression("test"));
        $this->assertEquals("{{test}}", OnString::removeNewlinesInNestedExpression("{{test}}"));
        $this->assertEquals("{{test }}", OnString::removeNewlinesInNestedExpression("{{test\n}}"));
        $this->assertEquals("{{test two}}", OnString::removeNewlinesInNestedExpression("{{test\ntwo}}"));
        $this->assertEquals("Pre{{test two}}Post", OnString::removeNewlinesInNestedExpression("Pre{{test\ntwo}}Post"));
        $this->assertEquals("{{test{{two}}}}", OnString::removeNewlinesInNestedExpression("{{test{{two}}}}"));
        $this->assertEquals("{{a {{b }}}}",
            OnString::removeNewlinesInNestedExpression("{{a\n{{b\n}}}}"));
        $this->assertEquals("{{a b{{c d}}{{e f}} h}}",
            OnString::removeNewlinesInNestedExpression("{{a\nb{{c\nd}}{{e\nf}}\nh}}"));
        $this->assertEquals("param1=abc\nsqlInsert = {{SELECT * FROM test WHERE '{{var}}'='true'}}",
            OnString::removeNewlinesInNestedExpression("param1=abc\nsqlInsert = {{SELECT *\nFROM test\nWHERE '{{var}}'='true'}}"));
        $this->assertEquals("sqlInsert = {{SELECT * FROM test WHERE '{{var}}'='true'}}\nparam1=abc",
            OnString::removeNewlinesInNestedExpression("sqlInsert = {{SELECT *\nFROM test\nWHERE '{{var}}'='true'}}\nparam1=abc"));
    }

Carsten  Rose's avatar
Carsten Rose committed
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
118
119
120
121
122
123
    /**
     * @throws UserFormException
     */
    public function testExtractFormRecordId() {
        $this->assertEquals('', OnString::extractFormRecordId(''));
        $this->assertEquals('', OnString::extractFormRecordId('/'));
        $this->assertEquals('', OnString::extractFormRecordId('//'));
        $this->assertEquals(TYPO3_FORM . '=path1' . PHP_EOL, OnString::extractFormRecordId('/path1'));
        $this->assertEquals(TYPO3_FORM . '=path1' . PHP_EOL, OnString::extractFormRecordId('/path1/'));
        $this->assertEquals(TYPO3_FORM . '=path1' . PHP_EOL, OnString::extractFormRecordId('path1/'));

        $expected=TYPO3_RECORD_ID . '=12' . PHP_EOL . TYPO3_FORM . '=path1' . PHP_EOL;
        $this->assertEquals($expected, OnString::extractFormRecordId('/path1/12'));

        $expected=TYPO3_RECORD_ID . '=12' . PHP_EOL . TYPO3_FORM . '=path1' . PHP_EOL;
        $this->assertEquals($expected, OnString::extractFormRecordId('/path1/12/'));

        $expected=TYPO3_FORM . '=path2' . PHP_EOL . TYPO3_REST_PATH . '=path1/12'. PHP_EOL;
        $this->assertEquals($expected, OnString::extractFormRecordId('/path1/12/path2'));

        $expected=TYPO3_RECORD_ID . '=34' . PHP_EOL . TYPO3_FORM . '=path2' . PHP_EOL . TYPO3_REST_PATH . '=path1/12'. PHP_EOL;
        $this->assertEquals($expected, OnString::extractFormRecordId('/path1/12/path2/34'));
    }

    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testExtractFormRecordId_1() {
        # An alnum string is requested as path
        $this->assertEquals('', OnString::extractFormRecordId('/%'));
    }

    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testExtractFormRecordId_2() {
        # A numerical value is requested as id
        $this->assertEquals('', OnString::extractFormRecordId('/path1/path2'));
    }

124
125
126
127
128
129
130
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testRemoveNewlinesInNestedExpression_missingClosing_1() {
        $str = OnString::removeNewlinesInNestedExpression("Hi! {{Test ");
    }
Carsten  Rose's avatar
Carsten Rose committed
131

132
133
134
135
136
137
138
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testRemoveNewlinesInNestedExpression_missingClosing_2() {
        $str = OnString::removeNewlinesInNestedExpression("Hi! {{Test}");
    }
Carsten  Rose's avatar
Carsten Rose committed
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testRemoveNewlinesInNestedExpression_missingClosing_3() {
        $str = OnString::removeNewlinesInNestedExpression("Hi! {{Test {{var }}");
    }

    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testRemoveNewlinesInNestedExpression_missingOpening_1() {
        $str = OnString::removeNewlinesInNestedExpression("}}");
    }
Carsten  Rose's avatar
Carsten Rose committed
155

156
157
158
159
160
161
162
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testRemoveNewlinesInNestedExpression_missingOpening_2() {
        $str = OnString::removeNewlinesInNestedExpression("{}}");
    }
Carsten  Rose's avatar
Carsten Rose committed
163

164
165
166
167
168
169
170
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testRemoveNewlinesInNestedExpression_missingOpening_3() {
        $str = OnString::removeNewlinesInNestedExpression("{{Test}}}}");
    }
171
}