How to write tests for plugins that use Kirby methods?

I’m developing a plugin that has a class which uses methods of $page and $site. Basically, the plugin provides an API endpoint that updates pages based on input data. I want to test whether pages are updated correctly. How would I do that?

I’ve set up PHPUnit, which is also used by Kirby. I know how to write tests for a specific class. However, in my case, that class relies on Kirby methods and global functions. I need to load those somehow.

I’m aware of the concept of mocking, but I want to avoid it in this case because I want to test the behavior of the Kirby methods as well. What I want in the end is:

  1. The test gets some input data
  2. That data is processed by the plugin
  3. The page is updated with the processed data
  4. The updated page data is tested for expected values

What would be the best approach to do this?

I discovered that I can do:

include __DIR__ . '/../../../../kirby/bootstrap.php';

…in my test case and it would correctly load Kirby.

It appears possible that I can create the necessary content, and site folders in the plugin folder. Then, the test script includes a Kirby bootstrap that uses the plugin’s content and site instead of the project’s and then runs the test. Basically, you run the plugin’s tests using the surrounding Kirby installation of a project. This way, I can include my plugin as a submodule in my project and run tests directly.

If I run a test without changing the roots, Kirby correctly sets up the whole site and I can test the project content. However, I want to use the test content. When I change the content root to the plugin’s content folder, site()->children() is 0 even though there are actual pages.

Could that be because the site content is located inside the site folder: project/site/plugins/myplugin/tests/content instead of project/content?

Edit: I simply had messed up the folder path. :neutral_face:

@hdodov i created a demo for how to test a complete kirby installation with routes, pagemodel and its plugins. any feedback would be welcome: https://github.com/bnomei/kirby3-unittestkit

1 Like