CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_Integration_Box
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils

Hooks

  • Hook Reference
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 
<?php
/**
 * CMB2 tests base
 *
 * @package   Tests_CMB2
 * @author    CMB2 team
 * @license   GPL-2.0+
 * @link      https://cmb2.io
 */
abstract class Test_CMB2 extends WP_UnitTestCase {

    public $hooks_to_die = array();

    /**
     * Set up the test fixture
     */
    public function set_up() {
        parent::set_up();
    }

    public function tear_down() {
        parent::tear_down();
    }

    public function normalize_string( $string ) {
        return trim( preg_replace( array(
            '/[\t\n\r]/', // Remove tabs and newlines
            '/\s{2,}/', // Replace repeating spaces with one space
            '/> </', // Remove spaces between carats
            ), array(
            '',
            ' ',
            '><',
        ), $string ) );
    }

    public function is_connected() {
        static $is_conn = null;
        if ( null === $is_conn ) {
            $connected = @fsockopen( 'www.youtube.com', 80 );
            if ( $connected ) {
                $is_conn = true;
                fclose( $connected );
            } else {
                $is_conn = false; //action in connection failure
            }
        }

        return $is_conn;
    }

    protected function oembed_success_result( $args ) {
        return sprintf( '<div class="cmb2-oembed embed-status">%s<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="%s">' . esc_html__( 'Remove Embed', 'cmb2' ) . '</a></p></div>', $args['oembed_result'], $args['field_id'] );
    }

    protected function oembed_success_result_verifiers( $args ) {
        $verifiers             = $args['oembed_result'];
        $args['oembed_result'] = 'SPLIT';
        return array_merge(
            $verifiers,
            explode( 'SPLIT', $this->oembed_success_result( $args ) )
        );
    }

