Jump to:

22977 Posts in 11720 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Unit Testing Pages

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1157 Views
  • mobius
    Avatar
    Community Member
    54 Posts

    Unit Testing Pages Link to this post

    Hello, I'm attempting to set up unit tests for a module I'm developing.

    I'm currently needing to test that a particular page type displays everything as it should (it's a subclass of Page).

    I've created the page in my .yml file, and it is being created correctly, but I can't figure out how to GET the page with a functionaltest - I'm just getting a 404...

    This is my CompanyPageTest.php file:

    <?php

    /**
    * Test the features of the Companies page
    *
    */
    class CompanyPageTest extends FunctionalTest {
       static $fixture_file = 'csm2/tests/models/clients/Companies.yml';
       
       protected $page;
       
       public function setUp() {
          parent::setUp();
          
          $this->page = $this->objFromFixture('CompanyPage', 'companies');
       }
       
       /**
        * Return a session that has a user logged in as someone
    */
    public function createSession($fixtureName) {
    return new Session(array(
    'loggedInAs' => $this->idFromFixture('Member', $fixtureName)
    ));
    }
       
       
       public function testIndexListsAllCompanies() {
          Debug::show($this->page->Link());
          $this->get($this->page->Link(), $this->createSession('admin'));
          Debug::show($this->content());
          
          $this->assertPartialMatchBySelector('.name', 'Company A');
          $this->assertPartialMatchBySelector('.name', 'Company B');
       }
    }

    And this is my Companies.yml file:

    Member:
    admin:
    FirstName: Admin
    Surname: Admin
    Email: admin@test.com
    engineer:
    FirstName: Joe
    Surname: Engineer
    Email: joe@test.com

    Company:
    a:
    Name: Company A
    BillingAddress: 'Company A, PO Box 1234, Some Town'
    PhysicalAddress: '123 Some Road, Some Town'
    AgreedRate: $120
    Phone: (03) 1234567

    b:
        Name: Company B
    BillingAddress: 'Company B, PO Box 4321, Some Town'
    PhysicalAddress: '321 Some Road, Some Town'
    AgreedRate: $120
    Phone: (03) 7654321

          
    CompanyPage:
    companies:
    Title: Companies
    URLSegment: companies
    CanViewType: LoggedInUsers

    Job:
    open-job:
    Title: An Open Job
    Description: Some description on how the job looks
    Due: 17/2/2030
    Status: 'New'
    For: =>Company.a
    CreatedBy: =>Member.admin
    AssignedTo: =>Member.engineer
    SLA:
    Invoice:

    The two debug statements in the test code show the url of the page I created ('companies'), and the content of a 404 page. Anyone able to point me in the right direction?

    Cheers

    Edit: Just noticed that the spacing in the yml code listing is all weird on the forum - I do have it correct in my actual file..

  • mobius
    Avatar
    Community Member
    54 Posts

    Re: Unit Testing Pages Link to this post

    Thanks to ajshort in IRC, I've managed to fix this problem.

    End code is:

    <?php

    /**
    * Test the features of the Companies page
    *
    */
    class CompanyPageTest extends FunctionalTest {
       static $fixture_file = 'csm2/tests/models/clients/Companies.yml';
       static $use_draft_site = true;
       
       protected $page;
       
       public function setUp() {
          parent::setUp();
          
          $this->page = $this->objFromFixture('CompanyPage', 'companies');
       }
       
       public function testIndexListsAllCompanies() {
          $this->loginAs('admin');
          $this->get(Director::makeRelative($this->page->Link()));
          
          $this->assertExactMatchBySelector('.name a', array('Company A', 'Company B'));
       }
    }

    Needed to use $this->loginAs(), and also static $use_draft_site = true;

    Cheers!

    1157 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.