浏览代码

CRUDControllerTest: added test for editAction

Andrej Hudec 11 年之前
父节点
当前提交
f64078ea74
共有 5 个文件被更改,包括 264 次插入12 次删除
  1. 1 5
      Admin/Admin.php
  2. 9 0
      Admin/AdminInterface.php
  3. 6 0
      CHANGELOG.md
  4. 2 1
      Controller/CRUDController.php
  5. 246 6
      Tests/Controller/CRUDControllerTest.php

+ 1 - 5
Admin/Admin.php

@@ -2555,11 +2555,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * Returning true will enable preview mode for
-     * the target entity and show a preview button
-     * when editing/creating an entity
-     *
-     * @return boolean
+     * {@inheritdoc}
      */
     public function supportsPreviewMode()
     {

+ 9 - 0
Admin/AdminInterface.php

@@ -517,6 +517,15 @@ interface AdminInterface
      */
     public function getLabelTranslatorStrategy();
 
+    /**
+     * Returning true will enable preview mode for
+     * the target entity and show a preview button
+     * when editing/creating an entity
+     *
+     * @return boolean
+     */
+    public function supportsPreviewMode();
+
     /**
      * add an Admin child to the current one
      *

+ 6 - 0
CHANGELOG.md

@@ -1,6 +1,12 @@
 CHANGELOG
 =========
 
+### 2013-10-01
+
+* [BC BREAK] added ``supportsPreviewMode`` to the AdminInterface
+  If you do not extend the Admin class, you need to add this method to
+  your admin.
+
 ### 2013-09-30
 
 * [BC BREAK] added ``getFilterParameters`` to the AdminInterface

+ 2 - 1
Controller/CRUDController.php

@@ -305,7 +305,6 @@ class CRUDController extends Controller
             // persist if the form was valid and if in preview mode the preview was approved
             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
                 $this->admin->update($object);
-                $this->addFlash('sonata_flash_success', 'flash_edit_success');
 
                 if ($this->isXmlHttpRequest()) {
                     return $this->renderJson(array(
@@ -314,6 +313,8 @@ class CRUDController extends Controller
                     ));
                 }
 
+                $this->addFlash('sonata_flash_success', 'flash_edit_success');
+
                 // redirect to edit mode
                 return $this->redirectTo($object);
             }

+ 246 - 6
Tests/Controller/CRUDControllerTest.php

@@ -148,6 +148,8 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                             return 'SonataAdminBundle::standard_layout.html.twig';
                         case 'show':
                             return 'SonataAdminBundle:CRUD:show.html.twig';
+                        case 'edit':
+                            return 'SonataAdminBundle:CRUD:edit.html.twig';
                     }
 
                     return null;
@@ -168,6 +170,17 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
                     return $result;
                 }));
 
+        $this->admin->expects($this->any())
+            ->method('generateObjectUrl')
+            ->will($this->returnCallback(function($name, $object, array $parameters = array(), $absolute = false) {
+                    $result = get_class($object).'_'.$name;
+                    if (!empty($parameters)) {
+                        $result .= '?'.http_build_query($parameters);
+                    }
+
+                    return $result;
+                }));
+
         $this->controller = new CRUDController();
         $this->controller->setContainer($container);
     }
@@ -453,12 +466,6 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
      */
     public function testRedirectTo($expected, $queryParams, $hasActiveSubclass)
     {
-        $this->admin->expects($this->any())
-            ->method('generateObjectUrl')
-            ->will($this->returnCallback(function($name, $object, array $parameters = array(), $absolute = false) {
-                    return get_class($object).'_'.$name;
-                }));
-
         $this->admin->expects($this->any())
             ->method('hasActiveSubclass')
             ->will($this->returnValue($hasActiveSubclass));
@@ -563,6 +570,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
         $this->assertEquals(json_encode(array('result'=>'ok')), $response->getContent());
+        $this->assertEquals(array(), $this->session->getFlashBag()->all());
     }
 
     public function testDeleteActionAjaxError()
@@ -592,6 +600,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
         $this->assertEquals(json_encode(array('result'=>'error')), $response->getContent());
+        $this->assertEquals(array(), $this->session->getFlashBag()->all());
     }
 
     public function testDeleteActionSuccess()
@@ -643,4 +652,235 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->assertSame(array('flash_delete_error'), $this->session->getFlashBag()->get('sonata_flash_error'));
         $this->assertEquals('list', $response->getTargetUrl());
     }
