mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge branch '4.8' into 4
This commit is contained in:
commit
f6e8d6e591
@ -3155,7 +3155,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
return $value;
|
||||
}
|
||||
|
||||
list($class, $spec) = explode('.', $helper);
|
||||
$pos = strpos($helper, '.');
|
||||
$class = substr($helper, 0, $pos);
|
||||
$spec = substr($helper, $pos + 1);
|
||||
|
||||
/** @var DBField $obj */
|
||||
$table = $schema->tableName($class);
|
||||
$obj = Injector::inst()->create($spec, $fieldName);
|
||||
|
@ -20,6 +20,15 @@ class DBInt extends DBField
|
||||
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”.
|
||||
*/
|
||||
|
@ -61,9 +61,13 @@ class EmbedShortcodeProvider implements ShortcodeHandler
|
||||
return '';
|
||||
}
|
||||
|
||||
$class = $arguments['class'] ?? '';
|
||||
$width = $arguments['width'] ?? '';
|
||||
$height = $arguments['height'] ?? '';
|
||||
|
||||
// Try to use cached result
|
||||
$cache = static::getCache();
|
||||
$key = static::deriveCacheKey($serviceURL);
|
||||
$key = static::deriveCacheKey($serviceURL, $class, $width, $height);
|
||||
try {
|
||||
if ($cache->has($key)) {
|
||||
return $cache->get($key);
|
||||
@ -273,11 +277,14 @@ class EmbedShortcodeProvider implements ShortcodeHandler
|
||||
if (!isset($tag['open']) || $tag['open'] != 'embed') {
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
$key = static::deriveCacheKey($url);
|
||||
$key = static::deriveCacheKey($url, $class, $width, $height);
|
||||
try {
|
||||
if (!$cache->has($key)) {
|
||||
continue;
|
||||
@ -301,9 +308,23 @@ class EmbedShortcodeProvider implements ShortcodeHandler
|
||||
* @param string $url
|
||||
* @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 $key;
|
||||
return implode('-', array_filter([
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
18
tests/php/ORM/DBIntTest.php
Normal file
18
tests/php/ORM/DBIntTest.php
Normal 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());
|
||||
}
|
||||
}
|
@ -2622,4 +2622,15 @@ class DataObjectTest extends SapphireTest
|
||||
'Salary' => 50,
|
||||
], 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());
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,10 @@ class Fixture extends DataObject implements TestOnly
|
||||
'MyInt' => 'Int',
|
||||
'MyCurrency' => 'Currency',
|
||||
'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 = [
|
||||
|
@ -139,10 +139,13 @@ EOS
|
||||
$cache = $method->invokeArgs($provider, []);
|
||||
$method = $reflector->getMethod('deriveCacheKey');
|
||||
$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
|
||||
$this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123', $key);
|
||||
$this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123-leftAloness-htmleditorfield-fileembed-480-270', $key);
|
||||
$cache->set($key, $embedHtml);
|
||||
$this->assertTrue($cache->has($key));
|
||||
EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);
|
||||
|
Loading…
Reference in New Issue
Block a user