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

Adjust ImageForm logic to support new behaviour to be introduce in silverstripe/assets 1.6 #78

Merged
merged 1 commit into from
May 6, 2020
Merged
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test against the old form layout

- php: 7.4
env:
- ASSET_VERSION=1.x-dev
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test against the new layout


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/assets $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.');
}
}