<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41266] trunk/tests/phpunit/tests/widgets.php: Widgets: Add tests for `retrieve_widgets()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41266">41266</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41266","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>obenland</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-08-18 15:03:43 +0000 (Fri, 18 Aug 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Widgets: Add tests for `retrieve_widgets()`.

Helps with maintaining back compat when making changes in the future.

See <a href="https://core.trac.wordpress.org/ticket/39693">#39693</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestswidgetsphp">trunk/tests/phpunit/tests/widgets.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestswidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/widgets.php     2017-08-18 14:23:06 UTC (rev 41265)
+++ trunk/tests/phpunit/tests/widgets.php       2017-08-18 15:03:43 UTC (rev 41266)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -678,4 +678,255 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Register nav menu sidebars.
+        *
+        * @param array $sidebars Sidebar slugs.
+        */
+       function register_sidebars( $sidebars ) {
+               foreach ( $sidebars as $sidebar ) {
+                       register_sidebar( array( 'id' => $sidebar ) );
+               }
+       }
+
+       /**
+        * Tests for when 'sidebars_widgets' theme mod is populated.
+        *
+        * @covers retrieve_widgets()
+        */
+       function test_retrieve_widgets_with_theme_mod() {
+               global $sidebars_widgets, $_wp_sidebars_widgets;
+
+               wp_widgets_init();
+               $this->register_sidebars( array( 'sidebar-1', 'sidebar-2','sidebar-3', 'wp_inactive_widgets' ) );
+
+               set_theme_mod( 'sidebars_widgets', array(
+                       'time' => time(),
+                       'data' => array(
+                               'sidebar-1' => array( 'tag_cloud-1' ),
+                               'sidebar-2' => array( 'text-1' ),
+                               'sidebar-3' => array( 'unregistered_widget-1' ),
+                               'fantasy'   => array( 'archives-2' ),
+                               'wp_inactive_widgets' => array(),
+                       ),
+               ) );
+
+               $result = retrieve_widgets( true );
+
+               $_wp_sidebars_widgets = array();
+               $this->assertInternalType( 'array', $result );
+               $this->assertNotEmpty( $result );
+
+               foreach ( $sidebars_widgets as $widgets ) {
+                       $this->assertInternalType( 'array', $widgets );
+               }
+
+               $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
+               $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
+               $this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] );
+
+               // Unregistered widget should be filtered out.
+               $this->assertEmpty( $sidebars_widgets['sidebar-3'] );
+
+               // 6 default widgets - 1 active text widget = 5.
+               $this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'meta-2',            $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'search-2',          $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'categories-2',      $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-posts-2',    $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
+
+               // Theme mode with previous widgets was removed.
+               $this->assertFalse( get_theme_mod( 'sidebars_widgets' ) );
+
+               // Sidebar_widgets option was updated.
+               $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
+       }
+
+       /**
+        * Tests for when sidebars widgets matches registered sidebars.
+        *
+        * @covers retrieve_widgets()
+        */
+       function test_retrieve_widgets_with_sidebars_widgets_matching_registered_sidebars() {
+               global $sidebars_widgets;
+
+               wp_widgets_init();
+               $this->register_sidebars( array( 'sidebar-1', 'sidebar-2','sidebar-3', 'wp_inactive_widgets' ) );
+
+               $sidebars_widgets = array(
+                       'sidebar-1' => array( 'tag_cloud-1' ),
+                       'sidebar-2' => array( 'text-1' ),
+                       'sidebar-3' => array( 'custom_widget-1' ),
+                       'wp_inactive_widgets' => array(),
+               );
+
+               $result = retrieve_widgets( true );
+
+               // $sidebars_widgets matches registered sidebars.
+               $this->assertNull( $result );
+
+               foreach ( $sidebars_widgets as $widgets ) {
+                       $this->assertInternalType( 'array', $widgets );
+               }
+
+               $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
+               $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
+
+               // No widget validity check when $sidebars_widgets matches registered sidebars.
+               $this->assertContains( 'custom_widget-1', $sidebars_widgets['sidebar-3'] );
+
+               // No lost widgets when $sidebars_widgets matches registered sidebars.
+               $this->assertEmpty( $sidebars_widgets['wp_inactive_widgets'] );
+       }
+
+       /**
+        * Tests for when sidebars widgets doesn't match registered sidebars.
+        *
+        * @covers retrieve_widgets()
+        */
+       function test_retrieve_widgets_with_sidebars_widgets_not_matching_registered_sidebars() {
+               global $sidebars_widgets, $_wp_sidebars_widgets;
+
+               wp_widgets_init();
+               $this->register_sidebars( array( 'sidebar-1', 'sidebar-2','sidebar-3', 'wp_inactive_widgets' ) );
+
+               $sidebars_widgets = array(
+                       'sidebar-1' => array( 'tag_cloud-1' ),
+                       'sidebar-2' => array( 'text-1' ),
+                       'fantasy'   => array( 'unregistered_widget-1' ),
+                       'wp_inactive_widgets' => array(),
+               );
+
+               // Theme changed.
+               $result = retrieve_widgets( true );
+
+               $_wp_sidebars_widgets = array();
+               $this->assertInternalType( 'array', $result );
+               $this->assertNotEmpty( $result );
+
+               foreach ( $sidebars_widgets as $widgets ) {
+                       $this->assertInternalType( 'array', $widgets );
+               }
+
+               // Current theme doesn't have a fantasy-sidebar.
+               $this->assertArrayNotHasKey( 'fantasy', $sidebars_widgets );
+               $this->assertArrayHasKey( 'sidebar-3', $sidebars_widgets );
+
+               $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
+               $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
+               $this->assertEmpty( $sidebars_widgets['sidebar-3'] );
+
+               // We should not have orphaned widgets, because widget was not registered.
+               $this->assertArrayNotHasKey( 'orphaned_widgets_1', $sidebars_widgets );
+
+               // 6 default widgets.
+               $this->assertCount( 6, $sidebars_widgets['wp_inactive_widgets'] );
+
+               $this->assertContains( 'archives-2',        $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'meta-2',            $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'search-2',          $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'categories-2',      $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-posts-2',    $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
+
+               // Sidebar_widgets option was updated.
+               $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
+
+               // Reset.
+               $sidebars_widgets = array(
+                       'sidebar-1' => array( 'tag_cloud-1' ),
+                       'sidebar-2' => array( 'text-1' ),
+                       'fantasy'   => array( 'archives-2' ),
+                       'wp_inactive_widgets' => array(),
+               );
+
+               // Theme did not change.
+               $result = retrieve_widgets();
+
+               $_wp_sidebars_widgets = array();
+               $this->assertInternalType( 'array', $result );
+               $this->assertNotEmpty( $result );
+
+               foreach ( $sidebars_widgets as $widgets ) {
+                       $this->assertInternalType( 'array', $widgets );
+               }
+
+               /*
+                * Only returns intersection of registered sidebars and saved sidebars,
+                * so neither fantasy-sidebar nor sidebar-3 will make the cut.
+                */
+               $this->assertArrayNotHasKey( 'fantasy', $sidebars_widgets );
+               $this->assertArrayNotHasKey( 'sidebar-3', $sidebars_widgets );
+
+               // archives-2 ends up as an orphan because of the above behavior.
+               $this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] );
+               $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
+               $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
+
+               // 6 default widgets - 1 active text widget = 5.
+               $this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] );
+
+               $this->assertContains( 'meta-2',            $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'search-2',          $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'categories-2',      $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-posts-2',    $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
+
+               // Sidebar_widgets option was updated.
+               $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
+       }
+
+       /**
+        * Tests for Customizer mode.
+        *
+        * @covers retrieve_widgets()
+        */
+       function test_retrieve_widgets_for_customizer() {
+               global $sidebars_widgets, $_wp_sidebars_widgets;
+
+               wp_widgets_init();
+               $this->register_sidebars( array( 'sidebar-1', 'sidebar-2','sidebar-3', 'wp_inactive_widgets' ) );
+
+               $old_sidebars_widgets = array(
+                       'time' => time(),
+                       'data' => array(
+                               'sidebar-1' => array( 'tag_cloud-1' ),
+                               'sidebar-2' => array( 'text-1' ),
+                               'sidebar-3' => array( 'unregistered_widget-1' ),
+                               'fantasy'   => array( 'archives-2' ),
+                               'wp_inactive_widgets' => array(),
+                       ),
+               );
+               set_theme_mod( 'sidebars_widgets', $old_sidebars_widgets );
+
+               $result = retrieve_widgets( 'customize' );
+
+               $_wp_sidebars_widgets = array();
+               $this->assertInternalType( 'array', $result );
+               $this->assertNotEmpty( $result );
+
+               foreach ( $sidebars_widgets as $widgets ) {
+                       $this->assertInternalType( 'array', $widgets );
+               }
+
+               $this->assertContains( 'tag_cloud-1', $sidebars_widgets['sidebar-1'] );
+               $this->assertContains( 'text-1', $sidebars_widgets['sidebar-2'] );
+               $this->assertContains( 'archives-2', $sidebars_widgets['orphaned_widgets_1'] );
+               $this->assertArrayHasKey( 'sidebar-3', $sidebars_widgets );
+               $this->assertEmpty( $sidebars_widgets['sidebar-3'] );
+               $this->assertCount( 5, $sidebars_widgets['wp_inactive_widgets'] );
+
+               $this->assertContains( 'meta-2',            $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'search-2',          $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'categories-2',      $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-posts-2',    $sidebars_widgets['wp_inactive_widgets'] );
+               $this->assertContains( 'recent-comments-2', $sidebars_widgets['wp_inactive_widgets'] );
+
+               // Theme mod with previous widgets was not removed.
+               $this->assertEqualSets( $old_sidebars_widgets, get_theme_mod( 'sidebars_widgets' ) );
+
+               // Sidebar_widgets option was not updated.
+               $this->assertNotEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>