1
1
<?php
2
2
namespace WP_Stream ;
3
3
4
+ use WP_UnitTestCase_Base ;
5
+
4
6
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 ;
5
13
6
14
/**
7
15
* The original contents of the file.
@@ -16,6 +24,15 @@ public function setUp(): void {
16
24
$ this ->plugin ->connectors ->unload_connectors ();
17
25
$ this ->original_contents = file_get_contents ( WP_PLUGIN_DIR . '/hello.php ' );
18
26
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
+
33
+ )
34
+ );
35
+
19
36
$ this ->mock = $ this ->getMockBuilder ( Connector_Editor::class )
20
37
->setMethods ( array ( 'log ' ) )
21
38
->getMock ();
@@ -31,6 +48,8 @@ public function test_log_changes() {
31
48
$ theme = wp_get_theme ( 'twentytwentythree ' );
32
49
$ plugin = get_plugins ()['hello.php ' ];
33
50
51
+ wp_set_current_user ( $ this ->admin_user_id );
52
+
34
53
$ this ->mock ->expects ( $ this ->exactly ( 2 ) )
35
54
->method ( 'log ' )
36
55
->withConsecutive (
@@ -76,22 +95,35 @@ public function test_log_changes() {
76
95
)
77
96
);
78
97
79
- // Update theme file .
98
+ // Update the request method .
80
99
$ _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
84
100
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
87
114
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
+ );
93
126
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
96
128
}
97
129
}
0 commit comments