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
require_once( 'cmb-tests-base.php' );
class Test_CMB2_Ajax extends Test_CMB2 {
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() );
}
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 );
$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>— 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 );
}
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 ] ) );
}
}
}
}
}
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' );
}
}