@@ -109,4 +109,95 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() {
109109 $ this ->assertTrue ( $ response ['success ' ] );
110110 $ this ->assertEquals ( $ expected , $ response ['data ' ] );
111111 }
112+
113+ public function test_wp_ajax_set_attachment_thumbnail_success () {
114+ // Become an administrator.
115+ $ post = $ _POST ;
116+ $ user_id = self ::factory ()->user ->create (
117+ array (
118+ 'role ' => 'administrator ' ,
119+ 'user_login ' => 'user_36578_administrator ' ,
120+ 'user_email ' =>
'[email protected] ' ,
121+ )
122+ );
123+ wp_set_current_user ( $ user_id );
124+ $ _POST = array_merge ( $ _POST , $ post );
125+
126+ // Upload the attachment itself.
127+ $ filename = DIR_TESTDATA . '/uploads/small-audio.mp3 ' ;
128+ $ contents = file_get_contents ( $ filename );
129+
130+ $ upload = wp_upload_bits ( wp_basename ( $ filename ), null , $ contents );
131+ $ attachment = $ this ->_make_attachment ( $ upload );
132+
133+ // Upload the thumbnail.
134+ $ filename = DIR_TESTDATA . '/images/waffles.jpg ' ;
135+ $ contents = file_get_contents ( $ filename );
136+
137+ $ upload = wp_upload_bits ( wp_basename ( $ filename ), null , $ contents );
138+ $ thumbnail = $ this ->_make_attachment ( $ upload );
139+
140+ // Set up a default request.
141+ $ _POST ['_ajax_nonce ' ] = wp_create_nonce ( 'set-attachment-thumbnail ' );
142+ $ _POST ['thumbnail_id ' ] = $ thumbnail ;
143+ $ _POST ['urls ' ] = array ( wp_get_attachment_url ( $ attachment ) );
144+
145+ // Make the request.
146+ try {
147+ $ this ->_handleAjax ( 'set-attachment-thumbnail ' );
148+ } catch ( WPAjaxDieContinueException $ e ) {
149+ unset( $ e );
150+ }
151+
152+ // Get the response.
153+ $ response = json_decode ( $ this ->_last_response , true );
154+
155+ // Ensure everything is correct.
156+ $ this ->assertTrue ( $ response ['success ' ] );
157+ }
158+
159+ public function test_wp_ajax_set_attachment_thumbnail_missing_nonce () {
160+ // Become an administrator.
161+ $ post = $ _POST ;
162+ $ user_id = self ::factory ()->user ->create (
163+ array (
164+ 'role ' => 'administrator ' ,
165+ 'user_login ' => 'user_36578_administrator ' ,
166+ 'user_email ' =>
'[email protected] ' ,
167+ )
168+ );
169+ wp_set_current_user ( $ user_id );
170+ $ _POST = array_merge ( $ _POST , $ post );
171+
172+ // Upload the attachment itself.
173+ $ filename = DIR_TESTDATA . '/uploads/small-audio.mp3 ' ;
174+ $ contents = file_get_contents ( $ filename );
175+
176+ $ upload = wp_upload_bits ( wp_basename ( $ filename ), null , $ contents );
177+ $ attachment = $ this ->_make_attachment ( $ upload );
178+
179+ // Upload the thumbnail.
180+ $ filename = DIR_TESTDATA . '/images/waffles.jpg ' ;
181+ $ contents = file_get_contents ( $ filename );
182+
183+ $ upload = wp_upload_bits ( wp_basename ( $ filename ), null , $ contents );
184+ $ thumbnail = $ this ->_make_attachment ( $ upload );
185+
186+ // Set up a default request.
187+ $ _POST ['thumbnail_id ' ] = $ thumbnail ;
188+ $ _POST ['urls ' ] = array ( wp_get_attachment_url ( $ attachment ) );
189+
190+ // Make the request.
191+ try {
192+ $ this ->_handleAjax ( 'set-attachment-thumbnail ' );
193+ } catch ( WPAjaxDieContinueException $ e ) {
194+ unset( $ e );
195+ }
196+
197+ // Get the response.
198+ $ response = json_decode ( $ this ->_last_response , true );
199+
200+ // Check that success is false without sending nonce.
201+ $ this ->assertFalse ( $ response ['success ' ] );
202+ }
112203}
0 commit comments