    protected function oembed_no_connection_result( $url ) {
        global $wp_embed;
        return sprintf(
            '<p class="ui-state-error-text">%s</p>',
            sprintf(
                /* translators: 1: results for. 2: link to codex.wordpress.org/Embeds */
                esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ),
                $wp_embed->maybe_make_link( $url ),
                '<a href="https://wordpress.org/support/article/embeds/" target="_blank">codex.wordpress.org/Embeds</a>'
            )
        );
    }

    protected function oembed_no_connection_result_verifiers( $url ) {
        return array( $this->oembed_no_connection_result( $url ) );
    }

    public function assertOEmbedResult( $args ) {
        $actual = $this->normalize_http_string( cmb2_ajax()->get_oembed( $args ) );

        if ( isset( $args['result_verifiers'] ) || is_array( $args['oembed_result'] ) ) {
            if ( empty( $args['result_verifiers']['connected'] ) ) {
                $args['result_verifiers']['connected'] = $this->oembed_success_result_verifiers( $args );
            }

            if ( empty( $args['result_verifiers']['no_connection'] ) ) {
                $args['result_verifiers']['no_connection'] = $this->oembed_no_connection_result_verifiers( $args['url'] );
            }

            $verifiers = $args['result_verifiers'];
            $this->assertVerifiersMatch( $verifiers, $actual );
        } else {
            $this->assertOEmbedResultString( $args, $actual );
        }
    }

    protected function assertOEmbedResultString( $args, $actual ) {
        $possibilities = array(
            $this->normalize_http_string( $this->oembed_success_result( $args ) ),
            $this->normalize_http_string( $this->oembed_no_connection_result( $args['url'] ) ),
        );

        $results = array();
        foreach ( $possibilities as $key => $expected ) {
            $results[ $key ] = $this->compare_strings( $expected, $actual );
        }

        if ( ! empty( $results[0] ) && ! empty( $results[1] ) ) {
            // If they both failed, this will tell us.
            $this->assertEquals( $possibilities[0], $actual, $results[0] );
            $this->assertEquals( $possibilities[1], $actual, $results[1] );
        } elseif ( empty( $results[0] ) ) {
            $this->assertEquals( $possibilities[0], $actual );
        } else {
            $this->assertEquals( $possibilities[1], $actual );
        }
    }

    protected function assertVerifiersMatch( $result_verifiers, $actual ) {
        $failed = array();

        $possibilities = array(
            'connected' => array_map( array( $this, 'normalize_http_string' ), $result_verifiers['connected'] ),
        );

        if ( ! empty( $result_verifiers['no_connection'] ) ) {
            $possibilities['no_connection'] = array_map( array( $this, 'normalize_http_string' ), $result_verifiers['no_connection'] );
        }

        $actual = $this->normalize_http_string( $actual );

        foreach ( $possibilities as $key => $verifiers ) {
            foreach ( $verifiers as $key2 => $expected ) {
                if ( false === strpos( $actual, $expected ) ) {
                    $failed[ $key ][ $key2 ] = $expected;
                }
            }
        }

        $failed_test = ! empty( $failed['connected'] );
        if ( ! empty( $result_verifiers['no_connection'] ) ) {
            $failed_test = $failed_test && ! empty( $failed['no_connection'] );
        }

        if ( $failed_test ) {
            $msg = "\nThese verifiers are missing:\n";
            foreach ( $failed as $key => $fails ) {
                foreach ( $fails as $key2 => $fail ) {
                    $msg .= "\n$key - $key2:\n$fail\n";
                }
            }

            $msg .= "\nin:\n$actual\n\n";
            $this->assertTrue( false, $msg );
        } elseif ( empty( $failed['connected'] ) ) {
            $this->assertTrue( true );
        } else {
            $this->assertTrue( empty( $failed['no_connection'] ) );
        }
    }

    public function normalize_http_string( $string ) {
        return preg_replace( '~https?://~', '', $this->normalize_string( $string ) );
    }

    protected function capture_render( $cb ) {
        ob_start();
        call_user_func( $cb );
        // grab the data from the output buffer and add it to our $content variable
        return ob_get_clean();
    }

    protected function render_field( $field ) {
        return $this->capture_render( array( $field, 'render_field' ) );
    }

    protected function hook_to_wp_die( $hook ) {
        $this->hooks_to_die[] = $hook;
        add_action( $hook, array( $this, 'wp_die' ) );
    }

    public function wp_die() {
        $hook = array_pop( $this->hooks_to_die );
        remove_action( $hook, array( $this, 'wp_die' ) );
        wp_die( $hook . ' die' );
    }

    public static function compare_strings( $orig_string, $new_string, $orig_label = 'Expected', $compare_label = 'Actual' ) {
        $orig_length = strlen( $orig_string );
        $new_length  = strlen( $new_string );
        $compare     = strcmp( $orig_string, $new_string );

        if ( 0 !== $compare ) {

            $label_spacer = str_repeat( ' ', abs( strlen( $compare_label ) - strlen( $orig_label ) ) );
            $compare_spacer = $orig_spacer = '';

            if ( strlen( $compare_label ) > strlen( $orig_label ) ) {
                $orig_spacer = $label_spacer;
            } elseif ( strlen( $compare_label ) < strlen( $orig_label ) ) {
                $compare_spacer = $label_spacer;
            }

            $compare      = strspn( $orig_string ^ $new_string, "\0" );
            $chars_before = 15;
            $chars_after  = 75;
            $start        = ( $compare - $chars_before );
            $pointer      = '| ----> |';
            $ol           = '  ' . $orig_label . ':  ' . $orig_spacer;
            $cl           = '  ' . $compare_label . ':  ' . $compare_spacer;
            $sep          = "\n" . str_repeat( '-', $chars_after + $chars_before + strlen( $pointer ) + strlen( $ol ) + 2 );

            $compare = sprintf(
                $sep . '%8$s%8$s  First difference at position %1$d.%8$s%8$s  %9$s length: %2$d, %10$s length: %3$d%8$s%8$s%4$s%5$s%8$s%6$s%7$s%8$s' . $sep,
                $compare,
                $orig_length,
                $new_length,
                $ol,
                substr( $orig_string, $start, 15 ) . $pointer . substr( $orig_string, $compare, $chars_after ),
                $cl,
                substr( $new_string, $start, 15 ) . $pointer . substr( $new_string, $compare, $chars_after ),
                "\n",
                $orig_label,
                $compare_label
            );
        }

        return $compare;
    }

    /**
     * Call protected/private method of a class.
     *
     * @param object $object     Instantiated object that we will run method on.
     * @param string $methodName Method name to call
     *
     * @return mixed Method return.
     */
    public function invokeMethod( $object, $methodName ) {
        if ( version_compare( phpversion(), '5.3', '<' ) ) {
            $this->markTestSkipped( 'PHP version does not support ReflectionClass::setAccessible()' );
        }

        $args = func_get_args();
        unset( $args[0], $args[1] );

        $reflection = new ReflectionClass( get_class( $object ) );
        $method = $reflection->getMethod( $methodName );
        $method->setAccessible( true );

        return $method->invokeArgs( $object, $args );
    }

    /**
     * Get protected/private property of a class.
     *
     * @param object $object       Instantiated object that we will get property for.
     * @param string $propertyName Property to get.
     *
     * @return mixed               Value of property.
     */
    // protected function getProperty( $object, $propertyName ) {
    //  $reflection = new ReflectionClass( get_class( $object ) );
    //  $property = $reflection->getProperty( $propertyName );
    //  $property->setAccessible( true );

    //  return $property->getValue( $object );
    // }

    public function assertHTMLstringsAreEqual( $expected_string, $string_to_test, $msg = null ) {
        $expected_string = $this->normalize_string( $expected_string );
        $string_to_test = $this->normalize_string( $string_to_test );

        $compare = $this->compare_strings( $expected_string, $string_to_test );

        if ( ! empty( $compare ) ) {
            $msg .= $compare;
        }

        return $this->assertEquals( $expected_string, $string_to_test, $msg );
    }

    public function assertIsDefined( $definition ) {
        return $this->assertTrue( defined( $definition ), "$definition is not defined." );
    }

    /**
     * Backport assertNotFalse to PHPUnit 3.6.12 which only runs in PHP 5.2
     *
     * @link   https://github.com/woothemes/woocommerce/blob/f5ff10711dc664f1c5ec5277634a5eea2b828765/tests/framework/class-wc-unit-test-case.php
     *
     */
    public static function assertNotFalse( $condition, $message = '' ) {
        if ( version_compare( phpversion(), '5.3', '<' ) ) {
            self::assertThat( $condition, self::logicalNot( self::isFalse() ), $message );
        } else {
            parent::assertNotFalse( $condition, $message );
        }
    }

    /**
     * Backport assertContainsOnlyInstancesOf to PHPUnit 3.6.12 which only runs in PHP 5.2
     */
    public static function assertContainsOnlyInstancesOf( $classname, $haystack, $message = '' ) {
        if ( version_compare( phpversion(), '5.3', '<' ) ) {
            foreach ( $haystack as $to_check ) {
                self::assertInstanceOf( $classname, $to_check, $message );
            }
        } else {
            parent::assertContainsOnlyInstancesOf( $classname, $haystack, $message );
        }
    }
}
CMB2 Documentation API documentation generated by ApiGen