-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJapaneseTextTest.php
More file actions
312 lines (262 loc) · 7.86 KB
/
Copy pathJapaneseTextTest.php
File metadata and controls
312 lines (262 loc) · 7.86 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* Japanese text functionality tests
*
* @package FE_CSV_Import_Export\Tests\Unit
*/
use PHPUnit\Framework\TestCase;
/**
* Japanese text tests
*
* @since 0.9.7
*/
class JapaneseTextTest extends TestCase {
/**
* Test CSV parsing with Japanese characters
*
* @since 0.9.7
* @return void
*/
public function test_csv_parsing_with_japanese_characters() {
$line = '山田太郎,yamada@example.com,30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ '山田太郎', 'yamada@example.com', '30' ], $result );
$this->assertCount( 3, $result );
}
/**
* Test CSV parsing with Japanese characters in quotes
*
* @since 0.9.7
* @return void
*/
public function test_csv_parsing_with_japanese_quotes() {
$line = '"山田, 太郎",yamada@example.com,30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ '山田, 太郎', 'yamada@example.com', '30' ], $result );
}
/**
* Test CSV parsing with full-width characters
*
* @since 0.9.7
* @return void
*/
public function test_csv_parsing_with_fullwidth_characters() {
$line = '山田太郎,田中@example.com,30';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ '山田太郎', '田中@example.com', '30' ], $result );
}
/**
* Test CSV parsing with mixed Japanese and English
*
* @since 0.9.7
* @return void
*/
public function test_csv_parsing_with_mixed_languages() {
$line = '山田Taro,yamada@example.com,Engineer';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ '山田Taro', 'yamada@example.com', 'Engineer' ], $result );
}
/**
* Test CSV parsing with Japanese special characters
*
* @since 0.9.7
* @return void
*/
public function test_csv_parsing_with_japanese_special_chars() {
$line = '株式会社山田商事,info@yamada.co.jp,東京都千代田区';
$delimiter = ',';
$result = str_getcsv( $line, $delimiter );
$this->assertEquals( [ '株式会社山田商事', 'info@yamada.co.jp', '東京都千代田区' ], $result );
}
/**
* Test Japanese text sanitization
*
* @since 0.9.7
* @return void
*/
public function test_japanese_text_sanitization() {
$input = '<script>alert("テスト")</script>山田太郎';
$result = strip_tags( $input );
$this->assertEquals( 'alert("テスト")山田太郎', $result );
$this->assertStringNotContainsString( '<script>', $result );
$this->assertStringNotContainsString( '</script>', $result );
}
/**
* Test Japanese text with HTML entities
*
* @since 0.9.7
* @return void
*/
public function test_japanese_text_html_entities() {
$input = '山田太郎 & 田中花子';
$result = htmlspecialchars( $input, ENT_QUOTES, 'UTF-8' );
$this->assertStringContainsString( '山田太郎', $result );
$this->assertStringContainsString( '田中花子', $result );
$this->assertStringContainsString( '&', $result );
}
/**
* Test Japanese email validation
*
* @since 0.9.7
* @return void
*/
public function test_japanese_email_validation() {
$valid_emails = [
'yamada@example.com',
'tanaka@example.jp',
];
$invalid_emails = [
'山田@example.com', // Japanese characters in local part are invalid
'tanaka@例題.com', // Japanese characters in domain are invalid in standard validation
'@ドメイン.com', // Missing local part
];
foreach ( $valid_emails as $email ) {
$this->assertTrue( filter_var( $email, FILTER_VALIDATE_EMAIL ) !== false, "Email {$email} should be valid" );
}
foreach ( $invalid_emails as $email ) {
$this->assertFalse( filter_var( $email, FILTER_VALIDATE_EMAIL ) !== false, "Email {$email} should be invalid" );
}
}
/**
* Test Japanese URL validation
*
* @since 0.9.7
* @return void
*/
public function test_japanese_url_validation() {
$valid_urls = [
'https://example.com',
'http://domain.org/path',
];
foreach ( $valid_urls as $url ) {
$this->assertTrue( filter_var( $url, FILTER_VALIDATE_URL ) !== false, "URL {$url} should be valid" );
}
}
/**
* Test Japanese slug generation
*
* @since 0.9.7
* @return void
*/
public function test_japanese_slug_generation() {
$input = '山田太郎のブログ';
$result = $this->generateSlug( $input );
// Japanese characters are removed by our simple slug function
$this->assertEquals( '', $result );
}
/**
* Test Japanese array sanitization
*
* @since 0.9.7
* @return void
*/
public function test_japanese_array_sanitization() {
$input = [
'name' => '<script>alert(1)</script>山田太郎',
'email' => 'yamada@example.com',
'company' => '株式会社山田商事',
'address' => '東京都千代田区丸の内1-1-1',
];
$sanitized = [];
foreach ( $input as $key => $value ) {
$sanitized[ $key ] = $this->sanitizeText( $value );
}
$this->assertEquals( 'alert(1)山田太郎', $sanitized['name'] );
$this->assertEquals( 'yamada@example.com', $sanitized['email'] );
$this->assertEquals( '株式会社山田商事', $sanitized['company'] );
$this->assertStringContainsString( '東京都', $sanitized['address'] );
}
/**
* Test Japanese CSV content parsing
*
* @since 0.9.7
* @return void
*/
public function test_japanese_csv_content_parsing() {
$csv_content = "名前,メールアドレス,年齢\n山田太郎,yamada@example.com,30\n田中花子,tanaka@example.com,25";
$lines = explode( "\n", $csv_content );
$headers = str_getcsv( $lines[0], ',' );
$row1 = str_getcsv( $lines[1], ',' );
$row2 = str_getcsv( $lines[2], ',' );
$this->assertEquals( [ '名前', 'メールアドレス', '年齢' ], $headers );
$this->assertEquals( [ '山田太郎', 'yamada@example.com', '30' ], $row1 );
$this->assertEquals( [ '田中花子', 'tanaka@example.com', '25' ], $row2 );
}
/**
* Test Japanese text length validation
*
* @since 0.9.7
* @return void
*/
public function test_japanese_text_length_validation() {
$short_text = '山田';
$long_text = str_repeat( 'これは非常に長い日本語のテキストです。', 50 );
$this->assertLessThan( 10, mb_strlen( $short_text, 'UTF-8' ) );
$this->assertGreaterThan( 100, mb_strlen( $long_text, 'UTF-8' ) );
// Test truncation
$max_length = 50;
$truncated = mb_substr( $long_text, 0, $max_length, 'UTF-8' );
$this->assertLessThanOrEqual( $max_length, mb_strlen( $truncated, 'UTF-8' ) );
}
/**
* Test Japanese empty value handling
*
* @since 0.9.7
* @return void
*/
public function test_japanese_empty_value_handling() {
$empty_inputs = [
'',
' ',
"\t\n",
' ', // Full-width spaces
null,
];
foreach ( $empty_inputs as $input ) {
$result = $this->sanitizeText( $input );
$this->assertTrue( empty( trim( $result ) ) );
}
}
/**
* Helper method to sanitize text
*
* @since 0.9.7
* @param mixed $value Input value.
* @return string Sanitized text.
*/
private function sanitizeText( $value ) {
if ( null === $value ) {
return '';
}
// Remove HTML tags
$text = strip_tags( (string) $value );
// Trim whitespace including full-width spaces
$text = trim( $text );
$text = trim( $text, " \t\n\r\0\x0B " );
// Convert special characters to HTML entities
$text = htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' );
return $text;
}
/**
* Helper method to generate slug
*
* @since 0.9.7
* @param string $text Input text.
* @return string Generated slug.
*/
private function generateSlug( $text ) {
// For Japanese text, we'll use a simple approach
// In a real implementation, you might want to use transliteration
// Convert to lowercase
$text = strtolower( $text );
// Replace non-alphanumeric characters with hyphens
$text = preg_replace( '/[^a-z0-9]+/', '-', $text );
// Remove leading/trailing hyphens
$text = trim( $text, '-' );
return $text;
}
}