作者:jacobbuc
项目:silverstripe-framewor
public function scaffoldFormField($title = null, $params = null)
{
if (empty($this->object)) {
return null;
}
$relationName = substr($this->name, 0, -2);
$hasOneClass = $this->object->hasOneComponent($relationName);
if (empty($hasOneClass)) {
return null;
}
$hasOneSingleton = singleton($hasOneClass);
if ($hasOneSingleton instanceof File) {
$field = new UploadField($relationName, $title);
if ($hasOneSingleton instanceof Image) {
$field->setAllowedFileCategories('image/supported');
}
return $field;
}
// Build selector / numeric field
$titleField = $hasOneSingleton->hasField('Title') ? "Title" : "Name";
$list = DataList::create($hasOneClass);
// Don't scaffold a dropdown for large tables, as making the list concrete
// might exceed the available PHP memory in creating too many DataObject instances
if ($list->count() < 100) {
$field = new DropdownField($this->name, $title, $list->map('ID', $titleField));
$field->setEmptyString(' ');
} else {
$field = new NumericField($this->name, $title);
}
return $field;
}
作者:SpiritLeve
项目:silverstripe-framewor
/**
* @param string $class
* @param GridField $gridField
* @param DataObject $record
* @param RequestHandler $requestHandler
*/
public function updateItemRequestClass(&$class, $gridField, $record, $requestHandler)
{
// Conditionally use a versioned item handler
if ($record && $record->has_extension('SilverStripe\\ORM\\Versioning\\Versioned')) {
$class = 'SilverStripe\\ORM\\Versioning\\VersionedGridFieldItemRequest';
}
}
作者:jacobbuc
项目:silverstripe-framewor
/**
* Adds the item to this relation.
*
* It does so by setting the relationFilters.
*
* @param $item The DataObject to be added, or its ID
*/
public function add($item)
{
if (is_numeric($item)) {
$item = DataObject::get_by_id($this->dataClass, $item);
} else {
if (!$item instanceof $this->dataClass) {
user_error("PolymorphicHasManyList::add() expecting a {$this->dataClass} object, or ID value", E_USER_ERROR);
}
}
$foreignID = $this->getForeignID();
// Validate foreignID
if (!$foreignID) {
user_error("PolymorphicHasManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
return;
}
if (is_array($foreignID)) {
user_error("PolymorphicHasManyList::add() can't be called on a list linked to mulitple foreign IDs", E_USER_WARNING);
return;
}
$foreignKey = $this->foreignKey;
$classForeignKey = $this->classForeignKey;
$item->{$foreignKey} = $foreignID;
$item->{$classForeignKey} = $this->getForeignClass();
$item->write();
}
作者:jacobbuc
项目:silverstripe-framewor
public function testMultipleRowInsert()
{
$query = SQLInsert::create('"SQLInsertTestBase"');
$query->addRow(array('"Title"' => 'First Object', '"Age"' => 10, '"Description"' => 'First the worst'));
$query->addRow(array('"Title"' => 'Second object', '"Age"' => 12));
$sql = $query->sql($parameters);
// Only test this case if using the default query builder
if (get_class(DB::get_conn()->getQueryBuilder()) === 'SilverStripe\\ORM\\Connect\\DBQueryBuilder') {
$this->assertSQLEquals('INSERT INTO "SQLInsertTestBase" ("Title", "Age", "Description") VALUES (?, ?, ?), (?, ?, ?)', $sql);
}
$this->assertEquals(array('First Object', 10, 'First the worst', 'Second object', 12, null), $parameters);
$query->execute();
$this->assertEquals(2, DB::affected_rows());
// Check inserted objects are correct
$firstObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'First Object'), false);
$this->assertNotEmpty($firstObject);
$this->assertEquals($firstObject->Title, 'First Object');
$this->assertEquals($firstObject->Age, 10);
$this->assertEquals($firstObject->Description, 'First the worst');
$secondObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'Second object'), false);
$this->assertNotEmpty($secondObject);
$this->assertEquals($secondObject->Title, 'Second object');
$this->assertEquals($secondObject->Age, 12);
$this->assertEmpty($secondObject->Description);
}
作者:jacobbuc
项目:silverstripe-framewor
public function Form()
{
$player = DataObject::get('GridFieldTest_Player')->find('Email', 'player1@test.com');
$config = GridFieldConfig::create()->addComponents($relationComponent = new GridFieldAddExistingAutocompleter('before'), new GridFieldDataColumns());
$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
return new Form($this, 'Form', new FieldList($field), new FieldList());
}
作者:SpiritLeve
项目:silverstripe-framewor
public function testCreateWithTransaction()
{
if (DB::get_conn()->supportsTransactions() == true) {
DB::get_conn()->transactionStart();
$obj = new TransactionTest_Object();
$obj->Title = 'First page';
$obj->write();
$obj = new TransactionTest_Object();
$obj->Title = 'Second page';
$obj->write();
//Create a savepoint here:
DB::get_conn()->transactionSavepoint('rollback');
$obj = new TransactionTest_Object();
$obj->Title = 'Third page';
$obj->write();
$obj = new TransactionTest_Object();
$obj->Title = 'Fourth page';
$obj->write();
//Revert to a savepoint:
DB::get_conn()->transactionRollback('rollback');
DB::get_conn()->transactionEnd();
$first = DataObject::get('TransactionTest_Object', "\"Title\"='First page'");
$second = DataObject::get('TransactionTest_Object', "\"Title\"='Second page'");
$third = DataObject::get('TransactionTest_Object', "\"Title\"='Third page'");
$fourth = DataObject::get('TransactionTest_Object', "\"Title\"='Fourth page'");
//These pages should be in the system
$this->assertTrue(is_object($first) && $first->exists());
$this->assertTrue(is_object($second) && $second->exists());
//These pages should NOT exist, we reverted to a savepoint:
$this->assertFalse(is_object($third) && $third->exists());
$this->assertFalse(is_object($fourth) && $fourth->exists());
} else {
$this->markTestSkipped('Current database does not support transactions');
}
}
作者:jacobbuc
项目:silverstripe-framewor
public function processRecord($record, $columnMap, &$results, $preview = false)
{
// We match by 'Code', the ID property is confusing the importer
if (isset($record['ID'])) {
unset($record['ID']);
}
$objID = parent::processRecord($record, $columnMap, $results, $preview);
$group = DataObject::get_by_id($this->objectClass, $objID);
// set group hierarchies - we need to do this after all records
// are imported to avoid missing "early" references to parents
// which are imported later on in the CSV file.
if (isset($record['ParentCode']) && $record['ParentCode']) {
$parentGroup = DataObject::get_one('SilverStripe\\Security\\Group', array('"Group"."Code"' => $record['ParentCode']));
if ($parentGroup) {
$group->ParentID = $parentGroup->ID;
$group->write();
}
}
// set permission codes - these are all additive, meaning
// existing permissions arent cleared.
if (isset($record['PermissionCodes']) && $record['PermissionCodes']) {
foreach (explode(',', $record['PermissionCodes']) as $code) {
$p = DataObject::get_one('SilverStripe\\Security\\Permission', array('"Permission"."Code"' => $code, '"Permission"."GroupID"' => $group->ID));
if (!$p) {
$p = new Permission(array('Code' => $code));
$p->write();
}
$group->Permissions()->add($p);
}
}
return $objID;
}
作者:jacobbuc
项目:silverstripe-framewor
public function testApplyReplationDeepInheretence()
{
//test has_one relation
$newDQ = new DataQuery('DataQueryTest_E');
//apply a relation to a relation from an ancestor class
$newDQ->applyRelation('TestA');
$this->assertTrue($newDQ->query()->isJoinedTo('DataQueryTest_C'));
$this->assertContains('"DataQueryTest_A"."ID" = "DataQueryTest_C"."TestAID"', $newDQ->sql($params));
//test many_many relation
//test many_many with separate inheritance
$newDQ = new DataQuery('DataQueryTest_C');
$baseDBTable = DataObject::getSchema()->baseDataTable('DataQueryTest_C');
$newDQ->applyRelation('ManyTestAs');
//check we are "joined" to the DataObject's table (there is no distinction between FROM or JOIN clauses)
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
//check we are explicitly selecting "FROM" the DO's table
$this->assertContains("FROM \"{$baseDBTable}\"", $newDQ->sql());
//test many_many with shared inheritance
$newDQ = new DataQuery('DataQueryTest_E');
$baseDBTable = DataObject::getSchema()->baseDataTable('DataQueryTest_E');
//check we are "joined" to the DataObject's table (there is no distinction between FROM or JOIN clauses)
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
//check we are explicitly selecting "FROM" the DO's table
$this->assertContains("FROM \"{$baseDBTable}\"", $newDQ->sql(), 'The FROM clause is missing from the query');
$newDQ->applyRelation('ManyTestGs');
//confirm we are still joined to the base table
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
//double check it is the "FROM" clause
$this->assertContains("FROM \"{$baseDBTable}\"", $newDQ->sql(), 'The FROM clause has been removed from the query');
//another (potentially less crude check) for checking "FROM" clause
$fromTables = $newDQ->query()->getFrom();
$this->assertEquals('"' . $baseDBTable . '"', $fromTables[$baseDBTable]);
}
作者:SpiritLeve
项目:silverstripe-framewor
public function processRecord($record, $columnMap, &$results, $preview = false)
{
$objID = parent::processRecord($record, $columnMap, $results, $preview);
$_cache_groupByCode = array();
// Add to predefined groups
/** @var Member $member */
$member = DataObject::get_by_id($this->objectClass, $objID);
foreach ($this->groups as $group) {
// TODO This isnt the most memory effective way to add members to a group
$member->Groups()->add($group);
}
// Add to groups defined in CSV
if (isset($record['Groups']) && $record['Groups']) {
$groupCodes = explode(',', $record['Groups']);
foreach ($groupCodes as $groupCode) {
$groupCode = Convert::raw2url($groupCode);
if (!isset($_cache_groupByCode[$groupCode])) {
$group = Group::get()->filter('Code', $groupCode)->first();
if (!$group) {
$group = new Group();
$group->Code = $groupCode;
$group->Title = $groupCode;
$group->write();
}
$member->Groups()->add($group);
$_cache_groupByCode[$groupCode] = $group;
}
}
}
$member->destroy();
unset($member);
return $objID;
}
作者:jacobbuc
项目:silverstripe-framewor
public function testDelete()
{
$role = $this->objFromFixture('SilverStripe\\Security\\PermissionRole', 'role');
$role->delete();
$this->assertEquals(0, DataObject::get('SilverStripe\\Security\\PermissionRole', "\"ID\"={$role->ID}")->count(), 'Role is removed');
$this->assertEquals(0, DataObject::get('SilverStripe\\Security\\PermissionRoleCode', "\"RoleID\"={$role->ID}")->count(), 'Permissions removed along with the role');
}
作者:jacobbuc
项目:silverstripe-framewor
public function getValue()
{
$id = $this->getIDValue();
$class = $this->getClassValue();
if ($id && $class && is_subclass_of($class, 'SilverStripe\\ORM\\DataObject')) {
return DataObject::get_by_id($class, $id);
}
return null;
}
作者:SpiritLeve
项目:silverstripe-framewor
public function testArrayValueWithSqlMapSource()
{
$member1 = $this->objFromFixture('SilverStripe\\Security\\Member', 'member1');
$member2 = $this->objFromFixture('SilverStripe\\Security\\Member', 'member2');
$member3 = $this->objFromFixture('SilverStripe\\Security\\Member', 'member3');
$source = DataObject::get('SilverStripe\\Security\\Member');
$f = new LookupField('test', 'test', $source->map('ID', 'FirstName'));
$f->setValue(array($member1->ID, $member2->ID));
$this->assertEquals(sprintf('<span class="readonly" id="test">member1, member2</span>' . '<input type="hidden" name="test" value="%s, %s" />', $member1->ID, $member2->ID), trim($f->Field()->getValue()));
}
作者:jacobbuc
项目:silverstripe-framewor
/**
* Test DataObject::composite_fields() and DataObject::is_composite_field()
*/
public function testCompositeFieldMetaDataFunctions()
{
$this->assertEquals('Money', DataObject::is_composite_field('DBCompositeTest_DataObject', 'MyMoney'));
$this->assertFalse(DataObject::is_composite_field('DBCompositeTest_DataObject', 'Title'));
$this->assertEquals(array('MyMoney' => 'Money', 'OverriddenMoney' => 'Money'), DataObject::composite_fields('DBCompositeTest_DataObject'));
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'MyMoney'));
$this->assertEquals('Money', DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherMoney'));
$this->assertFalse(DataObject::is_composite_field('SubclassedDBFieldObject', 'Title'));
$this->assertFalse(DataObject::is_composite_field('SubclassedDBFieldObject', 'OtherField'));
$this->assertEquals(array('MyMoney' => 'Money', 'OtherMoney' => 'Money', 'OverriddenMoney' => 'Money'), DataObject::composite_fields('SubclassedDBFieldObject'));
}
作者:SpiritLeve
项目:silverstripe-framewor
/**
* Test DataObject::composite_fields() and DataObject::is_composite_field()
*/
public function testCompositeFieldMetaDataFunctions()
{
$schema = DataObject::getSchema();
$this->assertEquals('Money', $schema->compositeField(DBCompositeTest_DataObject::class, 'MyMoney'));
$this->assertNull($schema->compositeField(DBCompositeTest_DataObject::class, 'Title'));
$this->assertEquals(array('MyMoney' => 'Money', 'OverriddenMoney' => 'Money'), $schema->compositeFields(DBCompositeTest_DataObject::class));
$this->assertEquals('Money', $schema->compositeField(SubclassedDBFieldObject::class, 'MyMoney'));
$this->assertEquals('Money', $schema->compositeField(SubclassedDBFieldObject::class, 'OtherMoney'));
$this->assertNull($schema->compositeField(SubclassedDBFieldObject::class, 'Title'));
$this->assertNull($schema->compositeField(SubclassedDBFieldObject::class, 'OtherField'));
$this->assertEquals(array('MyMoney' => 'Money', 'OtherMoney' => 'Money', 'OverriddenMoney' => 'Money'), $schema->compositeFields(SubclassedDBFieldObject::class));
}
作者:SpiritLeve
项目:silverstripe-framewor
public function testCleartextPasswordsAreHashedWithDefaultAlgo()
{
$loader = new MemberCsvBulkLoader();
$results = $loader->load($this->getCurrentRelativePath() . '/MemberCsvBulkLoaderTest_cleartextpws.csv');
$member = $results->Created()->First();
$memberID = $member->ID;
DataObject::flush_and_destroy_cache();
$member = DataObject::get_by_id('SilverStripe\\Security\\Member', $memberID);
// TODO Direct getter doesn't work, wtf!
$this->assertEquals(Security::config()->password_encryption_algorithm, $member->getField('PasswordEncryption'));
$result = $member->checkPassword('mypassword');
$this->assertTrue($result->valid());
}
作者:jacobbuc
项目:silverstripe-framewor
public function testLoadDataFromObject()
{
$article = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithouttags');
$articleWithTags = $this->objFromFixture('CheckboxSetFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('CheckboxSetFieldTest_Tag', 'tag2');
$field = new CheckboxSetField("Tags", "Test field", DataObject::get("CheckboxSetFieldTest_Tag")->map());
$form = new Form(new Controller(), 'Form', new FieldList($field), new FieldList());
$form->loadDataFrom($articleWithTags);
$value = $field->Value();
sort($value);
$this->assertEquals(array($tag1->ID, $tag2->ID), $value, 'CheckboxSetField loads data from a manymany relationship in an object through Form->loadDataFrom()');
}
作者:SpiritLeve
项目:silverstripe-framewor
public function testSQLInsert()
{
$factory = new FixtureFactory();
$relPath = ltrim(FRAMEWORK_DIR . '/tests/testing/YamlFixtureTest.yml', '/');
$fixture = Injector::inst()->create('SilverStripe\\Dev\\YamlFixture', $relPath);
$fixture->writeInto($factory);
$this->assertGreaterThan(0, $factory->getId("YamlFixtureTest_DataObject", "testobject1"));
$object1 = DataObject::get_by_id("YamlFixtureTest_DataObject", $factory->getId("YamlFixtureTest_DataObject", "testobject1"));
$this->assertTrue($object1->ManyManyRelation()->Count() == 2, "Should be two items in this relationship");
$this->assertGreaterThan(0, $factory->getId("YamlFixtureTest_DataObject", "testobject2"));
$object2 = DataObject::get_by_id("YamlFixtureTest_DataObject", $factory->getId("YamlFixtureTest_DataObject", "testobject2"));
$this->assertTrue($object2->ManyManyRelation()->Count() == 1, "Should be one item in this relationship");
}
作者:SpiritLeve
项目:silverstripe-framewor
public function testBasicUpdate()
{
$query = SQLUpdate::create()->setTable('"SQLUpdateTestBase"')->assign('"Description"', 'Description 1a')->addWhere(array('"Title" = ?' => 'Object 1'));
$sql = $query->sql($parameters);
// Check SQL
$this->assertSQLEquals('UPDATE "SQLUpdateTestBase" SET "Description" = ? WHERE ("Title" = ?)', $sql);
$this->assertEquals(array('Description 1a', 'Object 1'), $parameters);
// Check affected rows
$query->execute();
$this->assertEquals(1, DB::affected_rows());
// Check item updated
$item = DataObject::get_one('SQLUpdateTestBase', array('"Title"' => 'Object 1'));
$this->assertEquals('Description 1a', $item->Description);
}
作者:jacobbuc
项目:silverstripe-framewor
public function testRun()
{
$m = new Member();
$m->Password = 'plain';
$m->PasswordEncryption = 'none';
$m->write();
$t = new EncryptAllPasswordsTask();
$t->run(null);
$m = DataObject::get_by_id('SilverStripe\\Security\\Member', $m->ID);
$this->assertEquals($m->PasswordEncryption, 'blowfish');
$this->assertNotEquals($m->Password, 'plain');
$result = $m->checkPassword('plain');
$this->assertTrue($result->valid());
}
作者:jacobbuc
项目:silverstripe-framewor
public function setUp()
{
parent::setUp();
Versioned::set_stage(Versioned::DRAFT);
// Automatically publish any object named *_published
foreach ($this->getFixtureFactory()->getFixtures() as $class => $fixtures) {
foreach ($fixtures as $name => $id) {
if (stripos($name, '_published') !== false) {
/** @var Versioned|DataObject $object */
$object = DataObject::get($class)->byID($id);
$object->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
}
}
}
}