| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_View
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- // Call Zend_View_Helper_FormRadioTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormRadioTest::main");
- }
- require_once 'Zend/View/Helper/FormRadio.php';
- require_once 'Zend/View.php';
- /**
- * Zend_View_Helper_FormRadioTest
- *
- * Tests formRadio helper
- *
- * @category Zend
- * @package Zend_View
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_View
- * @group Zend_View_Helper
- */
- class Zend_View_Helper_FormRadioTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Runs the test methods of this class.
- *
- * @access public
- * @static
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormRadioTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- public function setUp()
- {
- $this->view = new Zend_View();
- $this->view->doctype('HTML4_LOOSE'); // Set default doctype
- $this->helper = new Zend_View_Helper_FormRadio();
- $this->helper->setView($this->view);
- }
- public function testRendersRadioLabelsWhenRenderingMultipleOptions()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- ));
- foreach ($options as $key => $value) {
- $this->assertRegexp('#<label.*?>.*?' . $value . '.*?</label>#', $html, $html);
- $this->assertRegexp('#<label.*?>.*?<input.*?</label>#', $html, $html);
- }
- }
- public function testCanSpecifyRadioLabelPlacement()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array('labelPlacement' => 'append')
- ));
- foreach ($options as $key => $value) {
- $this->assertRegexp('#<label.*?>.*?<input .*?' . $value . '</label>#', $html, $html);
- }
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array('labelPlacement' => 'prepend')
- ));
- foreach ($options as $key => $value) {
- $this->assertRegexp('#<label.*?>' . $value . '<input .*?</label>#', $html, $html);
- }
- }
- /**
- * @group ZF-3206
- */
- public function testSpecifyingLabelPlacementShouldNotOverwriteValue()
- {
- $options = array(
- 'bar' => 'Bar',
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array(
- 'labelPlacement' => 'append',
- )
- ));
- $this->assertRegexp('#<input[^>]*(checked="checked")#', $html, $html);
- }
- public function testCanSpecifyRadioLabelAttribs()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array('labelClass' => 'testclass', 'label_id' => 'testid')
- ));
- foreach ($options as $key => $value) {
- $this->assertRegexp('#<label[^>]*?class="testclass"[^>]*>.*?' . $value . '#', $html, $html);
- $this->assertRegexp('#<label[^>]*?id="testid"[^>]*>.*?' . $value . '#', $html, $html);
- }
- }
- public function testCanSpecifyRadioSeparator()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'listsep' => '--FunkySep--',
- ));
- $this->assertContains('--FunkySep--', $html);
- $count = substr_count($html, '--FunkySep--');
- $this->assertEquals(2, $count);
- }
- /**
- * ZF-2513
- */
- public function testCanDisableAllRadios()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array('disable' => true)
- ));
- $this->assertRegexp('/<input[^>]*?(disabled="disabled")/', $html, $html);
- $count = substr_count($html, 'disabled="disabled"');
- $this->assertEquals(3, $count);
- }
- /**
- * ZF-2513
- */
- public function testCanDisableIndividualRadios()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array('disable' => array('bar'))
- ));
- $this->assertRegexp('/<input[^>]*?(value="bar")[^>]*(disabled="disabled")/', $html, $html);
- $count = substr_count($html, 'disabled="disabled"');
- $this->assertEquals(1, $count);
- }
- /**
- * ZF-2513
- */
- public function testCanDisableMultipleRadios()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- 'attribs' => array('disable' => array('foo', 'baz'))
- ));
- foreach (array('foo', 'baz') as $test) {
- $this->assertRegexp('/<input[^>]*?(value="' . $test . '")[^>]*?(disabled="disabled")/', $html, $html);
- }
- $this->assertNotRegexp('/<input[^>]*?(value="bar")[^>]*?(disabled="disabled")/', $html, $html);
- $count = substr_count($html, 'disabled="disabled"');
- $this->assertEquals(2, $count);
- }
- public function testLabelsAreEscapedByDefault()
- {
- $options = array(
- 'bar' => '<b>Bar</b>',
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'options' => $options,
- ));
- $this->assertNotContains($options['bar'], $html);
- $this->assertContains('<b>Bar</b>', $html);
- }
- public function testXhtmlLabelsAreAllowed()
- {
- $options = array(
- 'bar' => '<b>Bar</b>',
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'options' => $options,
- 'attribs' => array('escape' => false)
- ));
- $this->assertContains($options['bar'], $html);
- }
- /**
- * ZF-1666
- */
- public function testDoesNotRenderHiddenElements()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'options' => $options,
- ));
- $this->assertNotRegexp('/<input[^>]*?(type="hidden")/', $html);
- }
- public function testSpecifyingAValueThatMatchesAnOptionChecksIt()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- ));
- if (!preg_match('/(<input[^>]*?(value="bar")[^>]*>)/', $html, $matches)) {
- $this->fail('Radio for a given option was not found?');
- }
- $this->assertContains('checked="checked"', $matches[1], var_export($matches, 1));
- }
- public function testOptionsWithMatchesInAnArrayOfValuesAreChecked()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => array('foo', 'baz'),
- 'options' => $options,
- ));
- foreach (array('foo', 'baz') as $value) {
- if (!preg_match('/(<input[^>]*?(value="' . $value . '")[^>]*>)/', $html, $matches)) {
- $this->fail('Radio for a given option was not found?');
- }
- $this->assertContains('checked="checked"', $matches[1], var_export($matches, 1));
- }
- }
- public function testEachRadioShouldHaveIdCreatedByAppendingFilteredValue()
- {
- $options = array(
- 'foo bar' => 'Foo',
- 'bar baz' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo[]',
- 'value' => 'bar',
- 'options' => $options,
- ));
- require_once 'Zend/Filter/Alnum.php';
- $filter = new Zend_Filter_Alnum();
- foreach ($options as $key => $value) {
- $id = 'foo-' . $filter->filter($key);
- $this->assertRegexp('/<input([^>]*)(id="' . $id . '")/', $html);
- }
- }
- public function testEachRadioShouldUseAttributeIdWhenSpecified()
- {
- $options = array(
- 'foo bar' => 'Foo',
- 'bar baz' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo[bar]',
- 'value' => 'bar',
- 'attribs' => array('id' => 'foo-bar'),
- 'options' => $options,
- ));
- require_once 'Zend/Filter/Alnum.php';
- $filter = new Zend_Filter_Alnum();
- foreach ($options as $key => $value) {
- $id = 'foo-bar-' . $filter->filter($key);
- $this->assertRegexp('/<input([^>]*)(id="' . $id . '")/', $html);
- }
- }
- /**
- * @group ZF-5681
- */
- public function testRadioLabelDoesNotContainHardCodedStyle()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- ));
- $this->assertNotContains('style="white-space: nowrap;"', $html);
- }
- /**
- * @group ZF-8709
- */
- public function testRadioLabelContainsNotForAttributeTag()
- {
- $actual = $this->helper->formRadio(
- array(
- 'name' => 'foo',
- 'options' => array(
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- ),
- )
- );
- $expected = '<label><input type="radio" name="foo" id="foo-bar" value="bar">Bar</label><br>'
- . "\n"
- . '<label><input type="radio" name="foo" id="foo-baz" value="baz">Baz</label>';
- $this->assertSame($expected, $actual);
- }
-
- /**
- * @group ZF-4191
- */
- public function testDashesShouldNotBeFilteredFromId()
- {
- $name = "Foo";
- $options = array(
- -1 => 'Test -1',
- 0 => 'Test 0',
- 1 => 'Test 1'
- );
-
- $formRadio = new Zend_View_Helper_FormRadio();
- $formRadio->setView(new Zend_View());
- $html = $formRadio->formRadio($name, -1, null, $options);
- foreach ( $options as $key=>$value ) {
- $fid = "{$name}-{$key}";
- $this->assertRegExp('/<input([^>]*)(id="'.$fid.'")/', $html);
- }
-
- // Assert that radio for value -1 is the selected one
- $this->assertRegExp('/<input([^>]*)(id="'.$name.'--1")([^>]*)(checked="checked")/', $html);
- }
-
- /**
- * @group ZF-11477
- */
- public function testRendersAsHtmlByDefault()
- {
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'options' => $options,
- ));
- $this->assertContains('value="foo">', $html);
- $this->assertContains('value="bar">', $html);
- $this->assertContains('value="baz">', $html);
- }
- /**
- * @group ZF-11477
- */
- public function testCanRendersAsXHtml()
- {
- $this->view->doctype('XHTML1_STRICT');
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'options' => $options,
- ));
- $this->assertContains('value="foo" />', $html);
- $this->assertContains('value="bar" />', $html);
- $this->assertContains('value="baz" />', $html);
- }
- /**
- * @group ZF-11620
- */
- public function testSeparatorCanRendersAsXhtmlByDefault()
- {
- $this->view->doctype('XHTML1_STRICT');
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- ));
-
- $this->assertContains('<br />', $html);
- $count = substr_count($html, '<br />');
- $this->assertEquals(2, $count);
- }
-
- /**
- * @group ZF-11620
- */
- public function testeparatorCanRendersAsHtml()
- {
- $this->view->doctype('HTML4_STRICT');
- $options = array(
- 'foo' => 'Foo',
- 'bar' => 'Bar',
- 'baz' => 'Baz'
- );
- $html = $this->helper->formRadio(array(
- 'name' => 'foo',
- 'value' => 'bar',
- 'options' => $options,
- ));
-
- $this->assertContains('<br>', $html);
- $count = substr_count($html, '<br>');
- $this->assertEquals(2, $count);
- }
- }
- // Call Zend_View_Helper_FormRadioTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormRadioTest::main") {
- Zend_View_Helper_FormRadioTest::main();
- }
|