view = new Zend_View(); $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('#.*?' . $value . '.*?#', $html, $html); $this->assertRegexp('#.*?#', $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('#.*?#', $html, $html); } $html = $this->helper->formRadio(array( 'name' => 'foo', 'value' => 'bar', 'options' => $options, 'attribs' => array('labelPlacement' => 'prepend') )); foreach ($options as $key => $value) { $this->assertRegexp('#' . $value . '#', $html, $html); } } /** * @see 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('#]*(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('#]*?class="testclass"[^>]*>.*?' . $value . '#', $html, $html); $this->assertRegexp('#]*?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('/]*?(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('/]*?(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('/]*?(value="' . $test . '")[^>]*?(disabled="disabled")/', $html, $html); } $this->assertNotRegexp('/]*?(value="bar")[^>]*?(disabled="disabled")/', $html, $html); $count = substr_count($html, 'disabled="disabled"'); $this->assertEquals(2, $count); } public function testLabelsAreEscapedByDefault() { $options = array( 'bar' => 'Bar', ); $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' => 'Bar', ); $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('/]*?(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('/(]*?(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('/(]*?(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('/]*)(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('/]*)(id="' . $id . '")/', $html); } } /** * @issue 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); } public function testRadioLabelContainsForAttributeTag() { $options = array( 'foo bar' => 'Foo', 'bar baz' => 'Bar', 'baz' => 'Baz' ); $html = $this->helper->formRadio(array( 'name' => 'foo[bar]', 'value' => '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('/]*)(for="' . $id . '")/', $html); } } } // 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(); }