From 6adbde93405c92bb8bde2a9d67c8e0271d444a41 Mon Sep 17 00:00:00 2001 From: Aaron Jorbin Date: Wed, 9 Mar 2016 20:43:28 +0000 Subject: [PATCH] Add Automated Tests for custom logo support [36698] introduced custom logos, this tests the API. Fixes #36086. See #33755. Props obenland. git-svn-id: https://develop.svn.wordpress.org/trunk@36905 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/general/template.php | 93 +++++++++++++++++++++++ tests/phpunit/tests/image/custom_logo.php | 45 +++++++++++ 2 files changed, 138 insertions(+) create mode 100644 tests/phpunit/tests/image/custom_logo.php diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 9ec228a0bf..b956033482 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -11,6 +11,9 @@ class Tests_General_Template extends WP_UnitTestCase { public $site_icon_id; public $site_icon_url; + public $custom_logo_id; + public $custom_logo_url; + function setUp() { parent::setUp(); @@ -18,6 +21,12 @@ class Tests_General_Template extends WP_UnitTestCase { $this->wp_site_icon = $GLOBALS['wp_site_icon']; } + function tearDown() { + $this->_remove_custom_logo(); + + parent::tearDown(); + } + /** * @group site_icon */ @@ -192,4 +201,88 @@ class Tests_General_Template extends WP_UnitTestCase { $this->site_icon_id = $this->_make_attachment( $upload ); return $this->site_icon_id; } + + /** + * @group custom_logo + * + * @since 4.5.0 + */ + function test_has_custom_logo() { + $this->assertFalse( has_custom_logo() ); + + $this->_set_custom_logo(); + $this->assertTrue( has_custom_logo() ); + + $this->_remove_custom_logo(); + $this->assertFalse( has_custom_logo() ); + } + + /** + * @group custom_logo + * + * @since 4.5.0 + */ + function test_get_custom_logo() { + $this->assertEmpty( get_custom_logo() ); + + $this->_set_custom_logo(); + $custom_logo = get_custom_logo(); + $this->assertNotEmpty( $custom_logo ); + $this->assertInternalType( 'string', $custom_logo ); + + $this->_remove_custom_logo(); + $this->assertEmpty( get_custom_logo() ); + } + + /** + * @group custom_logo + * + * @since 4.5.0 + */ + function test_the_custom_logo() { + $this->expectOutputString( '' ); + the_custom_logo(); + + $this->_set_custom_logo(); + $this->expectOutputString( '' ); + the_custom_logo(); + } + + /** + * Sets a site icon in options for testing. + * + * @since 4.5.0 + */ + function _set_custom_logo() { + if ( ! $this->custom_logo_id ) { + $this->_insert_custom_logo(); + } + + set_theme_mod( 'custom_logo', $this->custom_logo_id ); + } + + /** + * Removes the site icon from options. + * + * @since 4.5.0 + */ + function _remove_custom_logo() { + remove_theme_mod( 'custom_logo' ); + } + + /** + * Inserts an attachment for testing custom logos. + * + * @since 4.5.0 + */ + function _insert_custom_logo() { + $filename = DIR_TESTDATA . '/images/test-image.jpg'; + $contents = file_get_contents( $filename ); + $upload = wp_upload_bits( basename( $filename ), null, $contents ); + + // Save the data. + $this->custom_logo_url = $upload['url']; + $this->custom_logo_id = $this->_make_attachment( $upload ); + return $this->custom_logo_id; + } } diff --git a/tests/phpunit/tests/image/custom_logo.php b/tests/phpunit/tests/image/custom_logo.php new file mode 100644 index 0000000000..f8b1b43665 --- /dev/null +++ b/tests/phpunit/tests/image/custom_logo.php @@ -0,0 +1,45 @@ +custom_logo = null; + $this->remove_added_uploads(); + parent::tearDown(); + } + + function test_delete_attachment_data() { + $attachment_id = $this->_insert_attachment(); + set_theme_mod( 'custom_logo', $attachment_id ); + + wp_delete_attachment( $attachment_id, true ); + + $this->assertFalse( get_theme_mod( 'custom_logo' ) ); + } + + function _insert_attachment() { + if ( $this->attachment_id ) { + return $this->attachment_id; + } + + $filename = DIR_TESTDATA . '/images/test-image.jpg'; + $contents = file_get_contents( $filename ); + + $upload = wp_upload_bits( basename( $filename ), null, $contents ); + + $this->attachment_id = $this->_make_attachment( $upload ); + return $this->attachment_id; + } +}