Make WordPress Core

Ticket #39693: 39693-tests.2.diff

File 39693-tests.2.diff, 9.7 KB (added by obenland, 8 years ago)

Updated tests for original retrieve_widgets().

  • tests/phpunit/tests/widgets.php

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