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

HTTP Testing of Plugin not possible #1124

Open
der-On opened this issue May 9, 2024 · 4 comments
Open

HTTP Testing of Plugin not possible #1124

der-On opened this issue May 9, 2024 · 4 comments

Comments

@der-On
Copy link
Contributor

der-On commented May 9, 2024

Winter CMS Build

dev-develop

PHP Version

8.1

Database engine

SQLite

Plugins installed

No response

Issue description

I have a custom plugin with a routes.php file.
I've written a test that uses laravels HTTP Testing to test an endpoint from the routes.php file.
I use php artisan winter:test --plugin=My.Plugin to run it's tests.
The test request to any valid endpoint in that routes.php file returns the 404 page of the active theme.

The cause is that the plugin will not get registered under a PluginTestCase unless it is elevated: https://github.com/wintercms/winter/blob/develop/modules/system/classes/PluginManager.php#L338

The plugin and it's routes must get registered at this stage, to prevent the CMS routes beeing registered before the plugins routes.
So including the plugins routes manually in the test case wont work as it is too late in the process.

Steps to replicate

Create a plugin with routes.php
Create a plugin test case and use HTTP testing from Laravel (https://laravel.com/docs/9.x/http-tests#introduction) to run a test request against a route from routes.php of the plugin.

Use php artisan winter:test --plugin=Your.Plugin to run the test.

Workaround

Make the Plugin elevated: https://wintercms.com/docs/v1.2/docs/plugin/registration#elevated-permissions

@bennothommo
Copy link
Member

@der-On this would be related to 71fcf8e, which itself is related to an issue reporting in October years ago.

Essentially, we had to disable the plugin register() method and routes.php and init.php files within plugins when running CLI commands (such as winter:test) if the database is either unavailable or hasn't yet been configured/migrated, as multiple people would run commands for environments such as Vapor without the database being made available yet and would encounter exceptions if a plugin used a database during registration.

Personally, I'm not a fan of the fix - I think plugin developers should be more responsible and not be calling the database during registration, but I digress, since it was reported commonly enough, we had to err on the side of caution.

One thing you could do - I don't know how difficult it would be with your setup - but you could run the migrations before the tests run. If the database is available and migrated before the tests are run, you would side-step this fix.

Alternatively, we could make it so that this fix is applied conditionally (enabled by default) and could be disabled by an option to the winter:test command, but I'm not a massive fan of that either.

@der-On
Copy link
Contributor Author

der-On commented May 13, 2024

@bennothommo I'm already running the tests after the database is migrated. This is actually part of the PluginTestCase setUp method by default.

@bennothommo
Copy link
Member

@der-On have the migrations been run on the test database before you run the tests?

The reason I ask is because the function that disables plugin initialization (here in the code) specifically looks for whether the migration table is set up, as winter:test itself is not a privileged CLI command. In your unit tests, you would likely be using a different database connection.

@der-On
Copy link
Contributor Author

der-On commented May 15, 2024

@bennothommo As far as I can tell, It uses the test database. I however did create a config/testing/database.php.

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

2 participants