2

I am working on a SilverStripe project. I am writing functional tests for my application. I am writing a function test for a path within the CMS. In my case the path is /admin/settings/SecondaryMenuPages. It is the path within the CMS and so the user needs to be logged in.

This is my test class.

class MegaMenuTest extends FunctionalTest
{
    protected static $fixture_file = [
        'app/tests/Cms/cms_admin_user_fixture.yml',
        'app/tests/Cms/secondary_menu_fixture.yml',
    ];

    function testCmsUserCanUnlinkSecondaryMenuItem()
    {
        $member = $this->objFromFixture(Member::class, 'admin_user');
        $adminGroup = $this->objFromFixture(Group::class, 'admin_group');
        $this->assignMemberToGroup($member, $adminGroup);
        $this->assignPageToSecondaryMenu();
        $this->logInAs($member);
        $this->post("/admin/settings/SecondaryMenuPages", [
            'ID' => 1,
        ], null, null, json_encode([ 'ID' => 1 ]));
    }

    private function assignMemberToGroup($member, $adminGroup)
    {
        $sqlInsert = SQLInsert::create('Group_Members');
        $sqlInsert->addRows([
            [ '"GroupID"' => $adminGroup->ID, '"MemberID"' => $member->ID ],
        ]);
        $sqlInsert->execute();
    }

    private function assignPageToSecondaryMenu()
    {
        $page = $this->objFromFixture('Page', 'page_one');
        $siteConfig = $this->objFromFixture(SiteConfig::class, 'siteconfig_one');
        $sqlInsert = SQLInsert::create('SiteConfig_SecondaryMenuPages');
        $sqlInsert->addRows([
            [ '"PageID"' => $page->ID, '"SiteConfigID"' => $siteConfig->ID ],
        ]);
        $sqlInsert->execute();
    }
}

Literally, the page is under the settings within the CMS.

I seeded the admin user in the cms_admin_user_fixture.yml file as follows.

SilverStripe\Security\Member:
  admin_user:
    ID: 1
    LastEdited: 2020-03-28 17:15:53
    Created: 2019-01-08 17:04:53
    FirstName: Default Admin
    Email: admin

SilverStripe\Security\Group:
  admin_group:
    ID: 1
    LastEdited: 2019-01-08 17:04:53
    Created: 2019-01-08 17:04:53
    Title: Administrators
    Code: administrators
    Locked: 0

I seeded the page and required data for request in the secondary_menu_fixture.yml file as follows.

SilverStripe\CMS\Model\SiteTree:
  sitetree_page_one:
    ID: 1
    ClassName: Page
    Title: Page Title 1
    URLSegment: page-url-1
Page:
  page_one:
    ID: 1
    Heading: Page Title 1
    Title: Cision form content
    URLSegment: page-url-1

SilverStripe\SiteConfig\SiteConfig:
  siteconfig_one:
    ID: 1
    LastEdited: 2020-03-10 12:25:37
    Created: 2019-01-08 17:04:53
    Title: Siteconfig Title 1
    Tagline: Siteconfig Tagline 1
    CanViewType: Anyone
    CanEditType: OnlyTheseUsers
    CanCreateTopLevelType: OnlyTheseUsers

When I run my test, the post request to this path, "/admin/settings/SecondaryMenuPages" is always returning 404. What is wrong with my code and how can I fix it?

Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • 1
    You can try to set up xdebug with your tests. That's how I often dig in this kind of rabbit holes. The URL works in your normal site? Did you flush, both the webuser and the cli-user for running the tests? I always create a folder `silverstripe-cache` in my projects, where SS puts in the caches for each user (webserver and cli) then I can delete the cache folders to simulate a flush. – wmk Apr 02 '20 at 06:24
  • Hi the URL did work in my normal site. Can you please give me some guidelines on how to set up the xdebug? – Wai Yan Hein Apr 06 '20 at 06:26

0 Answers0