Skip to content

Commit 2b7da5e

Browse files
authored
Merge pull request #2 from nswdpc/cherrypick-improvements
Cherrypick improvements
2 parents 8e6f70b + 8f23f8a commit 2b7da5e

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,15 @@
2727
"require-dev": {
2828
"phpunit/phpunit": "^9.5",
2929
"friendsofphp/php-cs-fixer": "^3"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"NSWDPC\\Elemental\\Models\\Image\\Tests\\": [
34+
"tests/"
35+
],
36+
"NSWDPC\\Elemental\\Models\\Image\\": [
37+
"src/Models/Elements/"
38+
]
39+
}
3040
}
3141
}
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ class ElementImage extends BaseElement
2424

2525
private static $allowed_file_types = ["jpg", "jpeg", "gif", "png", "webp"];
2626

27+
const WIDTH_FULL = 'full';
28+
const WIDTH_CONTAINER = 'container';
29+
30+
const HEIGHT_SMALL = 'small';
31+
const HEIGHT_MEDIUM = 'medium';
32+
const HEIGHT_LARGE = 'large';
33+
const HEIGHT_ORIGINAL = 'original';
34+
2735
public function getType()
2836
{
2937
return _t(__CLASS__ . ".BlockType", "Image");
@@ -65,18 +73,18 @@ public function getCMSFields()
6573
"Width",
6674
_t(__CLASS__ . ".WIDTH", "Width"),
6775
[
68-
"container" => _t(__CLASS__ . ".CONTAINER_WIDTH", "Content width"),
69-
"full" => _t(__CLASS__ . ".BROWSER_WIDTH", "Browser width")
76+
self::WIDTH_CONTAINER => _t(__CLASS__ . ".CONTAINER_WIDTH", "Content width"),
77+
self::WIDTH_FULL => _t(__CLASS__ . ".BROWSER_WIDTH", "Browser width")
7078
]
7179
),
7280
DropdownField::create(
7381
"Height",
7482
_t(__CLASS__ . ".HEIGHT", "Height"),
7583
[
76-
"small" => _t(__CLASS__ . ".HEIGHT_SMALL", "Small"),
77-
"medium" => _t(__CLASS__ . ".HEIGHT_MEDIUM", "Medium"),
78-
"large" => _t(__CLASS__ . ".HEIGHT_LARGE", "Large"),
79-
"original" => _t(__CLASS__ . ".HEIGHT_ORIGINAL", "Original")
84+
self::HEIGHT_SMALL => _t(__CLASS__ . ".HEIGHT_SMALL", "Small"),
85+
self::HEIGHT_MEDIUM => _t(__CLASS__ . ".HEIGHT_MEDIUM", "Medium"),
86+
self::HEIGHT_LARGE => _t(__CLASS__ . ".HEIGHT_LARGE", "Large"),
87+
self::HEIGHT_ORIGINAL => _t(__CLASS__ . ".HEIGHT_ORIGINAL", "Original")
8088
]
8189
),
8290
UploadField::create(
@@ -96,7 +104,7 @@ public function getCMSFields()
96104
),
97105
TextareaField::create(
98106
'Caption',
99-
'Caption'
107+
_t(__CLASS__ . ".CAPTION", "Caption")
100108
)
101109
]);
102110
});
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
<img src="{$Image.URL}" alt="<% if $Title %>{$Title.XML}<% end_if %>">
1+
<% if $Image %>
2+
<% if $ShowTitle %>
3+
<h2>$Title</h2>
4+
<% end_if %>
5+
<div class="image">
6+
{$Image}
7+
</div>
8+
<% if $Caption %>
9+
<p class="caption">{$Caption}</p>
10+
<% end_if %>
11+
<% end_if %>

tests/ElementImageTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace NSWDPC\Elemental\Models\Image\Tests;
4+
5+
use SilverStripe\Dev\SapphireTest;
6+
use SilverStripe\Core\Config\Config;
7+
use NSWDPC\Elemental\Models\Image\ElementImage;
8+
9+
class ElementImageTest extends SapphireTest
10+
{
11+
12+
protected $usesDatabase = true;
13+
14+
public function testAllowedFileTypes()
15+
{
16+
$allowed = ["jpg", "jpeg"];
17+
Config::modify()->set(
18+
ElementImage::class,
19+
'allowed_file_types',
20+
$allowed
21+
);
22+
$element = ElementImage::create();
23+
$this->assertEquals( $allowed, $element->getAllowedFileTypes() );
24+
}
25+
26+
public function testDimensions()
27+
{
28+
$width = ElementImage::WIDTH_CONTAINER;
29+
$height = ElementImage::HEIGHT_LARGE;
30+
$element = ElementImage::create([
31+
'Width' => $width,
32+
'Height' => $height
33+
]);
34+
$this->assertEquals( $width, $element->Width );
35+
$this->assertEquals( $height, $element->Height );
36+
}
37+
38+
public function testCaption()
39+
{
40+
$caption = 'Test caption';
41+
$element = ElementImage::create([
42+
'Caption' => $caption
43+
]);
44+
$this->assertEquals( $caption, $element->Caption );
45+
}
46+
47+
}

0 commit comments

Comments
 (0)