Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 75 additions & 25 deletions admin/class-boldgrid-backup-admin-restore-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,39 +237,89 @@ public function prepare_restore() {
* @return bool True if permissions were able to be updated successfully.
*/
public function set_writable_permissions( $archive_filepath ) {
global $wp_filesystem;

$zip = new ZipArchive();

if ( $zip->open( $archive_filepath ) ) {
for ( $i = 0; $i < $zip->numFiles; $i++ ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
$data = $zip->statIndex( $i );

if ( empty( $data['name'] ) ) {
continue;
$is_compressor_type = $this->is_compressor_type();
if ( 'ZipArchive' === $is_compressor_type ) {
$zip = new ZipArchive();
if ( $zip->open( $archive_filepath ) ) {
for ( $i = 0; $i < $zip->numFiles; $i++ ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
$data = $zip->statIndex( $i );

if ( empty( $data['name'] ) ) {
continue;
}

if ( $this->change_file_permissions( $data['name'] ) ) {
continue;
} else {
return false;
}
}

$full_path = ABSPATH . $data['name'];

// If the file does not exists, no need to check its permissions.
if ( ! $wp_filesystem->exists( $full_path ) ) {
continue;
}

if ( ! $wp_filesystem->chmod( $full_path ) ) {
$this->errors[] = sprintf(
// translators: 1 The path to a file that cannot be restored due to file permissions.
__( 'Permission denied. Unable to restore the following file: %1$s', 'boldgrid-backup' ),
$full_path
);
return false;
}
} elseif ( 'PclZip' === $is_compressor_type ) {
$zip = new PclZip( $archive_filepath );
if ( $zip->listContent() ) {
foreach ( $zip->listContent() as $file ) {

if ( empty( $file['filename'] ) ) {
continue;
}

if ( $this->change_file_permissions( $file['filename'] ) ) {
continue;
} else {
return false;
}
}
}
}
return true;
}

/**
* Updates file permission per file
*
* This takes an individual filepath derived from the lists of files
* in an archive, and uses wp_filesystem to set the correct permissions
*
* @since SINCEVERSION
* @param string $file_name name of file to be changed.
* @return bool True if permissions were able to be updated successfully.
*/
public function change_file_permissions( $file_name ) {
global $wp_filesystem;
$full_path = ABSPATH . $file_name;

// If the file does not exists, no need to check its permissions.
if ( ! $wp_filesystem->exists( $full_path ) ) {
return true;
}
if ( ! $wp_filesystem->chmod( $full_path ) ) {
$this->errors[] = sprintf(
// translators: 1 The path to a file that cannot be restored due to file permissions.
__( 'Permission denied. Unable to restore the following file: %1$s', 'boldgrid-backup' ),
$full_path
);
return false;
}
return true;
}

/**
* Is Compressor Type
*
* @since SINCEVERSION
*
* @return string
*/
public function is_compressor_type() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should be get_compressor_type or something starting with get_. When it starts with is_, I would expect the return to be a boolean.

if ( class_exists( 'ZipArchive' ) ) {
return 'ZipArchive';
}
if ( class_exists( 'PclZip' ) ) {
return 'PclZip';
}
}

/**
* Action to take during shutdown hook.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/admin/test-class-boldgrid-backup-admin-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ public function setUp() {
define( 'BOLDGRID_BACKUP_VERSION', implode( get_file_data( $file, array( 'Version' ), 'plugin' ) ) );
}

$this->fake_restore_helper = $this->getMockBuilder( Boldgrid_Backup_Admin_Restore_Helper::class )
->setMethods( [ 'is_compressor_type' ] )
->getMock();
$this->fake_restore_helper->method( 'is_compressor_type' )->willReturn( 'PclZip' );

$this->core = apply_filters( 'boldgrid_backup_get_core', null );

$this->zip = new Boldgrid_Backup_Admin_Compressor_Pcl_Zip( $this->core );
Expand Down Expand Up @@ -350,4 +355,32 @@ public function test_restore_cli() {

$this->deleteBasic( 'restore' );
}

/**
* Test restore_archive_file_pcl.
*
* @since xxx
*/
public function test_restore_archive_file_pcl() {
/*
* Test: Basic test.
*
* Create a basic backup. Delete some stuff. Restore the backup. Make sure everything worked.
*/

// Create a backup if don't already have one.
$this->core->restore_helper = $this->fake_restore_helper;

$this->info = $this->core->archive_files( true );

$this->deleteBasic( 'delete' );
// Pad necessary $_POST vars.
$_POST['restore_now'] = 1;
$_POST['archive_key'] = 0;
$_POST['archive_filename'] = basename( $this->info['filepath'] );

$this->core->restore_archive_file();

$this->deleteBasic( 'restore' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ public function test_admin_enqueue_scripts() {

}

public function test_page() {
$this->premium_page->core->plugin->pluginData = $this->get_plugin_data( '2.1.1' );

$page_array = $this->premium_page->page();
$this->assertTrue( ! empty( $page_array['nav'] ) );
$this->assertTrue( ! empty( $page_array['dashboard'] ) );
$this->assertTrue( ! empty( $page_array['premium_box'] ) );
}

public function get_plugin_data( $this_version ) {

Expand Down
108 changes: 108 additions & 0 deletions tests/admin/test-class-boldgrid-backup-admin-restore-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* File: test-class-boldgrid-backup-admin-restore-helper.php
*
* @link https://www.boldgrid.com
* @since xxx
*
* @package Boldgrid_Backup
* @subpackage Boldgrid_Backup/tests/admin
* @copyright BoldGrid
* @version $Id$
* @author BoldGrid <[email protected]>
*
* phpcs:disable WordPress.VIP
*/

define( 'BOLDGRID_BACKUP_VERSION', '1.1.1' );
/**
* Class: Test_Boldgrid_Backup_Admin_Restore_Helper
*
* @since xxx
*/
class Test_Boldgrid_Backup_Admin_Restore_Helper extends WP_UnitTestCase {
/**
* An instance core.
*
* @since xxx
* @var Boldgrid_Backup_Admin_Core
*/
public $core;

/**
* Setup.
*
* @since SINCEVERSION
*/
public function setUp() {
global $wpdb;

$this->core = new \Boldgrid_Backup_Admin_Core();
}

/**
* Change File Permissions
*
* @since SINCEVERSION
*
* @param array $archive Array of Archive Info.
* @param string $archive_type Either PclZip or ZipArchive.
*/
public function change_file_permissions( $archive, $archive_type ) {
$mock_object = $this->getMockBuilder( \Boldgrid_Backup_Admin_Restore_Helper::class )
->setMethods( [ 'is_compressor_type', 'change_file_permissions' ] )
->getMock();
$mock_object->expects( $this->any() )
->method( 'is_compressor_type' )
->willReturn( $archive_type );

// Test with a failed change_file_permissions.
$result = $mock_object->set_writable_permissions( $archive['filepath'] );
$this->assertFalse( $result );

// Test with a successfull change_file_permissions.
$mock_object->expects( $this->any() )
->method( 'change_file_permissions' )
->willReturn( true );
$result = $mock_object->set_writable_permissions( $archive['filepath'] );
$this->assertTrue( $result );
}

/**
* Test set_writable_permissions
*
* @since SINCEVERSION
*/
public function test_set_writable_permissions() {
$archive = $this->core->archive_files( true );

// Test with PclZip.
$this->change_file_permissions( $archive, 'PclZip' );

// Test with ZipArchive.
$this->change_file_permissions( $archive, 'ZipArchive' );
}

/**
* Test change_file_permissions
*
* @since SINCEVERSION
*/
public function test_change_file_permissions() {
global $wp_filesystem;

$file_name = 'test.txt';
$path = ABSPATH . $file_name;
$restore_helper = new \Boldgrid_Backup_Admin_Restore_Helper();

// Tests success if file does not exist.
$result = $restore_helper->change_file_permissions( $file_name );
$this->assertTrue( $result );

// Tests success if file does exist.
touch( $path );
$result = $restore_helper->change_file_permissions( $file_name );
$this->assertTrue( $result );

}
}