2

I have created a wordpress plugin which converts shortcodes to content based on the entry saved on the dataase:

    global $wpdb;

    $post_id = get_the_ID();
    $post_content = get_the_content();

    $pattern = '/\[zam_tweets page=([0-9])\]/';
    preg_match($pattern, $post_content, $matches);

    if(!empty($matches)){

        $tweets_table = $wpdb->prefix . 'zam_tweets';
        $result = $wpdb->get_var("SELECT tweet FROM $tweets_table WHERE post_id = '$post_id'");
        $content = $result;
    }

    return $content;

My problem is how do I set the context to be that of a specific post so that I will get an actual post id when using the get_the_ID() method. Is this how I'm supposed to go with this or do I just have to specify them as arguments?

3

If you have phpUnit configured for testing a WP plugin, you can use a test case like this:

In your plugin-directory/tests/Some_Test_Case.php:

class Plugin_Test extends WP_UnitTestCase {
    /**
     * @dataProvider post_IDs_and_expected_results
     */
    public function test_something( $post_id, $expected_result ) {
        global $post;
        $post = get_post( $post_id );
        $plugin_content = call_plugin_function(); // Your function name here
        $this->assertEquals( $expected_result, $plugin_content, "Content OK for post $post_id" );
    }
    public function post_IDs_and_expected_results() {
        return array(
            array( 1, 'expected result for post_id = 1' ),
            array( 2, 'expected result for post_id = 2' )
        );
    }

}

Command line in your plugin's directory: phpunit ./tests/Some_Test_Case.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.