Skip to content

Commit

Permalink
fix test ; add test ; move "substituable" node to all subdefs (attrib…
Browse files Browse the repository at this point in the history
…ute)
  • Loading branch information
jygaulier committed Sep 25, 2023
1 parent 7379f17 commit f3796cc
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/Alchemy/Phrasea/Controller/Admin/SubdefsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ function changeSubdefsAction(Request $request, $sbas_id) {
$options[Image::OPTION_QUALITY] = $config["image"]["definitions"][$preset][Image::OPTION_QUALITY];
$options[Image::OPTION_ICODEC] = $config["image"]["definitions"][$preset][Image::OPTION_ICODEC];
$options[Image::OPTION_BACKGROUNDCOLOR] = $config["image"]["definitions"][$preset][Image::OPTION_BACKGROUNDCOLOR];
$options[Image::OPTION_SUBSTITUABLE] = $config["image"]["definitions"][$preset][Image::OPTION_SUBSTITUABLE];
foreach ($config["image"]["definitions"][$preset][Subdef::OPTION_DEVICE] as $devices) {
$options[Subdef::OPTION_DEVICE][] = $devices;
}
Expand Down Expand Up @@ -186,6 +185,7 @@ function changeSubdefsAction(Request $request, $sbas_id) {
$class = $request->request->get($post_sub . '_class');
$downloadable = $request->request->get($post_sub . '_downloadable');
$orderable = $request->request->get($post_sub . '_orderable');
$substituable = $request->request->get($post_sub . '_substituable');
$toBuild = $request->request->get($post_sub . '_tobuild');

$defaults = ['path', 'meta', 'mediatype'];
Expand All @@ -212,7 +212,7 @@ function changeSubdefsAction(Request $request, $sbas_id) {
}

$labels = $request->request->get($post_sub . '_label', []);
$subdefs->set_subdef($group, $name, $class, $downloadable, $options, $labels, $orderable, $preset, $toBuild);
$subdefs->set_subdef($group, $name, $class, $downloadable, $options, $labels, $orderable, $preset, $toBuild, $substituable);
}
}

Expand Down
2 changes: 0 additions & 2 deletions lib/Alchemy/Phrasea/Media/Subdef/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Image extends Provider
const OPTION_WATERMARKTEXT = 'watermarktext';
const OPTION_WATERMARKRID = 'watermarkrid';
const OPTION_BACKGROUNDCOLOR = 'backgroundcolor';
const OPTION_SUBSTITUABLE = 'substituable';

protected $options = [];

Expand All @@ -44,7 +43,6 @@ public function __construct(TranslatorInterface $translator)
$this->registerOption(new OptionType\Text($this->translator->trans('Watermark text'), self::OPTION_WATERMARKTEXT, ''));
$this->registerOption(new OptionType\Text($this->translator->trans('Watermark Record_id'), self::OPTION_WATERMARKRID, ''));
$this->registerOption(new OptionType\Text($this->translator->trans('Background Color'), self::OPTION_BACKGROUNDCOLOR, ''));
$this->registerOption(new OptionType\Boolean($this->translator->trans('Substituable'), self::OPTION_SUBSTITUABLE, false));
}

