Skip to content

Commit

Permalink
fix norm data when submit content form
Browse files Browse the repository at this point in the history
  • Loading branch information
alavieille committed Jun 16, 2016
1 parent d1bdc91 commit 15834b8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
21 changes: 9 additions & 12 deletions Backoffice/EventSubscriber/ContentTypeSubscriber.php
Expand Up @@ -8,6 +8,7 @@
use OpenOrchestra\ModelInterface\Model\ContentTypeInterface;
use OpenOrchestra\ModelInterface\Model\FieldTypeInterface;
use OpenOrchestra\ModelInterface\Repository\ContentTypeRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
Expand All @@ -20,7 +21,7 @@
/**
* Class ContentTypeSubscriber
*/
class ContentTypeSubscriber extends AbstractModulableTypeSubscriber
class ContentTypeSubscriber implements EventSubscriberInterface
{
protected $translationChoiceManager;
protected $contentTypeRepository;
Expand Down Expand Up @@ -59,9 +60,10 @@ public function __construct(
*/
public static function getSubscribedEvents()
{
return array_merge(
parent::getSubscribedEvents(),
array(FormEvents::POST_SET_DATA => 'postSetData')
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::POST_SET_DATA => 'postSetData',
FormEvents::POST_SUBMIT => 'postSubmit',
);
}

Expand Down Expand Up @@ -107,30 +109,25 @@ public function postSetData(FormEvent $event)
/**
* @param FormEvent $event
*/
public function preSubmit(FormEvent $event)
public function postSubmit(FormEvent $event)
{
$form = $event->getForm();
$content = $form->getData();
$contentType = $this->contentTypeRepository->findOneByContentTypeIdInLastVersion($content->getContentType());

if ($contentType instanceof ContentTypeInterface) {
$data = $event->getData();
$content->setContentTypeVersion($contentType->getVersion());
foreach ($contentType->getFields() as $contentTypeField) {
$contentTypeFieldId = $contentTypeField->getFieldId();
$data[$contentTypeFieldId] = isset($data[$contentTypeFieldId]) ? $data[$contentTypeFieldId] : null;
try {
$value = $this->transformData($data[$contentTypeFieldId], $form->get($contentTypeFieldId));
} catch (TransformationFailedException $e) {
}

$value = $form->get($contentTypeFieldId)->getData();
$attribute = $content->getAttributeByName($contentTypeFieldId);
if (is_null($attribute)) {
/** @var ContentAttributeInterface $attribute */
$attribute = new $this->contentAttributeClass();
$attribute->setName($contentTypeFieldId);
$content->addAttribute($attribute);
}

$attribute->setValue($value);
$attribute->setType($contentTypeField->getType());
$attribute->setStringValue($this->valueTransformerManager->transform($attribute->getType(), $value));
Expand Down
Expand Up @@ -42,7 +42,7 @@ public function transform($arrayChoices)
public function reverseTransform($choices)
{
if (null === $choices || '' === trim($choices)) {
return array();
return null;
}

$choices = explode(',', $choices);
Expand Down
16 changes: 11 additions & 5 deletions Backoffice/Tests/EventSubscriber/ContentTypeSubscriberTest.php
Expand Up @@ -20,6 +20,7 @@ class ContentTypeSubscriberTest extends AbstractBaseTestCase
protected $subscriber;

protected $form;
protected $subForm;
protected $event;
protected $content;
protected $collection;
Expand Down Expand Up @@ -66,7 +67,8 @@ public function setUp()
Phake::when($this->formConfig)->getModelTransformers()->thenReturn(array());
Phake::when($this->formConfig)->getViewTransformers()->thenReturn(array());
$this->form = Phake::mock('Symfony\Component\Form\Form');
Phake::when($this->form)->get(Phake::anyParameters())->thenReturn($this->form);
$this->subForm = Phake::mock('Symfony\Component\Form\Form');
Phake::when($this->form)->get(Phake::anyParameters())->thenReturn($this->subForm);
Phake::when($this->form)->getConfig()->thenReturn($this->formConfig);

$this->contentTypeId = 'contentTypeId';
Expand Down Expand Up @@ -130,7 +132,7 @@ public function testEventSubscribed()
{
$this->assertArrayHasKey(FormEvents::PRE_SET_DATA, $this->subscriber->getSubscribedEvents());
$this->assertArrayHasKey(FormEvents::POST_SET_DATA, $this->subscriber->getSubscribedEvents());
$this->assertArrayHasKey(FormEvents::PRE_SUBMIT, $this->subscriber->getSubscribedEvents());
$this->assertArrayHasKey(FormEvents::POST_SUBMIT, $this->subscriber->getSubscribedEvents());
}

/**
Expand Down Expand Up @@ -242,8 +244,9 @@ public function testSubmitWithExistingData()
'contentType' => $realContentTypeId,
'title' => $realValueArray,
);
Phake::when($this->event)->getData()->thenReturn($data);

Phake::when($this->event)->getData()->thenReturn($data);
Phake::when($this->subForm)->getData()->thenReturn($realValueArray);
Phake::when($this->form)->getData()->thenReturn($this->content);

$this->fieldCollection->add($this->fieldType1);
Expand All @@ -263,9 +266,10 @@ public function testSubmitWithExistingData()

Phake::when($this->content)->getAttributeByName($fieldId)->thenReturn($this->contentAttribute);

$this->subscriber->preSubmit($this->event);
$this->subscriber->postSubmit($this->event);

Phake::verify($this->form)->getData();
Phake::verify($this->subForm)->getData();
Phake::verify($this->repository)->findOneByContentTypeIdInLastVersion($realContentTypeId);
Phake::verify($this->content)->getAttributeByName($fieldId);
Phake::verify($this->contentAttribute)->setValue($realValueArray);
Expand Down Expand Up @@ -294,6 +298,7 @@ public function testSubmitWithNoExistingData()
Phake::when($this->event)->getData()->thenReturn($data);

Phake::when($this->form)->getData()->thenReturn($this->content);
Phake::when($this->subForm)->getData()->thenReturn($title);

$this->fieldCollection->add($this->fieldType1);
$fieldId = 'title';
Expand All @@ -303,9 +308,10 @@ public function testSubmitWithNoExistingData()

Phake::when($this->content)->getAttributeByName($fieldId)->thenReturn(null);

$this->subscriber->preSubmit($this->event);
$this->subscriber->postSubmit($this->event);

Phake::verify($this->form)->getData();
Phake::verify($this->subForm)->getData();
Phake::verify($this->repository)->findOneByContentTypeIdInLastVersion($realContentTypeId);
Phake::verify($this->content)->getAttributeByName($fieldId);
Phake::verify($this->content)->addAttribute(Phake::anyParameters());
Expand Down
Expand Up @@ -70,7 +70,7 @@ public function providerDifferentArray()
*/
public function testReverseTransformWithNullAndEmptyData($data)
{
$this->assertSame(array(), $this->transformer->reverseTransform($data));
$this->assertSame(null, $this->transformer->reverseTransform($data));
}

/**
Expand Down

0 comments on commit 15834b8

Please sign in to comment.