Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Unit Testing Pages


Reply

2 Posts   1246 Views

Avatar
mobius

30 January 2011 at 8:46pm (Last edited: 30 January 2011 8:47pm), Community Member, 54 Posts

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..

Avatar
mobius

30 January 2011 at 11:04pm Community Member, 54 Posts

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!