Skip to content

Commit

Permalink
Fix deprecation notice in Fieldmanager_TextArea when default value is…
Browse files Browse the repository at this point in the history
…n't set
  • Loading branch information
anubisthejackle committed Feb 29, 2024
1 parent 34d42e3 commit 51d2e8e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
7 changes: 7 additions & 0 deletions php/class-fieldmanager-textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Fieldmanager_TextArea extends Fieldmanager_Field {
*/
public $field_class = 'text';

/**
* The default value for the field, if unset.
*
* @var mixed Default value
*/
public $default_value = '';

/**
* Construct default attributes; 50x10 textarea.
*
Expand Down
1 change: 1 addition & 0 deletions tests/php/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ function _fm_phpunit_is_wp_at_least( $min_version ) {
// Load includes.
require_once __DIR__ . '/includes/class-fieldmanager-assets-unit-test-case.php';
require_once __DIR__ . '/includes/class-fieldmanager-options-mock.php';
require_once __DIR__ . '/includes/class-fieldmanager-deprecation-exception.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
/**
* An exception class that triggers on expected deprecations
*/

class Fieldmanager_Deprecation_Exception extends Exception {}
81 changes: 81 additions & 0 deletions tests/php/test-fieldmanager-textarea-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* Tests the Fieldmanager TextArea Field
*
* @group field
* @group textarea
*/
class Test_Fieldmanager_TextArea_Field extends WP_UnitTestCase {

/**
* The post ID of the test post.
*
* @var int
*/
private int $post_id = 0;

/**
* The post object of the test post.
*
* @var WP_Post
*/
private WP_Post $post;

public function set_up() {
parent::set_up();
Fieldmanager_Field::$debug = true;

$this->post_id = $this->factory->post->create(
array(
'post_title' => rand_str(),
'post_date' => '2024-02-29 00:00:00',
)
);
$this->post = get_post( $this->post_id );
}

/**
* Test to confirm that not defining a default value for a field
* does not cause that field to throw a deprecation warning when
* a textarea field is used.
*
* @see https://github.com/alleyinteractive/wordpress-fieldmanager/issues/863
*/
public function test_default_value_does_not_throw_deprecation() {
set_error_handler(
/**
* Convert deprecations to exceptions. This was removed from PHPUnit in
* PHPUnit 10, so doing so manually here for future compatibility.
*
* @see https://github.com/sebastianbergmann/phpunit/issues/5062
*/
function ( $errno, $errstr ) {
throw new Fieldmanager_Deprecation_Exception( $errstr, $errno );
},
E_DEPRECATED | E_USER_DEPRECATED
);

$this->expectNotToPerformAssertions();

try {
ob_start();

$fm = new Fieldmanager_Textarea(
[
'name' => 'example-textarea',
'description' => 'Description Text',
]
);
$fm->add_meta_box( 'Test TextArea', 'post' )
->render_meta_box( $this->post, array() );

ob_get_clean();
} catch( Fieldmanager_Deprecation_Exception $e ) {
$this->fail( $e->getMessage() );
}

// Restore default error handler.
restore_error_handler();
}
}

0 comments on commit 51d2e8e

Please sign in to comment.