mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Enhancement and fix for issue #3186
This commit is contained in:
parent
758c0273fa
commit
375bbf954e
@ -112,6 +112,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
'PublisherID' => true,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* An array of DataObject extensions that may require versioning for extra tables
|
||||
* The array value is a set of suffixes to form these table names, assuming a preceding '_'.
|
||||
@ -123,6 +124,23 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
* 'Extension2' => array('suffix2', 'suffix3'),
|
||||
* );
|
||||
*
|
||||
* This can also be manipulated by updating the current loaded config
|
||||
*
|
||||
* SiteTree:
|
||||
* versionableExtensions:
|
||||
* - Extension1:
|
||||
* - suffix1
|
||||
* - suffix2
|
||||
* - Extension2:
|
||||
* - suffix1
|
||||
* - suffix2
|
||||
*
|
||||
* or programatically:
|
||||
*
|
||||
* Config::inst()->update($this->owner->class, 'versionableExtensions',
|
||||
* array('Extension1' => 'suffix1', 'Extension2' => array('suffix2', 'suffix3')));
|
||||
*
|
||||
*
|
||||
* Make sure your extension has a static $enabled-property that determines if it is
|
||||
* processed by Versioned.
|
||||
*
|
||||
@ -387,13 +405,18 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
|
||||
// Build a list of suffixes whose tables need versioning
|
||||
$allSuffixes = array();
|
||||
foreach (Versioned::$versionableExtensions as $versionableExtension => $suffixes) {
|
||||
if ($this->owner->hasExtension($versionableExtension)) {
|
||||
$allSuffixes = array_merge($allSuffixes, (array)$suffixes);
|
||||
foreach ((array)$suffixes as $suffix) {
|
||||
$allSuffixes[$suffix] = $versionableExtension;
|
||||
$versionableExtensions = $this->owner->config()->versionableExtensions;
|
||||
if(count($versionableExtensions)){
|
||||
|
||||
foreach ($versionableExtensions as $versionableExtension => $suffixes) {
|
||||
if ($this->owner->hasExtension($versionableExtension)) {
|
||||
$allSuffixes = array_merge($allSuffixes, (array)$suffixes);
|
||||
foreach ((array)$suffixes as $suffix) {
|
||||
$allSuffixes[$suffix] = $versionableExtension;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Add the default table with an empty suffix to the list (table name = class name)
|
||||
@ -864,12 +887,16 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
* @return string
|
||||
*/
|
||||
public function extendWithSuffix($table) {
|
||||
foreach (Versioned::$versionableExtensions as $versionableExtension => $suffixes) {
|
||||
if ($this->owner->hasExtension($versionableExtension)) {
|
||||
$ext = $this->owner->getExtensionInstance($versionableExtension);
|
||||
$ext->setOwner($this->owner);
|
||||
$table = $ext->extendWithSuffix($table);
|
||||
$ext->clearOwner();
|
||||
$versionableExtensions = $this->owner->config()->versionableExtensions;
|
||||
|
||||
if(count($versionableExtensions)){
|
||||
foreach ($versionableExtensions as $versionableExtension => $suffixes) {
|
||||
if ($this->owner->hasExtension($versionableExtension)) {
|
||||
$ext = $this->owner->getExtensionInstance($versionableExtension);
|
||||
$ext->setOwner($this->owner);
|
||||
$table = $ext->extendWithSuffix($table);
|
||||
$ext->clearOwner();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
3
tests/model/VersionableExtensionsFixtures.yml
Normal file
3
tests/model/VersionableExtensionsFixtures.yml
Normal file
@ -0,0 +1,3 @@
|
||||
VersionableExtensionsTest_DataObject:
|
||||
object:
|
||||
Title: "Test"
|
97
tests/model/VersionableExtensionsTest.php
Normal file
97
tests/model/VersionableExtensionsTest.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* @package framework
|
||||
* @subpackage tests
|
||||
*/
|
||||
|
||||
class VersionableExtensionsTest extends SapphireTest
|
||||
{
|
||||
protected static $fixture_file = 'VersionableExtensionsFixtures.yml';
|
||||
|
||||
protected $requiredExtensions = array(
|
||||
'VersionableExtensionsTest_DataObject' => array('Versioned'),
|
||||
);
|
||||
|
||||
protected $extraDataObjects = array(
|
||||
'VersionableExtensionsTest_DataObject',
|
||||
);
|
||||
|
||||
|
||||
public function setUpOnce()
|
||||
{
|
||||
Config::nest();
|
||||
|
||||
VersionableExtensionsTest_DataObject::add_extension('Versioned');
|
||||
VersionableExtensionsTest_DataObject::add_extension('VersionableExtensionsTest_Extension');
|
||||
|
||||
$cfg = Config::inst();
|
||||
|
||||
$cfg->update('VersionableExtensionsTest_DataObject', 'versionableExtensions', array(
|
||||
'VersionableExtensionsTest_Extension' => array(
|
||||
'test1',
|
||||
'test2',
|
||||
'test3'
|
||||
)
|
||||
));
|
||||
|
||||
parent::setUpOnce();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
public function testTablesAreCreated()
|
||||
{
|
||||
$tables = DB::table_list();
|
||||
|
||||
$check = array(
|
||||
'versionableextensionstest_dataobject_test1_live', 'versionableextensionstest_dataobject_test2_live', 'versionableextensionstest_dataobject_test3_live',
|
||||
'versionableextensionstest_dataobject_test1_versions', 'versionableextensionstest_dataobject_test2_versions', 'versionableextensionstest_dataobject_test3_versions'
|
||||
);
|
||||
|
||||
// Check that the right tables exist
|
||||
foreach ($check as $tableName) {
|
||||
|
||||
$this->assertContains($tableName, array_keys($tables), 'Contains table: '.$tableName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class VersionableExtensionsTest_DataObject extends DataObject implements TestOnly {
|
||||
|
||||
private static $db = array(
|
||||
'Title' => 'Varchar'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
class VersionableExtensionsTest_Extension extends DataExtension implements TestOnly {
|
||||
|
||||
|
||||
public function isVersionedTable($table){
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* fieldsInExtraTables function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $suffix
|
||||
* @return array
|
||||
*/
|
||||
public function fieldsInExtraTables($suffix){
|
||||
$fields = array();
|
||||
//$fields['db'] = DataObject::database_fields($this->owner->class);
|
||||
$fields['indexes'] = $this->owner->databaseIndexes();
|
||||
|
||||
$fields['db'] = array_merge(
|
||||
DataObject::database_fields($this->owner->class)
|
||||
);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user