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 
<?php
/**
 * CMB2_Field tests
 *
 * @package   Tests_CMB2
 * @author    CMB2 team
 * @license   GPL-2.0+
 * @link      https://cmb2.io
 */

require_once( 'cmb-tests-base.php' );

/**
 * Test the oEmbed functionality
 */
class Test_CMB2_Ajax extends Test_CMB2 {

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

        $this->cmb = cmb2_get_metabox( array(
            'id'      => 'metabox_id',
            'hookup'  => false,
            'show_on' => array(
                'key'   => 'options-page',
                'value' => 'options-page-id',
            ),
            'fields' => array(
                array(
                    'id'   => 'test_embed',
                    'type' => 'oembed',
                ),
                array(
                    'id'   => 'another_value',
                    'type' => 'text',
                ),
            ),
        ), 'options-page-id', 'options-page' );

        $this->oembed_args = array(
            'url'         => 'https://www.youtube.com/watch?v=NCXyEKqmWdA',
            'object_id'   => 'options-page-id',
            'object_type' => 'options-page',
            'oembed_args' => array(
                'width' => '640',
            ),
            'field_id'    => 'test_embed',
            'src'         => 'https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed',
        );

    }

    public function tear_down() {
        delete_option( $this->oembed_args['object_id'] );
        parent::tear_down();
    }

    public function test_cmb2_ajax_instance() {
        $this->assertInstanceOf( 'CMB2_Ajax', cmb2_ajax() );
    }

    public function test_correct_properties() {
        $this->assertEquals( $this->oembed_args['object_id'], $this->cmb->object_id() );
        $this->assertEquals( $this->oembed_args['object_type'], $this->cmb->object_type() );
    }

    /**
     * @group cmb2-ajax-embed
     */
    public function test_get_oembed() {
        $args = $this->oembed_args;

        $args['oembed_result'] = array(
            sprintf( '<iframe width="640" height="360" src="%s"', $args['src'] ),
            '></iframe>',
        );

        if ( CMB2_Utils::wp_at_least( '5.2.0' ) ) {
            $args['oembed_result'][0] = str_replace( 'iframe ', 'iframe title="Hello - Adele" ', $args['oembed_result'][0] );
        }

        $this->assertOEmbedResult( $args );

        // Test another oembed URL
        $args['url'] = 'https://twitter.com/Jtsternberg/status/703434891518726144';

        $args['oembed_result'] = array(
            '<blockquote class="twitter-tweet" ',
            '<p lang="en" dir="ltr">That time we did Adele’s “Hello” at ',
            '<a href="https://t.co/aq89T5VM5x">https://t.co/aq89T5VM5x</a></p>&mdash; Justin Sternberg (@Jtsternberg) ',
            sprintf( '<a href="%1$s', $args['url'] ),
            '">February 27, 2016</a></blockquote><script async src="',
            'platform.twitter.com/widgets.js',
            '</script>',
        );

        $this->assertOEmbedResult( $args );
    }

    /**
     * @group cmb2-ajax-embed
     */
    public function test_values_cached() {
        $options = $this->get_option();

        $expected = array(
            '_oembed_887df34cb3e109936f1e848042f873a3' => array(
                '<iframe',
                'src="https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed"',
                '</iframe>',
            ),
            '_oembed_bc2b74b277d0e39ae9ec91eefaee8e31' => array( '{{unknown}}' ),
        );

        if ( $this->is_3_8() && $this->is_connected() ) {
            foreach ( $expected as $key => $value ) {
                $this->assertVerifiersMatch( array( 'connected' => $value ), $options[ $key ] );
            }
        } else {
            $opt_keys = array_keys( $options );
            $opt_values = array_values( $options );

            $_expected = $this->is_connected() ? array(
                array( '{{unknown}}' ),
                array(
                    '<blockquote class="twitter-tweet"',
                    'That time we did Adele’s',
                    'href="https://twitter.com/Jtsternberg/status/703434891518726144',
                    'February 27, 2016</a></blockquote><script async src="',
                    'platform.twitter.com/widgets.js" charset="utf-8"></script>',
                ),
                'time_2',
            ) : array(
                $expected['_oembed_bc2b74b277d0e39ae9ec91eefaee8e31'][0],
                $expected['_oembed_bc2b74b277d0e39ae9ec91eefaee8e31'][0],
            );

            foreach ( $_expected as $key => $expected_value ) {
                $opt_key = $opt_keys[ $key ];
                $val = $opt_values[ $key ];

                if ( is_array( $expected_value ) ) {
                    $this->assertVerifiersMatch( array( 'connected' => $expected_value ), $val );
                } else {

                    if ( 0 !== strpos( $expected_value, 'time_' ) ) {
                        $this->assertHTMLstringsAreEqual( $expected_value, $opt_values[ $key ] );
                        $this->assertTrue( 0 === strpos( $opt_key, '_oembed_' ) );
                    } else {
                        $this->assertTrue( 0 === strpos( $opt_key, '_oembed_time_' ) );
                        $this->assertTrue( is_int( $opt_values[ $key ] ) );
                    }
                }
            }
        }// End if().
    }

    public function test_get_oembed_delete_with_expired_ttl() {
        add_filter( 'oembed_ttl', '__return_zero' );
        add_action( 'cmb2_save_options-page_fields', array( 'CMB2_Ajax', 'clean_stale_options_page_oembeds' ) );

        $new = array(
            'another_value' => 'value',
        );
        if ( $this->is_3_8() ) {
            $new = array(
                '_oembed_887df34cb3e109936f1e848042f873a3' => '<iframe width="640" height="360" src="https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed" frameborder="0" allowfullscreen></iframe>',
            );
        }
        $_POST = array_merge( $new, $this->get_option() );

        $this->cmb->save_fields();
        $options = $this->get_option();

        if ( $this->is_3_8() ) {
            foreach ( array(
                '<iframe',
                'src="https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed"',
                '</iframe>',
            ) as $part ) {
                $this->assertTrue( false !== strpos( $options['_oembed_887df34cb3e109936f1e848042f873a3'], $part ), $part );
            }
        } else {
            $this->assertEquals( $new, $options );
        }
    }

    protected function get_option() {
        return cmb2_options( $this->oembed_args['object_id'] )->get_options();
    }

    protected function is_3_8() {
        return ! CMB2_Utils::wp_at_least( '3.8.1' );
    }

}
CMB2 Documentation API documentation generated by ApiGen