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

API Key: Add utility method for checking it is set #404

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
41 changes: 41 additions & 0 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -2057,4 +2057,45 @@ public function parsely_is_user_logged_in() {
public function convert_jsonld_to_parsely_type( $type ) {
return in_array( $type, $this->supported_jsonld_post_types ) ? 'post' : 'index';
}

/**
* Determine if an API key is saved in the options.
*
* @since 2.6.0
*
* @return bool True is API key is set, false if it is missing.
*/
public function api_key_is_set() {
$options = $this->get_options();

return (
isset( $options['apikey'] ) &&
is_string( $options['apikey'] ) &&
'' !== $options['apikey']
GaryJones marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
* Determine if an API key is not saved in the options.
*
* @since 2.6.0
*
* @return bool True if API key is missing, false if it is set.
*/
public function api_key_is_missing() {
GaryJones marked this conversation as resolved.
Show resolved Hide resolved
return ! $this->api_key_is_set();
}

/**
* Get the API key if set.
*
* @since 2.6.0
*
* @return string API key if set, or empty string if not.
*/
public function get_api_key() {
$options = $this->get_options();

return $this->api_key_is_set() ? $options['apikey'] : '';
}
}
36 changes: 36 additions & 0 deletions tests/OtherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ function() {
* Test that test_display_admin_warning action returns a warning when there is no key
*
* @covers \Parsely::should_display_admin_warning
* @uses \Parsely::get_options
*/
public function test_display_admin_warning_without_key() {
$should_display_admin_warning = self::getMethod( 'should_display_admin_warning' );
Expand Down Expand Up @@ -622,4 +623,39 @@ public function test_display_admin_warning_with_key() {
$response = $should_display_admin_warning->invoke( self::$parsely );
self::assertFalse( $response );
}

/**
* Check that utility methods for checking if the API key is set work correctly.
*
* @since 2.6.0
*
* @covers \Parsely::api_key_is_set
* @covers \Parsely::api_key_is_missing
* @uses \Parsely::get_options
*/
public function test_checking_API_key_is_set_or_not() {
self::set_options( array( 'apikey' => '' ) );
self::assertFalse( self::$parsely->api_key_is_set() );
self::assertTrue( self::$parsely->api_key_is_missing() );

self::set_options( array( 'apikey' => 'somekey' ) );
self::assertTrue( self::$parsely->api_key_is_set() );
self::assertFalse( self::$parsely->api_key_is_missing() );
}

/**
* Test the utility methods for retrieving the API key.
*
* @since 2.6.0
*
* @covers \Parsely::get_api_key
* @uses \Parsely::api_key_is_set
* @uses \Parsely::get_options
*/
public function test_can_retrieve_API_key() {
self::set_options( array( 'apikey' => 'somekey' ) );
self::assertSame( 'somekey', self::$parsely->get_api_key() );
self::set_options( array( 'apikey' => '' ) );
self::assertSame( '', self::$parsely->get_api_key() );
}
}