Skip to content

Commit 29233e8

Browse files
committed
General: Use DataProvider in unit tests
Suggested by Mukesh Panchal. I also added some tests for sanitize_email, for which there were none before. They didn't break, I just felt that having some is better than having none.
1 parent 07ceb2a commit 29233e8

2 files changed

Lines changed: 52 additions & 16 deletions

File tree

tests/phpunit/tests/formatting/antispambot.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@
66
* @covers ::antispambot
77
*/
88
class Tests_Formatting_antispambot extends WP_UnitTestCase {
9-
public function test_returns_valid_utf8() {
10-
$data = array(
11-
12-
13-
14-
15-
16-
17-
18-
'info@grå.org',
19-
'grå@grå.org',
20-
"gr\u{0061}\u{030a}blå@grå.org",
21-
9+
10+
/**
11+
* This is basically a driveby test. While working on ticket
12+
* 31992 I noticed that there was no unit testing for
13+
* antispambot, so I added a little, just so I'd leave
14+
* the code better than I found it.
15+
*
16+
* @ticket 31992
17+
*
18+
* @dataProvider data_returns_valid_utf8
19+
*/
20+
21+
public function test_returns_valid_utf8( $address, $validity ) {
22+
$this->assertSame( seems_utf8( antispambot( $address ) ), $validity );
23+
}
24+
25+
public function data_returns_valid_utf8() {
26+
return array(
27+
'plain' => array( '[email protected]', true ),
28+
'plain with ip' => array( '[email protected]', true ),
29+
'deep subdomain' => array( '[email protected]', true ),
30+
'short address' => array( '[email protected]', true ),
31+
'ascii@nonascii' => array( 'info@grå.org', true ),
32+
'nonascii@nonascii' => array( 'grå@grå.org', true ),
33+
'decomposed unicode' => array( 'gr\u{0061}\u{030a}blå@grå.org', true ),
34+
'weird but legal dots' => array( '[email protected]', true ),
2235
);
23-
foreach ( $data as $datum ) {
24-
$this->assertTrue( seems_utf8( antispambot( $datum ) ) );
25-
}
2636
}
2737
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* @group formatting
5+
*
6+
* @covers ::sanitize_email
7+
*/
8+
class Tests_Formatting_SanitizeEmail extends WP_UnitTestCase {
9+
public function data_for_sanitation() {
10+
return array(
11+
'shorter than 6 characters' => array( 'a@b', '' ),
12+
'contains no @' => array( 'ab', '' ),
13+
'just a TLD' => array( 'abc@com', '' ),
14+
'plain' => array( '[email protected]', '[email protected]' ),
15+
);
16+
}
17+
18+
/**
19+
* @ticket 31992
20+
* @dataProvider data_for_sanitation
21+
*/
22+
23+
public function test_returns_stripped_email_address( $address, $expected ) {
24+
$this->assertSame( sanitize_email( $address ), $expected );
25+
}
26+
}

0 commit comments

Comments
 (0)