Skip to content

Commit

Permalink
Revamp oembed tests to allow partial string matching (to account for …
Browse files Browse the repository at this point in the history
…param changes we don't care about)
  • Loading branch information
jtsternberg committed Oct 26, 2017
1 parent d026afb commit 18e29be
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 67 deletions.
92 changes: 78 additions & 14 deletions tests/cmb-tests-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ public function is_connected() {
return $is_conn;
}

public function expected_youtube_oembed_results( $args ) {
if ( $this->is_connected() ) {
$args['oembed_result'] = sprintf( '<iframe width="640" height="360" src="%s" frameborder="0" allowfullscreen></iframe>', $args['src'] );
return $this->expected_oembed_success_results( $args );
}

return $this->no_connection_oembed_result( $args['url'] );
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'] );
}

public function expected_oembed_success_results( $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 ) )
);
}

public function no_connection_oembed_result( $url ) {
protected function oembed_no_connection_result( $url ) {
global $wp_embed;
return sprintf(
'<p class="ui-state-error-text">%s</p>',
Expand All @@ -72,14 +72,35 @@ public function no_connection_oembed_result( $url ) {
);
}

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->expected_oembed_success_results( $args ) ),
$this->normalize_http_string( $this->no_connection_oembed_result( $args['url'] ) ),
$this->normalize_http_string( $this->oembed_success_result( $args ) ),
$this->normalize_http_string( $this->oembed_no_connection_result( $args['url'] ) ),
);

$actual = $this->normalize_http_string( cmb2_ajax()->get_oembed( $args ) );

