Skip to content

Commit 2334e45

Browse files
update the Test_WP_Stream_Connector_Editor::test_log_changes test case to reflect the recent changes
1 parent f373a91 commit 2334e45

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

connectors/class-connector-editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function get_edition_data() {
213213
return;
214214
}
215215

216-
$location = null;
216+
$location = null;
217217

218218
if ( $theme_slug ) {
219219
$location = 'theme-editor.php';

tests/phpunit/connectors/test-class-connector-editor.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?php
22
namespace WP_Stream;
33

4+
use WP_UnitTestCase_Base;
5+
46
class Test_WP_Stream_Connector_Editor extends WP_StreamTestCase {
7+
/**
8+
* Admin user ID
9+
*
10+
* @var int
11+
*/
12+
private int $admin_user_id;
513

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

27+
// Add admin user to test caps.
28+
$this->admin_user_id = WP_UnitTestCase_Base::factory()->user->create(
29+
array(
30+
'role' => 'administrator',
31+
'user_login' => 'test_admin',
32+
'email' => '[email protected]',
33+
)
34+
);
35+
1936
$this->mock = $this->getMockBuilder( Connector_Editor::class )
2037
->setMethods( array( 'log' ) )
2138
->getMock();
@@ -31,6 +48,8 @@ public function test_log_changes() {
3148
$theme = wp_get_theme( 'twentytwentythree' );
3249
$plugin = get_plugins()['hello.php'];
3350

51+
wp_set_current_user( $this->admin_user_id );
52+
3453
$this->mock->expects( $this->exactly( 2 ) )
3554
->method( 'log' )
3655
->withConsecutive(
@@ -76,22 +95,35 @@ public function test_log_changes() {
7695
)
7796
);
7897

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

85-
\file_put_contents( $theme->get_files( 'css' )['style.css'], "\r\n", FILE_APPEND ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
86-
apply_filters( 'wp_redirect', 'theme-editor.php' );
101+
// Generate the nonce and send a theme code update request.
102+
$nonce = wp_create_nonce( 'edit-theme_twentytwentythree_style.css' );
103+
$_REQUEST['nonce'] = $nonce;
104+
$_POST = array(
105+
'nonce' => $nonce,
106+
'_wp_http_referer' => '/wp-admin/network/theme-editor.php',
107+
'newcontent' => '# hello!',
108+
'action' => 'edit-theme-plugin-file',
109+
'file' => 'style.css',
110+
'theme' => 'twentytwentythree',
111+
);
112+
113+
do_action( 'wp_ajax_edit-theme-plugin-file' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
87114

88-
// Update plugin file
89-
$_POST['plugin'] = 'hello.php';
90-
$_POST['file'] = 'hello.php';
91-
unset( $_POST['theme'] );
92-
do_action( 'load-plugin-editor.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
115+
// Generate the nonce and send a plugin update request.
116+
$nonce = wp_create_nonce( 'edit-plugin_hello.php' );
117+
$_REQUEST['nonce'] = $nonce;
118+
$_POST = array(
119+
'nonce' => $nonce,
120+
'_wp_http_referer' => '/wp-admin/network/plugin-editor.php?plugin=hello.php&Submit=Select',
121+
'newcontent' => "<?php\n/**\n * Plugin Name: Hello Dolly!\n * Description: A plugin used for PHP unit tests\n */\n",
122+
'action' => 'edit-theme-plugin-file',
123+
'file' => 'hello.php',
124+
'plugin' => 'hello.php',
125+
);
93126

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

0 commit comments

Comments
 (0)