ENH Make the write call of StringTagField::saveInto method configurable (#208)

* Make the immediate write call of StringTagField::saveInto method configurable
* Set immediate_write_enabled option in the class instead of config; add deprecation notice
* Add tests for the immediate write option
This commit is contained in:
Daniel Kliemsch 2022-04-04 10:27:49 +02:00 committed by GitHub
parent 8477911890
commit 0a04e35825
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -33,6 +33,13 @@ class StringTagField extends DropdownField
'suggest',
];
/**
* @var bool Triggers a write call within the saveInto function if enabled
*
* @deprecated 3.0.0
*/
private static $immediate_write_enabled = true;
/**
* @var bool
*/
@ -273,7 +280,10 @@ class StringTagField extends DropdownField
$name = $this->getName();
$record->$name = $this->dataValue();
$record->write();
if (self::config()->get('immediate_write_enabled')) {
$record->write();
}
}
/**

View File

@ -61,6 +61,38 @@ class StringTagFieldTest extends SapphireTest
$this->assertEquals('Tag1,Tag2', $record->Tags);
}
public function testImmediateWriteEnabled()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
$record->write();
StringTagField::config()->update('immediate_write_enabled', true);
$field = new StringTagField('Tags');
$field->setValue(['Tag1', 'Tag2']);
$field->saveInto($record);
$this->assertEquals('Tag1,Tag2', StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags);
}
public function testImmediateWriteDisabled()
{
$record = $this->getNewStringTagFieldTestBlogPost('BlogPost1');
$record->write();
StringTagField::config()->update('immediate_write_enabled', false);
$field = new StringTagField('Tags');
$field->setValue(['Tag1', 'Tag2']);
$field->saveInto($record);
$this->assertNull(StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags);
$record->write();
$this->assertEquals('Tag1,Tag2', StringTagFieldTestBlogPost::get()->byID($record->ID)->Tags);
}
public function testItSuggestsTags()
{
$field = new StringTagField('SomeField', 'Some field', ['Tag1', 'Tag2'], []);