1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2015 Michael Herold <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace hemio\form; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Description of Examples |
24
|
|
|
* |
25
|
|
|
* @author Michael Herold <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ExamplesTest extends \Helpers { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @return \hemio\html\Document |
32
|
|
|
*/ |
33
|
|
|
public function exampleForm() { |
34
|
|
|
$doc = new \hemio\html\Document(new \hemio\html\Str(_('Title'))); |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
$stored = [ |
38
|
|
|
'input_text' => 'Default Text', |
39
|
|
|
'input_password' => 'my-secret-password' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$form = new FormPost('name', null, null, $stored); |
43
|
|
|
|
44
|
|
|
$body = $doc->getHtml()->getBody(); |
45
|
|
|
$body['form'] = $form; |
46
|
|
|
|
47
|
|
|
$section = new \hemio\html\Section(); |
48
|
|
|
$form[] = $section; |
49
|
|
|
|
50
|
|
|
$inputText = new FieldText('input_text', _('Text')); |
51
|
|
|
$section[] = $inputText; |
52
|
|
|
|
53
|
|
|
$select = new FieldSelect('select', _('Select')); |
54
|
|
|
foreach ([1, 2, 3, 4, 5, 6] as $i) |
55
|
|
|
$select->addOption('value' . $i, _('Value ' . $i)); |
56
|
|
|
$section[] = $select; |
57
|
|
|
|
58
|
|
|
$inputPassword = new FieldPassword('input_password', _('Password')); |
59
|
|
|
$section[] = $inputPassword; |
60
|
|
|
|
61
|
|
|
$textarea = new FieldTextarea('textarea', _('Long Text')); |
62
|
|
|
$section[] = $textarea; |
63
|
|
|
|
64
|
|
|
$checkbox = new FieldCheckbox('checkbox', _('Checkbox')); |
65
|
|
|
$section[] = $checkbox; |
66
|
|
|
|
67
|
|
|
$email = new FieldEmail('email', _('Email')); |
68
|
|
|
$email->getControlElement()->setAttribute('placeholder', '[email protected]'); |
69
|
|
|
$section[] = $email; |
70
|
|
|
|
71
|
|
|
$textWithPattern = new FieldText('text_with_pattern', 'Follow the pattern'); |
72
|
|
|
$textWithPattern->setPattern('[aAbB]+', _('Give some "A"s or "B"s')); |
73
|
|
|
$section[] = $textWithPattern; |
74
|
|
|
|
75
|
|
|
$submit = new FieldSubmit('submit', _('Submit')); |
76
|
|
|
|
77
|
|
|
$buttons = new ButtonGroup([$submit]); |
78
|
|
|
|
79
|
|
|
$section[] = $buttons; |
80
|
|
|
|
81
|
|
|
return $doc; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function test1() { |
85
|
|
|
$doc = $this->exampleForm(); |
86
|
|
|
|
87
|
|
|
$this->_assertEqualsXmlFile($doc, 'examplesBasicForm.html'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function test2() { |
91
|
|
|
$doc = $this->exampleForm(); |
92
|
|
|
$doc->getHtml()->getHead()->addCssFile('style.css'); |
93
|
|
|
|
94
|
|
|
$form = $doc->getHtml()->getBody()['form']; |
95
|
|
|
|
96
|
|
|
// default template to patch |
97
|
|
|
$template = $form->getSingleControlTemplate(); |
98
|
|
|
|
99
|
|
|
// patch template for <select> controls |
100
|
|
|
$templateSelect = $this->patchTemplateForSelect(clone $template); |
101
|
|
|
$form->addInheritableAppendage( |
102
|
|
|
FormPost::FORM_FIELD_TEMPLATE . '_SELECT', $templateSelect |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
// patch template for <input type=checkbox /> controls |
106
|
|
|
$templateSwitch = $this->patchTemplateForSwitch(clone $template); |
107
|
|
|
$form->addInheritableAppendage( |
108
|
|
|
FormPost::FORM_FIELD_TEMPLATE . '_CHECKBOX', $templateSwitch |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->_assertEqualsXmlFile($doc, 'examplesAdjustedForm.html'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function patchTemplateForSelect($templateSelect) { |
115
|
|
|
$templateSelect['P']['SPAN'] = new \hemio\html\Span(); |
116
|
|
|
$templateSelect['P']['SPAN']->addCssClass('select'); |
117
|
|
|
|
118
|
|
|
$templateSelect->setPostInitHook(function (Abstract_\TemplateFormField $template) { |
119
|
|
|
unset($template['P']['CONTROL']); |
120
|
|
|
$template['P']['SPAN']['CONTROL'] = $template->getControl(); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
return $templateSelect; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function patchTemplateForSwitch($templateSwitch) { |
127
|
|
|
$templateSwitch->setPostInitHook(function (Abstract_\TemplateFormField $template) { |
128
|
|
|
$template->getControl()->addCssClass('switch'); |
129
|
|
|
$template['P']->addChildBeginning($template->getControl()); |
130
|
|
|
unset($template['P']['CONTROL']); |
131
|
|
|
|
132
|
|
|
$labelText = $template['P']['LABEL'][0]; |
133
|
|
|
unset($template['P']['LABEL'][0]); |
134
|
|
|
$spanLabel = new \hemio\html\Span(); |
135
|
|
|
$spanLabel[] = $labelText; |
136
|
|
|
|
137
|
|
|
$template['P']['LABEL'][] = $spanLabel; |
138
|
|
|
$template['P']['LABEL'][] = new \hemio\html\Span(); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
return $templateSwitch; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|