function testEntityUriWithMissingDatasetUri()
 {
     $post_id = wl_create_post('A body', 'post-1', uniqid('post', true), 'draft', 'post');
     $dataset_uri = wl_configuration_get_redlink_dataset_uri();
     $this->assertNotEmpty($dataset_uri);
     // Remove the dataset uri
     wl_configuration_set_redlink_dataset_uri('');
     $entity_uri = wl_get_entity_uri($post_id);
     // Check the wl_get_entity_uri properly returns null
     $this->assertNull($entity_uri);
     // Check the are not custom meta set for the current post
     $this->assertEmpty(get_post_meta($post_id, WL_ENTITY_URL_META_NAME));
     // Set the dataset uri again
     wl_configuration_set_redlink_dataset_uri($dataset_uri);
 }
 function test_wl_configuration_redlink_dataset_uri()
 {
     $value = uniqid();
     wl_configuration_set_redlink_dataset_uri($value);
     $this->assertEquals($value, wl_configuration_get_redlink_dataset_uri());
 }
Beispiel #3
0
/**
 * Configure WordPress with the test settings (may vary according to the local PHP and WordPress versions).
 */
function wl_configure_wordpress_test()
{
    add_filter('wl_write_log_handler', 'wl_test_get_write_log_handler');
    do_action('activate_wordlift/wordlift.php');
    // If the WordLift key is set, then we'll configure it, otherwise we configure Redlink.
    if (false !== getenv('WORDLIFT_KEY')) {
        // When setting the WordLift Key, the Redlink dataset URI is provisioned by WordLift Server.
        wl_configuration_set_key(getenv('WORDLIFT_KEY'));
        if ('' === wl_configuration_get_redlink_dataset_uri()) {
            die('The Redlink dataset URI is not set (maybe the WordLift key is not valid?)');
        }
    } else {
        // TODO: remove this part.
        // or use Redlink.
        // Set the dataset name to the specified dataset or define it based on the current environment.
        $dataset_name = false !== getenv('REDLINK_DATASET_NAME') ? getenv('REDLINK_DATASET_NAME') : str_replace('.', '-', sprintf('%s-php-%s.%s-wp-%s-ms-%s', 'wordlift-tests', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, getenv('WP_VERSION'), getenv('WP_MULTISITE')));
        $app_name = false !== getenv('REDLINK_APP_NAME') ? getenv('REDLINK_APP_NAME') : 'wordlift';
        // Check that the API_URL env is set.
        if (false === getenv('API_URL')) {
            die('The API_URL environment variable is not set.');
        }
        wl_configuration_set_redlink_key(getenv('REDLINK_APP_KEY'));
        wl_configuration_set_redlink_user_id(getenv('REDLINK_USER_ID'));
        wl_configuration_set_api_url(getenv('API_URL'));
        wl_configuration_set_redlink_dataset_name($dataset_name);
        wl_configuration_set_redlink_application_name($app_name);
        wl_configuration_set_redlink_dataset_uri('http://data.redlink.io/' . getenv('REDLINK_USER_ID') . '/' . $dataset_name);
    }
}
/**
 * Intercept the change of the WordLift key in order to set the dataset URI.
 *
 * @since 3.0.0
 *
 * @param array $old_value The old settings.
 * @param array $new_value The new settings.
 */
function wl_configuration_update_key($old_value, $new_value)
{
    // wl_write_log( "Going to request set redlink dataset uri if needed" );
    // Check the old key value and the new one. We're going to ask for the dataset URI only if the key has changed.
    $old_key = isset($old_value['key']) ? $old_value['key'] : '';
    $new_key = isset($new_value['key']) ? $new_value['key'] : '';
    // wl_write_log( "[ old value :: $old_key ][ new value :: $new_key ]" );
    // If the key hasn't changed, don't do anything.
    // WARN The 'update_option' hook is fired only if the new and old value are not equal
    if ($old_key === $new_key) {
        return;
    }
    // If the key is empty, empty the dataset URI.
    if ('' === $new_key) {
        wl_configuration_set_redlink_dataset_uri('');
    }
    // Request the dataset URI.
    $response = wp_remote_get(wl_configuration_get_accounts_by_key_dataset_uri($new_key), unserialize(WL_REDLINK_API_HTTP_OPTIONS));
    // If the response is valid, then set the value.
    if (!is_wp_error($response) && 200 === (int) $response['response']['code']) {
        // wl_write_log( "[ Retrieved dataset :: " . $response['body'] . " ]" );
        wl_configuration_set_redlink_dataset_uri($response['body']);
    } else {
        wl_write_log("Error on dataset uri remote retrieving [ " . var_export($response, true) . " ]");
    }
}