Can I simulate a user being logged in from a WP_UnitTestCase?
Call wp_set_current_user($id) to make a user ID the currently logged in user.
Call wp_set_current_user($id) to make a user ID the currently logged in user.
You’ve just discovered an important feature of the core test suite: it forces any tables created during the test to be temporary tables. If you look in the WP_UnitTestCase::setUp() method you’ll see that it calls a method called start_transaction(). That start_transaction() method starts a MySQL database transaction: function start_transaction() { global $wpdb; $wpdb->query( ‘SET autocommit … Read more
Your best bet to set up a reusable test environment on a Windows machine is to use Vagrant with a setup such as this. Vagrant will allow you to build portable, reusable virtual machines with a very simple interface which you can use across different operating systems and team members. To set it up, you … Read more
If all that you want to test is that the translation files exist, then this is probably the simplest approach, since you’ll probably already be using PHPUnit to run unit/integration tests on the plugin code. However, just checking whether the files exist doesn’t tell you much. It doesn’t tell you whether those languages are actually … Read more
Just few notes: Looks like you’re doing functional/integration tests rather than unit tests in isolation. Here you’re creating a user, but not setting it as the current one: $user = $this->factory->user->create(); wp_set_current_user( 1 ); You probably want: $user_id = $this->factory->user->create(); wp_set_current_user( $user_id ); Note that cookies aren’t set here, because of: tests_add_filter( ‘send_auth_cookies’, ‘__return_false’ ); … Read more
TL;DR It can be anywhere on the filesystem. (But see the update below for a tool that will help you do this even more realistically.) Explanation The plugin can be anywhere on the filesystem, depending on whether your plugin has an install function and how you want to run that. If your plugin creates database … Read more
Is object injection really necessary? To be fully testable in isolation, code should avoid to instantiate classes directly, and inject any object that is needed inside objects. However, there are at least two exceptions to this rule: The class is a language class, e.g. ArrayObject The class is a proper “value object”. No need to … Read more
When it comes to unit testing themes, it’s a small jungle. I read on Make WordPress Core that things are changing (http://make.wordpress.org/core/2013/08/06/a-new-frontier-for-core-development/). I searched for blogs linking to that specific post in hope to find some useful. Found this: http://ben.lobaugh.net/blog/84669/how-to-add-unit-testing-and-continuous-integration-to-your-wordpress-plugin that looks promising. Note that it focuses on plugin testing, but useful for theme unit … Read more
I think that what you are looking for would actually technically be called functional testing, integration testing, or acceptance testing. It sounds like you want to test the behavior of the front-end of your website (or the front-end behavior of a plugin or theme), not each unit of the code itself. You might use a … Read more
Post can be accessed using post id by passing it as page_id query param to index.php. e.g. http://wordpress.loc/index.php?page_id=16 Or simply http://wordpress.loc/?page_id=16