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.
16
* @package Zend_Captcha
17
* @subpackage UnitTests
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: ImageTest.php 14291 2009-03-13 12:46:59Z alexander $
23
// Call Zend_Captcha_ImageTest::main() if this source file is executed directly.
24
if (!defined("PHPUnit_MAIN_METHOD")) {
25
define("PHPUnit_MAIN_METHOD", "Zend_Captcha_ImageTest::main");
28
require_once dirname(__FILE__) . '/../../TestHelper.php';
30
require_once 'Zend/Form/Element/Captcha.php';
31
require_once 'Zend/Captcha/Adapter.php';
35
* @package Zend_Captcha
36
* @subpackage UnitTests
37
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
38
* @license http://framework.zend.com/license/new-bsd New BSD License
40
class Zend_Captcha_ImageTest extends PHPUnit_Framework_TestCase
45
* Runs the test methods of this class.
49
public static function main()
51
require_once "PHPUnit/TextUI/TestRunner.php";
53
$suite = new PHPUnit_Framework_TestSuite("Zend_Captcha_ImageTest");
54
$result = PHPUnit_TextUI_TestRunner::run($suite);
58
* Sets up the fixture, for example, open a network connection.
59
* This method is called before a test is executed.
63
public function setUp()
65
if (!extension_loaded('gd')) {
66
$this->markTestSkipped('The GD extension is not available.');
69
if(!function_exists("imagepng")) {
70
$this->markTestSkipped("Image CAPTCHA requires PNG support");
72
if(!function_exists("imageftbbox")) {
73
$this->markTestSkipped("Image CAPTCHA requires FT fonts support");
76
if (isset($this->word)) {
79
$this->testDir = $this->_getTmpDir() . '/ZF_test_images';
80
if(!is_dir($this->testDir)) {
81
@mkdir($this->testDir);
83
$this->element = new Zend_Form_Element_Captcha('captchaI',
84
array('captcha' => array('Image',
85
'sessionClass' => 'Zend_Captcha_ImageTest_SessionContainer',
86
'imgDir' => $this->testDir,
87
'font' => dirname(__FILE__). '/../Pdf/_fonts/Vera.ttf')
89
$this->captcha = $this->element->getCaptcha();
93
* Tears down the fixture, for example, close a network connection.
94
* This method is called after a test is executed.
98
public function tearDown()
100
// remove chaptcha images
101
foreach(new DirectoryIterator($this->testDir) as $file) {
102
if(!$file->isDot() && !$file->isDir()) {
103
unlink($file->getPathname());
110
* Determine system TMP directory
113
* @throws Zend_File_Transfer_Exception if unable to determine directory
115
protected function _getTmpDir()
117
if (null === $this->_tmpDir) {
118
if (function_exists('sys_get_temp_dir')) {
119
$tmpdir = sys_get_temp_dir();
120
} elseif (!empty($_ENV['TMP'])) {
121
$tmpdir = realpath($_ENV['TMP']);
122
} elseif (!empty($_ENV['TMPDIR'])) {
123
$tmpdir = realpath($_ENV['TMPDIR']);
124
} else if (!empty($_ENV['TEMP'])) {
125
$tmpdir = realpath($_ENV['TEMP']);
127
// Attemp to detect by creating a temporary file
128
$tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
130
$tmpdir = realpath(dirname($tempFile));
133
require_once 'Zend/File/Transfer/Exception.php';
134
throw new Zend_File_Transfer_Exception('Could not determine temp directory');
137
$this->_tmpDir = rtrim($tmpdir, "/\\");
139
return $this->_tmpDir;
142
public function getView()
144
require_once 'Zend/View.php';
145
$view = new Zend_View();
146
$view->addHelperPath(dirname(__FILE__) . '/../../../../library/Zend/View/Helper');
150
public function testCaptchaIsRendered()
152
$html = $this->element->render($this->getView());
153
$this->assertContains($this->element->getName(), $html);
156
public function testCaptchaHasIdAndInput()
158
$html = $this->element->render($this->getView());
159
$expect = sprintf('type="hidden" name="%s\[id\]" value="%s"', $this->element->getName(), $this->captcha->getId());
160
$this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
161
$expect = sprintf('type="text" name="%s\[input\]"', $this->element->getName());
162
$this->assertRegexp("/<input[^>]*?$expect/", $html, $html);
165
public function testCaptchaHasImage()
167
$html = $this->element->render($this->getView());
168
$id = $this->captcha->getId();
169
$this->assertRegexp("|<img[^>]*?src=\"/images/captcha/$id.png\"|", $html, "Expected $id in HTML:\n" . $html);
172
public function testCaptchaHasAlt()
174
$html = $this->element->render($this->getView());
175
$this->assertRegexp('|<img[^>]*? alt=""|', $html, "Expected alt= in HTML:\n" . $html);
176
$this->captcha->setImgAlt("Test Image");
177
$html = $this->element->render($this->getView());
178
$this->assertRegexp('|<img[^>]*? alt="Test Image"|', $html, "Wrong alt in HTML:\n" . $html);
181
public function testCaptchaSetSuffix()
183
$this->captcha->setSuffix(".jpeg");
184
$html = $this->element->render($this->getView());
185
$this->assertContains(".jpeg", $html, $html);
188
public function testCaptchaSetImgURL()
190
$this->captcha->setImgURL("/some/other/URL/");
191
$html = $this->element->render($this->getView());
192
$this->assertContains("/some/other/URL/", $html, $html);
195
public function testCaptchaCreatesImage()
197
$this->element->render($this->getView());
198
$this->assertTrue(file_exists($this->testDir."/".$this->captcha->getId().".png"));
201
public function testCaptchaSetExpiration()
203
$this->assertEquals($this->captcha->getExpiration(), 600);
204
$this->captcha->setExpiration(3600);
205
$this->assertEquals($this->captcha->getExpiration(), 3600);
208
public function testCaptchaImageCleanup()
210
$this->element->render($this->getView());
211
$filename = $this->testDir."/".$this->captcha->getId().".png";
212
$this->assertTrue(file_exists($filename));
213
$this->captcha->setExpiration(1);
214
$this->captcha->setGcFreq(1);
216
$this->captcha->generate();
218
$this->assertFalse(file_exists($filename), "File $filename was found even after GC");
221
public function testGenerateReturnsId()
223
$id = $this->captcha->generate();
224
$this->assertFalse(empty($id));
225
$this->assertTrue(is_string($id));
229
public function testGetWordReturnsWord()
231
$this->captcha->generate();
232
$word = $this->captcha->getWord();
233
$this->assertFalse(empty($word));
234
$this->assertTrue(is_string($word));
235
$this->assertTrue(strlen($word) == 8);
239
public function testGetWordLength()
241
$this->captcha->setWordLen(4);
242
$this->captcha->generate();
243
$word = $this->captcha->getWord();
244
$this->assertTrue(is_string($word));
245
$this->assertTrue(strlen($word) == 4);
249
public function testAdapterElementName()
251
$this->assertEquals($this->captcha->getName(),
252
$this->element->getName());
255
public function testGenerateIsRandomised()
257
$id1 = $this->captcha->generate();
258
$word1 = $this->captcha->getWord();
259
$id2 = $this->captcha->generate();
260
$word2 = $this->captcha->getWord();
262
$this->assertFalse(empty($id1));
263
$this->assertFalse(empty($id2));
264
$this->assertFalse($id1 == $id2);
265
$this->assertFalse($word1 == $word2);
268
public function testRenderSetsValue()
270
$this->testCaptchaIsRendered();
271
$this->assertEquals($this->captcha->getId(),
272
$this->element->getValue());
275
public function testLabelIsNull()
277
$this->assertNull($this->element->getLabel());
280
public function testRenderInitializesSessionData()
282
$this->testCaptchaIsRendered();
283
$session = $this->captcha->getSession();
284
$this->assertEquals($this->captcha->getTimeout(), $session->setExpirationSeconds);
285
$this->assertEquals(1, $session->setExpirationHops);
286
$this->assertEquals($this->captcha->getWord(), $session->word);
289
public function testWordValidates()
291
$this->testCaptchaIsRendered();
292
$input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
293
$this->assertTrue($this->element->isValid("", $input));
296
public function testMissingNotValid()
298
$this->testCaptchaIsRendered();
299
$this->assertFalse($this->element->isValid("", array()));
300
$input = array($this->element->getName() => array("input" => "blah"));
301
$this->assertFalse($this->element->isValid("", $input));
304
public function testWrongWordNotValid()
306
$this->testCaptchaIsRendered();
307
$input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => "blah"));
308
$this->assertFalse($this->element->isValid("", $input));
314
public function testIsValidShouldAllowPassingArrayValueWithNoContext()
316
$this->testCaptchaIsRendered();
317
$input = array($this->element->getName() => array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord()));
318
$this->assertTrue($this->element->isValid($input));
324
public function testIsValidShouldNotRequireValueToBeNestedArray()
326
$this->testCaptchaIsRendered();
327
$input = array("id" => $this->captcha->getId(), "input" => $this->captcha->getWord());
328
$this->assertTrue($this->element->isValid($input));
332
class Zend_Captcha_ImageTest_SessionContainer
334
protected static $_word;
336
public function __get($name)
338
if ('word' == $name) {
345
public function __set($name, $value)
347
if ('word' == $name) {
348
self::$_word = $value;
350
$this->$name = $value;
354
public function __isset($name)
356
if (('word' == $name) && (null !== self::$_word)) {
363
public function __call($method, $args)
366
case 'setExpirationHops':
367
case 'setExpirationSeconds':
368
$this->$method = array_shift($args);
375
// Call Zend_Captcha_ImageTest::main() if this source file is executed directly.
376
if (PHPUnit_MAIN_METHOD == "Zend_Captcha_ImageTest::main") {
377
Zend_Captcha_ImageTest::main();