TYPO3 API  SVNRelease
t3lib_formprotection_InstallToolFormProtectionTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 * Copyright notice
00004 *
00005 * (c) 2010-2011 Oliver Klee (typo3-coding@oliverklee.de)
00006 * All rights reserved
00007 *
00008 * This script is part of the TYPO3 project. The TYPO3 project is
00009 * free software; you can redistribute it and/or modify
00010 * it under the terms of the GNU General Public License as published by
00011 * the Free Software Foundation; either version 2 of the License, or
00012 * (at your option) any later version.
00013 *
00014 * The GNU General Public License can be found at
00015 * http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 * This script is distributed in the hope that it will be useful,
00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 * GNU General Public License for more details.
00021 *
00022 * This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 require_once(t3lib_extMgm::extPath('install') . 'mod/class.tx_install.php');
00026 
00027 /**
00028  * Testcase for the t3lib_formprotection_InstallToolFormProtection class.
00029  *
00030  * $Id$
00031  *
00032  * @package TYPO3
00033  * @subpackage t3lib
00034  *
00035  * @author Oliver Klee <typo3-coding@oliverklee.de>
00036  */
00037 class t3lib_formprotection_InstallToolFormProtectionTest extends tx_phpunit_testcase {
00038     /**
00039      * @var t3lib_formprotection_InstallToolFormProtection
00040      */
00041     private $fixture;
00042 
00043     /**
00044      * backup of $_SESSION
00045      *
00046      * @var array
00047      */
00048     private $sessionBackup;
00049 
00050     public function setUp() {
00051         $this->sessionBackup = $_SESSION;
00052 
00053         $className = $this->createAccessibleProxyClass();
00054         $this->fixture = new $className();
00055     }
00056 
00057     public function tearDown() {
00058         $this->fixture->__destruct();
00059         unset($this->fixture);
00060 
00061         t3lib_FlashMessageQueue::getAllMessagesAndFlush();
00062 
00063         $_SESSION = $this->sessionBackup;
00064     }
00065 
00066 
00067     //////////////////////
00068     // Utility functions
00069     //////////////////////
00070 
00071     /**
00072      * Creates a subclass t3lib_formprotection_InstallToolFormProtection with retrieveTokens made
00073      * public.
00074      *
00075      * @return string the name of the created class, will not be empty
00076      */
00077     private function createAccessibleProxyClass() {
00078         $className = 't3lib_formprotection_InstallToolFormProtectionAccessibleProxy';
00079         if (!class_exists($className)) {
00080             eval(
00081                 'class ' . $className . ' extends t3lib_formprotection_InstallToolFormProtection {' .
00082                 '  public function createValidationErrorMessage() {' .
00083                 '    parent::createValidationErrorMessage();' .
00084                 '  }' .
00085                 '  public function retrieveTokens() {' .
00086                 '    return $this->tokens = parent::retrieveTokens();' .
00087                 '  }' .
00088                 '}'
00089             );
00090         }
00091 
00092         return $className;
00093     }
00094 
00095 
00096     ////////////////////////////////////
00097     // Tests for the utility functions
00098     ////////////////////////////////////
00099 
00100     /**
00101      * @test
00102      */
00103     public function createAccessibleProxyCreatesInstallToolFormProtectionSubclass() {
00104         $className = $this->createAccessibleProxyClass();
00105 
00106         $this->assertTrue(
00107             (new $className()) instanceof t3lib_formprotection_InstallToolFormProtection
00108         );
00109     }
00110 
00111 
00112     //////////////////////////////////////////////////////////
00113     // Tests concerning the reading and saving of the tokens
00114     //////////////////////////////////////////////////////////
00115 
00116     /**
00117      * @test
00118      */
00119     public function tokensFromSessionDataAreAvailableForValidateToken() {
00120         $tokenId = '51a655b55c54d54e5454c5f521f6552a';
00121         $formName = 'foo';
00122         $action = 'edit';
00123         $formInstanceName = '42';
00124 
00125         $_SESSION['installToolFormTokens'] = array(
00126             $tokenId => array(
00127                 'formName' => $formName,
00128                 'action' => $action,
00129                 'formInstanceName' => $formInstanceName,
00130             ),
00131         );
00132 
00133         $this->fixture->retrieveTokens();
00134 
00135         $this->assertTrue(
00136             $this->fixture->validateToken(
00137                 $tokenId, $formName, $action,  $formInstanceName
00138             )
00139         );
00140     }
00141 
00142     /**
00143      * @test
00144      */
00145     public function persistTokensWritesTokensToSession() {
00146         $formName = 'foo';
00147         $action = 'edit';
00148         $formInstanceName = '42';
00149 
00150         $tokenId = $this->fixture->generateToken(
00151             $formName, $action, $formInstanceName
00152         );
00153 
00154         $this->fixture->persistTokens();
00155 
00156         $this->assertEquals(
00157             array(
00158                 $tokenId => array(
00159                         'formName' => $formName,
00160                         'action' => $action,
00161                         'formInstanceName' => $formInstanceName,
00162                     ),
00163             ),
00164             $_SESSION['installToolFormTokens']
00165         );
00166     }
00167 
00168 
00169     //////////////////////////////////////////////////
00170     // Tests concerning createValidationErrorMessage
00171     //////////////////////////////////////////////////
00172 
00173     /**
00174      * @test
00175      */
00176     public function createValidationErrorMessageAddsErrorMessage() {
00177         $installTool = $this->getMock(
00178             'tx_install', array('addErrorMessage'), array(), '', FALSE
00179         );
00180         $installTool->expects($this->once())->method('addErrorMessage')
00181             ->with(
00182                 'Validating the security token of this form has failed. ' .
00183                     'Please reload the form and submit it again.'
00184             );
00185         $this->fixture->injectInstallTool($installTool);
00186 
00187         $this->fixture->createValidationErrorMessage();
00188     }
00189 }
00190 ?>