public function getType()
Expand Down
7 changes: 2 additions & 5 deletions lib/classes/databox/subdef.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public function __construct(SubdefType $type, SimpleXMLElement $sd, TranslatorIn
}
$this->name = strtolower($sd->attributes()->name);
$this->downloadable = p4field::isyes($sd->attributes()->downloadable);
$this->substituable = p4field::isyes($sd->substituable);
$this->orderable = isset($sd->attributes()->orderable) && p4field::isyes($sd->attributes()->orderable);
$this->substituable = isset($sd->attributes()->substituable) && p4field::isyes($sd->attributes()->substituable);
$this->orderable = !isset($sd->attributes()->orderable) || p4field::isyes($sd->attributes()->orderable);
$this->tobuild = !isset($sd->attributes()->tobuild) || p4field::isyes($sd->attributes()->tobuild);
$this->path = trim($sd->path) !== '' ? p4string::addEndSlash(trim($sd->path)) : '';
$this->preset = $sd->attributes()->presets;
Expand Down Expand Up @@ -165,9 +165,6 @@ protected function buildImageSubdef(SimpleXMLElement $sd)
if ($sd->backgroundcolor) {
$image->setOptionValue(Image::OPTION_BACKGROUNDCOLOR, $sd->backgroundcolor);
}
if ($sd->substituable) {
$image->setOptionValue(Image::OPTION_SUBSTITUABLE, p4field::isyes($sd->substituable));
}
return $image;
}
/**
Expand Down
3 changes: 2 additions & 1 deletion lib/classes/databox/subdefsStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function add_subdef($groupname, $name, $class, $mediatype, $preset, $path
* @return databox_subdefsStructure
* @throws Exception
*/
public function set_subdef($group, $name, $class, $downloadable, $options, $labels, $orderable = true, $preset = "Custom", $toBuild = true)
public function set_subdef($group, $name, $class, $downloadable, $options, $labels, $orderable = true, $preset = "Custom", $toBuild = true, $substituable = false)
{
$dom_struct = $this->databox->get_dom_structure();

Expand All @@ -256,6 +256,7 @@ public function set_subdef($group, $name, $class, $downloadable, $options, $labe
$subdef->setAttribute('downloadable', ($downloadable ? 'true' : 'false'));
$subdef->setAttribute('orderable', ($orderable ? 'true' : 'false'));
$subdef->setAttribute('tobuild', ($toBuild ? 'true' : 'false'));
$subdef->setAttribute('substituable', ($substituable ? 'true' : 'false'));
$subdef->setAttribute('presets', $preset);

foreach ($labels as $code => $label) {
Expand Down
32 changes: 7 additions & 25 deletions lib/classes/patch/418RC7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Core\Configuration\PropertyAccess;
use Alchemy\Phrasea\Databox\SubdefGroup;

class patch_418RC7 implements patchInterface
{
Expand Down Expand Up @@ -61,43 +60,26 @@ public function apply(base $base, Application $app)

private $thumbSubstitution = null;

private function patch_databox(databox $databox, Application $app)
public function patch_databox(databox $databox, Application $app)
{
/** @var PropertyAccess $conf */
$conf = $app['conf'];

if($this->thumbSubstitution === null) {
if ($this->thumbSubstitution === null) {
// first db
$this->thumbSubstitution = $conf->get(['registry', 'modules', 'thumb-substitution']);
$conf->remove(['registry', 'modules', 'thumb-substitution']);
}

if ($this->thumbSubstitution) {
$subdefStructure = $databox->get_subdef_structure();
$dom_struct = $databox->get_dom_structure();
$dom_xp = $databox->get_xpath_structure();

/** @var SubdefGroup $sdGroup */
foreach ($subdefStructure as $sdGroup) {
$group = $sdGroup->getName();

$nodes = $dom_xp->query('//record/subdefs/subdefgroup[@name="' . $group . '"]/subdef[@name="thumbnail"]');
if ($nodes->length > 0) {
$dom_sd = $nodes->item(0);

while (true) {
$nodes = $dom_xp->query('substituable', $dom_sd);
if ($nodes->length === 0) {
break;
}
$dom_sd->removeChild($nodes->item(0));
}
$dom_sd->appendChild(
$dom_struct->createElement('substituable')
)->appendChild(
$dom_struct->createTextNode('yes')
);
}
$nodes = $dom_xp->query('//record/subdefs/subdefgroup/subdef[@name="thumbnail"]');
for ($i = 0; $i < $nodes->length; $i++) {
/** @var \DOMElement $node */
$node = $nodes->item($i);
$node->setAttribute('substituable', 'true');
}

$databox->saveStructure($dom_struct);
Expand Down
Empty file modified logs/.gitkeep
100644 → 100755
Empty file.
7 changes: 7 additions & 0 deletions templates/web/admin/subdefs.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@
</td>
<td></td>
</tr>
<tr>
<td>{{ 'subdef.substituable' | trans }}</td>
<td><input type="checkbox" name="{{ subdefgroup }}_{{ subdefname }}_substituable"
{% if subdef.isSubstituable() %}checked="checked"{% endif %} value="1"/>
</td>
<td></td>
</tr>
<tr>
<td>
{{ 'classe' | trans }}
Expand Down
37 changes: 37 additions & 0 deletions tests/classes/databox/subdefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,43 @@ public function providesOrderableStatuses()
];
}

/**
* @param bool $expected
* @param null|string $configValue
* @dataProvider providesSubstituableStatuses
*/
public function testSubstituableStatus($expected, $configValue, $message)
{
$xmlTemplate = <<<'EOF'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<subdef class="thumbnail" name="gifou" downloadable="false">
<path>/home/datas/noweb/db_alch_phrasea/video/</path>
<mediatype>image</mediatype>
%s
</subdef>
EOF;

if (null !== $configValue) {
$configValue = ' orderable="' . $configValue . '"';
}

$xml = sprintf($xmlTemplate, $configValue ?: '');

$sut = new databox_subdef(new Type\Image(), simplexml_load_string($xml), $this->translator);

$this->assertSame($expected, $sut->isSubstituable(), $message);
}

public function providesSubstituableStatuses()
{
return [
[false, '', 'No substituable Status set should defaults to true'],
[false, '<substituable></substituable>', 'substituable should default to false'],
[true, '<substituable>yes</substituable>', 'substituable should be true'],
[false, '<substituable>no</substituable>', 'substituable should be false'],
];
}

/**
* @return PHPUnit_Framework_MockObject_MockObject|TranslatorInterface
*/
Expand Down

0 comments on commit f3796cc

Please sign in to comment.