$results = array();
foreach ( $possibilities as $key => $expected ) {
$results[ $key ] = $this->compare_strings( $expected, $actual );
Expand All @@ -96,6 +117,49 @@ public function assertOEmbedResult( $args ) {
}
}

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 ) );
}
Expand Down
72 changes: 47 additions & 25 deletions tests/test-cmb-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,24 @@ public function test_correct_properties() {
public function test_get_oembed() {
$args = $this->oembed_args;

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

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

$args['oembed_result'] = sprintf( '<blockquote class="twitter-tweet" data-width="550"><p lang="en" dir="ltr">That time we did Adele’s “Hello” at <a href="https://twitter.com/generationschch%1$s">@generationschch</a>…<a href="https://t.co/aq89T5VM5x">https://t.co/aq89T5VM5x</a></p>&mdash; Justin Sternberg (@Jtsternberg) <a href="%2$s%1$s">February 27, 2016</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>', CMB2_Utils::wp_at_least( '4.8.2' ) ? '?ref_src=twsrc%5Etfw' : '', $args['url'] );
$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 );
}
Expand All @@ -89,44 +100,55 @@ public function test_get_oembed() {
*/
public function test_values_cached() {
$options = $this->get_option();
if ( $this->is_3_8() && $this->is_connected() ) {
$expected = array(
'_oembed_887df34cb3e109936f1e848042f873a3' => '<iframe width="640" height="360" src="https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed" frameborder="0" allowfullscreen></iframe>',
'_oembed_bc2b74b277d0e39ae9ec91eefaee8e31' => '{{unknown}}',
);

foreach ( $expected as $key => $value ) {
$this->assertTrue( array_key_exists( $key, $options ) );
$expected = array(
'_oembed_887df34cb3e109936f1e848042f873a3' => array(
'<iframe',
'src="https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed"',
'</iframe>',
),
'_oembed_bc2b74b277d0e39ae9ec91eefaee8e31' => array( '{{unknown}}' ),
);

if ( 0 !== strpos( $key, '_oembed_time_' ) ) {
$this->assertEquals( $expected[ $key ], $options[ $key ] );
} else {
$this->assertTrue( is_int( $value ) );
}
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(
'<iframe width="640" height="360" src="https://www.youtube.com/embed/NCXyEKqmWdA?feature=oembed" frameborder="0" allowfullscreen></iframe>',
$_expected = $this->is_connected() ? array(
$expected['_oembed_887df34cb3e109936f1e848042f873a3'],
'time_1',
sprintf( '<blockquote class="twitter-tweet" data-width="550"><p lang="en" dir="ltr">That time we did Adele’s “Hello” at <a href="https://twitter.com/generationschch%1$s">@generationschch</a>…<a href="https://t.co/aq89T5VM5x">https://t.co/aq89T5VM5x</a></p>&mdash; Justin Sternberg (@Jtsternberg) <a href="https://twitter.com/Jtsternberg/status/703434891518726144%1$s">February 27, 2016</a></blockquote><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>', CMB2_Utils::wp_at_least( '4.8.2' ) ? '?ref_src=twsrc%5Etfw' : '' ),
array(
'<blockquote class="twitter-tweet"',
'That time we did Adele’s “Hello” at <a href="https://twitter.com/generationschch',
'>@generationschch</a>…<a href="https://t.co/aq89T5VM5x">https://t.co/aq89T5VM5x</a></p>&mdash; Justin Sternberg (@Jtsternberg) <a 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(
'{{unknown}}',
'{{unknown}}',
$expected['_oembed_bc2b74b277d0e39ae9ec91eefaee8e31'][0],
$expected['_oembed_bc2b74b277d0e39ae9ec91eefaee8e31'][0],
);

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

if ( 0 !== strpos( $expected_value, 'time_' ) ) {
$this->assertHTMLstringsAreEqual( $expected_value, $opt_values[ $key ] );
$this->assertTrue( 0 === strpos( $opt_key, '_oembed_' ) );
if ( is_array( $expected_value ) ) {
$this->assertVerifiersMatch( array( 'connected' => $expected_value ), $val );
} else {
$this->assertTrue( 0 === strpos( $opt_key, '_oembed_time_' ) );
$this->assertTrue( is_int( $opt_values[ $key ] ) );

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().
Expand Down
35 changes: 22 additions & 13 deletions tests/test-cmb-types-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,23 +189,32 @@ public function test_taxonomy_multicheck_inline() {
*/
public function test_oembed() {
$vid = 'EOfy5LDpEHo';
$value = 'https://www.youtube.com/watch?v=' . $vid;
update_post_meta( $this->post_id, $this->text_type_field['id'], $value );
$args = array(
'src' => 'http://www.youtube.com/embed/' . $vid . '?feature=oembed',
'url' => 'https://www.youtube.com/watch?v=' . $vid,
'field_id' => 'field_test_field',
'object_id' => $this->post_id,
);

$expected_field = $this->is_connected()
? '<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><div class="cmb2-oembed"><iframe width="300" height="169" src="www.youtube.com/embed/' . $vid . '?feature=oembed" frameborder="0" allowfullscreen></iframe></div></div>'
: '<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><p class="ui-state-error-text">' . sprintf( esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ), '<a href="www.youtube.com/watch?v=' . $vid . '">www.youtube.com/watch?v=' . $vid . '</a>', '<a href="codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>' ) . '</p></div>';
update_post_meta( $this->post_id, $this->text_type_field['id'], $args['url'] );

$actual_field = $this->capture_render( array( $this->get_field_object( 'oembed' ), 'render_column' ) );
$result_verifiers = array(
'connected' => array(
'<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><div class="cmb2-oembed"><iframe ',
'src="www.youtube.com/embed/' . $vid . '?feature=oembed"',
'</iframe></div></div>',
),
'no_connection' => array(
'<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><p class="ui-state-error-text">' . sprintf( esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ), '<a href="www.youtube.com/watch?v=' . $vid . '">www.youtube.com/watch?v=' . $vid . '</a>', '<a href="codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>' ) . '</p></div>',
),
);

if ( ! $this->is_connected() ) {
$expected_field = '<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><p class="ui-state-error-text">No oEmbed Results Found for <a href="www.youtube.com/watch?v=' . $vid . '">www.youtube.com/watch?v=' . $vid . '</a>. View more info at <a href="codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p></div>';
}
$cb = array( $this->get_field_object( 'oembed' ), 'render_column' );
$actual_field = $this->capture_render( $cb );

$this->assertHTMLstringsAreEqual(
preg_replace( '~https?://~', '', $expected_field ), // normalize http differences
preg_replace( '~https?://~', '', $actual_field ) // normalize http differences
);
$this->assertVerifiersMatch( $result_verifiers, $actual_field );

delete_post_meta( $this->post_id, $this->text_type_field['id'] );
}

public function test_file_list() {
Expand Down
28 changes: 13 additions & 15 deletions tests/test-cmb-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,25 +928,23 @@ public function test_oembed_field() {
* @group cmb2-ajax-embed
*/
public function test_oembed_field_after_value_update() {
$vid = 'EOfy5LDpEHo';
$value = 'https://www.youtube.com/watch?v=' . $vid;
update_post_meta( $this->post_id, $this->text_type_field['id'], $value );

$results = $this->expected_youtube_oembed_results( array(
'src' => 'http://www.youtube.com/embed/' . $vid . '?feature=oembed',
'url' => $value,
'field_id' => 'field_test_field',
) );

$expected_field = sprintf( '<input type="text" class="cmb2-oembed regular-text" name="field_test_field" id="field_test_field" value="%1$s" data-objectid=\'%2$d\' data-objecttype=\'post\'/><p class="cmb2-metabox-description">This is a description</p><p class="cmb-spinner spinner"></p><div id="field_test_field-status" class="cmb2-media-status ui-helper-clearfix embed_wrap">%3$s</div>', $value, $this->post_id, $results );
$args = array(
'src' => 'http://www.youtube.com/embed/EOfy5LDpEHo?feature=oembed',
'url' => 'https://www.youtube.com/watch?v=EOfy5LDpEHo',
'field_id' => 'field_test_field',
'object_id' => $this->post_id,
);

$actual_field = $this->capture_render( array( $this->get_field_type_object( 'oembed' ), 'render' ) );
update_post_meta( $this->post_id, $this->text_type_field['id'], $args['url'] );

$this->assertHTMLstringsAreEqual(
preg_replace( '~https?://~', '', $expected_field ), // normalize http differences
preg_replace( '~https?://~', '', $actual_field ) // normalize http differences
$args['oembed_result'] = array(
'<iframe ',
sprintf( 'src="%s"', $args['src'] ),
'</iframe>',
);

$this->assertOEmbedResult( $args );

delete_post_meta( $this->post_id, $this->text_type_field['id'] );
}

Expand Down

0 comments on commit 18e29be

Please sign in to comment.