Skip to content

Commit

Permalink
fix check of form fields because it didn't check correctly (sonata-pr…
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 authored and OskarStark committed Aug 17, 2018
1 parent 34a17fe commit a2ed503
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 90 deletions.
3 changes: 2 additions & 1 deletion src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Csrf\CsrfToken;
use function is_array;

// BC for Symfony < 3.3 where this trait does not exist
// NEXT_MAJOR: Remove the polyfill and inherit from \Symfony\Bundle\FrameworkBundle\Controller\Controller again
Expand Down Expand Up @@ -331,7 +332,7 @@ public function editAction($id = null)
$this->admin->setSubject($existingObject);
$objectId = $this->admin->getNormalizedIdentifier($existingObject);

if (!$this->admin->getFormTabs()) {
if (!is_array($fields = $this->admin->getForm()->all()) || 0 === count($fields)) {
throw new \RuntimeException(
'No editable field defined. Did you forget to implement the "configureFormFields" method?'
);
Expand Down
152 changes: 63 additions & 89 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1444,9 +1444,15 @@ public function testEditActionRuntimeException()
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(false));
->method('getForm')
->will($this->returnValue($form));

$form->expects($this->once())
->method('all')
->willReturn([]);

$this->controller->editAction(null, $this->request);
}
Expand Down Expand Up @@ -1502,15 +1508,9 @@ public function testEditAction()
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

Expand All @@ -1520,6 +1520,10 @@ public function testEditAction()
->method('createView')
->will($this->returnValue($formView));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->assertInstanceOf(Response::class, $this->controller->editAction(null, $this->request));

$this->assertSame($this->admin, $this->parameters['admin']);
Expand Down Expand Up @@ -1552,10 +1556,6 @@ public function testEditActionSuccess($expectedToStringValue, $toStringValue)
->method('checkAccess')
->with($this->equalTo('edit'));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));

$this->admin->expects($this->once())
->method('hasRoute')
->with($this->equalTo('edit'))
Expand All @@ -1566,15 +1566,13 @@ public function testEditActionSuccess($expectedToStringValue, $toStringValue)
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$form->expects($this->once())
->method('getData')
->will($this->returnValue($object));

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

Expand All @@ -1586,6 +1584,10 @@ public function testEditActionSuccess($expectedToStringValue, $toStringValue)
->method('isValid')
->will($this->returnValue(true));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->admin->expects($this->once())
->method('toString')
->with($this->equalTo($object))
Expand Down Expand Up @@ -1618,15 +1620,9 @@ public function testEditActionError($expectedToStringValue, $toStringValue)
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

Expand All @@ -1638,6 +1634,10 @@ public function testEditActionError($expectedToStringValue, $toStringValue)
->method('isValid')
->will($this->returnValue(false));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->admin->expects($this->once())
->method('toString')
->with($this->equalTo($object))
Expand Down Expand Up @@ -1684,15 +1684,9 @@ public function testEditActionAjaxSuccess()
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));
$form = $this->createMock(Form::class);

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

Expand All @@ -1708,6 +1702,10 @@ public function testEditActionAjaxSuccess()
->method('getData')
->will($this->returnValue($object));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->admin->expects($this->once())
->method('getNormalizedIdentifier')
->with($this->equalTo($object))
Expand Down Expand Up @@ -1740,15 +1738,9 @@ public function testEditActionAjaxError()
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));
$form = $this->createMock(Form::class);

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

Expand All @@ -1760,6 +1752,10 @@ public function testEditActionAjaxError()
->method('isValid')
->will($this->returnValue(false));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->request->setMethod('POST');
$this->request->headers->set('X-Requested-With', 'XMLHttpRequest');

Expand Down Expand Up @@ -1803,15 +1799,9 @@ public function testEditActionWithModelManagerException($expectedToStringValue,
->method('getClass')
->will($this->returnValue('stdClass'));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

Expand All @@ -1823,6 +1813,10 @@ public function testEditActionWithModelManagerException($expectedToStringValue,
->method('getData')
->will($this->returnValue($object));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->admin->expects($this->once())
->method('toString')
->with($this->equalTo($object))
Expand Down Expand Up @@ -1869,22 +1863,16 @@ public function testEditActionWithPreview()
->with($this->equalTo('edit'))
->will($this->returnValue(true));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
$this->admin->expects($this->exactly(2))
->method('getForm')
->will($this->returnValue($form));

$this->admin->expects($this->once())
->method('supportsPreviewMode')
->will($this->returnValue(true));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));

$formView = $this->createMock(FormView::class);

$form->expects($this->any())
Expand All @@ -1899,6 +1887,10 @@ public function testEditActionWithPreview()
->method('isValid')
->will($this->returnValue(true));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->request->setMethod('POST');
$this->request->request->set('btn_preview', 'Preview');

Expand Down Expand Up @@ -1934,13 +1926,7 @@ public function testEditActionWithLockException()
->method('getClass')
->will($this->returnValue($class));

$this->admin->expects($this->once())
->method('getFormTabs')
->will($this->returnValue(true));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$form->expects($this->any())
->method('isValid')
Expand All @@ -1950,6 +1936,10 @@ public function testEditActionWithLockException()
->method('getData')
->will($this->returnValue($object));

$form->expects($this->once())
->method('all')
->willReturn(['field' => 'fielddata']);

$this->admin->expects($this->any())
->method('getForm')
->will($this->returnValue($form));
Expand Down Expand Up @@ -2038,9 +2028,7 @@ public function testCreateAction()
->method('getNewInstance')
->will($this->returnValue($object));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
->method('getForm')
Expand Down Expand Up @@ -2109,9 +2097,7 @@ public function testCreateActionSuccess($expectedToStringValue, $toStringValue)
->method('create')
->will($this->returnArgument(0));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->any())
->method('getClass')
Expand Down Expand Up @@ -2172,9 +2158,7 @@ public function testCreateActionAccessDenied2()
->method('getNewInstance')
->will($this->returnValue($object));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->any())
->method('getClass')
Expand Down Expand Up @@ -2221,9 +2205,7 @@ public function testCreateActionError($expectedToStringValue, $toStringValue)
->method('getNewInstance')
->will($this->returnValue($object));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
->method('getForm')
Expand Down Expand Up @@ -2286,9 +2268,7 @@ public function testCreateActionWithModelManagerException($expectedToStringValue
->method('getNewInstance')
->will($this->returnValue($object));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
->method('getForm')
Expand Down Expand Up @@ -2363,9 +2343,7 @@ public function testCreateActionAjaxSuccess()
->method('create')
->will($this->returnArgument(0));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->once())
->method('getForm')
Expand Down Expand Up @@ -2415,9 +2393,7 @@ public function testCreateActionAjaxError()
->method('getNewInstance')
->will($this->returnValue($object));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->any())
->method('getClass')
Expand Down Expand Up @@ -2471,9 +2447,7 @@ public function testCreateActionWithPreview()
->method('getNewInstance')
->will($this->returnValue($object));

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form = $this->createMock(Form::class);

$this->admin->expects($this->any())
->method('getClass')
Expand Down

0 comments on commit a2ed503

Please sign in to comment.