Skip to content

Commit

Permalink
fix: modified tests, checking if an url contains gif image
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoreMihai authored and selul committed Oct 21, 2019
1 parent 3793de2 commit f76f2c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
5 changes: 3 additions & 2 deletions inc/tag_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ public function img_to_video( $image_url, $image_tag, &$content ) {
if ( $this->settings->get( 'img_to_video' ) === 'disabled' ) {
return false;
}

if ( $this->is_valid_mimetype_from_url( $image_url, [ 'gif' => false ] ) ) {
return false;
}

if ( ! $this->is_valid_mimetype_from_url( $image_url ) ) {
return false;
}
$link_mp4 = apply_filters(
'optml_content_url',
$image_url,
Expand Down
68 changes: 46 additions & 22 deletions tests/test-video.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,71 @@
*/
class Test_Video_Tag extends WP_UnitTestCase {

const IMG_TAGS_GIF = '<div id="wp-custom-header" class="wp-custom-header">
<img src="https://www.example.org/wp-content/uploads/2018/05/brands.gif"/>
</div> <div> <img src="https://www.example.org/wp-content/uploads/2018/05/image.gif"/> </div>';
const IMG_TAGS_NOT_GIF = '<div id="wp-custom-header" class="wp-custom-header">
<img src="https://www.example.org/wp-content/uploads/2018/05/brands.png"/>
<img src="https://www.example.org/wp-content/uploads/2018/05/brands.jpg"/>
<img src="https://www.example.org/wp-content/uploads/2018/05/brands.webp"/>
<img src="https://www.example.org/wp-content/uploads/2018/05/brands"/>
</div>';

public static $sample_post;
public function setUp() {

parent::setUp();
$settings = new Optml_Settings();
$settings->update( 'service_data', [
'cdn_key' => 'test123',
'cdn_secret' => '12345',
'whitelist' => [ 'encrypted-tbn0.gstatic.com' ],
'img_to_video' => 'enabled'
'whitelist' => [ 'example.com' ],

] );
$settings->update( 'lazyload', 'disabled' );
$settings->update( 'img_to_video', 'enabled' );


Optml_Url_Replacer::instance()->init();
Optml_Tag_Replacer::instance()->init();
Optml_Manager::instance()->init();

self::$sample_post = self::factory()->post->create( [
'post_title' => 'Test post',
'post_content' => self::IMG_TAGS
'post_content' => self::IMG_TAGS_GIF
]
);
self::$sample_attachement = self::factory()->attachment->create_upload_object( OPTML_PATH . 'assets/img/logo.png' );

}
public function test_replace_tag () {
$image_url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQK4kPpuaFciC2HriDrMGbBpnbOVMoIwCAa08l5q20ZUOWU067E";
$img_tag = '<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQK4kPpuaFciC2HriDrMGbBpnbOVMoIwCAa08l5q20ZUOWU067E" >';
$content = '<div class="before-footer">
<div class="codeinwp-container">
<p class="featuredon">Featured On</p>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQK4kPpuaFciC2HriDrMGbBpnbOVMoIwCAa08l5q20ZUOWU067E">
</div>
</div>';
$this -> assertTrue( Optml_Tag_Replacer::instance()->img_to_video($image_url, $img_tag, $content ));
$this->assertContains( 'i.optimole.com', $content );
$this->assertContains( '<video autoplay muted loop playsinline poster', $content );
$this->assertContains( 'type="video/mp4', $content );
$this->assertContains( 'type="video/webm', $content );
$this->assertContains( '/f:mp4', $content );
$this->assertContains( '/f:webm', $content );
$this->assertContains( 'https://encrypted-tbn0.gstatic.com', $content );
public function test_should_replace_tag () {

$replaced_content = Optml_Manager::instance()->process_images_from_content( self::IMG_TAGS_GIF );
$this->assertContains( 'i.optimole.com', $replaced_content );
$this->assertContains( '<video autoplay muted loop playsinline poster', $replaced_content );
$this->assertContains( 'type="video/mp4', $replaced_content );
$this->assertContains( 'type="video/webm', $replaced_content );
$this->assertContains( '/f:mp4', $replaced_content );
$this->assertContains( '/f:webm', $replaced_content );
$this->assertContains( 'https://www.example.org', $replaced_content );
}
public function test_should_not_replace_tag () {

$replaced_content = Optml_Manager::instance()->process_images_from_content( self::IMG_TAGS_NOT_GIF );
$this->assertNotContains( '<video autoplay muted loop playsinline poster', $replaced_content );
$this->assertNotContains( 'type="video/mp4', $replaced_content );
$this->assertNotContains( 'type="video/webm', $replaced_content );
$this->assertNotContains( '/f:mp4', $replaced_content );
$this->assertNotContains( '/f:webm', $replaced_content );

}
public function test_should_replace_tag_disabled () {

$replaced_content = Optml_Manager::instance()->process_images_from_content( self::IMG_TAGS_NOT_GIF );
$this->assertNotContains( '<video autoplay muted loop playsinline poster', $replaced_content );
$this->assertNotContains( 'type="video/mp4', $replaced_content );
$this->assertNotContains( 'type="video/webm', $replaced_content );
$this->assertNotContains( '/f:mp4', $replaced_content );
$this->assertNotContains( '/f:webm', $replaced_content );

}

}

0 comments on commit f76f2c9

Please sign in to comment.