From 5aff60efb563fa873da80f559ccd755c86600dfc Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 21 Aug 2007 01:38:20 +0000 Subject: [PATCH] More testing and debugging --- code/SubsiteAdmin.php | 14 +++--- tests/SubsiteAdminTest.php | 91 ++++++++++++++++++++++++-------------- 2 files changed, 68 insertions(+), 37 deletions(-) diff --git a/code/SubsiteAdmin.php b/code/SubsiteAdmin.php index 89d9284..66da3c9 100644 --- a/code/SubsiteAdmin.php +++ b/code/SubsiteAdmin.php @@ -46,14 +46,18 @@ class SubsiteAdmin extends GenericDataAdmin { return $html; } + /** + * Returns the form for adding subsites. + * @returns Form A nerw form object + */ function AddSubsiteForm() { $templates = $this->getIntranetTemplates(); if($templates) { - $templateArray = $templates->map('ID', 'Domain'); + $templateArray = $templates->map('ID', 'Title'); } - return new Form($this, 'AddIntranetForm', new FieldSet( + return new Form($this, 'AddSubsiteForm', new FieldSet( new TextField('Name', 'Name:'), new TextField('Subdomain', 'Subdomain:'), new DropdownField('TemplateID', 'Use template:', $templateArray), @@ -66,11 +70,10 @@ class SubsiteAdmin extends GenericDataAdmin { } public function getIntranetTemplates() { - return DataObject::get('Subsite_Template', '', 'Domain DESC'); + return DataObject::get('Subsite_Template', '', 'Title'); } function addintranet($data, $form) { - $SQL_email = Convert::raw2sql($data['AdminEmail']); $member = DataObject::get_one('Member', "`Email`='$SQL_email'"); @@ -85,7 +88,8 @@ class SubsiteAdmin extends GenericDataAdmin { // Create intranet from existing template // TODO Change template based on the domain selected. - $intranet = Intranet::createFromTemplate($data['Name'], $data['Subdomain'], $data['TemplateID']); + $template = DataObject::get_by_id('Subsite_Template', $data['TemplateID']); + $intranet = $template->createInstance($data['Name'], $data['Subdomain']); $groupObjects = array(); diff --git a/tests/SubsiteAdminTest.php b/tests/SubsiteAdminTest.php index 3efe664..0d715a2 100644 --- a/tests/SubsiteAdminTest.php +++ b/tests/SubsiteAdminTest.php @@ -3,37 +3,31 @@ class SubsiteAdminTest extends SapphireTest { static $fixture_file = 'subsites/tests/SubsiteTest.yml'; - /** - * Test generation of the view - */ - function testBasicView() { - // Open the admin area logged in as admin - $response1 = Director::test('admin/subsites/'); - - - // Confirm that this URL gets you the entire page, with the edit form loaded - $response2 = Director::test('admin/subsites/show/1'); - $this->assertTrue(strpos($response2->getBody(), 'id="Root_Configuration"') !== false); - $this->assertTrue(strpos($response2->getBody(), ' $this->idFromFixture('Member', 'admin') + )); + } - // Confirm that this URL gets you just the form content, with the edit form loaded - $response3 = Director::test('admin/subsites/show/1', array('ajax' => 1)); + /** + * Test generation of the view + */ + function testBasicView() { + // Open the admin area logged in as admin + $response1 = Director::test('admin/subsites/', null, $this->adminLoggedInSession()); + + // Confirm that this URL gets you the entire page, with the edit form loaded + $response2 = Director::test('admin/subsites/show/1', null, $this->adminLoggedInSession()); + $this->assertTrue(strpos($response2->getBody(), 'id="Root_Configuration"') !== false); + $this->assertTrue(strpos($response2->getBody(), 'assertTrue(strpos($response3->getBody(), 'id="Root_Configuration"') !== false); - $this->assertTrue(strpos($response3->getBody(), 'assertTrue(strpos($response3->getBody(), 'getIntranetTemplates(); - - $templateIDs = $this->allFixtureIDs('Subsite_Template'); - $this->assertTrue($templates->onlyContainsIDs($templateIDs)); - } + // Confirm that this URL gets you just the form content, with the edit form loaded + $response3 = Director::test('admin/subsites/show/1', array('ajax' => 1), $this->adminLoggedInSession()); + + $this->assertTrue(strpos($response3->getBody(), 'id="Root_Configuration"') !== false); + $this->assertTrue(strpos($response3->getBody(), 'assertTrue(strpos($response3->getBody(), 'pushCurrent(); - - $member = $this->objFromFixture('Member','admin'); - $member->logIn(); + $cont->setSession($this->adminLoggedInSession()); // Check that the logged-in member has the correct permissions $this->assertTrue(Permission::check('ADMIN') ? true : false); @@ -64,6 +56,41 @@ class SubsiteAdminTest extends SapphireTest { $cont->popCurrent(); } + + /** + * Test the intranet creation form. + */ + function testIntranetCreation() { + $cont = new SubsiteAdmin(); + $cont->pushCurrent(); + $cont->setSession($this->adminLoggedInSession()); + + $form = $cont->AddSubsiteForm(); + $source = $form->dataFieldByName('TemplateID')->getSource(); + + $templateIDs = $this->allFixtureIDs('Subsite_Template'); + foreach($templateIDs as $templateID) { + $this->assertArrayHasKey($templateID, $source); + } + + $templateObj = $this->objFromFixture('Subsite_Template','main'); + $this->assertEquals($templateObj->Title, $source[$templateObj->ID], "Template dropdown isn't listing Title values"); + + $response = $form->testSubmission('addintranet', array( + 'Name' => 'Test Intranet', + 'Subdomain' => 'Test', + 'TemplateID' => 1, + 'AdminEmail' => '', + 'AdminName' => '', + )); + + $this->assertTrue(true == preg_match('/admin\/subsites\/show\/([0-9]+)/i', $response->getHeader('Location'), $matches), "Intranet creation dowsn't redirect to new view"); + + $newIntranet = DataObject::get_by_id("Subsite", $matches[1]); + $this->assertEquals('Test Intranet', $newIntranet->Title, "New intranet not created properly."); + + $cont->popCurrent(); + } }