+
+    public function testEditActionNotFoundException()
+    {
+        $this->setExpectedException('Symfony\Component\HttpKernel\Exception\NotFoundHttpException');
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue(false));
+
+        $this->controller->editAction();
+    }
+
+    public function testEditActionAccessDenied()
+    {
+        $this->setExpectedException('Symfony\Component\Security\Core\Exception\AccessDeniedException');
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue(new \stdClass()));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('EDIT'))
+            ->will($this->returnValue(false));
+
+        $this->controller->editAction();
+    }
+
+    public function testEditAction()
+    {
+        $object = new \stdClass();
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue($object));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('EDIT'))
+            ->will($this->returnValue(true));
+
+        $form = $this->getMockBuilder('Symfony\Component\Form\Form')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->admin->expects($this->once())
+            ->method('getForm')
+            ->will($this->returnValue($form));
+
+        $formView = $this->getMock('Symfony\Component\Form\FormView');
+
+        $form->expects($this->any())
+            ->method('createView')
+            ->will($this->returnValue($formView));
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
+
+        $this->assertEquals($this->admin, $this->parameters['admin']);
+        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
+        $this->assertEquals($this->pool, $this->parameters['admin_pool']);
+
+        $this->assertEquals('edit', $this->parameters['action']);
+        $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
+        $this->assertEquals($object, $this->parameters['object']);
+    }
+
+    public function testEditActionSuccess()
+    {
+        $object = new \stdClass();
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue($object));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('EDIT'))
+            ->will($this->returnValue(true));
+
+        $form = $this->getMockBuilder('Symfony\Component\Form\Form')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->admin->expects($this->once())
+            ->method('getForm')
+            ->will($this->returnValue($form));
+
+        $form->expects($this->once())
+            ->method('isValid')
+            ->will($this->returnValue(true));
+
+        $this->request->setMethod('POST');
+
+        $response = $this->controller->editAction();
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
+        $this->assertSame(array('flash_edit_success'), $this->session->getFlashBag()->get('sonata_flash_success'));
+        $this->assertEquals('stdClass_edit', $response->getTargetUrl());
+    }
+
+    public function testEditActionError()
+    {
+        $object = new \stdClass();
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue($object));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('EDIT'))
+            ->will($this->returnValue(true));
+
+        $form = $this->getMockBuilder('Symfony\Component\Form\Form')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->admin->expects($this->once())
+            ->method('getForm')
+            ->will($this->returnValue($form));
+
+        $form->expects($this->once())
+            ->method('isValid')
+            ->will($this->returnValue(false));
+
+        $this->request->setMethod('POST');
+
+        $formView = $this->getMock('Symfony\Component\Form\FormView');
+
+        $form->expects($this->any())
+            ->method('createView')
+            ->will($this->returnValue($formView));
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
+
+        $this->assertEquals($this->admin, $this->parameters['admin']);
+        $this->assertEquals('SonataAdminBundle::standard_layout.html.twig', $this->parameters['base_template']);
+        $this->assertEquals($this->pool, $this->parameters['admin_pool']);
+
+        $this->assertEquals('edit', $this->parameters['action']);
+        $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
+        $this->assertEquals($object, $this->parameters['object']);
+    }
+
+    public function testEditActionAjaxSuccess()
+    {
+        $object = new \stdClass();
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue($object));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('EDIT'))
+            ->will($this->returnValue(true));
+
+        $form = $this->getMockBuilder('Symfony\Component\Form\Form')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->admin->expects($this->once())
+            ->method('getForm')
+            ->will($this->returnValue($form));
+
+        $form->expects($this->once())
+            ->method('isValid')
+            ->will($this->returnValue(true));
+
+        $this->admin->expects($this->once())
+            ->method('getNormalizedIdentifier')
+            ->with($this->equalTo($object))
+            ->will($this->returnValue('foo_normalized'));
+
+        $this->request->setMethod('POST');
+        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
+
+        $response = $this->controller->editAction();
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response);
+        $this->assertEquals(json_encode(array('result'=>'ok', 'objectId'  => 'foo_normalized')), $response->getContent());
+        $this->assertEquals(array(), $this->session->getFlashBag()->all());
+    }
+
+    public function testEditActionAjaxError()
+    {
+        $object = new \stdClass();
+
+        $this->admin->expects($this->once())
+            ->method('getObject')
+            ->will($this->returnValue($object));
+
+        $this->admin->expects($this->once())
+            ->method('isGranted')
+            ->with($this->equalTo('EDIT'))
+            ->will($this->returnValue(true));
+
+        $form = $this->getMockBuilder('Symfony\Component\Form\Form')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->admin->expects($this->once())
+            ->method('getForm')
+            ->will($this->returnValue($form));
+
+        $form->expects($this->once())
+            ->method('isValid')
+            ->will($this->returnValue(false));
+
+        $this->request->setMethod('POST');
+        $this->request->headers->set('X-Requested-With', 'XMLHttpRequest');
+
+        $formView = $this->getMock('Symfony\Component\Form\FormView');
+
+        $form->expects($this->any())
+            ->method('createView')
+            ->will($this->returnValue($formView));
+
+        $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $this->controller->editAction());
+
+        $this->assertEquals($this->admin, $this->parameters['admin']);
+        $this->assertEquals('SonataAdminBundle::ajax_layout.html.twig', $this->parameters['base_template']);
+        $this->assertEquals($this->pool, $this->parameters['admin_pool']);
+
+        $this->assertEquals('edit', $this->parameters['action']);
+        $this->assertInstanceOf('Symfony\Component\Form\FormView', $this->parameters['form']);
+        $this->assertEquals($object, $this->parameters['object']);
+
+        $this->assertEquals(array(), $this->session->getFlashBag()->all());
+    }
+
 }