HelperFormElementTest.php 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/26/16
 * Time: 2:56 PM
 */

namespace qfq;

require_once(__DIR__ . '/../../qfq/helper/HelperFormElement.php');
require_once(__DIR__ . '/../../qfq/Constants.php');

15
16
17
18
19
20
21
22
23
use PHPUnit\Framework\TestCase;

//class HelperFormElementTest extends \PHPUnit_Framework_TestCase {

/**
 * Class HelperFormElementTest
 * @package qfq
 */
class HelperFormElementTest extends TestCase {
24

25
26
27
    public function testExplodeParameter() {
        $a = array();

28
        HelperFormElement::explodeParameter($a, FE_PARAMETER);
29
30
31
        $this->assertEquals(array(), $a, "Both arrays should be equal and empty");

        $a = ['id' => 100, 'parameter' => "work=12\nlist=1:new,2:edit,3:delete"];
32
33
//        $b = ['id' => 100, 'parameter' => "work=12\nlist=1:new,2:edit,3:delete", 'work' => 12, 'list' => '1:new,2:edit,3:delete'];
        $b = ['id' => 100, 'parameter' => "", 'work' => 12, 'list' => '1:new,2:edit,3:delete'];
34

35
        HelperFormElement::explodeParameter($a, FE_PARAMETER);
36
37
38
39
        $this->assertEquals($b, $a, "Both arrays should be equal");
    }

    public function testExplodeParameterInArrayElements() {
40
        $a = array();
41
        HelperFormElement::explodeParameterInArrayElements($a, FE_PARAMETER);
42
43
44
        $this->assertEquals(array(), $a, "Both arrays should be equal and empty");

        $a = [0 => ['id' => 100, 'parameter' => "work=12\nlist=1:new,2:edit,3:delete"]];
45
46
//        $b = [0 => ['id' => 100, 'parameter' => "work=12\nlist=1:new,2:edit,3:delete", 'work' => 12, 'list' => '1:new,2:edit,3:delete']];
        $b = [0 => ['id' => 100, 'parameter' => "", 'work' => 12, 'list' => '1:new,2:edit,3:delete']];
47

48
        HelperFormElement::explodeParameterInArrayElements($a, FE_PARAMETER);
49
50
51
        $this->assertEquals($b, $a, "Both arrays should be equal");
    }

52

53
    /**
54
     * @expectedException \qfq\UserFormException
55
56
     *
     */
57
    public function testExplodeFieldParameterException() {
58
59

        $a = [0 => ['id' => 111, 'name' => 'hello', 'collision' => 'dummy', 'parameter' => "work=12\ncollision=1:new,2:edit,3:delete"]];
60
        HelperFormElement::explodeParameterInArrayElements($a, FE_PARAMETER);
61
62
63

    }

64
65
66
67
    /**
     *
     */
    public function testDuplicateRetypeElements() {
68
69
70
71
72
73
74
75
76
77
78
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

        $a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note']];
        $b = HelperFormElement::duplicateRetypeElements($a);
        $this->assertEquals($a, $b, "Both arrays should be equal");

        $a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '']];
        $b = HelperFormElement::duplicateRetypeElements($a);
        $this->assertEquals(count($a) + 1, count($b), "Both arrays should be same count");
        $this->assertEquals('1', $b[0][FE_RETYPE], "FE_RETYPE should be set to '1'");

        $a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '0']];
        $b = HelperFormElement::duplicateRetypeElements($a);
        $this->assertEquals($a, $b, "Both arrays should be same count");
        $this->assertEquals('0', $b[0][FE_RETYPE], "FE_RETYPE should be unchanged");

        $a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '1']];
        $b = HelperFormElement::duplicateRetypeElements($a);
        $this->assertEquals(count($a) + 1, count($b), "Both arrays should be same count");
        $this->assertEquals('1', $b[0][FE_RETYPE], "FE_RETYPE should be unchanged");
        $this->assertEquals($b[0][FE_NAME] . RETYPE_FE_NAME_EXTENSION, $b[1][FE_NAME], "New name should be extended by: " . RETYPE_FE_NAME_EXTENSION);
        $this->assertEquals($b[0][FE_LABEL], $b[1][FE_LABEL], "Both text should be equal");
        $this->assertEquals($b[0][FE_NOTE], $b[1][FE_NOTE], "Both text should be equal");

        $a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '1', FE_RETYPE_LABEL => '', FE_RETYPE_NOTE => '']];
        $b = HelperFormElement::duplicateRetypeElements($a);
        $this->assertEquals('', $b[1][FE_LABEL], "String should be empty");
        $this->assertEquals('', $b[1][FE_NOTE], "String should be empty");

        $a = [0 => [FE_NAME => 'hello', FE_LABEL => 'my label', FE_NOTE => 'my note', FE_RETYPE => '1', FE_RETYPE_LABEL => 'new label', FE_RETYPE_NOTE => 'new note']];
        $b = HelperFormElement::duplicateRetypeElements($a);

        $this->assertEquals('my label', $b[0][FE_LABEL], "String should be equal");
        $this->assertEquals('my note', $b[0][FE_NOTE], "String should be equal");
        $this->assertEquals('new label', $b[1][FE_LABEL], "String should be equal");
        $this->assertEquals('new note', $b[1][FE_NOTE], "String should be equal");

    }

