| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @since CakePHP(tm) v3.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Input;
- use Cake\Collection\Collection;
- use Cake\TestSuite\TestCase;
- use Cake\View\Input\Radio;
- use Cake\View\StringTemplate;
- /**
- * Radio test case
- */
- class RadioTest extends TestCase {
- /**
- * setup method.
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $templates = [
- 'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
- 'label' => '<label{{attrs}}>{{text}}</label>',
- 'radioContainer' => '{{input}}{{label}}',
- ];
- $this->templates = new StringTemplate($templates);
- }
- /**
- * Test rendering basic radio buttons.
- *
- * @return void
- */
- public function testRenderSimple() {
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Crayons[color]',
- 'options' => ['r' => 'Red', 'b' => 'Black']
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'r',
- 'id' => 'crayons-color-r'
- ]],
- ['label' => ['for' => 'crayons-color-r']],
- 'Red',
- '/label',
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'b',
- 'id' => 'crayons-color-b'
- ]],
- ['label' => ['for' => 'crayons-color-b']],
- 'Black',
- '/label',
- ];
- $this->assertTags($result, $expected);
- $data = [
- 'name' => 'Crayons[color]',
- 'options' => new Collection(['r' => 'Red', 'b' => 'Black'])
- ];
- $result = $radio->render($data);
- $this->assertTags($result, $expected);
- }
- /**
- * Test rendering inputs with the complex option form.
- *
- * @return void
- */
- public function testRenderComplex() {
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Crayons[color]',
- 'options' => [
- ['value' => 'r', 'text' => 'Red', 'id' => 'my_id'],
- ['value' => 'b', 'text' => 'Black', 'id' => 'my_id_2', 'data-test' => 'test'],
- ]
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'r',
- 'id' => 'my_id'
- ]],
- ['label' => ['for' => 'my_id']],
- 'Red',
- '/label',
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'b',
- 'id' => 'my_id_2',
- 'data-test' => 'test'
- ]],
- ['label' => ['for' => 'my_id_2']],
- 'Black',
- '/label',
- ];
- $this->assertTags($result, $expected);
- }
- /**
- * Test rendering the empty option.
- *
- * @return void
- */
- public function testRenderEmptyOption() {
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Crayons[color]',
- 'options' => ['r' => 'Red'],
- 'empty' => true,
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => '',
- 'id' => 'crayons-color'
- ]],
- ['label' => ['for' => 'crayons-color']],
- 'empty',
- '/label',
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'r',
- 'id' => 'crayons-color-r'
- ]],
- ['label' => ['for' => 'crayons-color-r']],
- 'Red',
- '/label',
- ];
- $this->assertTags($result, $expected);
- $data['empty'] = 'Choose one';
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => '',
- 'id' => 'crayons-color'
- ]],
- ['label' => ['for' => 'crayons-color']],
- 'Choose one',
- '/label',
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'r',
- 'id' => 'crayons-color-r'
- ]],
- ['label' => ['for' => 'crayons-color-r']],
- 'Red',
- '/label',
- ];
- $this->assertTags($result, $expected);
- }
- /**
- * Test rendering the input inside the label.
- *
- * @return void
- */
- public function testRenderInputInsideLabel() {
- $this->templates->add([
- 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
- 'radioContainer' => '{{label}}',
- ]);
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Crayons[color]',
- 'options' => ['r' => 'Red'],
- ];
- $result = $radio->render($data);
- $expected = [
- ['label' => ['for' => 'crayons-color-r']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Crayons[color]',
- 'value' => 'r',
- 'id' => 'crayons-color-r'
- ]],
- 'Red',
- '/label',
- ];
- $this->assertTags($result, $expected);
- }
- /**
- * test render() and selected inputs.
- *
- * @return void
- */
- public function testRenderSelected() {
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Versions[ver]',
- 'val' => '1',
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- ]
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'id' => 'versions-ver-1',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1',
- 'checked' => 'checked'
- ]],
- ['label' => ['for' => 'versions-ver-1']],
- 'one',
- '/label',
- ['input' => [
- 'id' => 'versions-ver-1x',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1x'
- ]],
- ['label' => ['for' => 'versions-ver-1x']],
- 'one x',
- '/label',
- ['input' => [
- 'id' => 'versions-ver-2',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '2'
- ]],
- ['label' => ['for' => 'versions-ver-2']],
- 'two',
- '/label',
- ];
- $this->assertTags($result, $expected);
- }
- /**
- * Test rendering with disable inputs
- *
- * @return void
- */
- public function testRenderDisabled() {
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Versions[ver]',
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- ],
- 'disabled' => true,
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'id' => 'versions-ver-1',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1',
- 'disabled' => 'disabled'
- ]],
- ['label' => ['for' => 'versions-ver-1']],
- 'one',
- '/label',
- ['input' => [
- 'id' => 'versions-ver-1x',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1x',
- 'disabled' => 'disabled'
- ]],
- ['label' => ['for' => 'versions-ver-1x']],
- 'one x',
- '/label',
- ];
- $this->assertTags($result, $expected);
- $data['disabled'] = ['1'];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'id' => 'versions-ver-1',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1',
- 'disabled' => 'disabled'
- ]],
- ['label' => ['for' => 'versions-ver-1']],
- 'one',
- '/label',
- ['input' => [
- 'id' => 'versions-ver-1x',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1x',
- ]],
- ['label' => ['for' => 'versions-ver-1x']],
- 'one x',
- '/label',
- ];
- $this->assertTags($result, $expected);
- }
- /**
- * Test rendering with label options.
- *
- * @return void
- */
- public function testRenderLabelOptions() {
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Versions[ver]',
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- ],
- 'label' => false,
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'id' => 'versions-ver-1',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1',
- ]],
- ['input' => [
- 'id' => 'versions-ver-1x',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1x',
- ]],
- ];
- $this->assertTags($result, $expected);
- $data = [
- 'name' => 'Versions[ver]',
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- ],
- 'label' => [
- 'class' => 'my-class',
- ]
- ];
- $result = $radio->render($data);
- $expected = [
- ['input' => [
- 'id' => 'versions-ver-1',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1',
- ]],
- ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1']],
- 'one',
- '/label',
- ['input' => [
- 'id' => 'versions-ver-1x',
- 'name' => 'Versions[ver]',
- 'type' => 'radio',
- 'value' => '1x',
- ]],
- ['label' => ['class' => 'my-class', 'for' => 'versions-ver-1x']],
- 'one x',
- '/label',
- ];
- $this->assertTags($result, $expected);
- }
- /**
- * Ensure that the input + label are composed with
- * a template.
- *
- * @return void
- */
- public function testRenderContainerTemplate() {
- $this->templates->add([
- 'radioContainer' => '<div class="radio">{{input}}{{label}}</div>'
- ]);
- $radio = new Radio($this->templates);
- $data = [
- 'name' => 'Versions[ver]',
- 'options' => [
- 1 => 'one',
- '1x' => 'one x',
- '2' => 'two',
- ],
- ];
- $result = $radio->render($data);
- $this->assertContains(
- '<div class="radio"><input type="radio"',
- $result
- );
- $this->assertContains(
- '</label></div>',
- $result
- );
- }
- }
|