| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280 |
- <?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_Form
- * @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$
- */
- if (!defined('PHPUnit_MAIN_METHOD')) {
- define('PHPUnit_MAIN_METHOD', 'Zend_Form_ElementTest::main');
- }
- require_once 'Zend/Form/Element.php';
- require_once 'Zend/Config.php';
- require_once 'Zend/Controller/Action/HelperBroker.php';
- require_once 'Zend/Form.php';
- require_once 'Zend/Form/Decorator/Abstract.php';
- require_once 'Zend/Form/Decorator/HtmlTag.php';
- require_once 'Zend/Loader/PluginLoader.php';
- require_once 'Zend/Registry.php';
- require_once 'Zend/Translate.php';
- require_once 'Zend/Validate/NotEmpty.php';
- require_once 'Zend/Validate/EmailAddress.php';
- require_once 'Zend/View.php';
- /**
- * @category Zend
- * @package Zend_Form
- * @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_Form
- */
- class Zend_Form_ElementTest extends PHPUnit_Framework_TestCase
- {
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite('Zend_Form_ElementTest');
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- public function setUp()
- {
- Zend_Registry::_unsetInstance();
- Zend_Form::setDefaultTranslator(null);
- if (isset($this->error)) {
- unset($this->error);
- }
- $this->element = new Zend_Form_Element('foo');
- Zend_Controller_Action_HelperBroker::resetHelpers();
- }
- public function tearDown()
- {
- }
- public function getView()
- {
- $view = new Zend_View();
- $libPath = dirname(__FILE__) . '/../../../library';
- $view->addHelperPath($libPath . '/Zend/View/Helper');
- return $view;
- }
- public function testConstructorRequiresMinimallyElementName()
- {
- try {
- $element = new Zend_Form_Element(1);
- $this->fail('Zend_Form_Element constructor should not accept integer argument');
- } catch (Zend_Form_Exception $e) {
- }
- try {
- $element = new Zend_Form_Element(true);
- $this->fail('Zend_Form_Element constructor should not accept boolean argument');
- } catch (Zend_Form_Exception $e) {
- }
- try {
- $element = new Zend_Form_Element('foo');
- } catch (Exception $e) {
- $this->fail('Zend_Form_Element constructor should accept String values');
- }
- $config = array('foo' => 'bar');
- try {
- $element = new Zend_Form_Element($config);
- $this->fail('Zend_Form_Element constructor requires array with name element');
- } catch (Zend_Form_Exception $e) {
- }
- $config = array('name' => 'bar');
- try {
- $element = new Zend_Form_Element($config);
- } catch (Zend_Form_Exception $e) {
- $this->fail('Zend_Form_Element constructor should accept array with name element');
- }
- $config = new Zend_Config(array('foo' => 'bar'));
- try {
- $element = new Zend_Form_Element($config);
- $this->fail('Zend_Form_Element constructor requires Zend_Config object with name element');
- } catch (Zend_Form_Exception $e) {
- }
- $config = new Zend_Config(array('name' => 'bar'));
- try {
- $element = new Zend_Form_Element($config);
- } catch (Zend_Form_Exception $e) {
- $this->fail('Zend_Form_Element constructor should accept Zend_Config with name element');
- }
- }
- public function testNoTranslatorByDefault()
- {
- $this->assertNull($this->element->getTranslator());
- }
- public function testGetTranslatorRetrievesGlobalDefaultWhenAvailable()
- {
- $this->testNoTranslatorByDefault();
- $translator = new Zend_Translate('array', array('foo' => 'bar'));
- Zend_Form::setDefaultTranslator($translator);
- $received = $this->element->getTranslator();
- $this->assertSame($translator->getAdapter(), $received);
- }
- public function testTranslatorAccessorsWork()
- {
- $translator = new Zend_Translate('array', array('foo' => 'bar'));
- $this->element->setTranslator($translator);
- $received = $this->element->getTranslator($translator);
- $this->assertSame($translator->getAdapter(), $received);
- }
- public function testCanDisableTranslation()
- {
- $this->testGetTranslatorRetrievesGlobalDefaultWhenAvailable();
- $this->element->setDisableTranslator(true);
- $this->assertNull($this->element->getTranslator());
- }
- public function testSetNameNormalizesValueToContainOnlyValidVariableCharacters()
- {
- $this->element->setName('f%\o^&*)o\(%$b#@!.a}{;-,r');
- $this->assertEquals('foobar', $this->element->getName());
- try {
- $this->element->setName('%\^&*)\(%$#@!.}{;-,');
- $this->fail('Empty names should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid name provided', $e->getMessage());
- }
- }
- public function testZeroIsAllowedAsElementName()
- {
- try {
- $this->element->setName(0);
- $this->assertSame('0', $this->element->getName());
- } catch (Zend_Form_Exception $e) {
- $this->fail('Should allow zero as element name');
- }
- }
- /**
- * @group ZF-2851
- */
- public function testSetNameShouldNotAllowEmptyString()
- {
- foreach (array('', ' ', ' ') as $name) {
- try {
- $this->element->setName($name);
- $this->fail('setName() should not allow empty string');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid name', $e->getMessage());
- }
- }
- }
- public function testElementValueInitiallyNull()
- {
- $this->assertNull($this->element->getValue());
- }
- public function testValueAccessorsWork()
- {
- $this->element->setValue('bar');
- $this->assertContains('bar', $this->element->getValue());
- }
- public function testGetValueFiltersValue()
- {
- $this->element->setValue('This 0 is 1 a-2-TEST')
- ->addFilter('alnum')
- ->addFilter('stringToUpper');
- $test = $this->element->getValue();
- $this->assertEquals('THIS0IS1A2TEST', $test);
- }
- public function checkFilterValues($item, $key)
- {
- $this->assertRegexp('/^[A-Z]+$/', $item);
- }
- public function testRetrievingArrayValueFiltersAllArrayValues()
- {
- $this->element->setValue(array(
- 'foo',
- array(
- 'bar',
- 'baz'
- ),
- 'bat'
- ))
- ->setIsArray(true)
- ->addFilter('StringToUpper');
- $test = $this->element->getValue();
- $this->assertTrue(is_array($test));
- array_walk_recursive($test, array($this, 'checkFilterValues'));
- }
- public function testRetrievingArrayValueDoesNotFilterAllValuesWhenNotIsArray()
- {
- $values = array(
- 'foo',
- array(
- 'bar',
- 'baz'
- ),
- 'bat'
- );
- $this->element->setValue($values)
- ->addFilter(new Zend_Form_ElementTest_ArrayFilter());
- $test = $this->element->getValue();
- $this->assertTrue(is_array($test));
- require_once 'Zend/Json.php';
- $test = Zend_Json::encode($test);
- $this->assertNotContains('foo', $test);
- foreach (array('bar', 'baz', 'bat') as $value) {
- $this->assertContains($value, $test);
- }
- }
- public function testGetUnfilteredValueRetrievesOriginalValue()
- {
- $this->element->setValue('bar');
- $this->assertSame('bar', $this->element->getUnfilteredValue());
- }
- public function testLabelInitiallyNull()
- {
- $this->assertNull($this->element->getLabel());
- }
- public function testLabelAccessorsWork()
- {
- $this->element->setLabel('FooBar');
- $this->assertEquals('FooBar', $this->element->getLabel());
- }
- public function testOrderNullByDefault()
- {
- $this->assertNull($this->element->getOrder());
- }
- public function testCanSetOrder()
- {
- $this->testOrderNullByDefault();
- $this->element->setOrder(50);
- $this->assertEquals(50, $this->element->getOrder());
- }
- public function testRequiredFlagFalseByDefault()
- {
- $this->assertFalse($this->element->isRequired());
- }
- public function testRequiredAcccessorsWork()
- {
- $this->assertFalse($this->element->isRequired());
- $this->element->setRequired(true);
- $this->assertTrue($this->element->isRequired());
- }
- public function testIsValidInsertsNotEmptyValidatorWhenElementIsRequiredByDefault()
- {
- $this->_checkZf2794();
- $this->element->setRequired(true);
- $this->assertFalse($this->element->isValid(''));
- $validator = $this->element->getValidator('NotEmpty');
- $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
- $this->assertTrue($validator->zfBreakChainOnFailure);
- }
- /**
- * @group ZF-2862
- */
- public function testBreakChainOnFailureFlagsForExistingValidatorsRemainSetWhenNotEmptyValidatorAutoInserted()
- {
- $this->_checkZf2794();
- $username = new Zend_Form_Element('username');
- $username->addValidator('stringLength', true, array(5, 20))
- ->addValidator('regex', true, array('/^[a-zA-Z0-9_]*$/'))
- ->addFilter('StringToLower')
- ->setRequired(true);
- $form = new Zend_Form(array('elements' => array($username)));
- $form->isValid(array('username' => '#'));
- $validator = $username->getValidator('stringLength');
- $this->assertTrue($validator->zfBreakChainOnFailure);
- $validator = $username->getValidator('regex');
- $this->assertTrue($validator->zfBreakChainOnFailure);
- }
- public function testAutoInsertNotEmptyValidatorFlagTrueByDefault()
- {
- $this->assertTrue($this->element->autoInsertNotEmptyValidator());
- }
- public function testCanSetAutoInsertNotEmptyValidatorFlag()
- {
- $this->testAutoInsertNotEmptyValidatorFlagTrueByDefault();
- $this->element->setAutoInsertNotEmptyValidator(false);
- $this->assertFalse($this->element->autoInsertNotEmptyValidator());
- $this->element->setAutoInsertNotEmptyValidator(true);
- $this->assertTrue($this->element->autoInsertNotEmptyValidator());
- }
- public function testIsValidDoesNotInsertNotEmptyValidatorWhenElementIsRequiredButAutoInsertNotEmptyValidatorFlagIsFalse()
- {
- $this->element->setAutoInsertNotEmptyValidator(false)
- ->setRequired(true);
- $this->assertTrue($this->element->isValid(''));
- }
- public function testDescriptionInitiallyNull()
- {
- $this->assertNull($this->element->getDescription());
- }
- public function testCanSetDescription()
- {
- $this->testDescriptionInitiallyNull();
- $this->element->setDescription('element hint');
- $this->assertEquals('element hint', $this->element->getDescription());
- }
- public function testElementIsNotArrayByDefault()
- {
- $this->assertFalse($this->element->isArray());
- }
- public function testCanSetArrayFlag()
- {
- $this->testElementIsNotArrayByDefault();
- $this->element->setIsArray(true);
- $this->assertTrue($this->element->isArray());
- $this->element->setIsArray(false);
- $this->assertFalse($this->element->isArray());
- }
- public function testElementBelongsToNullByDefault()
- {
- $this->assertNull($this->element->getBelongsTo());
- }
- public function testCanSetArrayElementBelongsTo()
- {
- $this->testElementBelongsToNullByDefault();
- $this->element->setBelongsTo('foo');
- $this->assertEquals('foo', $this->element->getBelongsTo());
- }
- public function testArrayElementBelongsToNormalizedToValidVariableCharactersOnly()
- {
- $this->testElementBelongsToNullByDefault();
- $this->element->setBelongsTo('f%\o^&*)o\(%$b#@!.a}{;-,r');
- $this->assertEquals('foobar', $this->element->getBelongsTo());
- }
- public function testGetTypeReturnsCurrentElementClass()
- {
- $this->assertEquals('Zend_Form_Element', $this->element->getType());
- }
- public function testCanUseAccessorsToSetIndidualAttribs()
- {
- $this->element->setAttrib('foo', 'bar')
- ->setAttrib('bar', 'baz')
- ->setAttrib('baz', 'bat');
- $this->assertEquals('bar', $this->element->getAttrib('foo'));
- $this->assertEquals('baz', $this->element->getAttrib('bar'));
- $this->assertEquals('bat', $this->element->getAttrib('baz'));
- }
- public function testGetUndefinedAttribShouldReturnNull()
- {
- $this->assertNull($this->element->getAttrib('bogus'));
- }
- public function testSetAttribThrowsExceptionsForKeysWithLeadingUnderscores()
- {
- try {
- $this->element->setAttrib('_foo', 'bar');
- $this->fail('setAttrib() should throw an exception for invalid keys');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid attribute', $e->getMessage());
- }
- }
- public function testPassingNullValueToSetAttribUnsetsAttrib()
- {
- $this->element->setAttrib('foo', 'bar');
- $this->assertEquals('bar', $this->element->getAttrib('foo'));
- $this->element->setAttrib('foo', null);
- $this->assertFalse(isset($this->element->foo));
- }
- public function testSetAttribsSetsMultipleAttribs()
- {
- $this->element->setAttribs(array(
- 'foo' => 'bar',
- 'bar' => 'baz',
- 'baz' => 'bat'
- ));
- $this->assertEquals('bar', $this->element->getAttrib('foo'));
- $this->assertEquals('baz', $this->element->getAttrib('bar'));
- $this->assertEquals('bat', $this->element->getAttrib('baz'));
- }
- public function testGetAttribsRetrievesAllAttributes()
- {
- $attribs = array(
- 'foo' => 'bar',
- 'bar' => 'baz',
- 'baz' => 'bat'
- );
- $this->element->setAttribs($attribs);
- $received = $this->element->getAttribs();
- $this->assertEquals($attribs, $received);
- }
- /**
- * @group ZF-6061
- */
- public function testHelperDoesNotShowUpInAttribs()
- {
- $attribs = array(
- 'foo' => 'bar',
- 'bar' => 'baz',
- 'baz' => 'bat'
- );
- $this->element->setAttribs($attribs);
- $this->assertFalse(array_key_exists('helper', $this->element->getAttribs()));
- }
- public function testPassingNullValuesToSetAttribsUnsetsAttribs()
- {
- $this->testSetAttribsSetsMultipleAttribs();
- $this->element->setAttribs(array('foo' => null));
- $this->assertNull($this->element->foo);
- }
- public function testRetrievingOverloadedValuesThrowsExceptionWithInvalidKey()
- {
- try {
- $name = $this->element->_name;
- $this->fail('Overloading should not return protected or private members');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Cannot retrieve value for protected/private', $e->getMessage());
- }
- }
- public function testCanSetAndRetrieveAttribsViaOverloading()
- {
- $this->element->foo = 'bar';
- $this->assertEquals('bar', $this->element->foo);
- }
- public function testGetPluginLoaderRetrievesDefaultValidatorPluginLoader()
- {
- $loader = $this->element->getPluginLoader('validate');
- $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
- $paths = $loader->getPaths('Zend_Validate');
- $this->assertTrue(is_array($paths), var_export($loader, 1));
- $this->assertTrue(0 < count($paths));
- $this->assertContains('Validate', $paths[0]);
- }
- public function testGetPluginLoaderRetrievesDefaultFilterPluginLoader()
- {
- $loader = $this->element->getPluginLoader('filter');
- $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
- $paths = $loader->getPaths('Zend_Filter');
- $this->assertTrue(is_array($paths));
- $this->assertTrue(0 < count($paths));
- $this->assertContains('Filter', $paths[0]);
- }
- public function testGetPluginLoaderRetrievesDefaultDecoratorPluginLoader()
- {
- $loader = $this->element->getPluginLoader('decorator');
- $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
- $paths = $loader->getPaths('Zend_Form_Decorator');
- $this->assertTrue(is_array($paths));
- $this->assertTrue(0 < count($paths));
- $this->assertContains('Decorator', $paths[0]);
- }
- public function testCanSetCustomValidatorPluginLoader()
- {
- $loader = new Zend_Loader_PluginLoader();
- $this->element->setPluginLoader($loader, 'validate');
- $test = $this->element->getPluginLoader('validate');
- $this->assertSame($loader, $test);
- }
- public function testPassingInvalidTypeToSetPluginLoaderThrowsException()
- {
- $loader = new Zend_Loader_PluginLoader();
- try {
- $this->element->setPluginLoader($loader, 'foo');
- $this->fail('Invalid loader type should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid type', $e->getMessage());
- }
- }
- public function testPassingInvalidTypeToGetPluginLoaderThrowsException()
- {
- try {
- $this->element->getPluginLoader('foo');
- $this->fail('Invalid loader type should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid type', $e->getMessage());
- }
- }
- public function testCanSetCustomFilterPluginLoader()
- {
- $loader = new Zend_Loader_PluginLoader();
- $this->element->setPluginLoader($loader, 'filter');
- $test = $this->element->getPluginLoader('filter');
- $this->assertSame($loader, $test);
- }
- public function testCanSetCustomDecoratorPluginLoader()
- {
- $loader = new Zend_Loader_PluginLoader();
- $this->element->setPluginLoader($loader, 'decorator');
- $test = $this->element->getPluginLoader('decorator');
- $this->assertSame($loader, $test);
- }
- public function testPassingInvalidLoaderTypeToAddPrefixPathThrowsException()
- {
- try {
- $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'foo');
- $this->fail('Invalid loader type should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid type', $e->getMessage());
- }
- }
- public function testCanAddValidatorPluginLoaderPrefixPath()
- {
- $loader = $this->element->getPluginLoader('validate');
- $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
- $paths = $loader->getPaths('Zend_Form');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Form', $paths[0]);
- }
- public function testAddingValidatorPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
- {
- $validateLoader = $this->element->getPluginLoader('validate');
- $filterLoader = $this->element->getPluginLoader('filter');
- $decoratorLoader = $this->element->getPluginLoader('decorator');
- $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
- $this->assertFalse($filterLoader->getPaths('Zend_Form'));
- $this->assertFalse($decoratorLoader->getPaths('Zend_Form'));
- }
- public function testCanAddFilterPluginLoaderPrefixPath()
- {
- $loader = $this->element->getPluginLoader('validate');
- $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'validate');
- $paths = $loader->getPaths('Zend_Form');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Form', $paths[0]);
- }
- public function testAddingFilterPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
- {
- $filterLoader = $this->element->getPluginLoader('filter');
- $validateLoader = $this->element->getPluginLoader('validate');
- $decoratorLoader = $this->element->getPluginLoader('decorator');
- $this->element->addPrefixPath('Zend_Form', 'Zend/Form/', 'filter');
- $this->assertFalse($validateLoader->getPaths('Zend_Form'));
- $this->assertFalse($decoratorLoader->getPaths('Zend_Form'));
- }
- public function testCanAddDecoratorPluginLoaderPrefixPath()
- {
- $loader = $this->element->getPluginLoader('decorator');
- $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
- $paths = $loader->getPaths('Zend_Foo');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Foo', $paths[0]);
- }
- public function testAddingDecoratorrPluginLoaderPrefixPathDoesNotAffectOtherLoaders()
- {
- $decoratorLoader = $this->element->getPluginLoader('decorator');
- $filterLoader = $this->element->getPluginLoader('filter');
- $validateLoader = $this->element->getPluginLoader('validate');
- $this->element->addPrefixPath('Zend_Foo', 'Zend/Foo/', 'decorator');
- $this->assertFalse($validateLoader->getPaths('Zend_Foo'));
- $this->assertFalse($filterLoader->getPaths('Zend_Foo'));
- }
- public function testCanAddAllPluginLoaderPrefixPathsSimultaneously()
- {
- $validatorLoader = new Zend_Loader_PluginLoader();
- $filterLoader = new Zend_Loader_PluginLoader();
- $decoratorLoader = new Zend_Loader_PluginLoader();
- $this->element->setPluginLoader($validatorLoader, 'validate')
- ->setPluginLoader($filterLoader, 'filter')
- ->setPluginLoader($decoratorLoader, 'decorator')
- ->addPrefixPath('Zend', 'Zend/');
- $paths = $filterLoader->getPaths('Zend_Filter');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Filter', $paths[0]);
- $paths = $validatorLoader->getPaths('Zend_Validate');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Validate', $paths[0]);
- $paths = $decoratorLoader->getPaths('Zend_Decorator');
- $this->assertTrue(is_array($paths), var_export($paths, 1));
- $this->assertContains('Decorator', $paths[0]);
- }
- public function testPassingInvalidValidatorToAddValidatorThrowsException()
- {
- try {
- $this->element->addValidator(123);
- $this->fail('Invalid validator should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid validator', $e->getMessage());
- }
- }
- public function testCanAddSingleValidatorAsString()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getValidator('digits'));
- $this->element->addValidator('digits');
- $validator = $this->element->getValidator('digits');
- $this->assertTrue($validator instanceof Zend_Validate_Digits, var_export($validator, 1));
- $this->assertFalse($validator->zfBreakChainOnFailure);
- }
- public function testCanNotRetrieveSingleValidatorRegisteredAsStringUsingClassName()
- {
- $this->assertFalse($this->element->getValidator('digits'));
- $this->element->addValidator('digits');
- $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
- }
- public function testCanAddSingleValidatorAsValidatorObject()
- {
- $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
- require_once 'Zend/Validate/Digits.php';
- $validator = new Zend_Validate_Digits();
- $this->element->addValidator($validator);
- $test = $this->element->getValidator('Zend_Validate_Digits');
- $this->assertSame($validator, $test);
- $this->assertFalse($validator->zfBreakChainOnFailure);
- }
- public function testOptionsAreCastToArrayWhenAddingValidator()
- {
- $this->_checkZf2794();
- try {
- $this->element->addValidator('Alnum', false, true);
- } catch (Exception $e) {
- $this->fail('Should be able to add non-array validator options');
- }
- $validator = $this->element->getValidator('Alnum');
- $this->assertTrue($validator instanceof Zend_Validate_Alnum);
- $this->assertTrue($validator->allowWhiteSpace);
- }
- public function testCanRetrieveSingleValidatorRegisteredAsValidatorObjectUsingShortName()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getValidator('digits'));
- require_once 'Zend/Validate/Digits.php';
- $validator = new Zend_Validate_Digits();
- $this->element->addValidator($validator);
- $test = $this->element->getValidator('digits');
- $this->assertSame($validator, $test);
- $this->assertFalse($validator->zfBreakChainOnFailure);
- }
- public function testRetrievingNamedValidatorShouldNotReorderValidators()
- {
- $this->element->addValidators(array(
- 'NotEmpty',
- 'Alnum',
- 'Digits',
- ));
- $validator = $this->element->getValidator('Alnum');
- $validators = $this->element->getValidators();
- $i = 0;
- $order = array();
- foreach (array_keys($validators) as $name) {
- $order[$name] = $i;
- ++$i;
- }
- $this->assertEquals(1, $order['Zend_Validate_Alnum'], var_export($order, 1));
- }
- public function testCanAddMultipleValidators()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
- $this->assertFalse($this->element->getValidator('Zend_Validate_Alnum'));
- $this->element->addValidators(array('digits', 'alnum'));
- $digits = $this->element->getValidator('digits');
- $this->assertTrue($digits instanceof Zend_Validate_Digits);
- $alnum = $this->element->getValidator('alnum');
- $this->assertTrue($alnum instanceof Zend_Validate_Alnum);
- }
- public function testRemovingUnregisteredValidatorReturnsObjectInstance()
- {
- $this->assertSame($this->element, $this->element->removeValidator('bogus'));
- }
- public function testPassingMessagesOptionToAddValidatorSetsValidatorMessages()
- {
- $messageTemplates = array(
- Zend_Validate_Digits::NOT_DIGITS => 'Value should only contain digits',
- Zend_Validate_Digits::STRING_EMPTY => 'Value needs some digits',
- );
- $this->element->setAllowEmpty(false)
- ->addValidator('digits', false, array('messages' => $messageTemplates));
- $this->element->isValid('');
- $messages = $this->element->getMessages();
- $found = false;
- foreach ($messages as $key => $message) {
- if ($key == Zend_Validate_Digits::STRING_EMPTY) {
- $found = true;
- break;
- }
- }
- $this->assertTrue($found, 'Empty string message not found: ' . var_export($messages, 1));
- $this->assertEquals($messageTemplates[Zend_Validate_Digits::STRING_EMPTY], $message);
- $this->element->isValid('abc');
- $messages = $this->element->getMessages();
- $found = false;
- foreach ($messages as $key => $message) {
- if ($key == Zend_Validate_Digits::NOT_DIGITS) {
- $found = true;
- break;
- }
- }
- $this->assertTrue($found, 'Not digits message not found');
- $this->assertEquals($messageTemplates[Zend_Validate_Digits::NOT_DIGITS], $message);
- }
- public function testCanPassSingleMessageToValidatorToSetValidatorMessages()
- {
- $this->_checkZf2794();
- $message = 'My custom empty message';
- $this->element->addValidator('notEmpty', false, array('messages' => $message))
- ->setRequired(true);
- $this->element->isValid('');
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals($message, current($messages));
- }
- public function testMessagesAreTranslatedForCurrentLocale()
- {
- $localeFile = dirname(__FILE__) . '/_files/locale/array.php';
- $translations = include($localeFile);
- $translator = new Zend_Translate('array', $translations, 'en');
- $translator->setLocale('en');
- $this->element->setAllowEmpty(false)
- ->setTranslator($translator)
- ->addValidator('digits');
- $this->element->isValid('');
- $messages = $this->element->getMessages();
- $found = false;
- foreach ($messages as $key => $message) {
- if ($key == 'digitsStringEmpty') {
- $found = true;
- break;
- }
- }
- $this->assertTrue($found, 'String Empty message not found: ' . var_export($messages, 1));
- $this->assertEquals($translations['stringEmpty'], $message);
- $this->element->isValid('abc');
- $messages = $this->element->getMessages();
- $found = false;
- foreach ($messages as $key => $message) {
- if ($key == 'notDigits') {
- $found = true;
- break;
- }
- }
- $this->assertTrue($found, 'Not Digits message not found');
- $this->assertEquals($translations['notDigits'], $message);
- }
- /**#@+
- * @group ZF-2988
- */
- public function testSettingErrorMessageShouldOverrideValidationErrorMessages()
- {
- $this->element->addValidator('Alpha');
- $this->element->addErrorMessage('Invalid value entered');
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals('Invalid value entered', array_shift($messages));
- }
- public function testCustomErrorMessagesShouldBeManagedInAStack()
- {
- $this->element->addValidator('Alpha');
- $this->element->addErrorMessage('Invalid value entered');
- $this->element->addErrorMessage('Really, it is not valid');
- $messages = $this->element->getErrorMessages();
- $this->assertEquals(2, count($messages));
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals(2, count($messages));
- $this->assertEquals('Invalid value entered', array_shift($messages));
- $this->assertEquals('Really, it is not valid', array_shift($messages));
- }
- public function testShouldAllowSettingMultipleErrorMessagesAtOnce()
- {
- $set1 = array('foo', 'bar', 'baz');
- $this->element->addErrorMessages($set1);
- $this->assertSame($set1, $this->element->getErrorMessages());
- }
- public function testSetErrorMessagesShouldOverwriteMessages()
- {
- $set1 = array('foo', 'bar', 'baz');
- $set2 = array('bat', 'cat');
- $this->element->addErrorMessages($set1);
- $this->assertSame($set1, $this->element->getErrorMessages());
- $this->element->setErrorMessages($set2);
- $this->assertSame($set2, $this->element->getErrorMessages());
- }
- public function testCustomErrorMessageStackShouldBeClearable()
- {
- $this->testCustomErrorMessagesShouldBeManagedInAStack();
- $this->element->clearErrorMessages();
- $messages = $this->element->getErrorMessages();
- $this->assertTrue(empty($messages));
- }
- public function testCustomErrorMessagesShouldBeTranslated()
- {
- $translations = array(
- 'foo' => 'Foo message',
- );
- $translate = new Zend_Translate('array', $translations);
- $this->element->setTranslator($translate)
- ->addErrorMessage('foo')
- ->addValidator('Alpha');
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals('Foo message', array_shift($messages));
- }
- public function testCustomErrorMessagesShouldAllowValueSubstitution()
- {
- $this->element->addErrorMessage('"%value%" is an invalid value')
- ->addValidator('Alpha');
- $this->assertFalse($this->element->isValid(123));
- $this->assertTrue($this->element->hasErrors());
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals('"123" is an invalid value', array_shift($messages));
- }
- public function testShouldAllowMarkingElementAsInvalid()
- {
- $this->element->setValue('foo');
- $this->element->addErrorMessage('Invalid value entered');
- $this->assertFalse($this->element->hasErrors());
- $this->element->markAsError();
- $this->assertTrue($this->element->hasErrors());
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals('Invalid value entered', array_shift($messages));
- }
- public function testShouldAllowPushingErrorsOntoErrorStackWithErrorMessages()
- {
- $this->element->setValue('foo');
- $this->assertFalse($this->element->hasErrors());
- $this->element->setErrors(array('Error 1', 'Error 2'))
- ->addError('Error 3')
- ->addErrors(array('Error 4', 'Error 5'));
- $this->assertTrue($this->element->hasErrors());
- $messages = $this->element->getMessages();
- $this->assertEquals(5, count($messages));
- foreach (range(1, 5) as $id) {
- $message = 'Error ' . $id;
- $this->assertContains($message, $messages);
- }
- }
- public function testHasErrorsShouldIndicateStatusOfValidationErrors()
- {
- $this->element->setValue('foo');
- $this->assertFalse($this->element->hasErrors());
- $this->element->markAsError();
- $this->assertTrue($this->element->hasErrors());
- }
- /**#@-*/
- public function testAddingErrorToArrayElementShouldLoopOverAllValues()
- {
- $this->element->setIsArray(true)
- ->setValue(array('foo', 'bar', 'baz'))
- ->addError('error with value %value%');
- $errors = $this->element->getMessages();
- require_once 'Zend/Json.php';
- $errors = Zend_Json::encode($errors);
- foreach (array('foo', 'bar', 'baz') as $value) {
- $message = 'error with value ' . $value;
- $this->assertContains($message, $errors);
- }
- }
- /** ZF-2568 */
- public function testTranslatedMessagesCanContainVariableSubstitution()
- {
- $localeFile = dirname(__FILE__) . '/_files/locale/array.php';
- $translations = include($localeFile);
- $translations['notDigits'] .= ' "%value%"';
- $translator = new Zend_Translate('array', $translations, 'en');
- $translator->setLocale('en');
- $this->element->setAllowEmpty(false)
- ->setTranslator($translator)
- ->addValidator('digits');
- $this->element->isValid('abc');
- $messages = $this->element->getMessages();
- $found = false;
- foreach ($messages as $key => $message) {
- if ($key == 'notDigits') {
- $found = true;
- break;
- }
- }
- $this->assertTrue($found, 'String Empty message not found: ' . var_export($messages, 1));
- $this->assertContains(' "abc"', $message);
- $this->assertContains('Translating the notDigits string', $message);
- }
- public function testCanRemoveValidator()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getValidator('Zend_Validate_Digits'));
- $this->element->addValidator('digits');
- $digits = $this->element->getValidator('digits');
- $this->assertTrue($digits instanceof Zend_Validate_Digits);
- $this->element->removeValidator('digits');
- $this->assertFalse($this->element->getValidator('digits'));
- }
- public function testCanClearAllValidators()
- {
- $this->_checkZf2794();
- $this->testCanAddMultipleValidators();
- $validators = $this->element->getValidators();
- $this->element->clearValidators();
- $test = $this->element->getValidators();
- $this->assertNotEquals($validators, $test);
- $this->assertTrue(empty($test));
- foreach (array_keys($validators) as $validator) {
- $this->assertFalse($this->element->getValidator($validator));
- }
- }
- public function testCanValidateElement()
- {
- $this->element->addValidator(new Zend_Validate_NotEmpty())
- ->addValidator(new Zend_Validate_EmailAddress());
- try {
- $result = $this->element->isValid('matthew@zend.com');
- } catch (Exception $e) {
- $this->fail('Validating an element should work');
- }
- }
- public function testCanValidateArrayValue()
- {
- $this->element->setIsArray(true)
- ->addValidator('InArray', false, array(array('foo', 'bar', 'baz', 'bat')));
- $this->assertTrue($this->element->isValid(array('foo', 'bat')));
- }
- public function testShouldAllowZeroAsNonEmptyValue()
- {
- $this->element->addValidator('between', false, array(1, 100));
- $this->assertFalse($this->element->isValid('0'));
- }
- public function testIsValidPopulatesElementValue()
- {
- $this->testCanValidateElement();
- $this->assertEquals('matthew@zend.com', $this->element->getValue());
- }
- public function testErrorsPopulatedFollowingFailedIsValidCheck()
- {
- $this->element->addValidator(new Zend_Validate_NotEmpty())
- ->addValidator(new Zend_Validate_EmailAddress());
- $result = $this->element->isValid('matthew');
- if ($result) {
- $this->fail('Invalid data should fail validations');
- }
- $errors = $this->element->getErrors();
- $this->assertTrue(is_array($errors));
- $this->assertTrue(0 < count($errors));
- }
- public function testMessagesPopulatedFollowingFailedIsValidCheck()
- {
- require_once 'Zend/Validate/NotEmpty.php';
- require_once 'Zend/Validate/EmailAddress.php';
- $this->element->addValidator(new Zend_Validate_NotEmpty())
- ->addValidator(new Zend_Validate_EmailAddress());
- $result = $this->element->isValid('matthew');
- if ($result) {
- $this->fail('Invalid data should fail validations');
- }
- $messages = $this->element->getMessages();
- $this->assertTrue(is_array($messages));
- $this->assertTrue(0 < count($messages));
- }
- public function testOptionalElementDoesNotPerformValidationsOnEmptyValuesByDefault()
- {
- $this->element->addValidator(new Zend_Validate_EmailAddress());
- $result = $this->element->isValid('');
- if (!$result) {
- $this->fail('Empty data should not fail validations');
- }
- $errors = $this->element->getErrors();
- $this->assertTrue(is_array($errors));
- $this->assertTrue(empty($errors));
- }
- public function testOptionalElementDoesPerformValidationsWhenAllowEmptyIsFalse()
- {
- $this->element->setAllowEmpty(false)
- ->addValidator(new Zend_Validate_EmailAddress());
- $result = $this->element->isValid('');
- if ($result) {
- $this->fail('Empty data should fail validations when AllowEmpty is false');
- }
- $errors = $this->element->getErrors();
- $this->assertTrue(is_array($errors));
- $this->assertTrue(0 < count($errors));
- }
- public function testAddingInvalidFilterTypeThrowsException()
- {
- try {
- $this->element->addFilter(123);
- $this->fail('Invalid filter type should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid filter', $e->getMessage());
- }
- }
- public function testCanAddSingleFilterAsString()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getFilter('digits'));
- $this->element->addFilter('digits');
- $filter = $this->element->getFilter('digits');
- $this->assertTrue($filter instanceof Zend_Filter_Digits);
- }
- public function testCanNotRetrieveSingleFilterRegisteredAsStringUsingClassName()
- {
- $this->assertFalse($this->element->getFilter('digits'));
- $this->element->addFilter('digits');
- $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
- }
- public function testCanAddSingleFilterAsFilterObject()
- {
- $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
- require_once 'Zend/Filter/Digits.php';
- $filter = new Zend_Filter_Digits();
- $this->element->addFilter($filter);
- $test = $this->element->getFilter('Zend_Filter_Digits');
- $this->assertSame($filter, $test);
- }
- public function testCanRetrieveSingleFilterRegisteredAsFilterObjectUsingShortName()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getFilter('digits'));
- require_once 'Zend/Filter/Digits.php';
- $filter = new Zend_Filter_Digits();
- $this->element->addFilter($filter);
- $test = $this->element->getFilter('digits');
- }
- public function testRetrievingNamedFilterShouldNotReorderFilters()
- {
- $this->element->addFilters(array(
- 'Alpha',
- 'Alnum',
- 'Digits',
- ));
- $filter = $this->element->getFilter('Alnum');
- $filters = $this->element->getFilters();
- $i = 0;
- $order = array();
- foreach (array_keys($filters) as $name) {
- $order[$name] = $i;
- ++$i;
- }
- $this->assertEquals(1, $order['Zend_Filter_Alnum'], var_export($order, 1));
- }
- public function testOptionsAreCastToArrayWhenAddingFilter()
- {
- $this->_checkZf2794();
- try {
- $this->element->addFilter('Alnum', true);
- } catch (Exception $e) {
- $this->fail('Should be able to add non-array filter options');
- }
- $filter = $this->element->getFilter('Alnum');
- $this->assertTrue($filter instanceof Zend_Filter_Alnum);
- $this->assertTrue($filter->allowWhiteSpace);
- }
- public function testShouldUseFilterConstructorOptionsAsPassedToAddFilter()
- {
- $this->element->addFilter('HtmlEntities', array(array('quotestyle' => ENT_QUOTES, 'charset' => 'UTF-8')));
- $filter = $this->element->getFilter('HtmlEntities');
- $this->assertTrue($filter instanceof Zend_Filter_HtmlEntities);
- $this->assertEquals(ENT_QUOTES, $filter->getQuoteStyle());
- $this->assertEquals('UTF-8', $filter->getCharSet());
- }
- public function testCanAddMultipleFilters()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
- $this->assertFalse($this->element->getFilter('Zend_Filter_Alnum'));
- $this->element->addFilters(array('digits', 'alnum'));
- $digits = $this->element->getFilter('digits');
- $this->assertTrue($digits instanceof Zend_Filter_Digits);
- $alnum = $this->element->getFilter('alnum');
- $this->assertTrue($alnum instanceof Zend_Filter_Alnum);
- }
- public function testRemovingUnregisteredFilterReturnsObjectInstance()
- {
- $this->assertSame($this->element, $this->element->removeFilter('bogus'));
- }
- public function testCanRemoveFilter()
- {
- $this->_checkZf2794();
- $this->assertFalse($this->element->getFilter('Zend_Filter_Digits'));
- $this->element->addFilter('digits');
- $digits = $this->element->getFilter('digits');
- $this->assertTrue($digits instanceof Zend_Filter_Digits);
- $this->element->removeFilter('digits');
- $this->assertFalse($this->element->getFilter('digits'));
- }
- public function testCanClearAllFilters()
- {
- $this->_checkZf2794();
- $this->testCanAddMultipleFilters();
- $filters = $this->element->getFilters();
- $this->element->clearFilters();
- $test = $this->element->getFilters();
- $this->assertNotEquals($filters, $test);
- $this->assertTrue(empty($test));
- foreach (array_keys($filters) as $filter) {
- $this->assertFalse($this->element->getFilter($filter));
- }
- }
- public function testGetViewReturnsNullWithNoViewRenderer()
- {
- $this->assertNull($this->element->getView());
- }
- public function testGetViewReturnsViewRendererViewInstanceIfViewRendererActive()
- {
- $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
- $viewRenderer->initView();
- $view = $viewRenderer->view;
- $test = $this->element->getView();
- $this->assertSame($view, $test);
- }
- public function testCanSetView()
- {
- $view = new Zend_View();
- $this->assertNull($this->element->getView());
- $this->element->setView($view);
- $received = $this->element->getView();
- $this->assertSame($view, $received);
- }
- public function testViewHelperDecoratorRegisteredByDefault()
- {
- $this->_checkZf2794();
- $decorator = $this->element->getDecorator('viewHelper');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
- }
- /**
- * @group ZF-4822
- */
- public function testErrorsDecoratorRegisteredByDefault()
- {
- $this->_checkZf2794();
- $decorator = $this->element->getDecorator('errors');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Errors);
- }
- /**
- * @group ZF-4822
- */
- public function testDescriptionDecoratorRegisteredByDefault()
- {
- $this->_checkZf2794();
- $decorator = $this->element->getDecorator('description');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Description);
- $options = $decorator->getOptions();
- $this->assertTrue(array_key_exists('tag', $options));
- $this->assertEquals('p', $options['tag']);
- $this->assertTrue(array_key_exists('class', $options));
- $this->assertEquals('description', $options['class']);
- }
- /**
- * @group ZF-4822
- */
- public function testHtmlTagDecoratorRegisteredByDefault()
- {
- $this->_checkZf2794();
- $decorator = $this->element->getDecorator('HtmlTag');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
- }
- /**
- * @group ZF-4822
- */
- public function testLabelDecoratorRegisteredByDefault()
- {
- $this->_checkZf2794();
- $decorator = $this->element->getDecorator('Label');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
- }
- public function testCanDisableRegisteringDefaultDecoratorsDuringInitialization()
- {
- $element = new Zend_Form_Element('foo', array('disableLoadDefaultDecorators' => true));
- $decorators = $element->getDecorators();
- $this->assertEquals(array(), $decorators);
- }
- public function testAddingInvalidDecoratorThrowsException()
- {
- try {
- $this->element->addDecorator(123);
- $this->fail('Invalid decorator type should raise exception');
- } catch (Zend_Form_Exception $e) {
- $this->assertContains('Invalid decorator', $e->getMessage());
- }
- }
- public function testCanAddSingleDecoratorAsString()
- {
- $this->_checkZf2794();
- $this->element->clearDecorators();
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $this->element->addDecorator('viewHelper');
- $decorator = $this->element->getDecorator('viewHelper');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
- }
- public function testCanNotRetrieveSingleDecoratorRegisteredAsStringUsingClassName()
- {
- $this->assertFalse($this->element->getDecorator('Zend_Form_Decorator_ViewHelper'));
- }
- public function testCanAddSingleDecoratorAsDecoratorObject()
- {
- $this->element->clearDecorators();
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $decorator = new Zend_Form_Decorator_ViewHelper;
- $this->element->addDecorator($decorator);
- $test = $this->element->getDecorator('Zend_Form_Decorator_ViewHelper');
- $this->assertSame($decorator, $test);
- }
- /**
- * @group ZF-3597
- */
- public function testAddingConcreteDecoratorShouldHonorOrder()
- {
- require_once dirname(__FILE__) . '/_files/decorators/TableRow.php';
- $decorator = new My_Decorator_TableRow();
- $this->element->setLabel('Foo')
- ->setDescription('sample description')
- ->clearDecorators()
- ->addDecorators(array(
- 'ViewHelper',
- $decorator,
- ));
- $html = $this->element->render($this->getView());
- $this->assertRegexp('#<tr><td>Foo</td><td>.*?<input[^>]+>.*?</td><td>sample description</td></tr>#s', $html, $html);
- }
- public function testCanRetrieveSingleDecoratorRegisteredAsDecoratorObjectUsingShortName()
- {
- $this->_checkZf2794();
- $this->element->clearDecorators();
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $decorator = new Zend_Form_Decorator_ViewHelper;
- $this->element->addDecorator($decorator);
- $test = $this->element->getDecorator('viewHelper');
- $this->assertSame($decorator, $test);
- }
- public function testCanAddMultipleDecorators()
- {
- $this->_checkZf2794();
- $this->element->clearDecorators();
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $testDecorator = new Zend_Form_ElementTest_Decorator;
- $this->element->addDecorators(array(
- 'ViewHelper',
- $testDecorator
- ));
- $viewHelper = $this->element->getDecorator('viewHelper');
- $this->assertTrue($viewHelper instanceof Zend_Form_Decorator_ViewHelper);
- $decorator = $this->element->getDecorator('decorator');
- $this->assertSame($testDecorator, $decorator);
- }
- public function testRemovingUnregisteredDecoratorReturnsObjectInstance()
- {
- $this->_checkZf2794();
- $this->assertSame($this->element, $this->element->removeDecorator('bogus'));
- }
- public function testCanRemoveDecorator()
- {
- $this->testViewHelperDecoratorRegisteredByDefault();
- $this->element->removeDecorator('viewHelper');
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- }
- /**
- * @group ZF-3069
- */
- public function testRemovingNamedDecoratorsShouldWork()
- {
- $this->_checkZf2794();
- $this->element->setDecorators(array(
- 'ViewHelper',
- array(array('div' => 'HtmlTag'), array('tag' => 'div')),
- array(array('div2' => 'HtmlTag'), array('tag' => 'div')),
- ));
- $decorators = $this->element->getDecorators();
- $this->assertTrue(array_key_exists('div', $decorators));
- $this->assertTrue(array_key_exists('div2', $decorators));
- $this->element->removeDecorator('div');
- $decorators = $this->element->getDecorators();
- $this->assertFalse(array_key_exists('div', $decorators));
- $this->assertTrue(array_key_exists('div2', $decorators));
- }
- public function testCanClearAllDecorators()
- {
- $this->testCanAddMultipleDecorators();
- $this->element->clearDecorators();
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $this->assertFalse($this->element->getDecorator('decorator'));
- }
- public function testCanAddDecoratorAliasesToAllowMultipleDecoratorsOfSameType()
- {
- $this->_checkZf2794();
- $this->element->setDecorators(array(
- array('HtmlTag', array('tag' => 'span')),
- array('decorator' => array('FooBar' => 'HtmlTag'), 'options' => array('tag' => 'div')),
- ));
- $decorator = $this->element->getDecorator('FooBar');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
- $this->assertEquals('div', $decorator->getOption('tag'));
- $decorator = $this->element->getDecorator('HtmlTag');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_HtmlTag);
- $this->assertEquals('span', $decorator->getOption('tag'));
- }
- public function testRetrievingNamedDecoratorShouldNotReorderDecorators()
- {
- $this->element->setDecorators(array(
- 'ViewHelper',
- 'Errors',
- array(array('inner' => 'HtmlTag'), array('tag' => 'div', 'class' => 'element')),
- 'Label',
- array(array('outer' => 'HtmlTag'), array('tag' => 'div')),
- ));
- $decorator = $this->element->getDecorator('inner');
- $decorators = $this->element->getDecorators();
- $i = 0;
- $order = array();
- foreach (array_keys($decorators) as $name) {
- $order[$name] = $i;
- ++$i;
- }
- $this->assertEquals(2, $order['inner'], var_export($order, 1));
- }
- /**
- * @group ZF-3376
- */
- public function testSetDecoratorsShouldAcceptReturnOfGetDecorators()
- {
- $this->element->setDecorators(array(
- 'ViewHelper',
- 'Errors',
- array('input' => 'HtmlTag', array('tag' => 'div', 'class' => 'input')),
- 'Label',
- array('element' => 'HtmlTag', array('tag' => 'div', 'class' => 'element')),
- ));
- $decorators = $this->element->getDecorators();
- $this->element->setDecorators($decorators);
- $this->assertSame($decorators, $this->element->getDecorators());
- }
- public function testRenderElementReturnsMarkup()
- {
- $this->element->setName('foo');
- $html = $this->element->render($this->getView());
- $this->assertTrue(is_string($html));
- $this->assertFalse(empty($html));
- $this->assertContains('<input', $html);
- $this->assertContains('"foo"', $html);
- }
- public function testRenderElementRendersLabelWhenProvided()
- {
- $this->element->setView($this->getView());
- $this->element->setName('foo')
- ->setLabel('Foo');
- $html = $this->element->render();
- $this->assertTrue(is_string($html));
- $this->assertFalse(empty($html));
- $this->assertContains('<label', $html);
- $this->assertContains('Foo', $html);
- $this->assertContains('</label>', $html);
- }
- public function testRenderElementRendersValueWhenProvided()
- {
- $this->element->setView($this->getView());
- $this->element->setName('foo')
- ->setValue('bar');
- $html = $this->element->render();
- $this->assertTrue(is_string($html));
- $this->assertFalse(empty($html));
- $this->assertContains('<input', $html);
- $this->assertContains('"foo"', $html);
- $this->assertContains('"bar"', $html);
- }
- public function testRenderElementRendersErrorsWhenProvided()
- {
- $this->_checkZf2794();
- $this->element->setView($this->getView())
- ->setRequired(true)
- ->setName('foo')
- ->addValidator('NotEmpty');
- $this->element->isValid('');
- $html = $this->element->render();
- $this->assertTrue(is_string($html));
- $this->assertFalse(empty($html));
- $this->assertContains('error', $html);
- $this->assertRegexp('/empty/i', $html);
- }
- public function testToStringProxiesToRender()
- {
- $this->element->setView($this->getView());
- $this->element->setName('foo');
- $html = $this->element->__toString();
- $this->assertTrue(is_string($html));
- $this->assertFalse(empty($html));
- $this->assertContains('<input', $html);
- $this->assertContains('"foo"', $html);
- }
- public function raiseDecoratorException($content, $element, $options)
- {
- throw new Exception('Raising exception in decorator callback');
- }
- public function handleDecoratorErrors($errno, $errstr, $errfile = '', $errline = 0, array $errcontext = array())
- {
- $this->error = $errstr;
- }
- public function testToStringRaisesErrorWhenExceptionCaught()
- {
- $this->element->setDecorators(array(
- array(
- 'decorator' => 'Callback',
- 'options' => array('callback' => array($this, 'raiseDecoratorException'))
- ),
- ));
- $origErrorHandler = set_error_handler(array($this, 'handleDecoratorErrors'), E_USER_WARNING);
- $text = $this->element->__toString();
- restore_error_handler();
- $this->assertTrue(empty($text));
- $this->assertTrue(isset($this->error));
- $this->assertEquals('Raising exception in decorator callback', $this->error);
- }
- public function getOptions()
- {
- $options = array(
- 'name' => 'changed',
- 'value' => 'foo',
- 'label' => 'bar',
- 'order' => 50,
- 'required' => false,
- 'foo' => 'bar',
- 'baz' => 'bat'
- );
- return $options;
- }
- public function testCanSetObjectStateViaSetOptions()
- {
- $options = $this->getOptions();
- $this->element->setOptions($options);
- $this->assertEquals('changed', $this->element->getName());
- $this->assertEquals('foo', $this->element->getValue());
- $this->assertEquals('bar', $this->element->getLabel());
- $this->assertEquals(50, $this->element->getOrder());
- $this->assertFalse($this->element->isRequired());
- $this->assertEquals('bar', $this->element->foo);
- $this->assertEquals('bat', $this->element->baz);
- }
- public function testSetOptionsSkipsCallsToSetOptionsAndSetConfig()
- {
- $options = $this->getOptions();
- $config = new Zend_Config($options);
- $options['config'] = $config;
- $options['options'] = $config->toArray();
- $this->element->setOptions($options);
- }
- public function testSetOptionsSkipsSettingAccessorsRequiringObjectsWhenNoObjectPresent()
- {
- $options = $this->getOptions();
- $options['translator'] = true;
- $options['pluginLoader'] = true;
- $options['view'] = true;
- $this->element->setOptions($options);
- }
- public function testSetOptionsSetsArrayOfStringValidators()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['validators'] = array(
- 'notEmpty',
- 'digits'
- );
- $this->element->setOptions($options);
- $validator = $this->element->getValidator('notEmpty');
- $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
- $validator = $this->element->getValidator('digits');
- $this->assertTrue($validator instanceof Zend_Validate_Digits);
- }
- public function testSetOptionsSetsArrayOfArrayValidators()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['validators'] = array(
- array('notEmpty', true, array(Zend_Validate_NotEmpty::ALL)),
- array('digits', true, array('bar')),
- );
- $this->element->setOptions($options);
- $validator = $this->element->getValidator('notEmpty');
- $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
- $this->assertTrue($validator->zfBreakChainOnFailure);
- $validator = $this->element->getValidator('digits');
- $this->assertTrue($validator instanceof Zend_Validate_Digits);
- $this->assertTrue($validator->zfBreakChainOnFailure);
- }
- public function testSetOptionsSetsArrayOfAssociativeArrayValidators()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['validators'] = array(
- array(
- 'options' => array(Zend_Validate_NotEmpty::ALL),
- 'breakChainOnFailure' => true,
- 'validator' => 'notEmpty',
- ),
- array(
- 'options' => array('bar'),
- 'validator' => 'digits',
- 'breakChainOnFailure' => true,
- ),
- );
- $this->element->setOptions($options);
- $validator = $this->element->getValidator('notEmpty');
- $this->assertTrue($validator instanceof Zend_Validate_NotEmpty);
- $this->assertTrue($validator->zfBreakChainOnFailure);
- $validator = $this->element->getValidator('digits');
- $this->assertTrue($validator instanceof Zend_Validate_Digits);
- $this->assertTrue($validator->zfBreakChainOnFailure);
- }
- public function testSetOptionsSetsArrayOfStringFilters()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['filters'] = array('StringToUpper', 'Alpha');
- $this->element->setOptions($options);
- $filter = $this->element->getFilter('StringToUpper');
- $this->assertTrue($filter instanceof Zend_Filter_StringToUpper);
- $filter = $this->element->getFilter('Alpha');
- $this->assertTrue($filter instanceof Zend_Filter_Alpha);
- }
- public function testSetOptionsSetsArrayOfArrayFilters()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['filters'] = array(
- array('Digits', array('bar' => 'baz')),
- array('Alpha', array('foo')),
- );
- $this->element->setOptions($options);
- $filter = $this->element->getFilter('Digits');
- $this->assertTrue($filter instanceof Zend_Filter_Digits);
- $filter = $this->element->getFilter('Alpha');
- $this->assertTrue($filter instanceof Zend_Filter_Alpha);
- }
- public function testSetOptionsSetsArrayOfAssociativeArrayFilters()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['filters'] = array(
- array(
- 'options' => array('baz'),
- 'filter' => 'Digits'
- ),
- array(
- 'options' => array('foo'),
- 'filter' => 'Alpha',
- ),
- );
- $this->element->setOptions($options);
- $filter = $this->element->getFilter('Digits');
- $this->assertTrue($filter instanceof Zend_Filter_Digits);
- $filter = $this->element->getFilter('Alpha');
- $this->assertTrue($filter instanceof Zend_Filter_Alpha);
- }
- public function testSetOptionsSetsArrayOfStringDecorators()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['decorators'] = array('label', 'form');
- $this->element->setOptions($options);
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $this->assertFalse($this->element->getDecorator('errors'));
- $decorator = $this->element->getDecorator('label');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
- $decorator = $this->element->getDecorator('form');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
- }
- public function testSetOptionsSetsArrayOfArrayDecorators()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['decorators'] = array(
- array('label', array('id' => 'mylabel')),
- array('form', array('id' => 'form')),
- );
- $this->element->setOptions($options);
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $this->assertFalse($this->element->getDecorator('errors'));
- $decorator = $this->element->getDecorator('label');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
- $options = $decorator->getOptions();
- $this->assertEquals('mylabel', $options['id']);
- $decorator = $this->element->getDecorator('form');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
- $options = $decorator->getOptions();
- $this->assertEquals('form', $options['id']);
- }
- public function testSetOptionsSetsArrayOfAssocArrayDecorators()
- {
- $this->_checkZf2794();
- $options = $this->getOptions();
- $options['decorators'] = array(
- array(
- 'options' => array('id' => 'mylabel'),
- 'decorator' => 'label',
- ),
- array(
- 'options' => array('id' => 'form'),
- 'decorator' => 'form',
- ),
- );
- $this->element->setOptions($options);
- $this->assertFalse($this->element->getDecorator('viewHelper'));
- $this->assertFalse($this->element->getDecorator('errors'));
- $decorator = $this->element->getDecorator('label');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Label);
- $options = $decorator->getOptions();
- $this->assertEquals('mylabel', $options['id']);
- $decorator = $this->element->getDecorator('form');
- $this->assertTrue($decorator instanceof Zend_Form_Decorator_Form);
- $options = $decorator->getOptions();
- $this->assertEquals('form', $options['id']);
- }
- public function testSetOptionsSetsGlobalPrefixPaths()
- {
- $options = $this->getOptions();
- $options['prefixPath'] = array(
- 'prefix' => 'Zend_Foo',
- 'path' => 'Zend/Foo/'
- );
- $this->element->setOptions($options);
- foreach (array('validate', 'filter', 'decorator') as $type) {
- $loader = $this->element->getPluginLoader($type);
- $paths = $loader->getPaths('Zend_Foo_' . ucfirst($type));
- $this->assertTrue(is_array($paths), "Failed for type $type: " . var_export($paths, 1));
- $this->assertFalse(empty($paths));
- $this->assertContains('Foo', $paths[0]);
- }
- }
- public function testSetOptionsSetsIndividualPrefixPathsFromKeyedArrays()
- {
- $options = $this->getOptions();
- $options['prefixPath'] = array(
- 'filter' => array('prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
- );
- $this->element->setOptions($options);
- $loader = $this->element->getPluginLoader('filter');
- $paths = $loader->getPaths('Zend_Foo');
- $this->assertTrue(is_array($paths));
- $this->assertFalse(empty($paths));
- $this->assertContains('Foo', $paths[0]);
- }
- public function testSetOptionsSetsIndividualPrefixPathsFromUnKeyedArrays()
- {
- $options = $this->getOptions();
- $options['prefixPath'] = array(
- array('type' => 'decorator', 'prefix' => 'Zend_Foo', 'path' => 'Zend/Foo/')
- );
- $this->element->setOptions($options);
- $loader = $this->element->getPluginLoader('decorator');
- $paths = $loader->getPaths('Zend_Foo');
- $this->assertTrue(is_array($paths));
- $this->assertFalse(empty($paths));
- $this->assertContains('Foo', $paths[0]);
- }
- public function testCanSetObjectStateViaSetConfig()
- {
- $config = new Zend_Config($this->getOptions());
- $this->element->setConfig($config);
- $this->assertEquals('changed', $this->element->getName());
- $this->assertEquals('foo', $this->element->getValue());
- $this->assertEquals('bar', $this->element->getLabel());
- $this->assertEquals(50, $this->element->getOrder());
- $this->assertFalse($this->element->isRequired());
- $this->assertEquals('bar', $this->element->foo);
- $this->assertEquals('bat', $this->element->baz);
- }
- public function testPassingConfigObjectToConstructorSetsObjectState()
- {
- $config = new Zend_Config($this->getOptions());
- $element = new Zend_Form_Element($config);
- $this->assertEquals('changed', $element->getName());
- $this->assertEquals('foo', $element->getValue());
- $this->assertEquals('bar', $element->getLabel());
- $this->assertEquals(50, $element->getOrder());
- $this->assertFalse($element->isRequired());
- $this->assertEquals('bar', $element->foo);
- $this->assertEquals('bat', $element->baz);
- }
- public function testValueIsFilteredPriorToValidation()
- {
- $this->_checkZf2794();
- $this->element->addFilter('StringTrim')
- ->addValidator('StringLength', false, array(3, 8));
- $this->assertTrue($this->element->isValid(' foobar '));
- $this->assertEquals('foobar', $this->element->getValue());
- $this->element->setFilters(array('StringTrim'))
- ->setRequired(true)
- ->setValidators(array('NotEmpty'));
- $this->assertFalse($this->element->isValid(' '));
- }
- public function testTranslatedLabel()
- {
- $this->element->setLabel('FooBar');
- $translator = new Zend_Translate('array', array('FooBar' => 'BazBar'));
- $this->element->setTranslator($translator);
- $this->assertEquals('BazBar', $this->element->getLabel());
- }
- // Extensions
- public function testInitCalledBeforeLoadDecorators()
- {
- $element = new Zend_Form_ElementTest_Element('test');
- $decorators = $element->getDecorators();
- $this->assertTrue(empty($decorators));
- }
- /**
- * @group ZF-3217
- */
- public function testElementShouldOverloadToRenderDecorators()
- {
- $this->element->setLabel('Foo Label')
- ->setView($this->getView());
- $html = $this->element->renderViewHelper();
- $this->assertContains('<input', $html);
- $this->assertContains('id="' . $this->element->getFullyQualifiedName() . '"', $html, 'Received: ' . $html);
- $this->assertNotContains('<dd', $html);
- $this->assertNotContains('<label', $html);
- $html = $this->element->renderLabel('this is the content');
- $this->assertRegexp('#<label[^>]*for="' . $this->element->getFullyQualifiedName() . '"[^>]*>Foo Label</label>#', $html);
- $this->assertContains('this is the content', $html);
- $this->assertNotContains('<input', $html);
- }
- /**
- * @group ZF-3217
- * @expectedException Zend_Form_Element_Exception
- */
- public function testOverloadingToInvalidMethodsShouldThrowAnException()
- {
- $html = $this->element->bogusMethodCall();
- }
- /**
- * @group ZF-5150
- */
- public function testMarkingAsErrorShouldCauseIsErrorToReturnFalse()
- {
- $this->element->setValue('foo');
- $this->element->markAsError();
- $this->assertFalse($this->element->isValid('foo'));
- }
- /**
- * @group ZF-4915
- */
- public function testElementShouldAllowSettingDefaultErrorMessageSeparator()
- {
- $this->element->setErrorMessageSeparator('|');
- $this->assertEquals('|', $this->element->getErrorMessageSeparator());
- }
- /**
- * @group ZF-4915
- */
- public function testElementShouldUseSemicolonAndSpaceAsDefaultErrorMessageSeparator()
- {
- $this->assertEquals('; ', $this->element->getErrorMessageSeparator());
- }
- /**
- * Used by test methods susceptible to ZF-2794, marks a test as incomplete
- *
- * @link http://framework.zend.com/issues/browse/ZF-2794
- * @return void
- */
- protected function _checkZf2794()
- {
- if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
- $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
- }
- }
- /**
- * @ZF-8882
- */
- public function testErrorMessagesShouldNotBeTranslatedWhenTranslatorIsDisabled()
- {
- $translations = array(
- 'foo' => 'Foo message',
- );
- $translate = new Zend_Translate('array', $translations);
- $this->element->setTranslator($translate)
- ->addErrorMessage('foo')
- ->addValidator('Alpha');
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals('Foo message', array_shift($messages));
- $this->element->setDisableTranslator(true);
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals(1, count($messages));
- $this->assertEquals('foo', array_shift($messages));
- }
- /**
- * @group ZF-9275
- */
- public function testElementDoesntOverrideDefaultValidatorTranslatorWithDefaultRegistryTranslator()
- {
- $registryTranslations = array('alphaInvalid' => 'Registry message');
- $registryTranslate = new Zend_Translate('array', $registryTranslations);
- Zend_Registry::set('Zend_Translate', $registryTranslate);
- $validatorTranslations = array('alphaInvalid' => 'Validator message');
- $validatorTranslate = new Zend_Translate('array', $validatorTranslations);
- Zend_Validate_Abstract::setDefaultTranslator($validatorTranslate);
- $elementTranslations = array('alphaInvalid' => 'Element message');
- $elementTranslate = new Zend_Translate('array', $elementTranslations);
- // the default validate translator should beat the registry one
- $this->element->addValidator('Alpha');
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals('Validator message', $messages['alphaInvalid']);
- }
- /**
- * @group ZF-9275
- */
- public function testDefaultTranslatorDoesntOverrideElementTranslatorOnValdiation()
- {
- $registryTranslations = array('alphaInvalid' => 'Registry message');
- $registryTranslate = new Zend_Translate('array', $registryTranslations);
- Zend_Registry::set('Zend_Translate', $registryTranslate);
- $validatorTranslations = array('alphaInvalid' => 'Validator message');
- $validatorTranslate = new Zend_Translate('array', $validatorTranslations);
- Zend_Validate_Abstract::setDefaultTranslator($validatorTranslate);
- $elementTranslations = array('alphaInvalid' => 'Element message');
- $elementTranslate = new Zend_Translate('array', $elementTranslations);
- $this->element->addValidator('Alpha');
- $this->element->setTranslator($elementTranslate);
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals('Element message', $messages['alphaInvalid']);
- }
- /**
- * @group ZF-9275
- */
- public function testValidatorsDefaultTranslatorDoesntOverrideFormsDefaultTranslator()
- {
- $formTranslations = array('alphaInvalid' => 'Form message');
- $formTranslate = new Zend_Translate('array', $formTranslations);
- Zend_Form::setDefaultTranslator($formTranslate);
- $validatorTranslations = array('alphaInvalid' => 'Validator message');
- $validatorTranslate = new Zend_Translate('array', $validatorTranslations);
- Zend_Validate_Abstract::setDefaultTranslator($validatorTranslate);
- // the default validate translator should beat the registry one
- $this->element->addValidator('Alpha');
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals('Form message', $messages['alphaInvalid']);
- }
- /**
- * @group ZF-9275
- */
- public function testElementsTranslatorDoesntOverrideValidatorsDirectlyAttachedTranslator()
- {
- $elementTranslations = array('alphaInvalid' => 'Element message');
- $elementTranslate = new Zend_Translate('array', $elementTranslations);
- $validatorTranslations = array('alphaInvalid' => 'Direct validator message');
- $validatorTranslate = new Zend_Translate('array', $validatorTranslations);
- $validator = new Zend_Validate_Alpha();
- $validator->setTranslator($validatorTranslate);
- $this->element->addValidator($validator);
- $this->assertFalse($this->element->isValid(123));
- $messages = $this->element->getMessages();
- $this->assertEquals('Direct validator message', $messages['alphaInvalid']);
- }
- /**
- * Prove the fluent interface on Zend_Form::loadDefaultDecorators
- *
- * @link http://framework.zend.com/issues/browse/ZF-9913
- * @return void
- */
- public function testFluentInterfaceOnLoadDefaultDecorators()
- {
- $this->assertSame($this->element, $this->element->loadDefaultDecorators());
- }
- /**
- * @group ZF-7552
- */
- public function testAddDecoratorsKeepsNonNumericKeyNames()
- {
- $this->element->addDecorators(array(array(array('td' => 'HtmlTag'),
- array('tag' => 'td')),
- array(array('tr' => 'HtmlTag'),
- array('tag' => 'tr')),
- array('HtmlTag', array('tag' => 'baz'))));
- $t1 = $this->element->getDecorators();
- $this->element->setDecorators($t1);
- $t2 = $this->element->getDecorators();
- $this->assertEquals($t1, $t2);
- }
- /**
- * Check array notation for validators
- */
- public function testValidatorsGivenArrayKeysOnValidation()
- {
- $username = new Zend_Form_Element('username');
- $username->addValidator('stringLength', true, array('min' => 5, 'max' => 20, 'ignore' => 'something'));
- $form = new Zend_Form(array('elements' => array($username)));
- $this->assertTrue($form->isValid(array('username' => 'abcde')));
- }
- /**
- * @group ZF-6822
- */
- public function testValidatorByUsingStringNotation()
- {
- $this->_checkZf2794();
- $username = new Zend_Form_Element('username');
- $username->addValidator('stringLength', true, array(5, 20))
- ->addValidator('regex', true, '/^[a-zA-Z0-9_]*$/')
- ->addFilter('StringToLower')
- ->setRequired(true);
- $form = new Zend_Form(array('elements' => array($username)));
- $form->isValid(array('username' => '#'));
- $validator = $username->getValidator('stringLength');
- $this->assertTrue($validator->zfBreakChainOnFailure);
- $validator = $username->getValidator('regex');
- $this->assertTrue($validator->zfBreakChainOnFailure);
- }
-
- /**
- * @group ZF-12173
- */
- public function testCanAddPluginLoaderPrefixPathsWithBackslashes()
- {
- if (version_compare(PHP_VERSION, '5.3.0', '<')) {
- $this->markTestSkipped(__CLASS__ . '::' . __METHOD__ . ' requires PHP 5.3.0 or greater');
- return;
- }
- $validatorLoader = new Zend_Loader_PluginLoader();
- $filterLoader = new Zend_Loader_PluginLoader();
- $decoratorLoader = new Zend_Loader_PluginLoader();
- $this->element->setPluginLoader($validatorLoader, 'validate')
- ->setPluginLoader($filterLoader, 'filter')
- ->setPluginLoader($decoratorLoader, 'decorator')
- ->addPrefixPath('Zf\Foo', 'Zf/Foo');
- $paths = $filterLoader->getPaths('Zf\Foo\Filter');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Filter', $paths[0]);
- $paths = $validatorLoader->getPaths('Zf\Foo\Validate');
- $this->assertTrue(is_array($paths));
- $this->assertContains('Validate', $paths[0]);
- $paths = $decoratorLoader->getPaths('Zf\Foo\Decorator');
- $this->assertTrue(is_array($paths), var_export($paths, 1));
- $this->assertContains('Decorator', $paths[0]);
- }
- /**
- * @group ZF-12439
- */
- public function testSettingAnEmptyArrayValueGeneratesAValidErrorMessage()
- {
- $this->element->setValue(array());
- $this->element->addErrorMessage('Invalid value entered');
- $this->element->markAsError();
- $messages = $this->element->getMessages();
- $this->assertEquals('Invalid value entered', array_shift($messages));
- }
- }
- class Zend_Form_ElementTest_Decorator extends Zend_Form_Decorator_Abstract
- {
- }
- class Zend_Form_ElementTest_Element extends Zend_Form_Element
- {
- public function init()
- {
- $this->setDisableLoadDefaultDecorators(true);
- }
- }
- class Zend_Form_ElementTest_ArrayFilter implements Zend_Filter_Interface
- {
- public function filter($value)
- {
- $value = array_filter($value, array($this, '_filter'));
- return $value;
- }
- protected function _filter($value)
- {
- if (is_array($value)) {
- return array_filter($value, array($this, '_filter'));
- }
- return (strstr($value, 'ba'));
- }
- }
- if (PHPUnit_MAIN_METHOD == 'Zend_Form_ElementTest::main') {
- Zend_Form_ElementTest::main();
- }
|