106
107
108
109
110
111
112
113
    /**
     *
     */
    public function testInitActionFormElement() {

        $list = [FE_TYPE, FE_SLAVE_ID, FE_SQL_VALIDATE, FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE,
            FE_SQL_AFTER, FE_EXPECT_RECORDS, FE_REQUIRED_LIST, FE_MESSAGE_FAIL, FE_SENDMAIL_TO, FE_SENDMAIL_CC,
            FE_SENDMAIL_BCC, FE_SENDMAIL_FROM, FE_SENDMAIL_SUBJECT, FE_SENDMAIL_REPLY_TO, FE_SENDMAIL_FLAG_AUTO_SUBMIT,
114
115
            FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID, FE_SENDMAIL_X_ID2, FE_SENDMAIL_X_ID3, FE_SENDMAIL_BODY_MODE,
            FE_SENDMAIL_BODY_HTML_ENTITY, FE_SENDMAIL_SUBJECT_HTML_ENTITY];
116
117

        $expect = array();
Carsten  Rose's avatar
Carsten Rose committed
118
        foreach ($list as $key) {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
            $expect[$key] = '';
        }

        // Complete empty
        $result = HelperFormElement::initActionFormElement(array());
        $this->assertEquals($expect, $result, "Both arrays should be equal");

        // Two known elements
        $fe = array();
        $fe[FE_TYPE] = FE_TYPE_AFTER_DELETE;
        $fe[FE_SENDMAIL_TO] = 'info@example.com';
        $expect[FE_TYPE] = $fe[FE_TYPE];
        $expect[FE_SENDMAIL_TO] = $fe[FE_SENDMAIL_TO];
        $result = HelperFormElement::initActionFormElement($fe);
        $this->assertEquals($expect, $result, "Both arrays should be equal");

        $fe['unknown'] = 'unknown';
        $expect['unknown'] = 'unknown';
        $result = HelperFormElement::initActionFormElement($fe);
        $this->assertEquals($expect, $result, "Both arrays should be equal");
    }

    /**
     *
     */
    public function testInitUploadFormElement() {

Carsten  Rose's avatar
Carsten Rose committed
146
        $list = [FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE, FE_SQL_AFTER];
147
148

        $expect = array();
Carsten  Rose's avatar
Carsten Rose committed
149
        foreach ($list as $key) {
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
            $expect[$key] = '';
        }

        // Complete empty
        $result = HelperFormElement::initUploadFormElement(array());
        $this->assertEquals($expect, $result, "Both arrays should be equal");

        // Two known elements
        $fe = array();
        $fe[FE_SQL_BEFORE] = 'SELECT willi';
        $fe[FE_SQL_AFTER] = 'UPDATE jane';
        $expect[FE_SQL_BEFORE] = $fe[FE_SQL_BEFORE];
        $expect[FE_SQL_AFTER] = $fe[FE_SQL_AFTER];
        $result = HelperFormElement::initUploadFormElement($fe);
        $this->assertEquals($expect, $result, "Both arrays should be equal");

        $fe['unknown'] = 'unknown';
        $expect['unknown'] = 'unknown';
        $result = HelperFormElement::initUploadFormElement($fe);
        $this->assertEquals($expect, $result, "Both arrays should be equal");
    }
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    public function testTgGetMaxLength() {
        $result = HelperFormElement::tgGetMaxLength('');
        $this->assertEquals(FE_TEMPLATE_GROUP_DEFAULT_MAX_LENGTH, $result, "Expect Default");

        $result = HelperFormElement::tgGetMaxLength(0);
        $this->assertEquals(FE_TEMPLATE_GROUP_DEFAULT_MAX_LENGTH, $result, "Expect Default");

        $result = HelperFormElement::tgGetMaxLength('0');
        $this->assertEquals(FE_TEMPLATE_GROUP_DEFAULT_MAX_LENGTH, $result, "Expect Default");

        $result = HelperFormElement::tgGetMaxLength('1');
        $this->assertEquals('1', $result, "Expect Default");

        $result = HelperFormElement::tgGetMaxLength('10');
        $this->assertEquals('10', $result, "Expect Default");

        $result = HelperFormElement::tgGetMaxLength(10);
        $this->assertEquals('10', $result, "Expect Default");

    }
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//    public function testExplodeTemplateGroupElements() {
//        $formElements = array();
//
//        $result = HelperFormElement::explodeTemplateGroupElements(array());
//        $this->assertEquals(array(), $result, "Both arrays should be equal");
//
//        $formElements = [[FE_NAME => 'nameA', FE_LABEL => 'labelA', FE_VALUE => '1'], [FE_NAME => 'nameB', FE_LABEL => 'labelB', FE_VALUE => '11']];
//        $result = HelperFormElement::explodeTemplateGroupElements($formElements);
//        $this->assertEquals($formElements, $result, "Both arrays should be equal");
//
//        $formElements = [[FE_NAME => 'nameA', FE_LABEL => 'label' . FE_TEMPLATE_GROUP_NAME_PATTERN . 'A', FE_VALUE => '1'], [FE_NAME => 'nameB', FE_LABEL => 'labelB', FE_VALUE => '11']];
//        $result = HelperFormElement::explodeTemplateGroupElements($formElements);
//        $this->assertEquals($formElements, $result, "Both arrays should be equal");
//
//        $formElements = [[FE_NAME => 'nameA', FE_LABEL => 'label' . FE_TEMPLATE_GROUP_NAME_PATTERN . 'A', FE_VALUE => '1', NAME_TG_COPIES => 2], [FE_NAME => 'nameB', FE_LABEL => 'labelB', FE_VALUE => '11']];
//        $result = HelperFormElement::explodeTemplateGroupElements($formElements);
//        $formElements = [[FE_NAME => 'nameA', FE_LABEL => 'label1A', FE_VALUE => '1'],
//            [FE_NAME => 'nameA', FE_LABEL => 'label2A', FE_VALUE => '1'],
//            [FE_NAME => 'nameB', FE_LABEL => 'labelB', FE_VALUE => '11']];
//        $this->assertEquals($formElements, $result, "Both arrays should be equal");
//
//        $formElements = [[FE_NAME => FE_TEMPLATE_GROUP_NAME_PATTERN . 'nameA', FE_LABEL => 'label' . FE_TEMPLATE_GROUP_NAME_PATTERN . 'A', FE_VALUE => '1', NAME_TG_COPIES => 2], [FE_NAME => 'nameB', FE_LABEL => 'labelB', FE_VALUE => '11']];
//        $result = HelperFormElement::explodeTemplateGroupElements($formElements);
//        $formElements = [[FE_NAME => '1nameA', FE_LABEL => 'label1A', FE_VALUE => '1'],
//            [FE_NAME => '2nameA', FE_LABEL => 'label2A', FE_VALUE => '1'],
//            [FE_NAME => 'nameB', FE_LABEL => 'labelB', FE_VALUE => '11']];
//        $this->assertEquals($formElements, $result, "Both arrays should be equal");
//    }
220
}