Skip to content

Commit

Permalink
Replace Settings::get_defaults() with private constant
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Dec 27, 2024
1 parent 7ec844b commit 603ef60
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 58 deletions.
43 changes: 16 additions & 27 deletions includes/avatar-privacy/core/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,18 @@ class Settings implements API {
const GRAVATAR_USE_DEFAULT = 'gravatar_use_default';

/**
* The defaults array.
* The defaults for the options array.
*
* @var array
* @since 2.8.0
*
* @var array<string,mixed>
* @phpstan-var SettingsFields
*/
private array $defaults;
private const DEFAULTS = [
self::UPLOAD_CUSTOM_DEFAULT_AVATAR => [],
self::GRAVATAR_USE_DEFAULT => false,
Options::INSTALLED_VERSION => '',
];

/**
* The fields definition array.
Expand Down Expand Up @@ -236,11 +241,10 @@ public function get_all_settings( $force = false ) {
*/
protected function load_settings() {
$_settings = $this->options->get( self::OPTION_NAME );
$_defaults = $this->get_defaults();
$modified = false;

if ( \is_array( $_settings ) ) {
foreach ( $_defaults as $name => $default_value ) {
foreach ( self::DEFAULTS as $name => $default_value ) {
if ( ! isset( $_settings[ $name ] ) ) {
$_settings[ $name ] = $default_value;
$modified = true;
Expand All @@ -253,7 +257,7 @@ protected function load_settings() {
* @phpstan-var SettingsFields $_settings
*/
} else {
$_settings = $_defaults;
$_settings = self::DEFAULTS;
$modified = true;
}

Expand Down Expand Up @@ -354,7 +358,7 @@ public function get_fields( $information_header = '' ) {
'erase_checkbox' => Custom_Default_Icon_Upload_Handler::CHECKBOX_ERASE,
'action' => Custom_Default_Icon_Upload_Handler::ACTION_UPLOAD,
'nonce' => Custom_Default_Icon_Upload_Handler::NONCE_UPLOAD,
'default' => 0,
'default' => self::DEFAULTS[ self::UPLOAD_CUSTOM_DEFAULT_AVATAR ],
'attributes' => [ 'accept' => 'image/*' ],
'settings_args' => [ 'class' => 'avatar-settings' ],
],
Expand All @@ -372,7 +376,7 @@ public function get_fields( $information_header = '' ) {
/* translators: 1: checkbox HTML */
'label' => \__( '%1$s Display Gravatar images by default.', 'avatar-privacy' ),
'help_text' => \__( 'Checking will ensure that gravatars are displayed when there is no explicit setting for the user or mail address (e.g. for comments made before installing Avatar Privacy). Please only enable this setting after careful consideration of the privacy implications.', 'avatar-privacy' ),
'default' => 0,
'default' => self::DEFAULTS[ self::GRAVATAR_USE_DEFAULT ],
'grouped_with' => self::INFORMATION_HEADER,
'outer_attributes' => [ 'class' => 'avatar-settings-enabled' ],
], // @codeCoverageIgnoreEnd
Expand All @@ -392,31 +396,16 @@ public function get_fields( $information_header = '' ) {
/**
* Retrieves the default settings.
*
* @deprecated 2.8.0
*
* @return array
*
* @phpstan-return SettingsFields
*/
public function get_defaults() {
if ( ! isset( $this->defaults ) ) {
$_defaults = [];
foreach ( $this->get_fields() as $index => $field ) {
if ( isset( $field['default'] ) ) {
$_defaults[ $index ] = $field['default'];
}
}

// Allow detection of new installations.
$_defaults[ Options::INSTALLED_VERSION ] = '';

/**
* PHPStan type.
*
* @phpstan-var SettingsFields $_defaults
*/
$this->defaults = $_defaults;
}
\_deprecated_function( __METHOD__, '2.8.0' );

return $this->defaults;
return self::DEFAULTS;
}

/**
Expand Down
53 changes: 22 additions & 31 deletions tests/avatar-privacy/core/class-settings-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,25 +232,16 @@ public function test_get_all_settings_version_mismatch() {
*/
public function test_load_settings() {
$setting1 = 'foo';
$setting2 = 'baz';
$settings = [
$setting1 => 'barfoo',
Options::INSTALLED_VERSION => '1.2.3',
];
$defaults = [
$setting1 => 'bar',
$setting2 => 'foobar',
];

$this->options->shouldReceive( 'get' )
->once()
->with( Settings::OPTION_NAME )
->andReturn( $settings );

$this->sut->shouldReceive( 'get_defaults' )
->once()
->andReturn( $defaults );

$this->options->shouldReceive( 'set' )
->once()
->with( Settings::OPTION_NAME, m::type( 'array' ) );
Expand All @@ -260,9 +251,12 @@ public function test_load_settings() {
$this->assert_is_array( $result );
$this->assertArrayHasKey( $setting1, $result );
$this->assertSame( 'barfoo', $result[ $setting1 ] );
$this->assertArrayHasKey( $setting2, $result );
$this->assertSame( 'foobar', $result[ $setting2 ] );
$this->assertArrayHasKey( Settings::UPLOAD_CUSTOM_DEFAULT_AVATAR, $result );
$this->assertSame( [], $result[ Settings::UPLOAD_CUSTOM_DEFAULT_AVATAR ] );
$this->assertArrayHasKey( Settings::GRAVATAR_USE_DEFAULT, $result );
$this->assertSame( false, $result[ Settings::GRAVATAR_USE_DEFAULT ] );
$this->assertArrayHasKey( Options::INSTALLED_VERSION, $result );
$this->assertSame( '1.2.3', $result[ Options::INSTALLED_VERSION ] );
}

/**
Expand All @@ -271,25 +265,24 @@ public function test_load_settings() {
* @covers ::load_settings
*/
public function test_load_settings_invalid_result() {
$defaults = [
'foo' => 'bar',
'baz' => 'foobar',
];

$this->options->shouldReceive( 'get' )
->once()
->with( Settings::OPTION_NAME )
->andReturn( false );

$this->sut->shouldReceive( 'get_defaults' )
->once()
->andReturn( $defaults );

$this->options->shouldReceive( 'set' )
->once()
->with( Settings::OPTION_NAME, m::type( 'array' ) );

$this->assertSame( $defaults, $this->sut->load_settings() );
$result = $this->sut->load_settings();

$this->assert_is_array( $result );
$this->assertArrayHasKey( Settings::UPLOAD_CUSTOM_DEFAULT_AVATAR, $result );
$this->assertSame( [], $result[ Settings::UPLOAD_CUSTOM_DEFAULT_AVATAR ] );
$this->assertArrayHasKey( Settings::GRAVATAR_USE_DEFAULT, $result );
$this->assertSame( false, $result[ Settings::GRAVATAR_USE_DEFAULT ] );
$this->assertArrayHasKey( Options::INSTALLED_VERSION, $result );
$this->assertSame( '', $result[ Options::INSTALLED_VERSION ] );
}

/**
Expand All @@ -299,23 +292,19 @@ public function test_load_settings_invalid_result() {
*/
public function test_load_settings_everything_in_order() {
$settings = [
'foo' => 'barfoo',
'baz' => 'foo',
];
$defaults = [
'foo' => 'bar',
'baz' => 'foobar',
Settings::UPLOAD_CUSTOM_DEFAULT_AVATAR => [
'file' => '/some/avatar-image.png',
'type' => 'image/png',
],
Settings::GRAVATAR_USE_DEFAULT => true,
Options::INSTALLED_VERSION => '9.9.9',
];

$this->options->shouldReceive( 'get' )
->once()
->with( Settings::OPTION_NAME )
->andReturn( $settings );

$this->sut->shouldReceive( 'get_defaults' )
->once()
->andReturn( $defaults );

$this->options->shouldReceive( 'set' )->never();

$this->assertSame( $settings, $this->sut->load_settings() );
Expand Down Expand Up @@ -473,6 +462,8 @@ public function test_get_fields() {
* @uses ::get_fields
*/
public function test_get_defaults() {
Functions\expect( '_deprecated_function' )->once()->with( m::type( 'string' ), '2.8.0' );

$result = $this->sut->get_defaults();

$this->assert_is_array( $result );
Expand Down

0 comments on commit 603ef60

Please sign in to comment.