Permalink
Browse files

Move logic for adding sidebars into separate function and add tests.

  • Loading branch information...
1 parent b62b16a commit 36e551fb75c38c9df6c89b7d5d3773cfcb821041 @andrewryno committed May 11, 2013
Showing with 68 additions and 31 deletions.
  1. +2 −0 .gitignore
  2. +8 −0 phpunit.xml
  3. +14 −0 tests/test-unique-page-sidebars.php
  4. +44 −31 unique-page-sidebars.php
View
@@ -0,0 +1,2 @@
+.DS_Store
+report
View
@@ -11,4 +11,12 @@
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
+ <logging>
+ <log type="coverage-html" target="report" charset="UTF-8" highlight="false" lowUpperBound="35" highLowerBound="70"/>
+ </logging>
+ <filter>
+ <whitelist processUncoveredFilesFromWhitelist="true">
+ <file>./unique-page-sidebars.php</file>
+ </whitelist>
+ </filter>
</phpunit>
@@ -14,4 +14,18 @@ public function testFilters() {
$this->assertTrue( has_filter( 'ups_sidebar' ) );
}
+ public function testAddingNewSidebars() {
+ $expected = array('name' => 'First Sidebar', 'description' => '', 'before_title' => '', 'after_title' => '', 'before_widget' => '', 'after_widget' => '', 'children' => 'off', 'locations' => array());
+ $add_sidebar = $this->plugin->add_sidebar( 'First Sidebar' );
+ $this->assertTrue(isset($add_sidebar['ups-sidebar-1']), 'First sidebar should be saved in array.');
+ $this->assertEquals($expected, $add_sidebar['ups-sidebar-1'], 'Sidebar should be saved with default options.');
+ $this->assertEquals(1, get_option('ups_sidebars_last_id'), 'Last sidebar ID should be saved as option.');
+
+ $expected = array('name' => 'Second Sidebar', 'description' => '', 'before_title' => '', 'after_title' => '', 'before_widget' => '', 'after_widget' => '', 'children' => 'off', 'locations' => array());
+ $add_sidebar = $this->plugin->add_sidebar( 'Second Sidebar' );
+ $this->assertTrue(isset($add_sidebar['ups-sidebar-2']), 'Second sidebar should be saved in array.');
+ $this->assertEquals($expected, $add_sidebar['ups-sidebar-2'], 'Sidebar should be saved with default options.');
+ $this->assertEquals(2, get_option('ups_sidebars_last_id'), 'Last sidebar ID should be saved as option.');
+ }
+
}
View
@@ -274,37 +274,7 @@ public function meta_box( $post, $metabox ) {
*/
public function validate( $input ) {
if ( isset( $input['add_sidebar'] ) ) {
- $sidebars = get_option( 'ups_sidebars' );
- if ( ! empty( $input['add_sidebar'] ) ) {
- // Get the last sidebar ID from the database
- $sidebar_num = get_option( 'ups_sidebars_last_id', -1 );
- if ( $sidebar_num < 0 ) {
- // Backward compatibility for existing sidebars
- if ( is_array( $sidebars ) ) {
- $last_id = end( array_keys( $sidebars ) );
- $last_num = end( explode( '-', $last_id ) );
- $sidebar_num = intval( $last_num );
- } else {
- $sidebar_num = 0;
- }
- }
-
- // Increment the sidebar number and save it
- $sidebar_num += 1;
- update_option( 'ups_sidebars_last_id', $sidebar_num );
-
- $sidebars['ups-sidebar-' . $sidebar_num] = array(
- 'name' => esc_html( $input['add_sidebar'] ),
- 'description' => '',
- 'before_title' => '',
- 'after_title' => '',
- 'before_widget' => '',
- 'after_widget' => '',
- 'children' => 'off',
- 'locations' => array()
- );
- }
- return $sidebars;
+ $input = $this->add_sidebar();
}
if ( isset( $input['delete'] ) ) {
@@ -319,6 +289,49 @@ public function validate( $input ) {
}
/**
+ * Adds a sidebar to the database.
+ *
+ * @param string $name
+ * @return array
+ */
+ public function add_sidebar( $name ) {
+ $sidebars = get_option( 'ups_sidebars' );
+ if ( empty( $name ) ) {
+ return false;
+ }
+
+ // Get the last sidebar ID from the database
+ $sidebar_num = get_option( 'ups_sidebars_last_id', -1 );
+ if ( $sidebar_num < 0 ) {
+ // Backward compatibility for existing sidebars
+ if ( is_array( $sidebars ) ) {
+ $last_id = end( array_keys( $sidebars ) );
+ $last_num = end( explode( '-', $last_id ) );
+ $sidebar_num = intval( $last_num );
+ } else {
+ $sidebar_num = 0;
+ }
+ }
+
+ // Increment the sidebar number and save it
+ $sidebar_num += 1;
+ update_option( 'ups_sidebars_last_id', $sidebar_num );
+
+ $sidebars['ups-sidebar-' . $sidebar_num] = array(
+ 'name' => esc_html( $name ),
+ 'description' => '',
+ 'before_title' => '',
+ 'after_title' => '',
+ 'before_widget' => '',
+ 'after_widget' => '',
+ 'children' => 'off',
+ 'locations' => array()
+ );
+
+ return $sidebars;
+ }
+
+ /**
* Handles the content of the metabox which allows adding new sidebars.
*/
public function new_sidebar() {

0 comments on commit 36e551f

Please sign in to comment.