Merge pull request #55 from open-sausages/pulls/4/tweak-framework-test

Update Frameworktest to be a vendormodule and tweaks
This commit is contained in:
Ingo Schommer 2018-10-18 17:27:03 +13:00 committed by GitHub
commit cace7979aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 149 additions and 17 deletions

View File

@ -2,6 +2,7 @@
namespace SilverStripe\FrameworkTest\Model;
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
@ -338,4 +339,9 @@ class Company extends DataObject
215 => array("Cisco", "Information technology", "40.04", "John T. Chambers"),
);
}
public function scaffoldSearchField()
{
return DropdownField::create('CompanyID', 'Company', self::get()->map())->setEmptyString('');
}
}

View File

@ -6,12 +6,19 @@ namespace SilverStripe\FrameworkTest\Model;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\ORM\Connect\MySQLSchemaManager;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\Filters\ExactMatchFilter;
use SilverStripe\ORM\Filters\FulltextFilter;
use SilverStripe\ORM\Filters\GreaterThanOrEqualFilter;
use SilverStripe\ORM\Filters\LessThanOrEqualFilter;
/**
@ -23,17 +30,62 @@ class Employee extends DataObject
private static $db = array(
'Name' => 'Varchar',
'Biography' => 'HTMLText'
'Biography' => 'HTMLText',
'DateOfBirth' => 'Date',
'Category' => 'Enum("marketing,management,rnd,hr")'
);
private static $has_one = array(
'Company' => 'SilverStripe\\FrameworkTest\\Model\\Company',
'ProfileImage' => 'SilverStripe\\Assets\\Image'
);
private static $has_one = [
'Company' => Company::class,
'ProfileImage' => Image::class
];
private static $belongs_many_many = array(
'PastCompanies' => 'SilverStripe\\FrameworkTest\\Model\\Company'
);
private static $belongs_many_many = [
'PastCompanies' => Company::class
];
private static $indexes = [
'SearchFields' => [
'type' => 'fulltext',
'columns' => ['Name', 'Biography'],
]
];
private static $create_table_options = [
MySQLSchemaManager::ID => 'ENGINE=MyISAM'
];
private static $summary_fields = [
'ID',
'Name',
'Biography',
'CompanyID',
'Company.Name' => 'Company',
'ColleagueNames' => 'Colleague Names',
'DateOfBirth',
'Category'
];
private static $searchable_fields = [
'Name',
'Company' => [
'title' => 'Company exact match filter',
'filter' => ExactMatchFilter::class,
],
'Company.Name' => array(
'title' => 'Company Name starting with',
'field' => TextField::class,
'filter' => 'StartsWithFilter'
),
'CompanyID' => [
'title' => 'Company ID less than',
'field' => NumericField::class,
'filter' => 'LessThanFilter'
],
'Company.Employees.Name' => array('title' => 'Colleague'),
'DateOfBirth',
'Category'
];
private static $table_name = 'Employee';
@ -49,17 +101,40 @@ class Employee extends DataObject
public function requireDefaultRecords()
{
parent::requireDefaultRecords();
$employeeSet = DataObject::get('SilverStripe\\FrameworkTest\\Model\\Employee');
$employeeSet = DataObject::get(Employee::class);
foreach ($employeeSet as $employee) {
$employee->delete();
}
// By explicitly seeding our random generator, we'll always get the same sequence
srand(5);
$companyIDs = Company::get()->column();
$companyCount = sizeof($companyIDs);
$words = $this->words();
$wordCount = sizeof($words);
$categories = ['marketing', 'management', 'rnd', 'hr'];
foreach ($this->data() as $employeeName) {
$employee = new Employee();
$employee->Name = $employeeName;
$employee = Employee::create([
'Name' => $employeeName,
'CompanyID' => $companyIDs[rand(0, $companyCount-1)],
'Biography' => implode([
$words[rand(0, $wordCount-1)],
$words[rand(0, $wordCount-1)],
$words[rand(0, $wordCount-1)],
$words[rand(0, $wordCount-1)],
], " "),
'DateOfBirth' => date("Y-m-d", rand(0, 500) * 24 * 60 * 60),
'Category' => $categories[rand(0, 3)]
]);
$employee->write();
}
DB::alteration_message("Added default records to Employee table", "created");
// Let's rescrambled our random generator
srand();
}
public function validate()
@ -91,4 +166,37 @@ class Employee extends DataObject
'Elvis','Illana','Lee','Ariana','Hilel','Juliet','Gage','Larissa','Richard','Allen'
);
}
/**
* Random words used to build the BIO
*
* @return array
*/
public function words()
{
return [
"aquarist", "macedonian", "kalian", "knew", "cloisterless", "separation", "husker",
"subhall", "relatable", "gyrofrequency", "mandating", "presentability", "catt", "folioing",
"theodore", "yavar", "mutular", "umbrage", "reinstated", "sert", "nonemulous", "nutwood",
"perikeiromene", "moi", "wordless", "downstage", "rsj", "nonanesthetized", "dunker",
"preloan", "lev", "marlow", "unpaneled", "overliberally", "monasticism", "philoctetes",
"amba", "fluffier", "volley", "unwasteful", "helpless", "gallico", "superexpectation",
"quartermaster", "extenuative", "marriage", "tiberius", "horn", "yankeedom", "chabazite",
];
}
public function getDefaultSearchContext()
{
$context = parent::getDefaultSearchContext();
$context->addField(TextField::create('SearchFields', 'Full Text search'));
$context->addFilter(FulltextFilter::create('SearchFields'));
return $context;
}
public function getColleagueNames()
{
return implode($this->Company()->Employees()->column('Name'), ' ');
}
}

View File

@ -4,6 +4,8 @@ namespace SilverStripe\FrameworkTest\Model;
use Page;
use PageController;
use SilverStripe\Core\Manifest\ModuleResource;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\ORM\DataObject;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FormAction;
@ -87,7 +89,10 @@ class TestPage_Controller extends PageController
new FormAction("save", "Save"),
$gohome = new FormAction("gohome", "Go home")
);
$gohome->setAttribute('src', 'frameworktest/images/test-button.png');
$gohome->setAttribute(
'src',
ModuleResourceLoader::resourceURL('silverstripe/frameworktest: images/test-button.png')
);
$form = new Form($this, "Form", $fields, $actions);
$form->loadDataFrom($this->dataRecord);
return $form;

View File

@ -1,7 +1,7 @@
{
"name": "silverstripe/frameworktest",
"description": "Aids core and module developers in testing their code against a set of sample data and behaviour.",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "frameworktest", "testing"],
"license": "BSD-3-Clause",
"authors": [
@ -15,6 +15,19 @@
"silverstripe/framework": "~4.0",
"silverstripe/cms": "~4.0",
"guzzlehttp/guzzle": "~6.0",
"fzaninotto/faker": "^1.7"
}
"fzaninotto/faker": "^1.7",
"silverstripe/vendor-plugin": "^1.0"
},
"extra": {
"expose": [
"client/dist",
"css",
"images"
]
},
"autoload": {
"psr-4": {
"SSilverStripe\\FrameworkTest\\": "code/"
}
}
}