From f789eedc6bf5f2f70792e9d66314aa6421dae70d Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Wed, 7 Jun 2023 16:58:26 -0400 Subject: [PATCH] Test and refactor Create_Block_Theme_Admin class Starting with the export_child_theme method. --- admin/class-create-theme.php | 57 +++++++++++++++++-------------- tests/test-sample.php | 20 ----------- tests/test-theme-create-theme.php | 55 +++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 45 deletions(-) delete mode 100644 tests/test-sample.php create mode 100644 tests/test-theme-create-theme.php diff --git a/admin/class-create-theme.php b/admin/class-create-theme.php index d8576c0b..c5123b6a 100644 --- a/admin/class-create-theme.php +++ b/admin/class-create-theme.php @@ -1,18 +1,18 @@ close(); - header( 'Content-Type: application/zip' ); - header( 'Content-Disposition: attachment; filename=' . $theme['slug'] . '.zip' ); - header( 'Content-Length: ' . filesize( $filename ) ); - flush(); - echo readfile( $filename ); - die(); + // Download the ZIP file. + $this->download_file( $filename, $theme['slug'] . '.zip' ); + + return $filename; } /** @@ -233,7 +244,6 @@ function clone_theme( $theme, $screenshot ) { * Create a child theme of the activated theme */ function create_child_theme( $theme, $screenshot ) { - $parent_theme_slug = Theme_Utils::get_theme_slug( $this->theme->get( 'Name' ) ); $child_theme_slug = Theme_Utils::get_theme_slug( $theme['name'] ); @@ -379,11 +389,9 @@ function create_blank_theme( $theme, $screenshot ) { file_put_contents( $theme_json_path, $theme_json_string ); } } - } function blockbase_save_theme() { - if ( ! empty( $_GET['page'] ) && 'create-block-theme' === $_GET['page'] && ! empty( $_POST['theme'] ) ) { // Check user capabilities. @@ -412,7 +420,6 @@ function blockbase_save_theme() { add_action( 'admin_notices', array( 'Form_Messages', 'admin_notice_save_success' ) ); } elseif ( 'variation' === $_POST['theme']['type'] ) { - if ( '' === $_POST['theme']['variation'] ) { return add_action( 'admin_notices', array( 'Form_Messages', 'admin_notice_error_variation_name' ) ); } diff --git a/tests/test-sample.php b/tests/test-sample.php deleted file mode 100644 index ec2b87ea..00000000 --- a/tests/test-sample.php +++ /dev/null @@ -1,20 +0,0 @@ -assertTrue( true ); - } -} diff --git a/tests/test-theme-create-theme.php b/tests/test-theme-create-theme.php new file mode 100644 index 00000000..d50700bd --- /dev/null +++ b/tests/test-theme-create-theme.php @@ -0,0 +1,55 @@ +class_instance = new Create_Block_Theme_Admin(); + } + + /** + * Test if the class exists. + */ + public function test_theme_instance() { + $class_name = get_class( $this->class_instance->theme ); + $expected = 'WP_Theme'; + + $this->assertEquals( $expected, $class_name ); + } + + /** + * Test if the export_child_theme correctly creates a child theme. + */ + public function test_export_child_theme() { + $create_block_theme_admin = $this->getMockBuilder( 'Create_Block_Theme_Admin' ) + ->setMethods( array( 'download_file' ) ) + ->getMock(); + + // Configure the stub. + $create_block_theme_admin->method( 'download_file' ) + ->willReturn( true ); + + $theme_name = 'twentytwentythree'; + $expected_child_theme_name = 'twentytwentythree-child'; + + switch_theme( $theme_name ); + $current_theme = wp_get_theme(); + + $filename = $create_block_theme_admin->export_child_theme( $current_theme ); + + // check that the zip file exists in the temp dir. + $this->assertFileExists( $filename ); + + // check that it contains a valid WordPress theme. + $zip = new ZipArchive(); + $zip->open( $filename ); + $zip->extractTo( get_theme_root() . '/' . $expected_child_theme_name ); + $zip->close(); + + $child_theme = wp_get_theme( $expected_child_theme_name ); + $this->assertTrue( $child_theme->exists() ); + } +}