-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathtest-captcha.php
More file actions
133 lines (109 loc) · 3 KB
/
test-captcha.php
File metadata and controls
133 lines (109 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// Test script for basic captcha image generation
// Setup autoloading
require_once __DIR__ . '/vendor/autoload.php';
// Include the BasicCaptcha class
require_once __DIR__ . '/classes/Captcha/BasicCaptcha.php';
use Grav\Plugin\Form\Captcha\BasicCaptcha;
// Mock Grav instance for testing
class MockGrav {
public $config;
public $session;
public function __construct() {
$this->config = new MockConfig();
$this->session = new MockSession();
}
public function offsetGet($offset) {
return $this->$offset;
}
}
class MockConfig {
private $data = [
'plugins.form.basic_captcha' => [
'type' => 'math',
'image' => [
'width' => 135,
'height' => 40,
'bg' => '#ffffff'
],
'chars' => [
'font' => 'zxx-xed.ttf',
'size' => 16
],
'math' => [
'min' => 1,
'max' => 12,
'operators' => ['+', '-', '*']
]
]
];
public function get($key) {
return $this->data[$key] ?? null;
}
}
class MockSession {
private $data = [];
public function __set($key, $value) {
$this->data[$key] = $value;
}
public function __get($key) {
return $this->data[$key] ?? null;
}
}
// Override Grav instance
namespace Grav\Common;
class Grav {
private static $instance;
public static function instance() {
if (!self::$instance) {
self::$instance = new \MockGrav();
}
return self::$instance;
}
}
// Test the captcha
$captcha = new BasicCaptcha();
// Test different types
$types = ['math', 'characters'];
foreach ($types as $type) {
echo "Testing $type captcha...\n";
// Update config for type
Grav::instance()->config = new MockConfig();
$configData = [
'plugins.form.basic_captcha' => [
'type' => $type,
'image' => [
'width' => 135,
'height' => 40,
'bg' => '#ffffff'
],
'chars' => [
'font' => 'zxx-xed.ttf',
'size' => 16,
'length' => 6
],
'math' => [
'min' => 1,
'max' => 12,
'operators' => ['+', '-', '*']
]
]
];
// Generate captcha code
$code = $captcha->getCaptchaCode();
echo " Code: $code\n";
// Create image
$image = $captcha->createCaptchaImage($code);
// Check image dimensions
$width = imagesx($image);
$height = imagesy($image);
echo " Image dimensions: {$width}x{$height}\n";
// Save test image
$filename = "test-captcha-{$type}.jpg";
imagejpeg($image, $filename);
echo " Saved to: $filename\n";
// Clean up
imagedestroy($image);
echo "\n";
}
echo "Test complete! Check the generated test-captcha-*.jpg files.\n";