2008-08-22 05:33:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class TableFieldTest extends SapphireTest {
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'TableFieldTest.yml';
|
2009-07-17 04:09:27 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'TableFieldTest_Object',
|
|
|
|
'TableFieldTest_HasManyRelation',
|
|
|
|
);
|
|
|
|
|
2009-07-17 04:09:27 +02:00
|
|
|
function testAdd() {
|
|
|
|
$group = $this->objFromFixture('Group','group1_no_perms');
|
2008-08-22 05:33:43 +02:00
|
|
|
|
|
|
|
$tableField = new TableField(
|
|
|
|
"Permissions",
|
|
|
|
"Permission",
|
|
|
|
array(
|
2009-07-17 04:09:27 +02:00
|
|
|
"Code" => 'Code',
|
|
|
|
"Arg" => 'Arg',
|
2008-08-22 05:33:43 +02:00
|
|
|
),
|
|
|
|
array(
|
2009-07-17 04:09:27 +02:00
|
|
|
"Code" => "TextField",
|
2008-08-22 05:33:43 +02:00
|
|
|
"Arg" => "TextField",
|
|
|
|
),
|
|
|
|
"GroupID",
|
|
|
|
$group->ID
|
|
|
|
);
|
2009-07-17 04:09:27 +02:00
|
|
|
$form = new Form(
|
|
|
|
new TableFieldTest_Controller(),
|
|
|
|
"Form",
|
|
|
|
new FieldSet($tableField),
|
|
|
|
new FieldSet()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test Insert
|
2008-08-22 05:33:43 +02:00
|
|
|
|
2009-07-17 04:09:27 +02:00
|
|
|
// The field starts emppty. Save some new data.
|
|
|
|
// We have replicated the array structure that the specific layout of the form generates.
|
2008-08-22 05:33:43 +02:00
|
|
|
$tableField->setValue(array(
|
|
|
|
'new' => array(
|
|
|
|
'Code' => array(
|
2011-03-29 06:54:08 +02:00
|
|
|
'CustomPerm1',
|
|
|
|
'CustomPerm2',
|
2008-08-22 05:33:43 +02:00
|
|
|
),
|
|
|
|
'Arg' => array(
|
|
|
|
'1',
|
|
|
|
'2'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
$tableField->saveInto($group);
|
|
|
|
|
2009-07-17 04:09:27 +02:00
|
|
|
// Let's check that the 2 permissions entries have been saved
|
2011-05-03 05:05:27 +02:00
|
|
|
$permissions = $group->Permissions()->map('Arg', 'Code');
|
2008-08-22 05:33:43 +02:00
|
|
|
$this->assertEquals(array(
|
2011-03-29 06:54:08 +02:00
|
|
|
1 => 'CustomPerm1',
|
|
|
|
2 => 'CustomPerm2',
|
2008-08-22 05:33:43 +02:00
|
|
|
), $permissions);
|
|
|
|
|
|
|
|
|
2009-07-17 04:09:27 +02:00
|
|
|
// Test repeated insert
|
2008-08-22 05:33:43 +02:00
|
|
|
$value = array();
|
|
|
|
foreach($group->Permissions() as $permission) {
|
|
|
|
$value[$permission->ID] = array("Code" => $permission->Code, "Arg" => $permission->Arg);
|
|
|
|
}
|
|
|
|
$value['new'] = array(
|
|
|
|
'Code' => array(
|
2011-03-29 06:54:08 +02:00
|
|
|
'CustomPerm3',
|
2008-08-22 05:33:43 +02:00
|
|
|
),
|
|
|
|
'Arg' => array(
|
|
|
|
'3',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$tableField->setValue($value);
|
|
|
|
$tableField->saveInto($group);
|
|
|
|
|
2009-07-17 04:09:27 +02:00
|
|
|
// Let's check that the 2 existing permissions entries, and the 1 new one, have been saved
|
2011-05-03 05:05:27 +02:00
|
|
|
$permissions = $group->Permissions()->map('Arg', 'Code');
|
2008-08-22 05:33:43 +02:00
|
|
|
$this->assertEquals(array(
|
2011-03-29 06:54:08 +02:00
|
|
|
1 => 'CustomPerm1',
|
|
|
|
2 => 'CustomPerm2',
|
|
|
|
3 => 'CustomPerm3',
|
2008-08-22 05:33:43 +02:00
|
|
|
), $permissions);
|
|
|
|
|
|
|
|
}
|
2009-07-17 01:49:32 +02:00
|
|
|
|
2009-07-17 04:09:27 +02:00
|
|
|
function testEdit() {
|
|
|
|
$group = $this->objFromFixture('Group','group2_existing_perms');
|
|
|
|
$perm1 = $this->objFromFixture('Permission', 'perm1');
|
|
|
|
$perm2 = $this->objFromFixture('Permission', 'perm2');
|
|
|
|
|
|
|
|
$tableField = new TableField(
|
|
|
|
"Permissions",
|
|
|
|
"Permission",
|
|
|
|
array(
|
|
|
|
"Code" => 'Code',
|
|
|
|
"Arg" => 'Arg',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"Code" => "TextField",
|
|
|
|
"Arg" => "TextField",
|
|
|
|
),
|
|
|
|
"GroupID",
|
|
|
|
$group->ID
|
|
|
|
);
|
|
|
|
$form = new Form(
|
|
|
|
new TableFieldTest_Controller(),
|
|
|
|
"Form",
|
|
|
|
new FieldSet($tableField),
|
|
|
|
new FieldSet()
|
|
|
|
);
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
$this->assertEquals(2, $tableField->sourceItems()->Count());
|
2009-07-17 04:09:27 +02:00
|
|
|
|
|
|
|
// We have replicated the array structure that the specific layout of the form generates.
|
|
|
|
$tableField->setValue(array(
|
|
|
|
$perm1->ID => array(
|
|
|
|
'Code' => 'Perm1 Modified',
|
|
|
|
'Arg' => '101'
|
|
|
|
),
|
|
|
|
$perm2->ID => array(
|
|
|
|
'Code' => 'Perm2 Modified',
|
|
|
|
'Arg' => '102'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$tableField->saveInto($group);
|
|
|
|
|
|
|
|
// Let's check that the 2 permissions entries have been saved
|
2011-05-03 05:05:27 +02:00
|
|
|
$permissions = $group->Permissions()->map('Arg', 'Code');
|
2009-07-17 04:09:27 +02:00
|
|
|
$this->assertEquals(array(
|
|
|
|
101 => 'Perm1 Modified',
|
|
|
|
102 => 'Perm2 Modified',
|
|
|
|
), $permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDelete() {
|
|
|
|
$group = $this->objFromFixture('Group','group2_existing_perms');
|
|
|
|
$perm1 = $this->objFromFixture('Permission', 'perm1');
|
|
|
|
$perm2 = $this->objFromFixture('Permission', 'perm2');
|
|
|
|
|
|
|
|
$tableField = new TableField(
|
|
|
|
"Permissions",
|
|
|
|
"Permission",
|
|
|
|
array(
|
|
|
|
"Code" => 'Code',
|
|
|
|
"Arg" => 'Arg',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"Code" => "TextField",
|
|
|
|
"Arg" => "TextField",
|
|
|
|
),
|
|
|
|
"GroupID",
|
|
|
|
$group->ID
|
|
|
|
);
|
|
|
|
$form = new Form(
|
|
|
|
new TableFieldTest_Controller(),
|
|
|
|
"Form",
|
|
|
|
new FieldSet($tableField),
|
|
|
|
new FieldSet()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertContains($perm1->ID, $tableField->sourceItems()->column('ID'));
|
|
|
|
|
|
|
|
$response = $tableField->Items()->find('ID', $perm1->ID)->delete();
|
|
|
|
|
|
|
|
$this->assertNotContains($perm1->ID, $tableField->sourceItems()->column('ID'));
|
|
|
|
}
|
|
|
|
|
2009-11-22 07:07:45 +01:00
|
|
|
/**
|
|
|
|
* Relation auto-setting is now the only option
|
|
|
|
*/
|
2009-07-17 01:49:32 +02:00
|
|
|
function testAutoRelationSettingOn() {
|
|
|
|
$o = new TableFieldTest_Object();
|
|
|
|
$o->write();
|
2009-11-22 07:07:45 +01:00
|
|
|
|
2009-07-17 01:49:32 +02:00
|
|
|
$tf = new TableField(
|
|
|
|
'HasManyRelations',
|
|
|
|
'TableFieldTest_HasManyRelation',
|
|
|
|
array(
|
|
|
|
'Value' => 'Value'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Value' => 'TextField'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test with auto relation setting
|
|
|
|
$form = new Form(new TableFieldTest_Controller(), "Form", new FieldSet($tf), new FieldSet());
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->loadDataFrom($o);
|
|
|
|
|
2009-07-17 01:49:32 +02:00
|
|
|
$tf->setValue(array(
|
|
|
|
'new' => array(
|
|
|
|
'Value' => array('one','two',)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2009-11-22 07:07:45 +01:00
|
|
|
$form->saveInto($o);
|
|
|
|
$this->assertEquals(2, $o->HasManyRelations()->Count());
|
2009-07-17 01:49:32 +02:00
|
|
|
}
|
2009-07-17 04:09:27 +02:00
|
|
|
|
|
|
|
function testHasItemsWhenSetAsArray() {
|
|
|
|
$tf = new TableField(
|
|
|
|
'TestTableField',
|
|
|
|
'TableFieldTest_HasManyRelation',
|
|
|
|
array(
|
|
|
|
'Value' => 'Value'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'Value' => 'TextField'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$tf->setValue(array(
|
|
|
|
'new' => array(
|
|
|
|
'Value' => array(
|
|
|
|
'One',
|
|
|
|
'Two',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$items = $tf->Items();
|
|
|
|
$itemsArr = $items->toArray();
|
|
|
|
|
|
|
|
// includes the two values and an "add" row
|
|
|
|
$this->assertEquals($items->Count(), 3);
|
|
|
|
|
|
|
|
// first row
|
|
|
|
$this->assertEquals(
|
|
|
|
$itemsArr[0]->Fields()->fieldByName('TestTableField[new][Value][]')->Value(),
|
|
|
|
'One'
|
|
|
|
);
|
|
|
|
|
|
|
|
// second row
|
|
|
|
$this->assertEquals(
|
|
|
|
$itemsArr[1]->Fields()->fieldByName('TestTableField[new][Value][]')->Value(),
|
|
|
|
'Two'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-08-22 05:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stub controller
|
|
|
|
*/
|
2011-02-13 23:14:51 +01:00
|
|
|
class TableFieldTest_Controller extends Controller implements TestOnly {
|
2009-10-11 02:07:16 +02:00
|
|
|
function Link($action = null) {
|
|
|
|
return Controller::join_links('TableFieldTest/', $action);
|
2008-08-22 05:33:43 +02:00
|
|
|
}
|
2009-07-17 01:49:32 +02:00
|
|
|
}
|
|
|
|
class TableFieldTest_Object extends DataObject implements TestOnly {
|
|
|
|
static $has_many = array(
|
|
|
|
"HasManyRelations" => 'TableFieldTest_HasManyRelation'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class TableFieldTest_HasManyRelation extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Value' => 'Text',
|
|
|
|
);
|
|
|
|
|
|
|
|
static $has_one = array(
|
|
|
|
'HasOneRelation' => 'TableFieldTest_Object'
|
|
|
|
);
|
2008-08-22 05:33:43 +02:00
|
|
|
}
|