Merge branch '4.8' into 4

This commit is contained in:
Steve Boyd 2021-07-07 14:03:02 +12:00
commit f6e8d6e591
7 changed files with 78 additions and 9 deletions

View File

@ -3155,7 +3155,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
return $value; return $value;
} }
list($class, $spec) = explode('.', $helper); $pos = strpos($helper, '.');
$class = substr($helper, 0, $pos);
$spec = substr($helper, $pos + 1);
/** @var DBField $obj */ /** @var DBField $obj */
$table = $schema->tableName($class); $table = $schema->tableName($class);
$obj = Injector::inst()->create($spec, $fieldName); $obj = Injector::inst()->create($spec, $fieldName);

View File

@ -20,6 +20,15 @@ class DBInt extends DBField
parent::__construct($name); parent::__construct($name);
} }
/**
* Ensure int values are always returned.
* This is for mis-configured databases that return strings.
*/
public function getValue()
{
return (int) $this->value;
}
/** /**
* Returns the number, with commas added as appropriate, eg “1,000. * Returns the number, with commas added as appropriate, eg “1,000.
*/ */

View File

@ -61,9 +61,13 @@ class EmbedShortcodeProvider implements ShortcodeHandler
return ''; return '';
} }
$class = $arguments['class'] ?? '';
$width = $arguments['width'] ?? '';
$height = $arguments['height'] ?? '';
// Try to use cached result // Try to use cached result
$cache = static::getCache(); $cache = static::getCache();
$key = static::deriveCacheKey($serviceURL); $key = static::deriveCacheKey($serviceURL, $class, $width, $height);
try { try {
if ($cache->has($key)) { if ($cache->has($key)) {
return $cache->get($key); return $cache->get($key);
@ -273,11 +277,14 @@ class EmbedShortcodeProvider implements ShortcodeHandler
if (!isset($tag['open']) || $tag['open'] != 'embed') { if (!isset($tag['open']) || $tag['open'] != 'embed') {
continue; continue;
} }
$url = $tag['content'] ?? $tag['attrs']['url'] ?? null; $url = $tag['content'] ?? $tag['attrs']['url'] ?? '';
$class = $tag['attrs']['class'] ?? '';
$width = $tag['attrs']['width'] ?? '';
$height = $tag['attrs']['height'] ?? '';
if (!$url) { if (!$url) {
continue; continue;
} }
$key = static::deriveCacheKey($url); $key = static::deriveCacheKey($url, $class, $width, $height);
try { try {
if (!$cache->has($key)) { if (!$cache->has($key)) {
continue; continue;
@ -301,9 +308,23 @@ class EmbedShortcodeProvider implements ShortcodeHandler
* @param string $url * @param string $url
* @return string * @return string
*/ */
private static function deriveCacheKey(string $url): string private static function deriveCacheKey(string $url, string $class, string $width, string $height): string
{ {
$key = 'embed-shortcode-' . preg_replace('/[^a-zA-Z0-9\-]/', '', $url); return implode('-', array_filter([
return $key; 'embed-shortcode',
self::cleanKeySegment($url),
self::cleanKeySegment($class),
self::cleanKeySegment($width),
self::cleanKeySegment($height)
]));
}
/**
* @param string $str
* @return string
*/
private static function cleanKeySegment(string $str): string
{
return preg_replace('/[^a-zA-Z0-9\-]/', '', $str);
} }
} }

View File

@ -0,0 +1,18 @@
<?php
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBInt;
class DBIntTest extends SapphireTest
{
public function testGetValueCastToInt()
{
$field = DBInt::create('MyField');
$field->setValue(3);
$this->assertSame(3, $field->getValue());
$field->setValue('3');
$this->assertSame(3, $field->getValue());
}
}

View File

@ -2622,4 +2622,15 @@ class DataObjectTest extends SapphireTest
'Salary' => 50, 'Salary' => 50,
], DataObject::CREATE_HYDRATED); ], DataObject::CREATE_HYDRATED);
} }
public function testDBObjectEnum()
{
$obj = new DataObjectTest\Fixture();
// enums are parsed correctly
$vals = ['25', '50', '75', '100'];
$this->assertSame(array_combine($vals, $vals), $obj->dbObject('MyEnum')->enumValues());
// enum with dots in their values are also parsed correctly
$vals = ['25.25', '50.00', '75.00', '100.50'];
$this->assertSame(array_combine($vals, $vals), $obj->dbObject('MyEnumWithDots')->enumValues());
}
} }

View File

@ -25,6 +25,10 @@ class Fixture extends DataObject implements TestOnly
'MyInt' => 'Int', 'MyInt' => 'Int',
'MyCurrency' => 'Currency', 'MyCurrency' => 'Currency',
'MyDecimal'=> 'Decimal', 'MyDecimal'=> 'Decimal',
// Enums
'MyEnum' => 'Enum("25,50,75,100", "50")',
'MyEnumWithDots' => 'Enum("25.25,50.00,75.00,100.50", "50.00")',
]; ];
private static $defaults = [ private static $defaults = [

View File

@ -139,10 +139,13 @@ EOS
$cache = $method->invokeArgs($provider, []); $cache = $method->invokeArgs($provider, []);
$method = $reflector->getMethod('deriveCacheKey'); $method = $reflector->getMethod('deriveCacheKey');
$method->setAccessible(true); $method->setAccessible(true);
$key = $method->invokeArgs($provider, [$url]); $class = 'leftAlone ss-htmleditorfield-file embed';
$width = '480';
$height = '270';
$key = $method->invokeArgs($provider, [$url, $class, $width, $height]);
// assertions // assertions
$this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123', $key); $this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123-leftAloness-htmleditorfield-fileembed-480-270', $key);
$cache->set($key, $embedHtml); $cache->set($key, $embedHtml);
$this->assertTrue($cache->has($key)); $this->assertTrue($cache->has($key));
EmbedShortcodeProvider::flushCachedShortcodes($parser, $content); EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);