silverstripe-userforms/tests/SubmittedFormTest.php
edchipman@gmail.com 5de26cd897 Replaced all instances of new FieldSet() with new FieldList()
Replaced the complex table field used for editing the email recipients with GridField

Replaced instances of SAPPHIRE_DIR with FRAMEWORK_DIR

Replaced instances of sapphire/ in css and templates with framework/

Re-organized tabs on user defined form so they are top level

Replaced calls to toDropdownMap() with map()

Renamed getCMSFields_forPopup() top getCMSFields()
2012-05-07 17:41:29 +12:00

158 lines
4.8 KiB
PHP

<?php
class SubmittedFormTest extends FunctionalTest {
static $fixture_file = 'userforms/tests/SubmittedFormTest.yml';
protected $controller, $form, $page, $field;
function setUp() {
parent::setUp();
$this->page = $this->objFromFixture('UserDefinedForm', 'popular-form');
$this->controller = new SubmittedFormTest_Controller($this->page);
$this->form = $this->controller->Form();
$this->field = $this->form->dataFieldByName('Report');
}
function testSubmissions() {
$submissions = $this->field->Submissions();
// test with 11 submissions. Should be over 2 pages. 10 per page.
// @todo add tests to ensure the order
$this->assertEquals($submissions->Count(), 10);
$this->assertEquals($submissions->TotalPages(), 2);
$this->assertEquals($submissions->TotalItems(), 11);
}
function testGetSubmissionns() {
$template = $this->field->getSubmissions();
$parser = new CSSContentParser($template);
// check to ensure that the pagination exists
$pagination = $parser->getBySelector('.userforms-submissions-pagination');
$this->assertEquals(str_replace("\n", ' ',(string) $pagination[0]->span), "Viewing rows 0 - 10 of 11 rows");
$this->assertEquals(str_replace("\n", ' ',(string) $pagination[0]->a), "Next page");
// ensure the actions exist
$actions = $parser->getBySelector('.userforms-submission-actions');
$this->assertEquals(count($actions[0]->li), 2);
// submissions
$submissions = $parser->getBySelector('.userform-submission');
$this->assertEquals(count($submissions), 10);
}
function testCSVExport() {
$export = $this->field->export($this->page->ID);
// Pretend we are opening via file
$fp = fopen('php://memory', 'w+');
fwrite($fp, $export);
rewind($fp);
$data = array();
while($data[] = fgetcsv($fp));
array_pop($data);
fclose($fp);
// Check the headers are fine and include every legacy field. They should also be ordered
// according to the latest form layout.
$this->assertEquals($data[0], array(
'First', 'Submitted Title 2', 'Submitted Title', 'Submitted'
));
// Check the number of records in the export
$this->assertEquals(count($data), 12);
// Make sure the number of columns matches
$this->assertEquals(count($data[1]), 4);
$this->assertEquals(count($data[2]), 4);
$this->assertEquals(count($data[3]), 4);
$this->assertEquals(count($data[11]), 4);
// Specific value tests
$this->assertEquals($data[1][1], 'quote " and comma , test');
$this->assertEquals($data[1][2], 'Value 1');
$this->assertEquals($data[2][1], 'Value 2');
$this->assertEquals($data[3][1], "multi\nline\ntest");
$this->assertEquals($data[11][0], 'First');
$this->assertEquals($data[11][1], 'Second');
}
function testdeletesubmission() {
$submission = $this->objFromFixture('SubmittedForm', 'long-1');
$count = $this->page->Submissions()->Count();
$this->assertTrue($this->field->deletesubmission($submission->ID));
$this->assertEquals($count - 1, $this->page->Submissions()->Count());
$this->assertFalse($this->field->deletesubmission(-1));
}
function testdeletesubmissions() {
$this->assertTrue($this->field->deletesubmissions($this->page->ID));
$this->assertEquals($this->page->Submissions()->Count(), 0);
}
function testOnBeforeDeleteOfForm() {
$field = $this->objFromFixture('SubmittedFormField', 'submitted-form-field-1');
$form = $field->Parent();
$this->assertEquals($form->Values()->Count(), 2);
$form->delete();
$fields = DataObject::get('SubmittedFormField', "\"ParentID\" = '$form->ID'");
$this->assertNull($fields);
}
function testGetFormattedValue() {
$field = $this->objFromFixture('SubmittedFormField', 'submitted-form-field-1');
$this->assertEquals('1', $field->getFormattedValue());
$textarea = $this->objFromFixture('SubmittedFormField', 'submitted-textarea-1');
$text = "I am here testing<br />\nTesting until I cannot<br />\nI love my testing";
$this->assertEquals($text, $textarea->getFormattedValue());
}
function testFileGetLink() {
$field = $this->objFromFixture('SubmittedFileField', 'submitted-file-1');
// @todo add checks for if no file can be downloaded
$this->assertContains('my-file.jpg', $field->getLink());
}
function testFileGetFormattedValue() {
$field = $this->objFromFixture('SubmittedFileField', 'submitted-file-1');
// @todo add checks for if no file can be downloaded
$this->assertContains('Download File', $field->getFormattedValue());
}
}
class SubmittedFormTest_Controller extends ContentController {
function Form() {
$form = new Form($this, 'Form', new FieldList(new SubmittedFormReportField('Report')), new FieldList(new FormAction('Submit')));
$form->loadDataFrom($this->data());
return $form;
}
function forTemplate() {
return $this->renderWith(array('ContentController'));
}
}