Skip to content

Commit 73d509a

Browse files
tijmenbruggemanrkoopmans
authored andcommitted
test: mock apply_filters and WP-CLI utils; simulate frontend context for Tiny_Picture…
1 parent eededfa commit 73d509a

4 files changed

Lines changed: 136 additions & 14 deletions

File tree

src/class-tiny-cli.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function optimize( $args, $assoc_args ) {
7676
$total = count( $attachments );
7777
WP_CLI::log( 'Optimizing ' . $total . ' images.' );
7878

79-
$progress = WP_CLI\Utils\make_progress_bar( 'Optimizing images', $total );
79+
$progress = Utils\make_progress_bar( 'Optimizing images', $total );
8080
$optimized = 0;
8181
foreach ( $attachments as $attachment_id ) {
8282
$attachment_id = intval( $attachment_id );

test/helpers/wordpress-cli.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
<?php
22

3-
/**
4-
* Mock WP_CLI to be used in php unit tests
5-
*/
6-
class WP_CLI
7-
{
8-
public static function log($message) {}
3+
namespace {
4+
/**
5+
* Mock WP_CLI to be used in php unit tests
6+
*/
7+
class WP_CLI
8+
{
9+
public static function log($message) {}
910

10-
public static function success($message) {}
11+
public static function success($message) {}
1112

12-
public static function warning($message) {}
13+
public static function warning($message) {}
1314

14-
public static function add_command($name, $command) {}
15+
public static function add_command($name, $command) {}
16+
}
1517
}
1618

17-
namespace WP_CLI\Utils {
19+
namespace Utils {
1820
class MockProgressBar
1921
{
2022
public function tick() {}
2123

2224
public function finish() {}
2325
}
24-
2526
function make_progress_bar($label, $count)
2627
{
2728
return new MockProgressBar();

test/helpers/wordpress.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ class WordPressStubs {
4848
private $metadata;
4949
private $calls;
5050
private $stubs;
51+
private $filters;
5152

5253
public function __construct( $vfs ) {
5354
$GLOBALS['wp'] = $this;
5455
$this->vfs = $vfs;
5556
$this->addMethod( 'add_action' );
5657
$this->addMethod( 'do_action' );
5758
$this->addMethod( 'add_filter' );
59+
$this->addMethod( 'apply_filters' );
5860
$this->addMethod( 'register_setting' );
5961
$this->addMethod( 'add_settings_section' );
6062
$this->addMethod( 'add_settings_field' );
@@ -76,6 +78,7 @@ public function __construct( $vfs ) {
7678
$this->addMethod( 'current_user_can' );
7779
$this->addMethod( 'wp_get_attachment_metadata' );
7880
$this->addMethod( 'is_admin' );
81+
$this->addMethod( 'is_customize_preview' );
7982
$this->addMethod( 'is_plugin_active' );
8083
$this->defaults();
8184
$this->create_filesystem();
@@ -91,6 +94,7 @@ public function defaults() {
9194
$this->admin_initFunctions = array();
9295
$this->options = new WordPressOptions();
9396
$this->metadata = array();
97+
$this->filters = array();
9498
$GLOBALS['_wp_additional_image_sizes'] = array();
9599
}
96100

@@ -103,6 +107,47 @@ public function call( $method, $args ) {
103107
$this->admin_initFunctions[] = $args[1];
104108
}
105109
}
110+
// Allow explicit stubs to override defaults/behaviors
111+
if ( isset( $this->stubs[ $method ] ) && $this->stubs[ $method ] ) {
112+
return call_user_func_array( $this->stubs[ $method ], $args );
113+
}
114+
if ( 'add_filter' === $method ) {
115+
$tag = isset( $args[0] ) ? $args[0] : '';
116+
$function_to_add = isset( $args[1] ) ? $args[1] : '';
117+
$priority = isset( $args[2] ) ? intval( $args[2] ) : 10;
118+
$accepted_args = isset( $args[3] ) ? intval( $args[3] ) : 1;
119+
if ( ! isset( $this->filters[ $tag ] ) ) {
120+
$this->filters[ $tag ] = array();
121+
}
122+
if ( ! isset( $this->filters[ $tag ][ $priority ] ) ) {
123+
$this->filters[ $tag ][ $priority ] = array();
124+
}
125+
$this->filters[ $tag ][ $priority ][] = array(
126+
'function' => $function_to_add,
127+
'accepted_args' => $accepted_args,
128+
);
129+
return true;
130+
}
131+
if ( 'apply_filters' === $method ) {
132+
$tag = isset( $args[0] ) ? $args[0] : '';
133+
// $value is the first value passed to filters
134+
$value = isset( $args[1] ) ? $args[1] : null;
135+
$call_args = array_slice( $args, 1 );
136+
if ( isset( $this->filters[ $tag ] ) ) {
137+
$priorities = array_keys( $this->filters[ $tag ] );
138+
sort( $priorities, SORT_NUMERIC );
139+
foreach ( $priorities as $priority ) {
140+
foreach ( $this->filters[ $tag ][ $priority ] as $callback ) {
141+
$accepted = max( 1, intval( $callback['accepted_args'] ) );
142+
$args_to_pass = array_slice( $call_args, 0, $accepted );
143+
$returned = call_user_func_array( $callback['function'], $args_to_pass );
144+
// Filters should return the (possibly modified) value as first argument.
145+
$call_args[0] = $returned;
146+
}
147+
}
148+
}
149+
return $call_args[0];
150+
}
106151
if ( 'translate' === $method ) {
107152
return $args[0];
108153
} elseif ( 'get_option' === $method ) {
@@ -123,8 +168,6 @@ public function call( $method, $args ) {
123168
return array( 'basedir' => $this->vfs->url() . '/' . self::UPLOAD_DIR, 'baseurl' => '/' . self::UPLOAD_DIR );
124169
} elseif ( 'is_admin' === $method ) {
125170
return true;
126-
} elseif ( $this->stubs[ $method ] ) {
127-
return call_user_func_array( $this->stubs[ $method ], $args );
128171
}
129172
}
130173

test/unit/TinyPluginTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,82 @@ public function test_when_files_is_compressed_will_only_cost_1_credit()
424424

425425
$this->assertEquals($cost, 0.01, 0.0001, 'a compressed image that will be converted will cost 1 credit at $0.009 (rounded $0.01) each when 500 compressions already used');
426426
}
427+
428+
public function test_conversion_enabled_but_filtered_off_not_load_picture()
429+
{
430+
// Mock settings with compression count
431+
$mock_settings = $this->createMock(Tiny_Settings::class);
432+
$mock_settings->method('get_conversion_enabled')->willReturn(true);
433+
434+
// set filter to disable picture, this can be done by devs in themes/plugins
435+
add_filter('tiny_replace_with_picture', function () {
436+
return false;
437+
}, 5);
438+
439+
// test is only valid when it is public
440+
$this->wp->stub('is_admin', function () {
441+
return false;
442+
});
443+
444+
$tiny_plugin = new Tiny_Plugin();
445+
446+
// set settings via reflection as settings is private
447+
$ref = new \ReflectionClass($tiny_plugin);
448+
$settings_prop = $ref->getProperty('settings');
449+
$settings_prop->setAccessible(true);
450+
$settings_prop->setValue($tiny_plugin, $mock_settings);
451+
452+
// Init plugin
453+
$tiny_plugin->init();
454+
455+
// verify tiny_replace_with_picture filter
456+
$filter_registered = false;
457+
foreach ($this->wp->getCalls('add_filter') as $call) {
458+
$priority = isset($call[2]) ? $call[2] : 10;
459+
if ($call[0] === 'tiny_replace_with_picture' && $priority === 5) {
460+
$filter_registered = true;
461+
break;
462+
}
463+
}
464+
$this->assertTrue(
465+
$filter_registered,
466+
'Expected tiny_replace_with_picture filter to be registered with priority 5.'
467+
);
468+
}
469+
470+
public function test_conversion_enabled_and_not_filtered()
471+
{
472+
// Mock settings with compression count
473+
$mock_settings = $this->createMock(Tiny_Settings::class);
474+
$mock_settings->method('get_conversion_enabled')->willReturn(true);
475+
476+
// hook is only done on public, so should not be admin
477+
$this->wp->stub('is_admin', function () {
478+
return false;
479+
});
480+
481+
$tiny_plugin = new Tiny_Plugin();
482+
483+
// set settings via reflection as settings is private
484+
$ref = new \ReflectionClass($tiny_plugin);
485+
$settings_prop = $ref->getProperty('settings');
486+
$settings_prop->setAccessible(true);
487+
$settings_prop->setValue($tiny_plugin, $mock_settings);
488+
489+
// Init plugin
490+
$tiny_plugin->init();
491+
492+
$template_redirect_registered = false;
493+
foreach ($this->wp->getCalls('add_action') as $call) {
494+
if ($call[0] === 'template_redirect') {
495+
$template_redirect_registered = true;
496+
break;
497+
}
498+
}
499+
500+
$this->assertTrue(
501+
$template_redirect_registered,
502+
'Expected Tiny_Picture to hook into template_redirect.'
503+
);
504+
}
427505
}

0 commit comments

Comments
 (0)