| | 681 | function test_retrieve_widgets_with_theme_mod() { |
| | 682 | global $sidebars_widgets, $_wp_sidebars_widgets; |
| | 683 | |
| | 684 | wp_widgets_init(); |
| | 685 | |
| | 686 | register_sidebar( array( |
| | 687 | 'name' => 'Primary Sidebar', |
| | 688 | 'id' => 'sidebar-1', |
| | 689 | ) ); |
| | 690 | register_sidebar( array( |
| | 691 | 'name' => 'Content Sidebar', |
| | 692 | 'id' => 'sidebar-2', |
| | 693 | ) ); |
| | 694 | register_sidebar( array( |
| | 695 | 'name' => 'Footer Widget Area', |
| | 696 | 'id' => 'sidebar-3', |
| | 697 | ) ); |
| | 698 | register_sidebar( array( 'id' => 'wp_inactive_widgets' ) ); |
| | 699 | |
| | 700 | set_theme_mod( 'sidebars_widgets', array( |
| | 701 | 'time' => time(), |
| | 702 | 'data' => array( |
| | 703 | 'sidebar-1' => array( 'tag_cloud-1' ), |
| | 704 | 'sidebar-2' => array( 'text-1' ), |
| | 705 | 'sidebar-3' => array( 'unregistered_widget-1' ), |
| | 706 | 'fantasy-sidebar' => array( 'archives-2' ), |
| | 707 | 'wp_inactive_widgets' => array(), |
| | 708 | ), |
| | 709 | ) ); |
| | 710 | |
| | 711 | $result = retrieve_widgets( true ); |
| | 712 | |
| | 713 | $_wp_sidebars_widgets = array(); |
| | 714 | $this->assertInternalType( 'array', $result ); |
| | 715 | $this->assertNotEmpty( $result ); |
| | 716 | $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] ); |
| | 717 | $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] ); |
| | 718 | $this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] ); |
| | 719 | |
| | 720 | // Unregistered widget should be filtered out. |
| | 721 | $this->assertEmpty( $sidebars_widgets['sidebar-3'] ); |
| | 722 | |
| | 723 | // 6 default widgets - 1 active text widget = 5. |
| | 724 | $this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] ); |
| | 725 | |
| | 726 | // Theme mode with previous widgets was removed. |
| | 727 | $this->assertFalse( get_theme_mod( 'sidebars_widgets' ) ); |
| | 728 | |
| | 729 | // Sidebar_widgets option was updated. |
| | 730 | $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() ); |
| | 731 | } |
| | 732 | |
| | 733 | function test_retrieve_widgets_with_sidebars_widgets_matching_registered_sidebars() { |
| | 734 | global $sidebars_widgets; |
| | 735 | |
| | 736 | wp_widgets_init(); |
| | 737 | |
| | 738 | register_sidebar( array( |
| | 739 | 'name' => 'Primary Sidebar', |
| | 740 | 'id' => 'sidebar-1', |
| | 741 | ) ); |
| | 742 | register_sidebar( array( |
| | 743 | 'name' => 'Content Sidebar', |
| | 744 | 'id' => 'sidebar-2', |
| | 745 | ) ); |
| | 746 | register_sidebar( array( |
| | 747 | 'name' => 'Footer Widget Area', |
| | 748 | 'id' => 'sidebar-3', |
| | 749 | ) ); |
| | 750 | register_sidebar( array( 'id' => 'wp_inactive_widgets' ) ); |
| | 751 | |
| | 752 | $sidebars_widgets = array( |
| | 753 | 'sidebar-1' => array( 'tag_cloud-1' ), |
| | 754 | 'sidebar-2' => array( 'text-1' ), |
| | 755 | 'sidebar-3' => array( 'custom_widget-1' ), |
| | 756 | 'wp_inactive_widgets' => array(), |
| | 757 | ); |
| | 758 | |
| | 759 | $result = retrieve_widgets( true ); |
| | 760 | |
| | 761 | // $sidebars_widgets matches registered sidebars. |
| | 762 | $this->assertNull( $result ); |
| | 763 | $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] ); |
| | 764 | $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] ); |
| | 765 | |
| | 766 | // No widget validity check when $sidebars_widgets matches registered sidebars. |
| | 767 | $this->assertNotEmpty( $sidebars_widgets['sidebar-3'] ); |
| | 768 | |
| | 769 | // No lost widgets when $sidebars_widgets matches registered sidebars. |
| | 770 | $this->assertEmpty( $sidebars_widgets['wp_inactive_widgets'] ); |
| | 771 | } |
| | 772 | |
| | 773 | function test_retrieve_widgets_with_sidebars_widgets_not_matching_registered_sidebars() { |
| | 774 | global $sidebars_widgets, $_wp_sidebars_widgets; |
| | 775 | |
| | 776 | wp_widgets_init(); |
| | 777 | |
| | 778 | register_sidebar( array( |
| | 779 | 'name' => 'Primary Sidebar', |
| | 780 | 'id' => 'sidebar-1', |
| | 781 | ) ); |
| | 782 | register_sidebar( array( |
| | 783 | 'name' => 'Content Sidebar', |
| | 784 | 'id' => 'sidebar-2', |
| | 785 | ) ); |
| | 786 | register_sidebar( array( |
| | 787 | 'name' => 'Footer Widget Area', |
| | 788 | 'id' => 'sidebar-3', |
| | 789 | ) ); |
| | 790 | register_sidebar( array( 'id' => 'wp_inactive_widgets' ) ); |
| | 791 | |
| | 792 | $sidebars_widgets = array( |
| | 793 | 'sidebar-1' => array( 'tag_cloud-1' ), |
| | 794 | 'sidebar-2' => array( 'text-1' ), |
| | 795 | 'fantasy-sidebar' => array( 'unregistered_widget-1' ), |
| | 796 | 'wp_inactive_widgets' => array(), |
| | 797 | ); |
| | 798 | |
| | 799 | // Theme changed. |
| | 800 | $result = retrieve_widgets( true ); |
| | 801 | |
| | 802 | $_wp_sidebars_widgets = array(); |
| | 803 | $this->assertInternalType( 'array', $result ); |
| | 804 | $this->assertNotEmpty( $result ); |
| | 805 | |
| | 806 | // Current theme doesn't have a fantasy-sidebar. |
| | 807 | $this->assertArrayNotHasKey( 'fantasy-sidebar', $sidebars_widgets ); |
| | 808 | $this->assertArrayHasKey( 'sidebar-3', $sidebars_widgets ); |
| | 809 | |
| | 810 | $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] ); |
| | 811 | $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] ); |
| | 812 | |
| | 813 | // We should not have orphaned widgets, because widget was not registered. |
| | 814 | $this->assertArrayNotHasKey( 'orphaned_widgets_1', $sidebars_widgets ); |
| | 815 | |
| | 816 | // 6 default widgets. |
| | 817 | $this->assertCount( 6, $sidebars_widgets['wp_inactive_widgets'] ); |
| | 818 | |
| | 819 | // Sidebar_widgets option was updated. |
| | 820 | $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() ); |
| | 821 | |
| | 822 | // Reset. |
| | 823 | $sidebars_widgets = array( |
| | 824 | 'sidebar-1' => array( 'tag_cloud-1' ), |
| | 825 | 'sidebar-2' => array( 'text-1' ), |
| | 826 | 'fantasy-sidebar' => array( 'archives-2' ), |
| | 827 | 'wp_inactive_widgets' => array(), |
| | 828 | ); |
| | 829 | |
| | 830 | // Theme did not change. |
| | 831 | $result = retrieve_widgets(); |
| | 832 | |
| | 833 | $_wp_sidebars_widgets = array(); |
| | 834 | $this->assertInternalType( 'array', $result ); |
| | 835 | $this->assertNotEmpty( $result ); |
| | 836 | |
| | 837 | /* |
| | 838 | * Only returns intersection of registered sidebars and saved sidebars, |
| | 839 | * so neither fantasy-sidebar nor sidebar-3 will make the cut. |
| | 840 | */ |
| | 841 | $this->assertArrayNotHasKey( 'fantasy-sidebar', $sidebars_widgets ); |
| | 842 | $this->assertArrayNotHasKey( 'sidebar-3', $sidebars_widgets ); |
| | 843 | |
| | 844 | // archives-2 ends up as an orphan because of the above behavior. |
| | 845 | $this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] ); |
| | 846 | $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] ); |
| | 847 | $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] ); |
| | 848 | |
| | 849 | // 6 default widgets - 1 active text widget = 5. |
| | 850 | $this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] ); |
| | 851 | |
| | 852 | // Sidebar_widgets option was updated. |
| | 853 | $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() ); |
| | 854 | } |
| | 855 | |
| | 856 | function test_retrieve_widgets_for_customizer() { |
| | 857 | global $sidebars_widgets, $_wp_sidebars_widgets; |
| | 858 | |
| | 859 | wp_widgets_init(); |
| | 860 | |
| | 861 | register_sidebar( array( |
| | 862 | 'name' => 'Primary Sidebar', |
| | 863 | 'id' => 'sidebar-1', |
| | 864 | ) ); |
| | 865 | register_sidebar( array( |
| | 866 | 'name' => 'Content Sidebar', |
| | 867 | 'id' => 'sidebar-2', |
| | 868 | ) ); |
| | 869 | register_sidebar( array( |
| | 870 | 'name' => 'Footer Widget Area', |
| | 871 | 'id' => 'sidebar-3', |
| | 872 | ) ); |
| | 873 | register_sidebar( array( 'id' => 'wp_inactive_widgets' ) ); |
| | 874 | |
| | 875 | $old_sidebars_widgets = array( |
| | 876 | 'time' => time(), |
| | 877 | 'data' => array( |
| | 878 | 'sidebar-1' => array( 'tag_cloud-1' ), |
| | 879 | 'sidebar-2' => array( 'text-1' ), |
| | 880 | 'sidebar-3' => array( 'unregistered_widget-1' ), |
| | 881 | 'fantasy-sidebar' => array( 'archives-2' ), |
| | 882 | 'wp_inactive_widgets' => array(), |
| | 883 | ), |
| | 884 | ); |
| | 885 | set_theme_mod( 'sidebars_widgets', $old_sidebars_widgets ); |
| | 886 | |
| | 887 | $result = retrieve_widgets( 'customize' ); |
| | 888 | |
| | 889 | $_wp_sidebars_widgets = array(); |
| | 890 | $this->assertInternalType( 'array', $result ); |
| | 891 | $this->assertNotEmpty( $result ); |
| | 892 | $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] ); |
| | 893 | $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] ); |
| | 894 | $this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] ); |
| | 895 | $this->assertEmpty( $sidebars_widgets['sidebar-3'] ); |
| | 896 | $this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] ); |
| | 897 | |
| | 898 | // Theme mod with previous widgets was not removed. |
| | 899 | $this->assertEqualSets( $old_sidebars_widgets, get_theme_mod( 'sidebars_widgets' ) ); |
| | 900 | |
| | 901 | // Sidebar_widgets option was not updated. |
| | 902 | $this->assertNotEquals( $sidebars_widgets, wp_get_sidebars_widgets() ); |
| | 903 | } |