Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlideImage's Image is no longer required all the time #155

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/SlideImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class SlideImage extends DataObject implements PermissionProvider
*/
private static $versioned_gridfield_extensions = true;

/**
* @var bool
*/
private static $require_image = true;

/**
* @var array
*/
Expand Down Expand Up @@ -179,7 +184,7 @@ public function validate()
);
}

if (!$this->ImageID) {
if (!$this->ImageID && $this->getRequiresImage()) {
$result->addError(
_t(__CLASS__ . '.IMAGE_REQUIRED', 'An Image is required before you can save')
);
Expand All @@ -188,6 +193,24 @@ public function validate()
return $result;
}

/**
* @return bool
*/
public function getRequiresImage() {
$require = $this->config()->require_image;

// default realtion to \Page class
foreach ($this->hasOne() as $relation => $type) {
// strict check due to null equating to false otherwise
if ($this->{$relation}()->config()->require_image === false) {
$require = false;
}
}

$this->extend('updateRequiresImage', $require);
return $require;
}

/**
* @return array
*/
Expand Down
60 changes: 56 additions & 4 deletions tests/SlideImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Dynamic\FlexSlider\Model\SlideImage;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Member;

class SlideImageTest extends SapphireTest
Expand All @@ -16,6 +18,13 @@ class SlideImageTest extends SapphireTest
*/
protected static $fixture_file = 'fixtures.yml';

/**
* @var array
*/
protected static $extra_dataobjects = [
TestPage::class,
];

/**
*
*/
Expand All @@ -35,7 +44,7 @@ public function testValidateName()
{
$object = $this->objFromFixture(SlideImage::class, 'slide1');
$object->Name = '';
$this->setExpectedException(ValidationException::class);
$this->expectException(ValidationException::class);
$object->write();
}

Expand All @@ -44,10 +53,53 @@ public function testValidateName()
*/
public function testValidateImage()
{
/** @var SlideImage $object */
$object = $this->objFromFixture(SlideImage::class, 'slide1');
$object->ImageID = '';
$this->setExpectedException(ValidationException::class);
$object->write();
$object->Name = 'Test';
$object->ImageID = 0;

// test valid if require_image is default(true) on SlideImage
$object->config()->set('require_image', true);
$result = $object->validate();
$this->assertInstanceOf(ValidationResult::class, $result);
$this->assertFalse($result->isValid());
$this->assertContains([
'message' => 'An Image is required before you can save',
'fieldName' => null,
'messageType' => 'error',
'messageCast' => 'text',
], $result->getMessages());

// test valid if require_image is false on SlideImage
$object->config()->set('require_image', false);
$result = $object->validate();
$this->assertInstanceOf(ValidationResult::class, $result);
$this->assertTrue($result->isValid());

// TestPage init
/** @var \Dynamic\FlexSlider\Test\TestPage $page */
$page = Injector::inst()->get(TestPage::class);
$object->PageID = $page->ID;

// test valid if require_image is true on TestPage
$object->config()->set('require_image', true);
$page->config()->set('require_image', false);
$result = $object->validate();
$this->assertInstanceOf(ValidationResult::class, $result);
$this->assertTrue($result->isValid());

// test valid if require_image is false on TestPage
$object->config()->set('require_image', true);
$page->config()->set('require_image', true);
$result = $object->validate();
$this->assertInstanceOf(ValidationResult::class, $result);
$this->assertFalse($result->isValid());
$this->assertContains([
'message' => 'An Image is required before you can save',
'fieldName' => null,
'messageType' => 'error',
'messageCast' => 'text',
], $result->getMessages());
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SilverStripe\Security\Group:
Title: Administrators
content_authors:
Title: "Content Authors"

SilverStripe\Security\Permission:
administrators:
Code: ADMIN
Expand All @@ -20,6 +21,7 @@ SilverStripe\Security\Permission:
Code: Slide_CREATE
Type: 1
Group: =>SilverStripe\Security\Group.content_authors

SilverStripe\Security\Member:
admin:
FirstName: Default
Expand All @@ -35,14 +37,16 @@ SilverStripe\Security\Member:
FirstName: Default
Surname: Member
Email: member

Dynamic\FlexSlider\Model\SlideImage:
slide1:
Name: 'Hello World'
Description: 'What a fine place it is too!'
slide2:
Name: 'Goodbye World'
Description: 'It was fun while it lasted!'

SilverStripe\Assets\Image:
image1:
Name: Test Image
Filename: test.jpg
Filename: test.jpg