Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit be2a9cd

Browse files
committed
Add test for Customize_Snapshot_Manager::should_import_and_preview_snapshot()
1 parent 19a224e commit be2a9cd

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

php/class-customize-snapshot-manager.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ public function is_theme_active() {
292292
public function should_import_and_preview_snapshot( Customize_Snapshot $snapshot ) {
293293
global $pagenow;
294294

295-
// Ignore if in the admin.
296-
if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && 'customize.php' !== $pagenow ) {
295+
// Ignore if in the admin, but not Admin Ajax or Customizer.
296+
if ( is_admin() && ! in_array( $pagenow, array( 'admin-ajax.php', 'customize.php' ), true ) ) {
297297
return false;
298298
}
299299

@@ -316,7 +316,7 @@ public function should_import_and_preview_snapshot( Customize_Snapshot $snapshot
316316
* Note that wp.customize.Snapshots.extendPreviewerQuery() will extend the
317317
* previewer data to include the current snapshot UUID.
318318
*/
319-
if ( count( $this->customize_manager->unsanitized_post_values() ) > 0 ) {
319+
if ( $this->customize_manager && count( $this->customize_manager->unsanitized_post_values() ) > 0 ) {
320320
return false;
321321
}
322322

tests/php/test-class-customize-snapshot-manager.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,55 @@ public function test_is_theme_active() {
377377
* @covers Customize_Snapshot_Manager::should_import_and_preview_snapshot()
378378
*/
379379
public function test_should_import_and_preview_snapshot() {
380-
$this->markTestIncomplete();
380+
global $pagenow, $wp_customize;
381+
$_REQUEST['customize_snapshot_uuid'] = self::UUID;
382+
$manager = $this->plugin->customize_snapshot_manager;
383+
$post_id = $manager->post_type->save( array(
384+
'uuid' => self::UUID,
385+
'data' => array( 'blogname' => array( 'value' => 'Foo' ) ),
386+
) );
387+
$snapshot = new Customize_Snapshot( $manager, self::UUID );
388+
389+
// Not if admin.
390+
set_current_screen( 'posts' );
391+
$pagenow = 'posts.php'; // WPCS: global override ok.
392+
$this->assertTrue( is_admin() );
393+
$this->assertFalse( $manager->should_import_and_preview_snapshot( $snapshot ) );
394+
395+
// Not if theme switch error.
396+
set_current_screen( 'customize' );
397+
$pagenow = 'customize.php'; // WPCS: global override ok.
398+
update_post_meta( $post_id, '_snapshot_theme', 'Foo' );
399+
$this->assertFalse( $manager->should_import_and_preview_snapshot( $snapshot ) );
400+
delete_post_meta( $post_id, '_snapshot_theme' );
401+
402+
// Not if customize_save.
403+
$_REQUEST['action'] = 'customize_save';
404+
$this->assertFalse( $manager->should_import_and_preview_snapshot( $snapshot ) );
405+
unset( $_REQUEST['action'] );
406+
407+
// Not if published snapshot.
408+
$manager->post_type->save( array(
409+
'uuid' => self::UUID,
410+
'status' => 'publish',
411+
) );
412+
$this->assertFalse( $manager->should_import_and_preview_snapshot( $snapshot ) );
413+
$manager->post_type->save( array(
414+
'uuid' => self::UUID,
415+
'status' => 'draft',
416+
) );
417+
418+
// Not if unsanitized post values is not empty.
419+
$manager->customize_manager = new \WP_Customize_Manager();
420+
$wp_customize = $manager->customize_manager; // WPCS: global override ok.
421+
$wp_customize->set_post_value( 'name', 'value' );
422+
$this->assertNotEmpty( $manager->customize_manager->unsanitized_post_values() );
423+
$this->assertFalse( $manager->should_import_and_preview_snapshot( $snapshot ) );
424+
425+
// OK.
426+
$manager->customize_manager = new \WP_Customize_Manager();
427+
$wp_customize = $manager->customize_manager; // WPCS: global override ok.
428+
$this->assertTrue( $manager->should_import_and_preview_snapshot( $snapshot ) );
381429
}
382430

383431
/**

0 commit comments

Comments
 (0)