LockExtensionTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\Form\FormBuilder;
  16. use Symfony\Component\Form\FormEvent;
  17. use Symfony\Component\Form\FormEvents;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class LockExtensionTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * @var LockExtension
  23. */
  24. private $lockExtension;
  25. /**
  26. * @var EventDispatcherInterface
  27. */
  28. private $eventDispatcher;
  29. /**
  30. * @var AdminInterface
  31. */
  32. private $admin;
  33. /**
  34. * @var LockInterface
  35. */
  36. private $modelManager;
  37. /**
  38. * @var stdClass
  39. */
  40. private $object;
  41. /**
  42. * @var Request
  43. */
  44. private $request;
  45. public function setUp()
  46. {
  47. $this->eventDispatcher = new EventDispatcher();
  48. $this->request = new Request();
  49. $this->object = new \stdClass();
  50. $this->lockExtension = new LockExtension();
  51. $this->modelManager = $this->getMock('Sonata\AdminBundle\Model\LockInterface');
  52. $this->admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. }
  56. public function testConfigureFormFields()
  57. {
  58. $formMapper = $this->configureFormMapper();
  59. $form = $this->configureForm();
  60. $event = new FormEvent($form, array());
  61. $this->modelManager->expects($this->once())
  62. ->method('getLockVersion')
  63. ->will($this->returnValue(1));
  64. $this->admin->expects($this->once())
  65. ->method('getModelManager')
  66. ->will($this->returnValue($this->modelManager));
  67. $form->expects($this->once())
  68. ->method('add')
  69. ->with(
  70. $this->equalTo('_lock_version'),
  71. $this->equalTo('hidden'),
  72. $this->equalTo(array(
  73. 'mapped' => false,
  74. 'data' => 1,
  75. ))
  76. );
  77. $this->lockExtension->configureFormFields($formMapper);
  78. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  79. }
  80. public function testConfigureFormFieldsWhenModelManagerIsNotImplementingLockerInterface()
  81. {
  82. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  83. $formMapper = $this->configureFormMapper();
  84. $form = $this->configureForm();
  85. $event = new FormEvent($form, array());
  86. $this->admin->expects($this->any())
  87. ->method('getModelManager')
  88. ->will($this->returnValue($modelManager));
  89. $form->expects($this->never())
  90. ->method('add');
  91. $this->lockExtension->configureFormFields($formMapper);
  92. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  93. }
  94. public function testConfigureFormFieldsWhenFormEventHasNoData()
  95. {
  96. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  97. $formMapper = $this->configureFormMapper();
  98. $form = $this->configureForm();
  99. $event = new FormEvent($form, null);
  100. $form->expects($this->never())
  101. ->method('add');
  102. $this->lockExtension->configureFormFields($formMapper);
  103. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  104. }
  105. public function testConfigureFormFieldsWhenFormHasParent()
  106. {
  107. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  108. $formMapper = $this->configureFormMapper();
  109. $form = $this->configureForm();
  110. $event = new FormEvent($form, array());
  111. $form->expects($this->once())
  112. ->method('getParent')
  113. ->will($this->returnValue('parent'));
  114. $form->expects($this->never())
  115. ->method('add');
  116. $this->lockExtension->configureFormFields($formMapper);
  117. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  118. }
  119. public function testConfigureFormFieldsWhenModelManagerHasNoLockedVersion()
  120. {
  121. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  122. $formMapper = $this->configureFormMapper();
  123. $form = $this->configureForm();
  124. $event = new FormEvent($form, array());
  125. $this->admin->expects($this->any())
  126. ->method('getModelManager')
  127. ->will($this->returnValue($this->modelManager));
  128. $this->modelManager->expects($this->once())
  129. ->method('getLockVersion')
  130. ->will($this->returnValue(null));
  131. $form->expects($this->never())
  132. ->method('add');
  133. $this->lockExtension->configureFormFields($formMapper);
  134. $this->eventDispatcher->dispatch(FormEvents::PRE_SET_DATA, $event);
  135. }
  136. public function testPreUpdateIfAdminHasNoRequest()
  137. {
  138. $this->modelManager->expects($this->never())
  139. ->method('lock');
  140. $this->lockExtension->preUpdate($this->admin, $this->object);
  141. }
  142. public function testPreUpdateIfObjectIsNotVersioned()
  143. {
  144. $this->configureAdminRequest();
  145. $this->modelManager->expects($this->never())
  146. ->method('lock');
  147. $this->lockExtension->preUpdate($this->admin, $this->object);
  148. }
  149. public function testPreUpdateIfRequestDoesNotHaveLockVersion()
  150. {
  151. $uniqid = 'admin123';
  152. $this->admin->expects($this->any())
  153. ->method('getUniqId')
  154. ->will($this->returnValue($uniqid));
  155. $this->configureAdminRequest();
  156. $this->request->request->set($uniqid, array('something'));
  157. $this->modelManager->expects($this->never())
  158. ->method('lock');
  159. $this->lockExtension->preUpdate($this->admin, $this->object);
  160. }
  161. public function testPreUpdateIfModelManagerIsNotImplementingLockerInterface()
  162. {
  163. $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
  164. $uniqid = 'admin123';
  165. $this->admin->expects($this->any())
  166. ->method('getUniqId')
  167. ->will($this->returnValue($uniqid));
  168. $this->configureAdminRequest();
  169. $this->request->request->set($uniqid, array('_lock_version' => 1));
  170. $this->admin->expects($this->any())
  171. ->method('getModelManager')
  172. ->will($this->returnValue($modelManager));
  173. $this->modelManager->expects($this->never())
  174. ->method('lock');
  175. $this->lockExtension->preUpdate($this->admin, $this->object);
  176. }
  177. public function testPreUpdateIfObjectIsVersioned()
  178. {
  179. $uniqid = 'admin123';
  180. $this->modelManager->expects($this->once())
  181. ->method('lock')
  182. ->with($this->object, 1);
  183. $this->configureAdminRequest();
  184. $this->request->request->set($uniqid, array('_lock_version' => 1));
  185. $this->admin->expects($this->any())
  186. ->method('getModelManager')
  187. ->will($this->returnValue($this->modelManager));
  188. $this->admin->expects($this->any())
  189. ->method('getUniqId')
  190. ->will($this->returnValue($uniqid));
  191. $this->lockExtension->preUpdate($this->admin, $this->object);
  192. }
  193. private function configureForm()
  194. {
  195. $form = $this->getMock('Symfony\Component\Form\FormInterface');
  196. $form->expects($this->any())
  197. ->method('getData')
  198. ->will($this->returnValue($this->object));
  199. return $form;
  200. }
  201. private function configureFormMapper()
  202. {
  203. $contractor = $this->getMock('Sonata\AdminBundle\Builder\FormContractorInterface');
  204. $formFactory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  205. $formBuilder = new FormBuilder('form', null, $this->eventDispatcher, $formFactory);
  206. $formMapper = new FormMapper($contractor, $formBuilder, $this->admin);
  207. return $formMapper;
  208. }
  209. private function configureAdminRequest()
  210. {
  211. $this->admin->expects($this->once())
  212. ->method('getRequest')
  213. ->will($this->returnValue($this->request));
  214. $this->admin->expects($this->once())
  215. ->method('hasRequest')
  216. ->will($this->returnValue(true));
  217. }
  218. }