diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index e67fc8201..5d9732e73 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -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); diff --git a/src/ORM/FieldType/DBInt.php b/src/ORM/FieldType/DBInt.php index f7863c813..eb283f4e9 100644 --- a/src/ORM/FieldType/DBInt.php +++ b/src/ORM/FieldType/DBInt.php @@ -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”. */ diff --git a/src/View/Shortcodes/EmbedShortcodeProvider.php b/src/View/Shortcodes/EmbedShortcodeProvider.php index 031538b31..ab0eb976b 100644 --- a/src/View/Shortcodes/EmbedShortcodeProvider.php +++ b/src/View/Shortcodes/EmbedShortcodeProvider.php @@ -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); } } diff --git a/tests/php/ORM/DBIntTest.php b/tests/php/ORM/DBIntTest.php new file mode 100644 index 000000000..554d80232 --- /dev/null +++ b/tests/php/ORM/DBIntTest.php @@ -0,0 +1,18 @@ +setValue(3); + $this->assertSame(3, $field->getValue()); + $field->setValue('3'); + $this->assertSame(3, $field->getValue()); + } +} diff --git a/tests/php/ORM/DataObjectTest.php b/tests/php/ORM/DataObjectTest.php index 92c7b142f..8ad2cc166 100644 --- a/tests/php/ORM/DataObjectTest.php +++ b/tests/php/ORM/DataObjectTest.php @@ -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()); + } } diff --git a/tests/php/ORM/DataObjectTest/Fixture.php b/tests/php/ORM/DataObjectTest/Fixture.php index 96fc7e664..0ad04794b 100644 --- a/tests/php/ORM/DataObjectTest/Fixture.php +++ b/tests/php/ORM/DataObjectTest/Fixture.php @@ -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 = [ diff --git a/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php b/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php index b7d38eb1a..d6e877a75 100644 --- a/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php +++ b/tests/php/View/Shortcodes/EmbedShortcodeProviderTest.php @@ -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);