Skip to content

Commit

Permalink
Site Editor: Add unit test for site templates export (#28323)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-szabo97 authored Jan 23, 2021
1 parent bbec827 commit cdf61b8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/full-site-editing/edit-site-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ function _remove_theme_attribute_from_content( $template_content ) {
return $template_content;
}


/**
* Output a ZIP file with an export of the current templates
* and template parts from the site editor, and close the connection.
* Creates an export of the current templates and
* template parts from the site editor at the
* specified path in a ZIP file.
*
* @param string $filename path of the ZIP file.
*/
function gutenberg_edit_site_export() {
// Create ZIP file and directories.
$filename = tempnam( get_temp_dir(), 'edit-site-export' );
function gutenberg_edit_site_export_create_zip( $filename ) {
if ( ! class_exists( 'ZipArchive' ) ) {
return new WP_Error( 'Zip Export not supported.' );
}

$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::OVERWRITE );
$zip->addEmptyDir( 'theme' );
Expand All @@ -73,8 +74,19 @@ function gutenberg_edit_site_export() {
);
}

// Send back the ZIP file.
// Save changes to the zip file.
$zip->close();
}

/**
* Output a ZIP file with an export of the current templates
* and template parts from the site editor, and close the connection.
*/
function gutenberg_edit_site_export() {
// Create ZIP file in the temporary directory.
$filename = tempnam( get_temp_dir(), 'edit-site-export' );
gutenberg_edit_site_export_create_zip( $filename );

header( 'Content-Type: application/zip' );
header( 'Content-Disposition: attachment; filename=edit-site-export.zip' );
header( 'Content-Length: ' . filesize( $filename ) );
Expand Down
29 changes: 29 additions & 0 deletions phpunit/class-edit-site-export-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,34 @@ function test_remove_theme_attribute_from_content() {
$template_content = _remove_theme_attribute_from_content( $content_with_no_template_part );
$this->assertEquals( $content_with_no_template_part, $template_content );
}

function test_gutenberg_edit_site_export() {
$filename = tempnam( get_temp_dir(), 'edit-site-export' );
gutenberg_edit_site_export_create_zip( $filename );
$this->assertTrue( file_exists( $filename ), 'zip file is created at the specified path' );
$this->assertTrue( filesize( $filename ) > 0, 'zip file is larger than 0 bytes' );

// Open ZIP file and make sure the directories exist.
$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::RDONLY );
$has_theme_dir = $zip->locateName( 'theme/' ) !== false;
$has_block_templates_dir = $zip->locateName( 'theme/block-templates/' ) !== false;
$has_block_template_parts_dir = $zip->locateName( 'theme/block-template-parts/' ) !== false;
$this->assertTrue( $has_theme_dir, 'theme directory exists' );
$this->assertTrue( $has_block_templates_dir, 'theme/block-templates directory exists' );
$this->assertTrue( $has_block_template_parts_dir, 'theme/block-template-parts directory exists' );

// ZIP file contains at least one HTML file.
$has_html_files = false;
$num_files = $zip->count();
for ( $i = 0; $i < $num_files; $i++ ) {
$filename = $zip->getNameIndex( $i );
if ( '.html' === substr( $filename, -5 ) ) {
$has_html_files = true;
break;
}
}
$this->assertTrue( $has_html_files, 'contains at least one html file' );
}
}

0 comments on commit cdf61b8

Please sign in to comment.