| 1 | <?php␊ |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */␊ |
| 3 | /*␊ |
| 4 | # ***** BEGIN LICENSE BLOCK *****␊ |
| 5 | # This file is part of Photon, The High Speed PHP Framework.␊ |
| 6 | # Copyright (C) 2010, 2011 Loic d'Anterroches and contributors.␊ |
| 7 | #␊ |
| 8 | # Photon is free software; you can redistribute it and/or modify␊ |
| 9 | # it under the terms of the GNU Lesser General Public License as published by␊ |
| 10 | # the Free Software Foundation; either version 2.1 of the License.␊ |
| 11 | #␊ |
| 12 | # Photon is distributed in the hope that it will be useful,␊ |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
| 15 | # GNU Lesser General Public License for more details.␊ |
| 16 | #␊ |
| 17 | # You should have received a copy of the GNU Lesser General Public License␊ |
| 18 | # along with this program; if not, write to the Free Software␊ |
| 19 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA␊ |
| 20 | #␊ |
| 21 | # ***** END LICENSE BLOCK ***** */␊ |
| 22 | ␊ |
| 23 | ␊ |
| 24 | namespace photon\tests\form\formTest;␊ |
| 25 | ␊ |
| 26 | use \photon\form\field;␊ |
| 27 | use \photon\form\Form;␊ |
| 28 | use \photon\form\Invalid;␊ |
| 29 | ␊ |
| 30 | class Simple extends Form␊ |
| 31 | {␊ |
| 32 | public function initFields($extra=array())␊ |
| 33 | {␊ |
| 34 | $this->fields['login'] = new field\Varchar(␊ |
| 35 | array('required' => true,␊ |
| 36 | 'label' => 'Your login',␊ |
| 37 | 'max_length' => 15,␊ |
| 38 | 'min_length' => 3,␊ |
| 39 | 'help_text' => 'The login must...',␊ |
| 40 | 'widget_attrs' => array(␊ |
| 41 | 'maxlength' => 15,␊ |
| 42 | 'size' => 10,␊ |
| 43 | ),␊ |
| 44 | ));␊ |
| 45 | }␊ |
| 46 | ␊ |
| 47 | public function clean_login()␊ |
| 48 | {␊ |
| 49 | if (preg_match('/[^A-Za-z0-9]/', $this->cleaned_data['login'])) {␊ |
| 50 | throw new Invalid(sprintf('Invalid login: %s', $this->cleaned_data['login']));␊ |
| 51 | }␊ |
| 52 | return $this->cleaned_data['login'];␊ |
| 53 | }␊ |
| 54 | ␊ |
| 55 | public function clean()␊ |
| 56 | {␊ |
| 57 | if ('hello' === $this->cleaned_data['login']) {␊ |
| 58 | throw new Invalid(sprintf('Invalid login: %s', $this->cleaned_data['login']));␊ |
| 59 | }␊ |
| 60 | return $this->cleaned_data;␊ |
| 61 | }␊ |
| 62 | }␊ |
| 63 | ␊ |
| 64 | class Minimal extends Form␊ |
| 65 | {␊ |
| 66 | public function initFields($extra=array())␊ |
| 67 | {␊ |
| 68 | $this->fields['login'] = new field\Varchar(␊ |
| 69 | array('required' => true,␊ |
| 70 | 'max_length' => 15,␊ |
| 71 | 'min_length' => 3,␊ |
| 72 | 'widget_attrs' => array(␊ |
| 73 | 'maxlength' => 15,␊ |
| 74 | 'size' => 10,␊ |
| 75 | ),␊ |
| 76 | ));␊ |
| 77 | }␊ |
| 78 | }␊ |
| 79 | ␊ |
| 80 | class Hidden extends Form␊ |
| 81 | {␊ |
| 82 | public function initFields($extra=array())␊ |
| 83 | {␊ |
| 84 | $this->fields['hidden'] = new field\Integer(␊ |
| 85 | array('widget' => '\photon\form\widget\HiddenInput',␊ |
| 86 | 'required' => true));␊ |
| 87 | }␊ |
| 88 | }␊ |
| 89 | ␊ |
| 90 | class HiddenAnd extends Form␊ |
| 91 | {␊ |
| 92 | public function initFields($extra=array())␊ |
| 93 | {␊ |
| 94 | $this->fields['hidden'] = new field\Integer(␊ |
| 95 | array('widget' => '\photon\form\widget\HiddenInput',␊ |
| 96 | 'required' => true));␊ |
| 97 | $this->fields['login'] = new field\Varchar(␊ |
| 98 | array('required' => true,␊ |
| 99 | 'max_length' => 15,␊ |
| 100 | 'min_length' => 3,␊ |
| 101 | 'widget_attrs' => array(␊ |
| 102 | 'maxlength' => 15,␊ |
| 103 | 'size' => 10,␊ |
| 104 | ),␊ |
| 105 | ));␊ |
| 106 | ␊ |
| 107 | }␊ |
| 108 | }␊ |
| 109 | ␊ |
| 110 | class FormTest extends \PHPUnit_Framework_TestCase␊ |
| 111 | {␊ |
| 112 | public function testNotImplemented()␊ |
| 113 | {␊ |
| 114 | $this->setExpectedException('\photon\form\Exception'); ␊ |
| 115 | $form = new Form();␊ |
| 116 | }␊ |
| 117 | ␊ |
| 118 | public function testInvalid()␊ |
| 119 | {␊ |
| 120 | $inv = new Invalid(array('mes1', 'mes2'));␊ |
| 121 | $this->assertEquals(array('mes1', 'mes2'), $inv->messages);␊ |
| 122 | }␊ |
| 123 | ␊ |
| 124 | public function testMinimal()␊ |
| 125 | {␊ |
| 126 | $form = new Minimal();␊ |
| 127 | $this->assertSame(false, $form->isValid());␊ |
| 128 | $form = new Minimal(array('login' => 'abcdefgh'));␊ |
| 129 | $this->assertSame(true, $form->isValid());␊ |
| 130 | }␊ |
| 131 | ␊ |
| 132 | public function testMinimalRender()␊ |
| 133 | {␊ |
| 134 | $form = new Minimal();␊ |
| 135 | $this->assertEquals('<p><label for="id_login">Login:</label> <input maxlength="15" size="10" name="login" type="text" id="id_login" /></p>', (string) $form->render_p());␊ |
| 136 | $this->assertEquals('<li><label for="id_login">Login:</label> <input maxlength="15" size="10" name="login" type="text" id="id_login" /></li>', (string) $form->render_ul());␊ |
| 137 | $this->assertEquals('<tr><th><label for="id_login">Login:</label></th><td><input maxlength="15" size="10" name="login" type="text" id="id_login" /></td></tr>', (string) $form->render_table());␊ |
| 138 | foreach ($form as $name => $field) {␊ |
| 139 | $this->assertEquals('login', $name);␊ |
| 140 | }␊ |
| 141 | $this->assertEquals('photon\form\BoundField', get_class($field));␊ |
| 142 | $foo = $form['login'];␊ |
| 143 | $this->assertEquals('photon\form\field\Varchar', get_class($foo));␊ |
| 144 | $foo = $form->field('login');␊ |
| 145 | $this->assertEquals('photon\form\BoundField', get_class($foo));␊ |
| 146 | $foo = $form->f->login;␊ |
| 147 | $this->assertEquals('photon\form\BoundField', get_class($foo));␊ |
| 148 | $this->assertEquals(false, isset($form['badone']));␊ |
| 149 | $this->assertEquals(true, isset($form['login']));␊ |
| 150 | unset($form['login']);␊ |
| 151 | $this->assertEquals(false, isset($form['login']));␊ |
| 152 | $form['login'] = $foo;␊ |
| 153 | $this->assertEquals(true, isset($form['login']));␊ |
| 154 | $this->setExpectedException('\photon\form\Exception'); ␊ |
| 155 | $foo = $form['badone'];␊ |
| 156 | }␊ |
| 157 | ␊ |
| 158 | public function testBoundField()␊ |
| 159 | {␊ |
| 160 | $form = new HiddenAnd();␊ |
| 161 | $bf = new \photon\form\BoundField($form, $form['login'], 'login');␊ |
| 162 | $this->assertEquals('<label for="id_login" class="test">Login</label>',␊ |
| 163 | (string) $bf->labelTag(null, array('class' => 'test')));␊ |
| 164 | ␊ |
| 165 | $form = new HiddenAnd();␊ |
| 166 | $form->id_fields = 'dummy_';␊ |
| 167 | $bf = new \photon\form\BoundField($form, $form['login'], 'login');␊ |
| 168 | $this->assertEquals('<label for="login" class="test">Login</label>',␊ |
| 169 | (string) $bf->labelTag(null, array('class' => 'test')));␊ |
| 170 | ␊ |
| 171 | $form = new HiddenAnd();␊ |
| 172 | $form->id_fields = '';␊ |
| 173 | $bf = new \photon\form\BoundField($form, $form['login'], 'login');␊ |
| 174 | $this->assertEquals('<label for="" class="test">Login</label>',␊ |
| 175 | (string) $bf->labelTag(null, array('class' => 'test')));␊ |
| 176 | ␊ |
| 177 | $form = new HiddenAnd();␊ |
| 178 | $bf = new \photon\form\BoundField($form, $form['login'], 'login');␊ |
| 179 | $this->assertEquals('<input maxlength="15" size="10" name="login" type="text" id="id_login" />',␊ |
| 180 | (string) $bf);␊ |
| 181 | $this->assertEquals('<input maxlength="15" size="10" name="login" type="text" id="id_login" />',␊ |
| 182 | (string) $bf->render_w);␊ |
| 183 | // forced empty list␊ |
| 184 | $this->assertEquals('<ul class="errorlist"></ul>', ␊ |
| 185 | (string) $bf->fieldErrors());␊ |
| 186 | }␊ |
| 187 | ␊ |
| 188 | public function testMinimalErrorRender()␊ |
| 189 | {␊ |
| 190 | $form = new Minimal(array('login' => '12'));␊ |
| 191 | $this->assertSame(false, $form->isValid());␊ |
| 192 | $this->assertEquals('<ul class="errorlist"><li>Ensure this value has at least 3 characters (it has 2).</li></ul>␊ |
| 193 | <p><label for="id_login">Login:</label> <input maxlength="15" size="10" name="login" type="text" id="id_login" value="12" /></p>', (string) $form->render_p());␊ |
| 194 | $this->assertEquals('<li><ul class="errorlist"><li>Ensure this value has at least 3 characters (it has 2).</li></ul><label for="id_login">Login:</label> <input maxlength="15" size="10" name="login" type="text" id="id_login" value="12" /></li>', (string) $form->render_ul());␊ |
| 195 | $this->assertEquals('<tr><th><label for="id_login">Login:</label></th><td><ul class="errorlist"><li>Ensure this value has at least 3 characters (it has 2).</li></ul><input maxlength="15" size="10" name="login" type="text" id="id_login" value="12" /></td></tr>', (string) $form->render_table);␊ |
| 196 | }␊ |
| 197 | ␊ |
| 198 | public function testTopErrors()␊ |
| 199 | {␊ |
| 200 | $form = new Minimal(array('login' => '12123'));␊ |
| 201 | $this->assertSame(true, $form->isValid());␊ |
| 202 | $this->assertSame(array(), $form->get_top_errors());␊ |
| 203 | $form = new Simple(array('login' => 'hello'));␊ |
| 204 | $this->assertSame(false, $form->isValid());␊ |
| 205 | $this->assertSame(1, count($form->get_top_errors()));␊ |
| 206 | $this->assertEquals('<ul class="errorlist"><li>Invalid login: hello</li></ul>', ␊ |
| 207 | (string) $form->render_top_errors);␊ |
| 208 | $this->assertEquals('<ul class="errorlist"><li>Invalid login: hello</li></ul>␊ |
| 209 | <p><label for="id_login">Your login:</label> <input maxlength="15" size="10" name="login" type="text" id="id_login" value="hello" /> The login must...</p>', (string) $form->render_p());␊ |
| 210 | ␊ |
| 211 | }␊ |
| 212 | ␊ |
| 213 | public function testSimpleForm()␊ |
| 214 | {␊ |
| 215 | $form = new Simple();␊ |
| 216 | $this->assertSame(false, $form->isValid());␊ |
| 217 | $form = new Simple(array('login' => 'abcdefgh'));␊ |
| 218 | $this->assertSame(true, $form->isValid());␊ |
| 219 | $form = new Simple(array('login' => 'ab'));␊ |
| 220 | $this->assertSame(false, $form->isValid());␊ |
| 221 | $form = new Simple(array('login' => 'absaonetuhasnoteuhasonteuhaaoen'));␊ |
| 222 | $this->assertSame(false, $form->isValid());␊ |
| 223 | $form = new Simple(array('login' => 'hel!lo'));␊ |
| 224 | $this->assertSame(false, $form->isValid());␊ |
| 225 | $form = new Simple(array('login' => 'hello'));␊ |
| 226 | $this->assertSame(false, $form->isValid());␊ |
| 227 | $this->assertSame(false, $form->isValid());␊ |
| 228 | }␊ |
| 229 | ␊ |
| 230 | public function testHidden()␊ |
| 231 | {␊ |
| 232 | $form = new Hidden();␊ |
| 233 | $this->assertSame(false, $form->isValid());␊ |
| 234 | $form = new Hidden(array('hidden' => 'abc'));␊ |
| 235 | $this->assertSame(false, $form->isValid());␊ |
| 236 | $this->assertEquals('<ul class="errorlist"><li>(Hidden field hidden) The value must be an integer.</li></ul>␊ |
| 237 | <input name="hidden" type="hidden" id="id_hidden" value="abc" />', (string) $form->render_p());␊ |
| 238 | }␊ |
| 239 | ␊ |
| 240 | public function testHiddenAnd()␊ |
| 241 | {␊ |
| 242 | $form = new HiddenAnd();␊ |
| 243 | $this->assertSame(false, $form->isValid());␊ |
| 244 | $form = new HiddenAnd(array('hidden' => 'abc'));␊ |
| 245 | $this->assertSame(false, $form->isValid());␊ |
| 246 | $this->assertEquals('<ul class="errorlist"><li>(Hidden field hidden) The value must be an integer.</li></ul>␊ |
| 247 | <ul class="errorlist"><li>This field is required.</li></ul>␊ |
| 248 | <p><label for="id_login">Login:</label> <input maxlength="15" size="10" name="login" type="text" id="id_login" /><input name="hidden" type="hidden" id="id_hidden" value="abc" /></p>', (string) $form->render_p());␊ |
| 249 | }␊ |
| 250 | } |