LockExtensionTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Admin\Extension;
  11. use Sonata\AdminBundle\Admin\Extension\LockExtension;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. use Symfony\Component\EventDispatcher\EventDispatcher;
  14. use Symfony\Component\Form\FormBuilder;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class LockExtensionTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var LockExtension
  22. */
  23. private $lockExtension;
  24. /**
  25. * @var EventDispatcherInterface
  26. */
  27. private $eventDispatcher;
  28. /**
  29. * @var AdminInterface
  30. */
  31. private $admin;
  32. /**
  33. * @var LockInterface
  34. */
  35. private $modelManager;
  36. /**
  37. * @var stdClass
  38. */
  39. private $object;
  40. /**
  41. * @var Request
  42. */
  43. private $request;
  44. protected function setUp()
  45. {
  46. $this->modelManager = $this->prophesize('Sonata\AdminBundle\Model\LockInterface');
  47. $this->admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
  48. $this->eventDispatcher = new EventDispatcher();
  49. $this->request = new Request();
  50. $this->object = new \stdClass();
  51. $this->lockExtension = new LockExtension();
  52. }
  53. public function testConfigureFormFields()
  54. {
  55. $formMapper = $this->configureFormMapper();
  56. $form = $this->configureForm();
  57. $this->configureAdmin(null, null, $this->modelManager->reveal());
  58. $event = new FormEvent($form->reveal(), array());
  59. $this->modelManager->getLockVersion(array())->willReturn(1);
  60. $form->add(
  61. '_lock_version',
  62. // NEXT_MAJOR: remove the check and add the FQCN
  63. method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
  64. ? 'Symfony\Component\Form\Extension\Core\Type\HiddenType'
  65. : 'hidden',
  66. array('mapped' => false, 'data' => 1)
  67. )->shouldBeCalled();
  68. $this->lockExtension->configureFormFields($formMapper);
  69. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  70. }
  71. public function testConfigureFormFieldsWhenModelManagerIsNotImplementingLockerInterface()
  72. {
  73. $modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
  74. $formMapper = $this->configureFormMapper();
  75. $form = $this->configureForm();
  76. $this->configureAdmin(null, null, $modelManager->reveal());
  77. $event = new FormEvent($form->reveal(), array());
  78. $form->add()->shouldNotBeCalled();
  79. $this->lockExtension->configureFormFields($formMapper);
  80. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  81. }
  82. public function testConfigureFormFieldsWhenFormEventHasNoData()
  83. {
  84. $formMapper = $this->configureFormMapper();
  85. $form = $this->configureForm();
  86. $event = new FormEvent($form->reveal(), null);
  87. $form->add()->shouldNotBeCalled();
  88. $this->lockExtension->configureFormFields($formMapper);
  89. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  90. }
  91. public function testConfigureFormFieldsWhenFormHasParent()
  92. {
  93. $formMapper = $this->configureFormMapper();
  94. $form = $this->configureForm();
  95. $event = new FormEvent($form->reveal(), array());
  96. $form->getParent()->willReturn('parent');
  97. $form->add()->shouldNotBeCalled();
  98. $this->lockExtension->configureFormFields($formMapper);
  99. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  100. }
  101. public function testConfigureFormFieldsWhenModelManagerHasNoLockedVersion()
  102. {
  103. $formMapper = $this->configureFormMapper();
  104. $form = $this->configureForm();
  105. $this->configureAdmin(null, null, $this->modelManager->reveal());
  106. $event = new FormEvent($form->reveal(), array());
  107. $this->modelManager->getLockVersion(array())->willReturn(null);
  108. $form->add()->shouldNotBeCalled();
  109. $this->lockExtension->configureFormFields($formMapper);
  110. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  111. }
  112. public function testPreUpdateIfAdminHasNoRequest()
  113. {
  114. $this->modelManager->lock()->shouldNotBeCalled();
  115. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  116. }
  117. public function testPreUpdateIfObjectIsNotVersioned()
  118. {
  119. $this->configureAdmin();
  120. $this->modelManager->lock()->shouldNotBeCalled();
  121. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  122. }
  123. public function testPreUpdateIfRequestDoesNotHaveLockVersion()
  124. {
  125. $uniqid = 'admin123';
  126. $this->configureAdmin($uniqid, $this->request);
  127. $this->modelManager->lock()->shouldNotBeCalled();
  128. $this->request->request->set($uniqid, array('something'));
  129. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  130. }
  131. public function testPreUpdateIfModelManagerIsNotImplementingLockerInterface()
  132. {
  133. $modelManager = $this->prophesize('Sonata\AdminBundle\Model\ModelManagerInterface');
  134. $uniqid = 'admin123';
  135. $this->configureAdmin($uniqid, $this->request, $modelManager->reveal());
  136. $this->request->request->set($uniqid, array('_lock_version' => 1));
  137. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  138. }
  139. public function testPreUpdateIfObjectIsVersioned()
  140. {
  141. $uniqid = 'admin123';
  142. $this->configureAdmin($uniqid, $this->request, $this->modelManager->reveal());
  143. $this->modelManager->lock($this->object, 1)->shouldBeCalled();
  144. $this->request->request->set($uniqid, array('_lock_version' => 1));
  145. $this->lockExtension->preUpdate($this->admin->reveal(), $this->object);
  146. }
  147. private function configureForm()
  148. {
  149. $form = $this->prophesize('Symfony\Component\Form\FormInterface');
  150. $form->getData()->willReturn($this->object);
  151. $form->getParent()->willReturn(null);
  152. return $form;
  153. }
  154. private function configureFormMapper()
  155. {
  156. $contractor = $this->prophesize('Sonata\AdminBundle\Builder\FormContractorInterface');
  157. $formFactory = $this->prophesize('Symfony\Component\Form\FormFactoryInterface');
  158. $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory->reveal());
  159. return new FormMapper($contractor->reveal(), $formBuilder, $this->admin->reveal());
  160. }
  161. private function configureAdmin($uniqid = null, $request = null, $modelManager = null)
  162. {
  163. $this->admin->getUniqid()->willReturn($uniqid);
  164. $this->admin->getRequest()->willReturn($request);
  165. $this->admin->hasRequest()->willReturn($request !== null);
  166. $this->admin->getModelManager()->willReturn($modelManager);
  167. }
  168. }