Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast variable #993

Merged
merged 2 commits into from Jul 7, 2017
Merged

Cast variable #993

merged 2 commits into from Jul 7, 2017

Conversation

anhskohbo
Copy link
Contributor

The method just return cast $this->options as array but actually $this->options assign before still a string in some case.

Bugs will be show when I try empty cmb setting in wp_options table.

The method just return cast `$this->options` as array but actually `$this->options` assign before still a string in some case.

Bugs will be show when I try empty cmb setting in `wp_options` table.
@jtsternberg
Copy link
Member

You will need to write unit tests to prove/test your scenario.

@anhskohbo
Copy link
Contributor Author

Added unit tests

@jtsternberg
Copy link
Member

jtsternberg commented Jul 7, 2017

I just ran the tests you wrote with and without the changes you made in includes/CMB2_Options.php, and they all pass either way. To properly test your changes, at least 1 of your assertions should fail without your patch. Otherwise, there is no reason to make the change. Make sense?

@jtsternberg jtsternberg merged commit 0515a91 into CMB2:trunk Jul 7, 2017
@jtsternberg
Copy link
Member

Thank you for your PR. I have actually merged it to get your unit tests (and given you props), but I reverted the Cast variable changes, as they are actually causing incorrect results. I have written another unit test (and moved them all to tests/test-cmb-options.php) that asserts that an empty option value is still cast to an array (as it should be). When I apply your patch, those tests break, so I'm not sure what the intention of your patch, but it breaks the way CMB2_Options should work.

@anhskohbo
Copy link
Contributor Author

I miss forgot line in last commit:

public function setUp() {
    ...

    add_option( 'cmb_empty_option', '' );
}

See the unit tests failures:

peek 2017-07-08 00-02

The bug is because this CMB2_Option::get_options:

$this->options = get_option( $this->key, $default );

...

return (array) $this->options;

and on CMB2_Option::update() or CMB2_Option::remove() method:

public function remove( $field_id, $resave = false ) {

	$this->get_options();

	if ( isset( $this->options[ $field_id ] ) ) {
		unset( $this->options[ $field_id ] );
	}

If some reasons, I have a option with empty string (or nothing value). And call CMB2_Option::remove() will be given error Illegal string offset ....

Because you only cast when return options. Actually $this->options still is a string.

jtsternberg added a commit that referenced this pull request Jul 7, 2017
@jtsternberg
Copy link
Member

Ah, now I understand! ed2e58f should address that specific issue. Thank you for providing more info/clarification.

jtsternberg added a commit that referenced this pull request Jul 7, 2017
@anhskohbo anhskohbo deleted the patch-2 branch July 27, 2017 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants