Skip to content

Commit

Permalink
Adjust ImageForm logic to support new behaviour to be introduce in si…
Browse files Browse the repository at this point in the history
…lverstripe/assets 1.6
  • Loading branch information
Maxime Rainville committed Mar 2, 2020
1 parent 847f479 commit b2f1665
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 11 deletions.
24 changes: 19 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@

language: php
sudo: false
dist: trusty
dist: xenial

services:
- mysql
- postgresql

env:
global:
- COMPOSER_ROOT_VERSION=3.0.x-dev
- COMPOSER_ROOT_VERSION=3.x-dev
- SS_ENVIRONMENT_TYPE="dev"
- DB=MYSQL

matrix:
include:
- php: 5.6
env:
- DB=MYSQL
- ASSET_VERSION=1.4.x-dev
- php: 7.1
env:
- DB=PGSQL
- php: 7.3
env:
- ASSET_VERSION=1.5.x-dev
- php: 7.4
env:
- ASSET_VERSION=1.x-dev

before_script:
# Init PHP
Expand All @@ -27,8 +38,11 @@ before_script:
# Install composer dependencies
- export PATH=~/.composer/vendor/bin:$PATH
- composer validate
- composer install --prefer-source
- if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:^2.2 --prefer-source; fi
- composer require silverstripe/asset $ASSET_VERSION --no-update
- composer require silverstripe/asset-admin $ASSET_VERSION --no-update
- if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:^2.2 --no-update; fi
- composer update --prefer-source


script:
- vendor/bin/phpunit
16 changes: 10 additions & 6 deletions src/Extensions/FocusPointAssetFormFactoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ public function updateFormFields(FieldList $fields, $controller, $formName, $con
$image = isset($context['Record']) ? $context['Record'] : null;
if ($image && $image->appCategory() === 'image') {
$fpField = FocusPointField::create('FocusPoint', $image->fieldLabel('FocusPoint'), $image);
$titleField = $fields->dataFieldByName('Title');
if ($titleField && $titleField->isReadonly()) $fpField = $fpField->performReadonlyTransformation();
$fields->insertAfter(
'Title',
$fpField
);

$titleField = $fields->fieldByName('Editor.Details.Title');
if ($titleField) {
if ($titleField->isReadonly()) $fpField = $fpField->performReadonlyTransformation();
$fields->insertAfter(
'Title',
$fpField
);
}

}
}
}
91 changes: 91 additions & 0 deletions tests/Extensions/FocusPointAssetFormFactoryExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace JonoM\FocusPoint\Tests\Extensions;


use JonoM\FocusPoint\Extensions\FocusPointAssetFormFactoryExtension;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;

class FocusPointAssetFormFactoryExtensionTest extends SapphireTest
{

protected static $fixture_file = '../ImageManipulationTest.yml';

public function testUpdateFormFieldsOnImageEditForm()
{
$ext = new FocusPointAssetFormFactoryExtension();

$fields = FieldList::create(
TabSet::create(
'Editor',
Tab::create(
'Details',
TextField::create('Title')
)
)
);
$controller = new Controller();
$formName = 'fileEditForm';
$context = [
'Record' => $this->objFromFixture(Image::class, 'pngLeftTop')
];


$ext->updateFormFields($fields, $controller, $formName, $context);

$focusField = $fields->fieldByName('Editor.Details.FocusPoint');
$this->assertNotEmpty($focusField, 'Focus field has been added to image edit form.');
}

public function testUpdateFormFieldsOnPlacementForm()
{
$ext = new FocusPointAssetFormFactoryExtension();

$fields = FieldList::create(
TextField::create('Title')
);
$controller = new Controller();
$formName = 'fileEditForm';
$context = [
'Record' => $this->objFromFixture(Image::class, 'pngLeftTop')
];


$ext->updateFormFields($fields, $controller, $formName, $context);

$focusField = $fields->fieldByName('Editor.Details.FocusPoint');
$this->assertEmpty($focusField, 'Focus field has NOT been added to the form.');
}

public function testUpdateFormFieldsOnNonImageForm()
{
$ext = new FocusPointAssetFormFactoryExtension();

$fields = FieldList::create(
TabSet::create(
'Editor',
Tab::create(
'Details',
TextField::create('Title')
)
)
);
$controller = new Controller();
$formName = 'fileEditForm';
$context = [
'Record' => $this->objFromFixture(Folder::class, 'folder1')
];

$ext->updateFormFields($fields, $controller, $formName, $context);

$focusField = $fields->fieldByName('Editor.Details.FocusPoint');
$this->assertEmpty($focusField, 'Focus field has NOT been added to the form.');
}
}

0 comments on commit b2f1665

Please sign in to comment.