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

How can I use WordPress functions in tests? #71

Closed
tangrufus opened this issue May 7, 2017 · 4 comments
Closed

How can I use WordPress functions in tests? #71

tangrufus opened this issue May 7, 2017 · 4 comments
Assignees

Comments

@tangrufus
Copy link
Contributor

Following this blog post http://www.theaveragedev.com/testing-the-rest-calculator/ , I added a permission check:

add_action( 'rest_api_init', function () {register_rest_route( 'calc', 'add/(?P<o1>\d+)/(?P<o2>\d+)',
    [
        'methods'  => 'GET',
        'callback' => [ new Calculator, 'process' ],
        'permission_callback' => function () {
            return current_user_can('manage_options');
        },
    ] );
} );

I am using this in a ajax script on the admin area. It works fine when I add a X-WP-Nonce header (wp_create_nonce('wp_rest')).

However, I find no way to inject this nonce in the tests:
$I->sendGET( '/calc/add/4/5' ); returns 403 errors

Question: How can I use WordPress functions (e.g: wp_create_nonce('wp_rest')) in the tests?

Thanks!

tangrufus added a commit to typisttech/sunny that referenced this issue May 7, 2017
@JDGrimes
Copy link

JDGrimes commented May 7, 2017

I'm not sure if that option is available in wp browser now or not. I created a custom Codeception module a while back to load WordPress when the browser tests were being run: https://github.com/WordPoints/dev-lib/blob/develop/wpcept/includes/Modules/WordPointsLoader.php

@lucatume lucatume self-assigned this May 8, 2017
@lucatume
Copy link
Owner

lucatume commented May 8, 2017

As JDGrimes rightly states that option was not explicitly supported, yet offered, from the WPLoader module.
With some "tricks" one could use the WPLoader module in functional tests.
I've pushed today an update on the master branch that should make it easier to do that and make it explicit; the feature has been requested enough times to make a point for it and while the work pushed today is just the start it's better than nothing.
To answer your question I've [pushed the tests/functional-hack branch to the rest-calculator project] to show how I'm using the new option in it.
See the functional suite configuration file and the test file that should resemble what you are trying to do.

Let me know if that helps; I'm releasing the new support in the next days.

@tangrufus
Copy link
Contributor Author

tangrufus commented May 11, 2017

Thank you both.

One issue: If two test suites both use WPLoader and have different tablePrefix config, the second suite would use first suite's tablePrefix.

This works

$ codecept run suiteA
$ codecept run suiteB

This results in errors

$ codecept run 

SuiteA passes. However, suitB's WPLoader loads with suiteA's tablePrefix

My affected plugin: https://github.com/TypistTech/sunny

@lucatume
Copy link
Owner

Thanks for reporting this; I'm working to improve WPLoader compatibility and use in this context in these days and this is a good thing to know.
I've opened a separate issue here.
I'm closing this issue and tracking further progress there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants