From ddd86e6f5a5955627b18904fb16cc670cfa11efc Mon Sep 17 00:00:00 2001 From: efalcy <efalcy@git-test.afi-sa.fr> Date: Thu, 15 Nov 2012 16:31:10 +0000 Subject: [PATCH] Formulaire : remplace les actions et submit --- .gitattributes | 1 + library/Class/Article.php | 30 +++++ tests/library/Class/ArticleFormulaireTest.php | 111 ++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 tests/library/Class/ArticleFormulaireTest.php diff --git a/.gitattributes b/.gitattributes index 3289aa654a9..3f08122637a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4598,6 +4598,7 @@ tests/library/Class/AlbumRessourceTest.php -text tests/library/Class/AlbumTest.php -text tests/library/Class/ArteVodLinkTest.php -text tests/library/Class/ArticleCategorieTest.php -text +tests/library/Class/ArticleFormulaireTest.php -text tests/library/Class/ArticleLoaderTest.php -text tests/library/Class/ArticleTest.php -text tests/library/Class/AvisNoticeTest.php -text diff --git a/library/Class/Article.php b/library/Class/Article.php index 52decd951cb..e12c7a24f80 100644 --- a/library/Class/Article.php +++ b/library/Class/Article.php @@ -708,6 +708,7 @@ class Class_Article extends Storm_Model_Abstract { return (trim($head_and_content[0])); } + /** * @return string */ @@ -720,6 +721,35 @@ class Class_Article extends Storm_Model_Abstract { return $content; } + + /** + * @return string + */ + public function getContenu() { + $contenu = parent::_get('contenu'); + + if (preg_match('/(<form[^>]+)action=[\"\']http/', $contenu)) + return $contenu; + + $replaced_form = preg_replace(['/(<form[^>]+)action=[\"\'][^\"\']+\"? /', + '/(<form )/'], + ['$1 ', + '$1action="'.BASE_URL.'/formulaire/add" '], + $contenu); + + $typesubmit = 'type=[\'\"]submit[\'\"]'; + $namesubmit = 'name=[\"\'][^\"\']+[\'\"]'; + $otherattributes = '[^>]+'; + $inputtag = '<input'; + return preg_replace(['/('.$inputtag.$otherattributes.')'.$typesubmit.'('.$otherattributes.')'.$namesubmit.'/', + '/('.$inputtag.$otherattributes.')'.$namesubmit.'('.$otherattributes.')'.$typesubmit.'/'], + ['$1$2type="submit"', + '$1$2type="submit"'], + $replaced_form); + + } + + /** * @return string */ diff --git a/tests/library/Class/ArticleFormulaireTest.php b/tests/library/Class/ArticleFormulaireTest.php new file mode 100644 index 00000000000..952c15ad75a --- /dev/null +++ b/tests/library/Class/ArticleFormulaireTest.php @@ -0,0 +1,111 @@ +<?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 + */ + + +class ArticleFormulaireInternalTest extends Storm_Test_ModelTestCase { + public function setUp() { + parent::setUp(); + $this->_article = Class_Article::newInstanceWithId(2,['titre' => 'Contactez-nous !', + 'contenu' => '<form id="idform" action="form" method="post" name="form" target="_blank"> + <p> Donnee 1 :<br /><input name="champs texte" type="text" value="champtxt" /> + <input value="champ2" name=\'champs texte\' type="text"/></p> + <p> </p> + <p> <input name="click" type="submit" value="click !" /></p> + <input name="envoi" value="envoi" type="submit"/> + <input type="submit" value="send" name="send"/> + + </form> + <form method="POST"> + <input type="submit"/> + </form> + ']); + } + + /** @test */ + public function formIdFormActionShouldBeFormulaireAdd() { + $this->assertContains('<form action="'.BASE_URL.'/formulaire/add" id="idform"', + $this->_article->getContenu()); + } + + + /** @test */ + public function emptyFormActionShouldBeFormulaireAdd() { + $this->assertContains('<form action="'.BASE_URL.'/formulaire/add" method="POST">', + $this->_article->getContenu()); + } + + + /** @test */ + public function formSubmitButtonShouldHaveNoName() { + $this->assertContains('<input type="submit" value="click !"', + $this->_article->getContenu()); + } + + + + /** @test */ + public function formNotSubmitButtonShouldNotBeChanged() { + $this->assertContains( + '<input name="champs texte" type="text" value="champtxt" />', + $this->_article->getContenu()); + } + + + /** @test */ + public function formSubmitButtonNamedEnvoiShouldHaveNoName() { + $this->assertContains('<input value="envoi" type="submit"/>', + $this->_article->getContenu()); + } + + /** @test */ + public function formSubmitButtonNamedSendShouldHaveNoName() { + $this->assertContains('<input value="send" type="submit"/>', + $this->_article->getContenu()); + } + + + +} +class ArticleFormulaireExternalTest extends Storm_Test_ModelTestCase { + public function setUp() { + parent::setUp(); + $this->_article = Class_Article::newInstanceWithId(2,['titre' => 'Contactez-nous !', + 'contenu' => ' + <form id="extern" action="http://monserveur/post" > + <input name="extenvoi" value="extenvoi" type="submit"/> + </form> + ']); + } + + + /** @test */ + public function formWithExternalUrlShouldNotChange() { + $this->assertContains('<form id="extern" action="http://monserveur/post" >', + $this->_article->getContenu()); + } + + /** @test */ + public function formSubmitWithExternalUrlShouldNotChange() { + $this->assertContains(' <input name="extenvoi" value="extenvoi" type="submit"/>', + $this->_article->getContenu()); + } + +} -- GitLab