[FEATURE] Add several testcases for validators
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Validation / AlphabeticValidatorTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Validation;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Andreas Lappe <a.lappe@kuehlhaus.com>, kuehlhaus AG
8 *
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Test case for class \TYPO3\CMS\Form\System\Validation\AlphabeticValidator.
30 *
31 * @author Andreas Lappe <a.lappe@kuehlhaus.com>
32 * @package TYPO3
33 * @subpackage form
34 */
35 class AlphabeticValidatorTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
36 /**
37 * @var \TYPO3\CMS\Form\Tests\Unit\Validation\Helper
38 */
39 protected $helper;
40
41 /**
42 * @var \TYPO3\CMS\Form\Validation\AlphabeticValidator
43 */
44 protected $fixture;
45
46 public function setUp() {
47 $this->helper = new \TYPO3\CMS\Form\Tests\Unit\Validation\Helper();
48 $this->fixture = new \TYPO3\CMS\Form\Validation\AlphabeticValidator();
49 }
50
51 public function tearDown() {
52 unset($this->helper, $this->fixture);
53 }
54
55 public function validDataProviderWithoutWhitespace() {
56 return array(
57 'ascii without spaces' => array('thisismyinput'),
58 'accents without spaces' => array('éóéàèò'),
59 'umlauts without spaces' => array('üöä'),
60 'empty string' => array('')
61 );
62 }
63
64 public function validDataProviderWithWhitespace() {
65 return array(
66 'ascii with spaces' => array('This is my input'),
67 'accents with spaces' => array('Sigur Rós'),
68 'umlauts with spaces' => array('Hürriyet Daily News'),
69 'space' => array(' '),
70 'empty string' => array('')
71 );
72 }
73
74 public function invalidDataProviderWithoutWhitespace() {
75 return array(
76 'ascii with dash' => array('my-name'),
77 'accents with underscore' => array('Sigur_Rós'),
78 'umlauts with periods' => array('Hürriyet.Daily.News'),
79 'space' => array(' '),
80 );
81 }
82
83 public function invalidDataProviderWithWhitespace() {
84 return array(
85 'ascii with spaces and dashes' => array('This is my-name'),
86 'accents with spaces and underscores' => array('Listen to Sigur_Rós_Band'),
87 'umlauts with spaces and periods' => array('Go get the Hürriyet.Daily.News')
88 );
89 }
90
91 /**
92 * @test
93 * @dataProvider validDataProviderWithoutWhitespace
94 */
95 public function isValidForValidInputWithoutAllowedWhitespaceReturnsTrue($input) {
96 $requestHandlerMock = $this->helper->getRequestHandler(array(
97 'name' => $input
98 ));
99
100 $this->fixture->setFieldName('name');
101 $this->fixture->injectRequestHandler($requestHandlerMock);
102
103 $this->assertTrue(
104 $this->fixture->isValid()
105 );
106 }
107
108 /**
109 * @test
110 * @dataProvider validDataProviderWithWhitespace
111 */
112 public function isValidForValidInputWithWhitespaceAllowedReturnsTrue($input) {
113 $requestHandlerMock = $this->helper->getRequestHandler(array(
114 'name' => $input
115 ));
116
117 $this->fixture->setAllowWhiteSpace(TRUE);
118 $this->fixture->setFieldName('name');
119 $this->fixture->injectRequestHandler($requestHandlerMock);
120
121 $this->assertTrue(
122 $this->fixture->isValid()
123 );
124 }
125
126 /**
127 * @test
128 * @dataProvider invalidDataProviderWithoutWhitespace
129 */
130 public function isValidForInvalidInputWithoutAllowedWhitespaceReturnsFalse($input) {
131 $requestHandlerMock = $this->helper->getRequestHandler(array(
132 'name' => $input
133 ));
134
135 $this->fixture->setFieldName('name');
136 $this->fixture->injectRequestHandler($requestHandlerMock);
137
138 $this->assertFalse(
139 $this->fixture->isValid()
140 );
141 }
142
143 /**
144 * @test
145 * @dataProvider invalidDataProviderWithWhitespace
146 */
147 public function isValidForInvalidInputWithWhitespaceAllowedReturnsFalse($input) {
148 $requestHandlerMock = $this->helper->getRequestHandler(array(
149 'name' => $input
150 ));
151
152 $this->fixture->setAllowWhiteSpace(TRUE);
153 $this->fixture->setFieldName('name');
154 $this->fixture->injectRequestHandler($requestHandlerMock);
155
156 $this->assertFalse(
157 $this->fixture->isValid()
158 );
159 }
160 }
161 ?>