|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPress unit test plugin. |
| 4 | + * |
| 5 | + * @package Optimole-WP |
| 6 | + * @subpackage Tests |
| 7 | + * @copyright Copyright (c) 2017, ThemeIsle |
| 8 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Class Test_Generic. |
| 13 | + */ |
| 14 | +class Test_Lazyload_Class_Exclusion extends WP_UnitTestCase |
| 15 | +{ |
| 16 | + |
| 17 | + public static $sample_attachement; |
| 18 | + |
| 19 | + public function setUp() |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + $settings = new Optml_Settings(); |
| 23 | + $settings->update('service_data', [ |
| 24 | + 'cdn_key' => 'test123', |
| 25 | + 'cdn_secret' => '12345', |
| 26 | + 'whitelist' => ['example.com'], |
| 27 | + ]); |
| 28 | + |
| 29 | + $settings->update('lazyload', 'enabled'); |
| 30 | + $settings->update('filters', array( |
| 31 | + Optml_Settings::FILTER_TYPE_LAZYLOAD => array ( |
| 32 | + Optml_Settings::FILTER_CLASS => array ( |
| 33 | + 'test' => true, |
| 34 | + 'whatever' => true, |
| 35 | + 'testing' => true, |
| 36 | + ) |
| 37 | + ))); |
| 38 | + Optml_Url_Replacer::instance()->init(); |
| 39 | + Optml_Tag_Replacer::instance()->init(); |
| 40 | + Optml_Lazyload_Replacer::instance()->init(); |
| 41 | + Optml_Manager::instance()->init(); |
| 42 | + |
| 43 | + self::$sample_attachement = self::factory()->attachment->create_upload_object(OPTML_PATH . 'assets/img/logo.png'); |
| 44 | + } |
| 45 | + |
| 46 | + public function test_class_exclusions() |
| 47 | + { |
| 48 | + $content = '<div> |
| 49 | + <img class="something another whatever" src="http://example.org/wp-content/uploads/2019/09/Screenshot.png" alt=""/>; |
| 50 | + <img class="test" src="http://example.org/wp-content/uploads/2019/09/img.jpg" alt=""/>; |
| 51 | + <img class="testing class" src="http://example.org/img.png" alt=""/>; |
| 52 | + </div>'; |
| 53 | + $replaced_content = Optml_Manager::instance()->process_images_from_content($content); |
| 54 | + $this->assertEquals( 3, substr_count( $replaced_content, 'i.optimole.com' ) ); |
| 55 | + $this->assertNotContains('data-opt-src', $replaced_content); |
| 56 | + } |
| 57 | + public function test_class_exclusions_does_not_affect_regular_replacement() |
| 58 | + { |
| 59 | + $content = '<div> |
| 60 | + <img class="something another code" src="http://example.org/wp-content/uploads/2019/09/Screenshot.png" alt=""/>; |
| 61 | + <img class="whatever_code_test" src="http://example.org/img.jpg" alt=""/>; |
| 62 | + </div>'; |
| 63 | + $replaced_content = Optml_Manager::instance()->process_images_from_content($content); |
| 64 | + |
| 65 | + $this->assertEquals( 4, substr_count( $replaced_content, 'i.optimole.com' ) ); |
| 66 | + $this->assertEquals( 1, substr_count( $replaced_content, 'data-opt-src' ) ); |
| 67 | + $this->assertEquals( 1, substr_count( $replaced_content, '<noscript>' ) ); |
| 68 | + } |
| 69 | +} |
0 commit comments