-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1676 from xwp/fix/connector-edit-tests
Update the `Test_WP_Stream_Connector_Editor::test_log_changes` test case to reflect the recent changes
- Loading branch information
Showing
2 changed files
with
46 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -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(); | ||
|
@@ -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( | ||
|
@@ -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 | ||
} | ||
} |