HelperFormElementTest.php 8.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

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

namespace qfq;

use qfq\HelperFormElement;

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

class HelperFormElementTest extends \PHPUnit_Framework_TestCase {

19
20
21
    public function testExplodeParameter() {
        $a = array();

22
        HelperFormElement::explodeParameter($a, FE_PARAMETER);
23
24
25
26
27
        $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'];

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

    public function testExplodeParameterInArrayElements() {
33
        $a = array();
34
        HelperFormElement::explodeParameterInArrayElements($a, FE_PARAMETER);
35
36
37
38
39
        $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']];

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

44

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

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

    }

56
57
58
59
    /**
     *
     */
    public function testDuplicateRetypeElements() {
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
96
97

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

    }

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    /**
     *
     */
    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,
            FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID];

        $expect = array();
        foreach($list as $key) {
            $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() {

        $list = [ FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE, FE_SQL_AFTER ];

        $expect = array();
        foreach($list as $key) {
            $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");
    }
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    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");
    }
191
}