Integration testing backend widgets #186
-
Is there any sort of documentation which explains how to perform integration testing with phpunit on backend widgets? Here's my current test code, and for some reason this test works locally but when it runs in github actions it fails with an HTTP error code of 403. public function test_it_can_upload_to_an_uploadable_widget()
{
$this->app->singleton('backend.auth', function ($app) {
$app['auth.loaded'] = true;
return AuthManager::instance();
});
$user = new BackendUser();
$this->actingAs($user);
$controller = new Controller();
$widget = new myCustomWidget($controller, 'uploader');
$widget->bindToController();
$this->app['router']->post('/upload', function () use ($controller) {
return $controller->run();
});
$fileUpload = new UploadedFile(
$this->getTestFilesDirectory('test.jpg'),
'alternativename.jpg',
'image/jpeg',
filesize($this->getTestFilesDirectory('test.jpg'))
);
$result = $this->post('upload', ['file_data' => $fileUpload], [
'X-Requested-With' => 'XMLHttpRequest',
'X-WINTER-REQUEST-HANDLER' => $widget->getEventHandler('onUpload'),
]);
$result
->assertStatus(200)
->assertJson([
'result' => 'success',
]);
} Any help is appreciated, Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
403 is an authentication error, so perhaps you're having some problems with the actingAs() method in github actions? |
Beta Was this translation helpful? Give feedback.
-
@ericp-mrel Check out the Dusk plugin and the tests within - it might give you some pointers on how to correctly authenticate and test front-end functionality. |
Beta Was this translation helpful? Give feedback.
-
Ahh, I figured out my problem. I was copying the cms.php file from Glad I've finally figured out my issue and that explains why it was happening in Github Actions and not my local copy. |
Beta Was this translation helpful? Give feedback.
Ahh, I figured out my problem. I was copying the cms.php file from
config/cms.php
intoconfig/testing/cms.php
because I saw the Winter CMStests.yml
file doing that, but I didn't think anything of it until I realized by doing that I was effectively enabling CSRF protection which was the reason I was seeing the 403 error. So, not overriding the supplied cms.php in the testing config folder fixed my issue so that CSRF protection is not being enable during testing.Glad I've finally figured out my issue and that explains why it was happening in Github Actions and not my local copy.