123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
- /**
- * Tests KO7 Form helper
- *
- * @group ko7
- * @group ko7.core
- * @group ko7.core.form
- *
- * @package KO7
- * @category Tests
- *
- * @author Jeremy Bush <contractfrombelow@gmail.com>
- * @copyright (c) 2007-2016 Kohana Team
- * @copyright (c) since 2016 Koseven Team
- * @license https://koseven.dev/LICENSE
- */
- class KO7_FormTest extends Unittest_TestCase {
- /**
- * Defaults for this test
- * @var array
- */
- // @codingStandardsIgnoreStart
- protected $environmentDefault = [
- 'KO7::$base_url' => '/',
- 'HTTP_HOST' => 'koseven.dev',
- 'KO7::$index_file' => '',
- ];
- // @codingStandardsIgnoreEnd
- /**
- * Provides test data for test_open()
- *
- * @return array
- */
- public function provider_open()
- {
- return [
- [
- ['', NULL],
- ['action' => '']
- ],
- [
- [NULL, NULL],
- ['action' => '']
- ],
- [
- ['foo', NULL],
- ['action' => '/foo']
- ],
- [
- ['foo', ['method' => 'get']],
- ['action' => '/foo', 'method' => 'get']
- ],
- [
- ['//www.example.com/', NULL],
- ['action' => '//www.example.com/']
- ],
- ];
- }
- /**
- * Tests Form::open()
- *
- * @test
- * @dataProvider provider_open
- * @param boolean $input Input for Form::open
- * @param boolean $expected Output for Form::open
- */
- public function test_open($input, $expected)
- {
- list($action, $attributes) = $input;
- $tag = Form::open($action, $attributes);
- $matcher = [
- 'tag' => 'form',
- // Default attributes
- 'attributes' => [
- 'method' => 'post',
- 'accept-charset' => 'utf-8',
- ],
- ];
- $matcher['attributes'] = $expected + $matcher['attributes'];
- $this->assertTag($matcher, $tag);
- }
- /**
- * Tests Form::close()
- *
- * @test
- */
- public function test_close()
- {
- $this->assertSame('</form>', Form::close());
- }
- /**
- * Provides test data for test_input()
- *
- * @return array
- */
- public function provider_input()
- {
- return [
- // $value, $result
- ['input', 'foo', 'bar', NULL, 'input'],
- ['input', 'foo', NULL, NULL, 'input'],
- ['hidden', 'foo', 'bar', NULL, 'hidden'],
- ['password', 'foo', 'bar', NULL, 'password'],
- ];
- }
- /**
- * Tests Form::input()
- *
- * @test
- * @dataProvider provider_input
- * @param boolean $input Input for Form::input
- * @param boolean $expected Output for Form::input
- */
- public function test_input($type, $name, $value, $attributes)
- {
- $matcher = [
- 'tag' => 'input',
- 'attributes' => ['name' => $name, 'type' => $type]
- ];
- // Form::input creates a text input
- if ($type === 'input')
- {
- $matcher['attributes']['type'] = 'text';
- }
- // NULL just means no value
- if ($value !== NULL)
- {
- $matcher['attributes']['value'] = $value;
- }
- // Add on any attributes
- if (is_array($attributes))
- {
- $matcher['attributes'] = $attributes + $matcher['attributes'];
- }
- $tag = Form::$type($name, $value, $attributes);
- $this->assertTag($matcher, $tag, $tag);
- }
- /**
- * Provides test data for test_file()
- *
- * @return array
- */
- public function provider_file()
- {
- return [
- // $value, $result
- ['foo', NULL, '<input type="file" name="foo" />'],
- ];
- }
- /**
- * Tests Form::file()
- *
- * @test
- * @dataProvider provider_file
- * @param boolean $input Input for Form::file
- * @param boolean $expected Output for Form::file
- */
- public function test_file($name, $attributes, $expected)
- {
- $this->assertSame($expected, Form::file($name, $attributes));
- }
- /**
- * Provides test data for test_check()
- *
- * @return array
- */
- public function provider_check()
- {
- return [
- // $value, $result
- ['checkbox', 'foo', NULL, FALSE, NULL],
- ['checkbox', 'foo', NULL, TRUE, NULL],
- ['checkbox', 'foo', 'bar', TRUE, NULL],
- ['radio', 'foo', NULL, FALSE, NULL],
- ['radio', 'foo', NULL, TRUE, NULL],
- ['radio', 'foo', 'bar', TRUE, NULL],
- ];
- }
- /**
- * Tests Form::check()
- *
- * @test
- * @dataProvider provider_check
- * @param boolean $input Input for Form::check
- * @param boolean $expected Output for Form::check
- */
- public function test_check($type, $name, $value, $checked, $attributes)
- {
- $matcher = ['tag' => 'input', 'attributes' => ['name' => $name, 'type' => $type]];
- if ($value !== NULL)
- {
- $matcher['attributes']['value'] = $value;
- }
- if (is_array($attributes))
- {
- $matcher['attributes'] = $attributes + $matcher['attributes'];
- }
- if ($checked === TRUE)
- {
- $matcher['attributes']['checked'] = 'checked';
- }
- $tag = Form::$type($name, $value, $checked, $attributes);
- $this->assertTag($matcher, $tag, $tag);
- }
- /**
- * Provides test data for test_text()
- *
- * @return array
- */
- public function provider_text()
- {
- return [
- // $value, $result
- ['textarea', 'foo', 'bar', NULL],
- ['textarea', 'foo', 'bar', ['rows' => 20, 'cols' => 20]],
- ['button', 'foo', 'bar', NULL],
- ['label', 'foo', 'bar', NULL],
- ['label', 'foo', NULL, NULL],
- ];
- }
- /**
- * Tests Form::textarea()
- *
- * @test
- * @dataProvider provider_text
- * @param boolean $input Input for Form::textarea
- * @param boolean $expected Output for Form::textarea
- */
- public function test_text($type, $name, $body, $attributes)
- {
- $matcher = [
- 'tag' => $type,
- 'attributes' => [],
- 'content' => $body,
- ];
- if ($type !== 'label')
- {
- $matcher['attributes'] = ['name' => $name];
- }
- else
- {
- $matcher['attributes'] = ['for' => $name];
- }
- if (is_array($attributes))
- {
- $matcher['attributes'] = $attributes + $matcher['attributes'];
- }
- $tag = Form::$type($name, $body, $attributes);
- $this->assertTag($matcher, $tag, $tag);
- }
- /**
- * Provides test data for test_select()
- *
- * @return array
- */
- public function provider_select()
- {
- return [
- // $value, $result
- ['foo', NULL, NULL, "<select name=\"foo\"></select>"],
- ['foo', ['bar' => 'bar'], NULL, "<select name=\"foo\">\n<option value=\"bar\">bar</option>\n</select>"],
- ['foo', ['bar' => 'bar'], 'bar', "<select name=\"foo\">\n<option value=\"bar\" selected=\"selected\">bar</option>\n</select>"],
- ['foo', ['bar' => ['foo' => 'bar']], NULL, "<select name=\"foo\">\n<optgroup label=\"bar\">\n<option value=\"foo\">bar</option>\n</optgroup>\n</select>"],
- ['foo', ['bar' => ['foo' => 'bar']], 'foo', "<select name=\"foo\">\n<optgroup label=\"bar\">\n<option value=\"foo\" selected=\"selected\">bar</option>\n</optgroup>\n</select>"],
- // #2286
- ['foo', ['bar' => 'bar', 'unit' => 'test', 'foo' => 'foo'], ['bar', 'foo'], "<select name=\"foo\" multiple=\"multiple\">\n<option value=\"bar\" selected=\"selected\">bar</option>\n<option value=\"unit\">test</option>\n<option value=\"foo\" selected=\"selected\">foo</option>\n</select>"],
- ];
- }
- /**
- * Tests Form::select()
- *
- * @test
- * @dataProvider provider_select
- * @param boolean $input Input for Form::select
- * @param boolean $expected Output for Form::select
- */
- public function test_select($name, $options, $selected, $expected)
- {
- // Much more efficient just to assertSame() rather than assertTag() on each element
- $this->assertSame($expected, Form::select($name, $options, $selected));
- }
- /**
- * Provides test data for test_submit()
- *
- * @return array
- */
- public function provider_submit()
- {
- return [
- // $value, $result
- ['foo', 'Foobar!', '<input type="submit" name="foo" value="Foobar!" />'],
- ];
- }
- /**
- * Tests Form::submit()
- *
- * @test
- * @dataProvider provider_submit
- * @param boolean $input Input for Form::submit
- * @param boolean $expected Output for Form::submit
- */
- public function test_submit($name, $value, $expected)
- {
- $matcher = [
- 'tag' => 'input',
- 'attributes' => ['name' => $name, 'type' => 'submit', 'value' => $value]
- ];
- $this->assertTag($matcher, Form::submit($name, $value));
- }
- /**
- * Provides test data for test_image()
- *
- * @return array
- */
- public function provider_image()
- {
- return [
- // $value, $result
- ['foo', 'bar', ['src' => 'media/img/login.png'], '<input type="image" name="foo" value="bar" src="/media/img/login.png" />'],
- ];
- }
- /**
- * Tests Form::image()
- *
- * @test
- * @dataProvider provider_image
- * @param boolean $name Input for Form::image
- * @param boolean $value Input for Form::image
- * @param boolean $attributes Input for Form::image
- * @param boolean $expected Output for Form::image
- */
- public function test_image($name, $value, $attributes, $expected)
- {
- $this->assertSame($expected, Form::image($name, $value, $attributes));
- }
- /**
- * Provides test data for test_label()
- *
- * @return array
- */
- function provider_label()
- {
- return [
- // $value, $result
- // Single for provided
- ['email', NULL, NULL, '<label for="email">Email</label>'],
- ['email_address', NULL, NULL, '<label for="email_address">Email Address</label>'],
- ['email-address', NULL, NULL, '<label for="email-address">Email Address</label>'],
- // For and text values provided
- ['name', 'First name', NULL, '<label for="name">First name</label>'],
- // with attributes
- ['lastname', 'Last name', ['class' => 'text'], '<label class="text" for="lastname">Last name</label>'],
- ['lastname', 'Last name', ['class' => 'text', 'id'=>'txt_lastname'], '<label id="txt_lastname" class="text" for="lastname">Last name</label>'],
- ];
- }
- /**
- * Tests Form::label()
- *
- * @test
- * @dataProvider provider_label
- * @param boolean $for Input for Form::label
- * @param boolean $text Input for Form::label
- * @param boolean $attributes Input for Form::label
- * @param boolean $expected Output for Form::label
- */
- function test_label($for, $text, $attributes, $expected)
- {
- $this->assertSame($expected, Form::label($for, $text, $attributes));
- }
- }
|