1<?php
2
3namespace Phalcon\Test\Unit\Forms\Element;
4
5use Phalcon\Forms\Element\Text;
6use Phalcon\Tag;
7use Phalcon\Test\Module\UnitTest;
8
9/**
10 * \Phalcon\Test\Unit\Forms\Element\TextTest
11 * Tests the \Phalcon\Test\Unit\Forms\Element\Text component
12 *
13 * @copyright (c) 2011-2017 Phalcon Team
14 * @link      http://www.phalconphp.com
15 * @author    Andres Gutierrez <andres@phalconphp.com>
16 * @author    Serghei Iakovlev <serghei@phalconphp.com>
17 * @package   Phalcon\Test\Unit\Forms\Element
18 *
19 * The contents of this file are subject to the New BSD License that is
20 * bundled with this package in the file LICENSE.txt
21 *
22 * If you did not receive a copy of the license and are unable to obtain it
23 * through the world-wide-web, please send an email to license@phalconphp.com
24 * so that we can send you a copy immediately.
25 */
26class TextTest extends UnitTest
27{
28    /**
29     * executed after each test
30     */
31    protected function _after()
32    {
33        // Setting the doctype to XHTML5 for other tests to run smoothly
34        Tag::setDocType(Tag::XHTML5);
35    }
36
37    /**
38     * Tests Text::render
39     *
40     * @issue  https://github.com/phalcon/cphalcon/issues/10398
41     * @author Serghei Iakovlev <serghei@phalconphp.com>
42     * @since  2016-07-17
43     */
44    public function testCreatingTextElementWithNameSimilarToTheFormMethods()
45    {
46        $this->specify('Text::render does not return expected result', function ($name) {
47            $element = new Text($name);
48
49            expect($element->getName())->equals($name);
50            expect($element->render())->equals(sprintf('<input type="text" id="%s" name="%s" />', $name, $name));
51            expect($element->getValue())->null();
52        }, ['examples' => $this->nameLikeFormMethodsProvider()]);
53    }
54
55    protected function nameLikeFormMethodsProvider()
56    {
57        return [
58            ['validation'],
59            ['action'],
60            ['useroption'],
61            ['useroptions'],
62            ['entity'],
63            ['elements'],
64            ['messages'],
65            ['messagesfor'],
66            ['label'],
67            ['value'],
68            ['di'],
69            ['eventsmanager'],
70        ];
71    }
72
73    public function testIssue1210()
74    {
75        $this->specify(
76            "Labels are not properly rendered",
77            function () {
78                $element = new Text("test");
79
80                $element->setLabel("Test");
81
82                $actual   = $element->label();
83                $expected = '<label for="test">Test</label>';
84
85                expect($actual)->equals($expected);
86            }
87        );
88    }
89
90    public function testIssue2045()
91    {
92        $this->specify(
93            "Attributes are not properly rendered",
94            function () {
95                $element = new Text("name");
96
97                $element->setAttributes(
98                    [
99                        "class" => "big-input",
100                    ]
101                );
102
103                $element->setAttribute("id", null);
104
105                $expected = '<input type="text" name="name" class="big-input" />';
106
107                expect($element->render())->equals($expected);
108            }
109        );
110    }
111
112    public function testPrepareAttributesNoDefault()
113    {
114        $this->specify(
115            "Prepared attributes are not properly rendered",
116            function () {
117                $element1 = new Text("name");
118
119                $element1->setLabel("name");
120
121                $actual = $element1->prepareAttributes(
122                    [
123                        "class" => "big-input",
124                    ]
125                );
126
127                $expected = [
128                    "name",
129                    "class" => "big-input",
130                ];
131
132                expect($actual)->equals($expected);
133            }
134        );
135    }
136
137    public function testFormElementEmpty()
138    {
139        $this->specify(
140            "Default/empty values are not set properly",
141            function () {
142                $element = new Text("name");
143
144                expect($element->getLabel())->null();
145                expect($element->getAttributes())->equals([]);
146            }
147        );
148    }
149
150    public function testFormElement()
151    {
152        $this->specify(
153            "Form elements do not store attributes/labels properly",
154            function () {
155                $element = new Text("name");
156
157                $element->setLabel('name');
158                $element->setAttributes(array('class' => 'big-input'));
159                $element->setAttribute('placeholder', 'Type the name');
160
161                expect($element->getLabel())->equals('name');
162                expect($element->getAttributes())->equals(array(
163                    'class' => 'big-input',
164                    'placeholder' => 'Type the name'
165                ));
166
167                expect($element->getAttribute('class'))->equals('big-input');
168                expect($element->getAttribute('placeholder', 'the name'))->equals('Type the name');
169                expect($element->getAttribute('lang', 'en'))->equals('en');
170
171                $element->setLabel(0);
172                expect($element->label())->equals('<label for="name">0</label>');
173            }
174        );
175    }
176
177    public function testFormPrepareAttributes()
178    {
179        $this->specify(
180            "Attributes are not prepared properly",
181            function () {
182                $element1 = new Text("name");
183
184                $element1->setLabel('name');
185
186                expect($element1->prepareAttributes())->equals(['name']);
187            }
188        );
189    }
190
191    public function testFormPrepareAttributesDefault()
192    {
193        $this->specify(
194            "Attributes are not prepared properly (2)",
195            function () {
196                $element1 = new Text("name");
197
198                $element1->setLabel('name');
199                $element1->setAttributes(['class' => 'big-input']);
200
201                expect($element1->prepareAttributes())->equals(['name', 'class' => 'big-input']);
202            }
203        );
204    }
205
206    public function testFormOptions()
207    {
208        $this->specify(
209            "Text elements don't properly store user options or attributes",
210            function () {
211                $element1 = new Text("name");
212
213                $element1->setAttributes(array('class' => 'big-input'));
214                $element1->setUserOptions(array('some' => 'value'));
215
216                expect($element1->getUserOptions())->equals(array('some' => 'value'));
217
218                expect($element1->getUserOption('some'))->equals('value');
219
220                expect($element1->getUserOption('some-non'))->null();
221
222                expect($element1->getUserOption('some-non', 'default'))->equals('default');
223            }
224        );
225    }
226}
227