0
votes

I am working on a SilverStripe project. I am trying to write functional tests for my application following this documentation, https://docs.silverstripe.org/en/4/developer_guides/testing/functional_testing/. I am testing a POST request. But it is not working. You can see my code below.

I have a controller class called CustomFormPageController with the following code.

class CustomFormPageController extends PageController
{
    private static $allowed_actions = [
        'testPostRequest',
    ];

    private static $url_handlers = [
        'testPostRequest' => 'testPostRequest',
    ];

    public function testPostRequest(HTTPRequest $request)
    {
        if (! $request->isPOST()) {
            return "Bad request";
        }

        return "Request successfully processed";
    }
}

I also have a page class for that controller called, CustomFormPage. Following is the implementation of the class.

class CustomFormPage extends Page
{

}

What I am trying to test is that I am trying to test testPostRequest method returns the correct value.

Following is my test class

class CustomFormPageTest extends FunctionalTest
{
    protected static $fixture_file = 'fixtures.yml';

    public function testTestingPost()
    {
        $formPage = $this->objFromFixture(CustomFormPage::class, 'form_page');

        $response = $this->post($formPage->URLSegment . '/testPostRequest', [
            'name' => 'testing'
        ]);

        var_dump($response);
    }
}

Following is my fixtures.yml file.

SilverStripe\CMS\Model\SiteTree:
    sitetree_page_one:
        ID: 1
        ClassName: CustomFormPage
        Title: Page Title 1
        URLSegment: custom-form-page
CustomFormPage:
    form_page:
        ID: 1
        Title: Page Title 1
        URLSegment: custom-form-page

When I run the test, it is always returning 404 not found status even though the page is created in the database. What is missing in my code and how can I fix it?

1

1 Answers

0
votes

I just found the solution. We need to publish the page in the test as follows.

$formPage->publish("Stage", "Live");