HelperFormElementTest.php 9.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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');

class HelperFormElementTest extends \PHPUnit_Framework_TestCase {

17
18
19
    public function testExplodeParameter() {
        $a = array();

20
        HelperFormElement::explodeParameter($a, FE_PARAMETER);
21
22
23
24
25
        $this->assertEquals(array(), $a, "Both arrays should be equal and empty");

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

26
        HelperFormElement::explodeParameter($a, FE_PARAMETER);
27
28
29
30
        $this->assertEquals($b, $a, "Both arrays should be equal");
    }

    public function testExplodeParameterInArrayElements() {
31
        $a = array();
32
        HelperFormElement::explodeParameterInArrayElements($a, FE_PARAMETER);
33
34
35
36
37
        $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"]];
        $b = [0 => ['id' => 100, 'parameter' => "work=12\nlist=1:new,2:edit,3:delete", 'work' => 12, 'list' => '1:new,2:edit,3:delete']];

38
        HelperFormElement::explodeParameterInArrayElements($a, FE_PARAMETER);
39
40
41
        $this->assertEquals($b, $a, "Both arrays should be equal");
    }

42

43
    /**
44
     * @expectedException \qfq\UserFormException
45
46
     *
     */
47
    public function testExplodeFieldParameterException() {
48
49

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

    }

54
55
56
57
    /**
     *
     */
    public function testDuplicateRetypeElements() {
58
59
60
61
62
63
64
65
66
67
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

        $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");

    }

96
97
98
99
100
101
102
103
    /**
     *
     */
    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,
Carsten  Rose's avatar
Carsten Rose committed
104
            FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID, FE_SENDMAIL_X_ID2, FE_SENDMAIL_X_ID3];
105
106

        $expect = array();
Carsten  Rose's avatar
Carsten Rose committed
107
        foreach ($list as $key) {
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
            $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
135
        $list = [FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE, FE_SQL_AFTER];
136
137

        $expect = array();
Carsten  Rose's avatar
Carsten Rose committed
138
        foreach ($list as $key) {
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
            $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");
    }
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    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");

    }
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//    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");
//    }
209
}