Skip to content

Commit

Permalink
✨ Add test and fixed lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Dec 20, 2024
1 parent 8fd3220 commit 34e0436
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
67 changes: 67 additions & 0 deletions Tests/Integration/inc/classes/Media/upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);

namespace Imagify\Tests\Integration\inc\classes\Media;

use Imagify\Tests\Integration\TestCase;
use Brain\Monkey\Functions;
use ReflectionClass;

/**
* @covers \Imagify\Media\Upload\Upload::add_imagify_filter_to_attachments_dropdown
* @group Upload
*/
class Upload extends TestCase {
protected $view_instance;

public function tear_down() {
remove_all_filters( 'imagify_display_library_stats' );
remove_all_filters( 'imagify_count_optimized_attachments' );
remove_all_filters( 'imagify_count_unoptimized_attachments' );
remove_all_filters( 'imagify_count_error_attachments' );

if ($this->view_instance) {

}
parent::tear_down();
}

public function set_up() {

parent::set_up();
}

public function testShouldReturnExpected() {
$upload = new \Imagify\Media\Upload\Upload();

add_filter( 'imagify_display_library_stats', '__return_true' );

$this->mock_imagify_count_functions();
ob_start();
$upload->add_imagify_filter_to_attachments_dropdown();
$output = ob_get_clean();

$this->assertStringContainsString( 'Filter by status', $output );
$this->assertStringContainsString( '<select id="filter-by-optimization-status"', $output );
$this->assertStringContainsString( 'selected="selected"', $output );
$this->assertStringContainsString( 'Errors (2)', $output );
$this->assertSame( 1, 1 );
}

/**
* Mock the return values of imagify count functions.
*
* @return void
*/
public function mock_imagify_count_functions() {
add_filter( 'imagify_count_optimized_attachments', function() {
return 10;
} );
add_filter( 'imagify_count_unoptimized_attachments', function() {
return 5;
} );
add_filter( 'imagify_count_error_attachments', function() {
return 2;
} );
}
}
5 changes: 4 additions & 1 deletion classes/Media/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class Subscriber implements SubscriberInterface {

/**
* @var Upload
* @var Upload Upload Instance.
*/
private $upload;
/**
Expand Down Expand Up @@ -41,6 +41,9 @@ public static function get_subscribed_events(): array {
* @return void
*/
public function imagify_attachments_filter_dropdown() {
if ( ! \Imagify_Views::get_instance()->is_wp_library_page() ) {
return;
}
$this->upload->add_imagify_filter_to_attachments_dropdown();
}
}
11 changes: 3 additions & 8 deletions classes/Media/Upload/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
declare(strict_types=1);

namespace Imagify\Media\Upload;

/**
* Upload Media Class.
*
*/
*/
class Upload {

/**
* Adds a dropdown that allows filtering on the attachments Imagify status.
*
* @return void
*/
public function add_imagify_filter_to_attachments_dropdown() {
if ( ! \Imagify_Views::get_instance()->is_wp_library_page() ) {
return;
}

/**
* Tell if imagify stats query should run.
*
Expand All @@ -39,7 +34,7 @@ public function add_imagify_filter_to_attachments_dropdown() {
echo '<label class="screen-reader-text" for="filter-by-optimization-status">' . __( 'Filter by status', 'imagify' ) . '</label>';
echo '<select id="filter-by-optimization-status" name="imagify-status">';
echo '<option value="0" selected="selected">' . __( 'All Media Files', 'imagify' ) . '</option>';

$filter_value = '';
foreach ( $options as $value => $label ) {
if ( isset( ${$value} ) ) {
$filter_value = ' (' . ${$value} . ')';
Expand Down

0 comments on commit 34e0436

Please sign in to comment.