Skip to content

Commit

Permalink
Merge pull request #1676 from xwp/fix/connector-edit-tests
Browse files Browse the repository at this point in the history
Update the `Test_WP_Stream_Connector_Editor::test_log_changes` test case to reflect the recent changes
  • Loading branch information
bartoszgadomski authored Jan 14, 2025
2 parents f373a91 + 2334e45 commit 2160136
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion connectors/class-connector-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function get_edition_data() {
return;
}

$location = null;
$location = null;

if ( $theme_slug ) {
$location = 'theme-editor.php';
Expand Down
58 changes: 45 additions & 13 deletions tests/phpunit/connectors/test-class-connector-editor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
namespace WP_Stream;

use WP_UnitTestCase_Base;

class Test_WP_Stream_Connector_Editor extends WP_StreamTestCase {
/**
* Admin user ID
*
* @var int
*/
private int $admin_user_id;

/**
* The original contents of the file.
Expand All @@ -16,6 +24,15 @@ public function setUp(): void {
$this->plugin->connectors->unload_connectors();
$this->original_contents = file_get_contents( WP_PLUGIN_DIR . '/hello.php' );

// Add admin user to test caps.
$this->admin_user_id = WP_UnitTestCase_Base::factory()->user->create(
array(
'role' => 'administrator',
'user_login' => 'test_admin',
'email' => '[email protected]',
)
);

$this->mock = $this->getMockBuilder( Connector_Editor::class )
->setMethods( array( 'log' ) )
->getMock();
Expand All @@ -31,6 +48,8 @@ public function test_log_changes() {
$theme = wp_get_theme( 'twentytwentythree' );
$plugin = get_plugins()['hello.php'];

wp_set_current_user( $this->admin_user_id );

$this->mock->expects( $this->exactly( 2 ) )
->method( 'log' )
->withConsecutive(
Expand Down Expand Up @@ -76,22 +95,35 @@ public function test_log_changes() {
)
);

// Update theme file.
// Update the request method.
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['action'] = 'update';
$_POST['theme'] = 'twentytwentythree';
do_action( 'load-theme-editor.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

\file_put_contents( $theme->get_files( 'css' )['style.css'], "\r\n", FILE_APPEND ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
apply_filters( 'wp_redirect', 'theme-editor.php' );
// Generate the nonce and send a theme code update request.
$nonce = wp_create_nonce( 'edit-theme_twentytwentythree_style.css' );
$_REQUEST['nonce'] = $nonce;
$_POST = array(
'nonce' => $nonce,
'_wp_http_referer' => '/wp-admin/network/theme-editor.php',
'newcontent' => '# hello!',
'action' => 'edit-theme-plugin-file',
'file' => 'style.css',
'theme' => 'twentytwentythree',
);

do_action( 'wp_ajax_edit-theme-plugin-file' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

// Update plugin file
$_POST['plugin'] = 'hello.php';
$_POST['file'] = 'hello.php';
unset( $_POST['theme'] );
do_action( 'load-plugin-editor.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
// Generate the nonce and send a plugin update request.
$nonce = wp_create_nonce( 'edit-plugin_hello.php' );
$_REQUEST['nonce'] = $nonce;
$_POST = array(
'nonce' => $nonce,
'_wp_http_referer' => '/wp-admin/network/plugin-editor.php?plugin=hello.php&Submit=Select',
'newcontent' => "<?php\n/**\n * Plugin Name: Hello Dolly!\n * Description: A plugin used for PHP unit tests\n */\n",
'action' => 'edit-theme-plugin-file',
'file' => 'hello.php',
'plugin' => 'hello.php',
);

\file_put_contents( WP_PLUGIN_DIR . '/hello.php', "\r\n", FILE_APPEND ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
apply_filters( 'wp_redirect', 'plugin-editor.php' );
do_action( 'wp_ajax_edit-theme-plugin-file' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
}

0 comments on commit 2160136

Please sign in to comment.