Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions inc/Engine/Common/PerformanceHints/Admin/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,22 @@ private function delete_by_url( string $url ) {
$factory->queries()->delete_by_url( untrailingslashit( $url ) );
}
}

/**
* Clean performance hints when changing the logo.
*
* @param mixed $value The new value of the theme modification.
* @param mixed $old_value The old value of the theme modification.
*
* @return mixed
*/
public function clean_on_logo_change( $value, $old_value ) {
if ( $value === $old_value ) {
return $value;
}

$this->truncate_tables();

return $value;
}
}
13 changes: 13 additions & 0 deletions inc/Engine/Common/PerformanceHints/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static function get_subscribed_events(): array {
'rocket_dashboard_actions' => 'display_dashboard_button',
'admin_post_rocket_clean_performance_hints' => 'clean_performance_hints',
'admin_post_rocket_clean_performance_hints_url' => 'clean_url_performance_hints',
'pre_set_theme_mod_custom_logo' => [ 'clean_performance_hints_on_logo_change', 10, 2 ],
];
}

Expand Down Expand Up @@ -202,4 +203,16 @@ public function clean_url_performance_hints(): void {
public function clean_performance_hint_result(): void {
$this->notices->clean_performance_hint_result();
}

/**
* Clean performance hints when changing the logo.
*
* @param mixed $value The new value of the theme modification.
* @param mixed $old_value The old value of the theme modification.
*
* @return mixed
*/
public function clean_performance_hints_on_logo_change( $value, $old_value ) {
return $this->controller->clean_on_logo_change( $value, $old_value );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

return [
'testShouldNotTruncateWhenLogoNotChanged' => [
'config' => [
'value' => 123,
'old_value' => 123,
],
'expected' => [
'truncate' => false,
'return' => 123,
],
],
'testShouldTruncateWhenLogoChanged' => [
'config' => [
'value' => 456,
'old_value' => 123,
],
'expected' => [
'truncate' => true,
'return' => 456,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Tests\Unit\inc\Engine\Common\PerformanceHints\Admin\Controller;

use WP_Rocket\Engine\Common\PerformanceHints\Admin\Controller;
use WP_Rocket\Engine\Media\AboveTheFold\Factory as ATFFactory;
use WP_Rocket\Engine\Media\AboveTheFold\Database\Queries\AboveTheFold;
use WP_Rocket\Engine\Media\AboveTheFold\Database\Tables\AboveTheFold as ATFTable;
use WP_Rocket\Tests\Unit\TestCase;

/**
* Test class covering \WP_Rocket\Engine\Common\PerformanceHints\Admin\Controller::clean_on_logo_change
*
* @group PerformanceHints
*/
class CleanOnLogoChangeTest extends TestCase {
private $queries;
private $table;
private $factories;

protected function setUp(): void {
parent::setUp();

$this->queries = $this->createMock( AboveTheFold::class );
$this->table = $this->createMock( ATFTable::class );

$atf_factory = $this->createMock( ATFFactory::class );
$atf_factory->method( 'queries' )->willReturn( $this->queries );
$atf_factory->method( 'table' )->willReturn( $this->table );

$this->factories = [ $atf_factory ];
}

/**
* @dataProvider configTestData
*/
public function testShouldDoExpected( $config, $expected ) {
$controller = new Controller( $this->factories );

if ( $expected['truncate'] ) {
$this->queries->expects( $this->once() )
->method( 'get_not_completed_count' )
->willReturn( 0 );

$this->table->expects( $this->once() )
->method( 'truncate' );
} else {
$this->queries->expects( $this->never() )
->method( 'get_not_completed_count' );

$this->table->expects( $this->never() )
->method( 'truncate' );
}

$result = $controller->clean_on_logo_change( $config['value'], $config['old_value'] );

$this->assertSame( $expected['return'], $result );
}
}
Loading