From 4720c6e4820ac44b6e1d4a82ead5aee7f61e19c2 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 2 Sep 2015 14:19:21 +0000 Subject: [PATCH] Add some more passing unit tests for `esc_url()` in preparation for upcoming changes. See #23605, #20771, #16859 git-svn-id: https://develop.svn.wordpress.org/trunk@33851 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/formatting/EscUrl.php | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/phpunit/tests/formatting/EscUrl.php b/tests/phpunit/tests/formatting/EscUrl.php index b4d05d3921..2a31cc0195 100644 --- a/tests/phpunit/tests/formatting/EscUrl.php +++ b/tests/phpunit/tests/formatting/EscUrl.php @@ -30,9 +30,47 @@ class Tests_Formatting_EscUrl extends WP_UnitTestCase { $this->assertEquals('?foo=bar', esc_url('?foo=bar')); } + function test_bare() { + $this->assertEquals( 'http://example.com', esc_url( 'example.com' ) ); + $this->assertEquals( 'http://localhost', esc_url( 'localhost' ) ); + $this->assertEquals( 'http://example.com/foo', esc_url( 'example.com/foo' ) ); + $this->assertEquals( 'http://баба.org/баба', esc_url( 'баба.org/баба' ) ); + } + + function test_encoding() { + $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) ); + $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) ); + $this->assertEquals( 'http://example.com?foo=1&bar=2', esc_url( 'http://example.com?foo=1&bar=2' ) ); + } + function test_protocol() { $this->assertEquals('http://example.com', esc_url('http://example.com')); $this->assertEquals('', esc_url('nasty://example.com/')); + $this->assertEquals( '', esc_url( 'example.com', array( + 'https', + ) ) ); + $this->assertEquals( '', esc_url( 'http://example.com', array( + 'https', + ) ) ); + $this->assertEquals( 'https://example.com', esc_url( 'https://example.com', array( + 'http', 'https', + ) ) ); + + foreach ( wp_allowed_protocols() as $scheme ) { + $this->assertEquals( "{$scheme}://example.com", esc_url( "{$scheme}://example.com" ), $scheme ); + $this->assertEquals( "{$scheme}://example.com", esc_url( "{$scheme}://example.com", array( + $scheme, + ) ), $scheme ); + } + + $this->assertTrue( ! in_array( 'data', wp_allowed_protocols(), true ) ); + $this->assertEquals( '', esc_url( 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D' ) ); + + $this->assertTrue( ! in_array( 'foo', wp_allowed_protocols(), true ) ); + $this->assertEquals( 'foo://example.com', esc_url( 'foo://example.com', array( + 'foo', + ) ) ); + } /**