Adding enclosures now treats type and length parameters as optional for Atom, but...
[zend.git] / tests / Zend / Captcha / FigletTest.php
blob33029e6e3238497ca9a55ed7d1244a451d525fbe
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Captcha
17 * @subpackage UnitTests
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id$
23 // Call Zend_Captcha_FigletTest::main() if this source file is executed directly.
24 if (!defined("PHPUnit_MAIN_METHOD")) {
25 define("PHPUnit_MAIN_METHOD", "Zend_Captcha_FigletTest::main");
28 require_once dirname(__FILE__) . '/../../TestHelper.php';
30 require_once 'Zend/Form/Element/Captcha.php';
31 require_once 'Zend/Captcha/Adapter.php';
32 require_once 'Zend/Config.php';
34 /**
35 * @category Zend
36 * @package Zend_Captcha
37 * @subpackage UnitTests
38 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
40 * @group Zend_Captcha
42 class Zend_Captcha_FigletTest extends PHPUnit_Framework_TestCase
44 /**
45 * Runs the test methods of this class.
47 * @return void
49 public static function main()
51 $suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_FigletTest");
52 $result = PHPUnit_TextUI_TestRunner::run($suite);
55 /**
56 * Sets up the fixture, for example, open a network connection.
57 * This method is called before a test is executed.
59 * @return void
61 public function setUp()
63 if (isset($this->word)) {
64 unset($this->word);
67 $this->element = new Zend_Form_Element_Captcha(
68 'captchaF',
69 array(
70 'captcha' => array(
71 'Figlet',
72 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer'
76 $this->captcha = $this->element->getCaptcha();
79 /**
80 * Tears down the fixture, for example, close a network connection.
81 * This method is called after a test is executed.
83 * @return void
85 public function tearDown()
89 public function testCaptchaAdapterCreated()
91 $this->assertTrue($this->element->getCaptcha() instanceof Zend_Captcha_Adapter);
94 public function getView()
96 require_once 'Zend/View.php';
97 $view = new Zend_View();
98 $view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
99 return $view;
102 public function testCaptchaIsRendered()
104 $html = $this->element->render($this->getView());
105 $this->assertContains($this->element->getName(), $html);
108 public function testCaptchaHasIdAndInput()
110 $html = $this->element->render($this->getView());
111 $expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId());
112 $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
113 $expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName());
114 $this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
118 * @see ZF-8268
119 * @group ZF-8268
121 public function testLabelIdIsCorrect()
123 require_once 'Zend/Form.php';
124 $form = new Zend_Form();
125 $form->setElementsBelongTo('comment');
126 $this->element->setLabel("My Captcha");
127 $form->addElement($this->element);
128 $html = $form->render($this->getView());
129 $expect = sprintf('for="comment-%s-input"', $this->element->getName());
130 $this->assertRegexp("/<label [^>]*?$expect/", $html, $html);
133 public function testTimeoutPopulatedByDefault()
135 $ttl = $this->captcha->getTimeout();
136 $this->assertFalse(empty($ttl));
137 $this->assertTrue(is_int($ttl));
140 public function testCanSetTimeout()
142 $ttl = $this->captcha->getTimeout();
143 $this->captcha->setTimeout(3600);
144 $this->assertNotEquals($ttl, $this->captcha->getTimeout());
145 $this->assertEquals(3600, $this->captcha->getTimeout());
148 public function testGenerateReturnsId()
150 $id = $this->captcha->generate();
151 $this->assertFalse(empty($id));
152 $this->assertTrue(is_string($id));
153 $this->id = $id;
156 public function testGetWordReturnsWord()
158 $this->captcha->generate();
159 $word = $this->captcha->getWord();
160 $this->assertFalse(empty($word));
161 $this->assertTrue(is_string($word));
162 $this->assertTrue(strlen($word) == 8);
163 $this->word = $word;
166 public function testGetWordLength()
168 $this->captcha->setWordLen(4);
169 $this->captcha->generate();
170 $word = $this->captcha->getWord();
171 $this->assertTrue(is_string($word));
172 $this->assertTrue(strlen($word) == 4);
173 $this->word = $word;
176 public function testAdapterElementName()
178 $this->assertEquals($this->captcha->getName(),
179 $this->element->getName());
182 public function testGenerateIsRandomised()
184 $id1 = $this->captcha->generate();
185 $word1 = $this->captcha->getWord();
186 $id2 = $this->captcha->generate();
187 $word2 = $this->captcha->getWord();
189 $this->assertFalse(empty($id1));
190 $this->assertFalse(empty($id2));
191 $this->assertFalse($id1 == $id2);
192 $this->assertFalse($word1 == $word2);
195 public function testRenderSetsValue()
197 $this->testCaptchaIsRendered();
198 $this->assertEquals($this->captcha->getId(),
199 $this->element->getValue());
202 public function testLabelIsNull()
204 $this->assertNull($this->element->getLabel());
207 public function testRenderInitializesSessionData()
209 $this->testCaptchaIsRendered();
210 $session = $this->captcha->getSession();
211 $this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
212 $this->assertEquals(1, $session->setExpirationHops);
213 $this->assertEquals($this->captcha->getWord(), $session->word);
216 public function testWordValidates()
218 $this->testCaptchaIsRendered();
219 $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
220 $this->assertTrue($this->element->isValid("", $input));
223 public function testMissingNotValid()
225 $this->testCaptchaIsRendered();
226 $this->assertFalse($this->element->isValid("", array()));
227 $input = array($this->element->getName() => array("input" => "blah"));
228 $this->assertFalse($this->element->isValid("", $input));
231 public function testWrongWordNotValid()
233 $this->testCaptchaIsRendered();
234 $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
235 $this->assertFalse($this->element->isValid("", $input));
238 public function testUsesWordCaptchaDecoratorByDefault()
240 $this->assertEquals('Captcha_Word', $this->element->getCaptcha()->getDecorator());
243 public function testCaptchaShouldBeConfigurableViaConfigObject()
245 $options = array(
246 'name' => 'foo',
247 'sessionClass' => 'Zend_Captcha_FigletTest_SessionContainer',
248 'wordLen' => 6,
249 'timeout' => 300,
251 $config = new Zend_Config($options);
252 $captcha = new Zend_Captcha_Figlet($config);
253 $test = $captcha->getOptions();
254 $this->assertEquals($options, $test);
257 public function testShouldAllowFigletsLargerThanFourteenCharacters()
259 $this->captcha->setName('foo')
260 ->setWordLen(14);
261 $id = $this->captcha->generate();
264 public function testShouldNotValidateEmptyInputAgainstEmptySession()
266 // Regression Test for ZF-4245
267 $this->captcha->setName('foo')
268 ->setWordLen(6)
269 ->setTimeout(300);
270 $id = $this->captcha->generate();
271 // Unset the generated word
272 // we have to reset $this->captcha for that
273 $this->captcha->getSession()->word = null;
274 $this->setUp();
275 $this->captcha->setName('foo')
276 ->setWordLen(6)
277 ->setTimeout(300);
278 $empty = array($this->captcha->getName() => array('id' => $id, 'input' => ''));
279 $this->assertEquals(false, $this->captcha->isValid(null, $empty));
283 * @group ZF-3995
285 public function testIsValidShouldAllowPassingArrayValueAndOmittingContext()
287 $this->testCaptchaIsRendered();
288 $input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
289 $this->assertTrue($this->element->isValid($input));
293 * @group ZF-3995
295 public function testIsValidShouldNotRequireValueToBeNestedArray()
297 $this->testCaptchaIsRendered();
298 $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
299 $this->assertTrue($this->element->isValid($input));
303 * @group ZF-5728
305 public function testSetSessionWorks()
307 if(headers_sent($file, $line)) {
308 $this->markTestSkipped("Cannot use sessions because headers already sent");
310 require_once 'Zend/Session/Namespace.php';
311 $session = new Zend_Session_Namespace('captcha');
312 $this->captcha->setSession($session);
313 $this->testCaptchaIsRendered();
314 $input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
315 $this->assertTrue($this->element->isValid($input));
316 $this->assertEquals($session->word, $this->captcha->getWord());
320 class Zend_Captcha_FigletTest_SessionContainer
322 protected static $_word;
324 public function __get($name)
326 if ('word' == $name) {
327 return self::$_word;
330 return null;
333 public function __set($name, $value)
335 if ('word' == $name) {
336 self::$_word = $value;
337 } else {
338 $this->$name = $value;
342 public function __isset($name)
344 if (('word' == $name) && (null !== self::$_word)) {
345 return true;
348 return false;
351 public function __call($method, $args)
353 switch ($method) {
354 case 'setExpirationHops':
355 case 'setExpirationSeconds':
356 $this->$method = array_shift($args);
357 break;
358 default:
363 // Call Zend_Captcha_FigletTest::main() if this source file is executed directly.
364 if (PHPUnit_MAIN_METHOD == "Zend_Captcha_FigletTest::main") {
365 Zend_Captcha_FigletTest::main();