Skip to content

Commit

Permalink
Test and refactor Create_Block_Theme_Admin class
Browse files Browse the repository at this point in the history
Starting with the export_child_theme method.
  • Loading branch information
Vicente Canales committed Jun 7, 2023
1 parent 605e862 commit f789eed
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 45 deletions.
57 changes: 32 additions & 25 deletions admin/class-create-theme.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

require_once( __DIR__ . '/resolver_additions.php' );
require_once( __DIR__ . '/create-theme/theme-tags.php' );
require_once( __DIR__ . '/create-theme/theme-zip.php' );
require_once( __DIR__ . '/create-theme/theme-media.php' );
require_once( __DIR__ . '/create-theme/theme-blocks.php' );
require_once( __DIR__ . '/create-theme/theme-patterns.php' );
require_once( __DIR__ . '/create-theme/theme-templates.php' );
require_once( __DIR__ . '/create-theme/theme-styles.php' );
require_once( __DIR__ . '/create-theme/theme-json.php' );
require_once( __DIR__ . '/create-theme/theme-utils.php' );
require_once( __DIR__ . '/create-theme/theme-readme.php' );
require_once( __DIR__ . '/create-theme/theme-form.php' );
require_once( __DIR__ . '/create-theme/form-messages.php' );
require_once __DIR__ . '/resolver_additions.php';
require_once __DIR__ . '/create-theme/theme-tags.php';
require_once __DIR__ . '/create-theme/theme-zip.php';
require_once __DIR__ . '/create-theme/theme-media.php';
require_once __DIR__ . '/create-theme/theme-blocks.php';
require_once __DIR__ . '/create-theme/theme-patterns.php';
require_once __DIR__ . '/create-theme/theme-templates.php';
require_once __DIR__ . '/create-theme/theme-styles.php';
require_once __DIR__ . '/create-theme/theme-json.php';
require_once __DIR__ . '/create-theme/theme-utils.php';
require_once __DIR__ . '/create-theme/theme-readme.php';
require_once __DIR__ . '/create-theme/theme-form.php';
require_once __DIR__ . '/create-theme/form-messages.php';

/**
* The admin-specific functionality of the plugin.
Expand All @@ -22,7 +22,7 @@
* @author WordPress.org
*/
class Create_Block_Theme_Admin {
private $theme;
public $theme;

/**
* Initialize the class and set its properties.
Expand All @@ -42,7 +42,7 @@ function create_block_theme_enqueue() {
return;
}

$asset_file = include( plugin_dir_path( dirname( __FILE__ ) ) . 'build/plugin-sidebar.asset.php' );
$asset_file = include plugin_dir_path( dirname( __FILE__ ) ) . 'build/plugin-sidebar.asset.php';

wp_register_script(
'create-block-theme-slot-fill',
Expand Down Expand Up @@ -75,6 +75,19 @@ function save_variation( $export_type, $theme ) {
Theme_Json::add_theme_json_variation_to_local( 'variation', $theme );
}

public function download_file( $file, $filename ) {
// Set headers.
header( 'Content-Type: application/zip' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Content-Length: ' . filesize( $file ) );

// Send file.
readfile( $file );

// Delete file.
unlink( $file );
}

/**
* Export activated child theme
*/
Expand All @@ -91,12 +104,10 @@ function export_child_theme( $theme ) {

$zip->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;
}

/**
Expand Down Expand Up @@ -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'] );

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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' ) );
}
Expand Down
20 changes: 0 additions & 20 deletions tests/test-sample.php

This file was deleted.

55 changes: 55 additions & 0 deletions tests/test-theme-create-theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Class WP_Create_Block_Theme_Admin
*
* @package Create_Block_Theme
*/
class Create_Block_Theme_AdminTest extends WP_UnitTestCase {
public function setUp(): void {
parent::setUp();
$this->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() );
}
}

0 comments on commit f789eed

Please sign in to comment.