Added feature #16437: Introduce a form protection API (Thanks to the Security Team...
[Packages/TYPO3.CMS.git] / tests / t3lib / formprotection / t3lib_formprotection_FactoryTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Oliver Klee (typo3-coding@oliverklee.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 require_once('fixtures/class.t3lib_formprotection_testing.php');
26
27 /**
28 * Testcase for the t3lib_formprotection_Factory class.
29 *
30 * $Id$
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 *
35 * @author Oliver Klee <typo3-coding@oliverklee.de>
36 * @author Ernesto Baschny <ernst@cron-it.de>
37 */
38 class t3lib_formprotection_FactoryTest extends tx_phpunit_testcase {
39 public function setUp() {
40 }
41
42 public function tearDown() {
43 t3lib_formprotection_Factory::purgeInstances();
44 }
45
46
47 /////////////////////////
48 // Tests concerning get
49 /////////////////////////
50
51 /**
52 * @test
53 *
54 * @expectedException InvalidArgumentException
55 */
56 public function getForInexistentClassThrowsException() {
57 t3lib_formprotection_Factory::get('noSuchClass');
58 }
59
60 /**
61 * @test
62 *
63 * @expectedException InvalidArgumentException
64 */
65 public function getForClassThatIsNoFormProtectionSubclassThrowsException() {
66 t3lib_formprotection_Factory::get('t3lib_formprotection_FactoryTest');
67 }
68
69 /**
70 * @test
71 */
72 public function getForTypeBackEndWithExistingBackEndReturnsBackEndFormProtection() {
73 $this->assertTrue(
74 t3lib_formprotection_Factory::get(
75 't3lib_formprotection_BackendFormProtection'
76 ) instanceof t3lib_formprotection_BackendFormProtection
77 );
78 }
79
80 /**
81 * @test
82 */
83 public function getForTypeBackEndCalledTwoTimesReturnsTheSameInstance() {
84 $this->assertSame(
85 t3lib_formprotection_Factory::get(
86 't3lib_formprotection_BackendFormProtection'
87 ),
88 t3lib_formprotection_Factory::get(
89 't3lib_formprotection_BackendFormProtection'
90 )
91 );
92 }
93
94 /**
95 * @test
96 */
97 public function getForTypeInstallToolReturnsInstallToolFormProtection() {
98 $this->assertTrue(
99 t3lib_formprotection_Factory::get(
100 't3lib_formprotection_InstallToolFormProtection'
101 ) instanceof t3lib_formprotection_InstallToolFormProtection
102 );
103 }
104
105 /**
106 * @test
107 */
108 public function getForTypeInstallToolCalledTwoTimesReturnsTheSameInstance() {
109 $this->assertSame(
110 t3lib_formprotection_Factory::get(
111 't3lib_formprotection_InstallToolFormProtection'
112 ),
113 t3lib_formprotection_Factory::get(
114 't3lib_formprotection_InstallToolFormProtection'
115 )
116 );
117 }
118
119 /**
120 * @test
121 */
122 public function getForTypesInstallToolAndBackEndReturnsDifferentInstances() {
123 $this->assertNotSame(
124 t3lib_formprotection_Factory::get(
125 't3lib_formprotection_InstallToolFormProtection'
126 ),
127 t3lib_formprotection_Factory::get(
128 't3lib_formprotection_BackendFormProtection'
129 )
130 );
131 }
132
133
134 /////////////////////////
135 // Tests concerning set
136 /////////////////////////
137
138 /**
139 * @test
140 */
141 public function setSetsInstanceForType() {
142 $instance = new t3lib_formProtection_Testing();
143 t3lib_formprotection_Factory::set(
144 't3lib_formprotection_BackendFormProtection', $instance
145 );
146
147 $this->assertSame(
148 $instance,
149 t3lib_formprotection_Factory::get(
150 't3lib_formprotection_BackendFormProtection'
151 )
152 );
153 }
154
155 /**
156 * @test
157 */
158 public function setNotSetsInstanceForOtherType() {
159 $instance = new t3lib_formProtection_Testing();
160 t3lib_formprotection_Factory::set(
161 't3lib_formprotection_BackendFormProtection', $instance
162 );
163
164 $this->assertNotSame(
165 $instance,
166 t3lib_formprotection_Factory::get(
167 't3lib_formprotection_InstallToolFormProtection'
168 )
169 );
170 }
171 }
172 ?>