|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the antispambot() function. |
| 4 | + * |
| 5 | + * @group formatting |
| 6 | + * @covers ::antispambot |
| 7 | + */ |
| 8 | +class Tests_Formatting_Antispambot extends WP_UnitTestCase { |
| 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 the code |
| 14 | + * better than I found it. |
| 15 | + * |
| 16 | + * @ticket 31992 |
| 17 | + * |
| 18 | + * @dataProvider data_returns_valid_utf8 |
| 19 | + * @param string $address The email address to obfuscate. |
| 20 | + * @param bool $validity Whether the obfuscated address should be valid UTF-8. |
| 21 | + */ |
| 22 | + public function test_returns_valid_utf8( $address, $validity ) { |
| 23 | + $this->assertSame( wp_is_valid_utf8( antispambot( $address ) ), $validity ); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Data provider for test_returns_valid_utf8. |
| 28 | + */ |
| 29 | + public function data_returns_valid_utf8() { |
| 30 | + return array( |
| 31 | + 'plain' => array( 'bob@example.com', true ), |
| 32 | + 'plain with ip' => array( 'ace@204.32.222.14', true ), |
| 33 | + 'deep subdomain' => array( 'kevin@many.subdomains.make.a.happy.man.edu', true ), |
| 34 | + 'short address' => array( 'a@b.co', true ), |
| 35 | + 'weird but legal dots' => array( '..@example.com', true ), |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * This tests that antispambot performs some sort of |
| 41 | + * obfuscation, and that its obfuscated form will be rendered |
| 42 | + * sensibly by browsers. |
| 43 | + * |
| 44 | + * @dataProvider data_antispambot_obfuscates |
| 45 | + * @param string $provided The email address to obfuscate. |
| 46 | + */ |
| 47 | + public function test_antispambot_obfuscates( $provided ) { |
| 48 | + $obfuscated = antispambot( $provided ); |
| 49 | + $p = new WP_HTML_Tag_Processor( $obfuscated ); |
| 50 | + $p->next_token(); |
| 51 | + $decoded = $p->get_modifiable_text(); |
| 52 | + $decoded = preg_replace_callback( '~%\d\d~', function () { }, $decoded ); |
| 53 | + |
| 54 | + $this->assertNotEquals( $provided, $obfuscated ); |
| 55 | + $this->assertSame( $provided, $decoded ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Data provider for test_antispambot_obfuscates. |
| 60 | + */ |
| 61 | + public function data_antispambot_obfuscates() { |
| 62 | + return array( |
| 63 | + 'example@example.com', |
| 64 | + '#@example.com', |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
0 commit comments