From 0bf347cc4e483fa5bb54db5aff5cad7c53e46753 Mon Sep 17 00:00:00 2001 From: llaffont <llaffont@git-test.afi-sa.fr> Date: Wed, 18 Apr 2012 10:01:34 +0000 Subject: [PATCH] Tests du renderform + n'alerte pas sur annuler si modif du formulaire et boutons Zend --- .gitattributes | 1 + library/ZendAfi/View/Helper/RenderForm.php | 47 ++++--- .../ZendAfi/View/Helper/RenderFormTest.php | 124 ++++++++++++++++++ 3 files changed, 153 insertions(+), 19 deletions(-) create mode 100644 tests/library/ZendAfi/View/Helper/RenderFormTest.php diff --git a/.gitattributes b/.gitattributes index 0b0c61cd571..a85d318e2f7 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3525,6 +3525,7 @@ tests/library/ZendAfi/View/Helper/AvisTest.php -text tests/library/ZendAfi/View/Helper/FicheAbonneLinksTest.php -text tests/library/ZendAfi/View/Helper/IconeSupportTest.php -text tests/library/ZendAfi/View/Helper/IframeContainerTest.php -text +tests/library/ZendAfi/View/Helper/RenderFormTest.php -text tests/library/ZendAfi/View/Helper/SubwordsTest.php -text tests/library/ZendAfi/View/Helper/TagAnchorTest.php -text tests/library/ZendAfi/View/Helper/TagArticleEventTest.php -text diff --git a/library/ZendAfi/View/Helper/RenderForm.php b/library/ZendAfi/View/Helper/RenderForm.php index 725babbf3a7..4e1890cfad9 100644 --- a/library/ZendAfi/View/Helper/RenderForm.php +++ b/library/ZendAfi/View/Helper/RenderForm.php @@ -24,10 +24,6 @@ class ZendAfi_View_Helper_RenderForm extends ZendAfi_View_Helper_BaseHelper { * @return string */ public function renderForm($form) { - Class_ScriptLoader::getInstance() - ->addAdminScript('controle_maj') - ->addJQueryReady('$("form input").change(function(){setFlagMaj(true)})'); - $form->setAttrib('class', trim($form->getAttrib('class').' form')); // compatibilité avec les tables admin standard @@ -36,23 +32,31 @@ class ZendAfi_View_Helper_RenderForm extends ZendAfi_View_Helper_BaseHelper { if ($this->isFormContainsSubmit($form)) return $form->render(); + + return $this->renderFormWithButtonsValiderAnnuler($form); + } + + + public function renderFormWithButtonsValiderAnnuler($form) { + Class_ScriptLoader::getInstance() + ->addAdminScript('controle_maj') + ->addJQueryReady('$("form input").change(function(){setFlagMaj(true)})'); + return $form->render().$this->_buttonsFor($form->getAttrib('id')); } public function isFormContainsSubmit($form) { - foreach($form->getElements() as $element) { - if ($element->helper == 'formSubmit') - return true; - } - return false; + $isFormContainsSubmit = false; + + foreach($form->getElements() as $element) + $isFormContainsSubmit = ($isFormContainsSubmit || ($element->helper == 'formSubmit')); + + return $isFormContainsSubmit; } public function setupTableDecoratorsForForm($form) { - $td_gauche_tag = array(array('input_data' => 'HtmlTag'), - array('tag' => 'td', 'class' => 'gauche')); - $form ->setDisplayGroupDecorators(array( 'FormElements', @@ -61,9 +65,16 @@ class ZendAfi_View_Helper_RenderForm extends ZendAfi_View_Helper_BaseHelper { ->removeDecorator('HtmlTag'); - foreach ($form->getElements() as $element) { - $decorators = $element->getDecorators(); + foreach ($form->getElements() as $element) + $element->setDecorators($this->_decoratorsForTableRendering($element)); + + return $this; + } + + + protected function _decoratorsForTableRendering($element) { $newDecorators = array(); + $decorators = $element->getDecorators(); foreach ($decorators as $name => $decorator) { $name = explode('_', $name); @@ -72,7 +83,8 @@ class ZendAfi_View_Helper_RenderForm extends ZendAfi_View_Helper_BaseHelper { switch ($name) { case 'label': - $newDecorators[] = $td_gauche_tag; + $newDecorators[] = array(array('input_data' => 'HtmlTag'), + array('tag' => 'td', 'class' => 'gauche')); $decorator->setOption('tag', 'td'); $newDecorators[$name] = $decorator; $newDecorators[] = array('HtmlTag', array('tag' => 'tr')); @@ -92,10 +104,7 @@ class ZendAfi_View_Helper_RenderForm extends ZendAfi_View_Helper_BaseHelper { } } - - $element->setDecorators($newDecorators); - } - return $this; + return $newDecorators; } diff --git a/tests/library/ZendAfi/View/Helper/RenderFormTest.php b/tests/library/ZendAfi/View/Helper/RenderFormTest.php new file mode 100644 index 00000000000..075079fc2bb --- /dev/null +++ b/tests/library/ZendAfi/View/Helper/RenderFormTest.php @@ -0,0 +1,124 @@ +<?php +/** + * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved. + * + * AFI-OPAC 2.0 is free software; you can redistribute it and/or modify + * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by + * the Free Software Foundation. + * + * There are special exceptions to the terms and conditions of the AGPL as it + * is applied to this software (see README file). + * + * AFI-OPAC 2.0 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE + * along with AFI-OPAC 2.0; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +abstract class ZendAfi_View_Helper_RenderFormTestCase extends ViewHelperTestCase { + /** @var ZendAfi_View_Helper_RenderForm */ + protected $_helper; + + /** @var string */ + protected $_html; + + public function setUp() { + parent::setUp(); + Class_ScriptLoader::resetInstance(); + + $view = new ZendAfi_Controller_Action_Helper_View(); + $this->_helper = new ZendAfi_View_Helper_RenderForm(); + $this->_helper->setView($view); + + $this->_form = $view->newForm(array('id' => 'form_personne')) + ->addElement('text', 'nom', array('label' => 'nom')) + ->addElement('text', 'prenom', array('label' => 'prenom')) + ->addDisplayGroup(array('nom', 'prenom'), + 'personne', + array('legend' => 'Personne')); + } +} + + +class ZendAfi_View_Helper_RenderFormWithoutSubmitButtonsTest extends ZendAfi_View_Helper_RenderFormTestCase { + public function setUp() { + parent::setUp(); + $this->_html = $this->_helper->renderForm($this->_form); + } + + + /** @test */ + public function labelNomShouldBeInFirstTD() { + $this->assertXPathContentContains($this->_html, '//table/tr[1]/td[1]', 'nom'); + } + + + /** @test */ + public function textFieldNomShouldBeInSecondTD() { + $this->assertXPath($this->_html, '//table/tr[1]/td[2][@class="gauche"]/input[@name="nom"]'); + } + + + /** @test */ + public function labelPrenomShouldBeInFirstTDOfSecondTR() { + $this->assertXPathContentContains($this->_html, '//table/tr[2]/td[1]', 'prenom'); + } + + + /** @test */ + public function buttonValiderShouldBePresent() { + $this->assertXPath($this->_html, + '//div[@class="bouton"][contains(@onclick, "submit")][contains(@onclick, "setFlagMaj(false)")]//img[@alt="valider"]', + $this->_html); + } + + + /** @test */ + public function buttonAnnulerShouldBePresent() { + $this->assertXPath($this->_html, + '//div[@class="bouton"][not(contains(@onclick, "submit"))]', + $this->_html); + } + + + /** @test */ + public function scriptLoaderShouldSetFlagMaj() { + $this->assertContains('setFlagMaj(true)', Class_ScriptLoader::getInstance()->html()); + } +} + + + +class ZendAfi_View_Helper_RenderFormWithSubmitButtonsTest extends ZendAfi_View_Helper_RenderFormTestCase { + public function setUp() { + parent::setUp(); + $this->_form + ->addElement('Submit', 'Valider') + ->addElement('Submit', 'Annuler'); + $this->_html = $this->_helper->renderForm($this->_form); + } + + + /** @test */ + public function customButtonHelpersShouldNotBePresent() { + $this->assertNotXPath($this->_html, '//div[@class="bouton"]'); + } + + + /** @test */ + public function scriptLoaderShouldNotSetFlagMaj() { + $this->assertNotContains('setFlagMaj', Class_ScriptLoader::getInstance()->html()); + } + + + /** @test */ + public function inputShouldNotBeInTD() { + $this->assertNotXPath($this->_html, '//td//submit'); + } +} + +?> \ No newline at end of file -- GitLab