PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/src/Unit/Entity/PaymentStatus/PaymentStatusFormTest.php

https://gitlab.com/Drulenium-bot/payment
PHP | 310 lines | 198 code | 52 blank | 60 comment | 1 complexity | ac5882cac408e18481a840c03a15d2e0 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\payment\Unit\Entity\PaymentStatus {
  3. use Drupal\Core\Entity\EntityManagerInterface;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\Core\Form\FormState;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Language\LanguageInterface;
  8. use Drupal\payment\Entity\PaymentStatus\PaymentStatusForm;
  9. use Drupal\payment\Entity\PaymentStatusInterface;
  10. use Drupal\payment\Plugin\Payment\Status\PaymentStatusManagerInterface;
  11. use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface;
  12. use Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface;
  13. use Drupal\plugin\PluginType\PluginTypeInterface;
  14. use Drupal\plugin\PluginType\PluginTypeManagerInterface;
  15. use Drupal\Tests\UnitTestCase;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. /**
  18. * @coversDefaultClass \Drupal\payment\Entity\PaymentStatus\PaymentStatusForm
  19. *
  20. * @group Payment
  21. */
  22. class PaymentStatusFormTest extends UnitTestCase {
  23. /**
  24. * The payment status.
  25. *
  26. * @var \Drupal\payment\Entity\PaymentStatus|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $paymentStatus;
  29. /**
  30. * The payment method configuration manager.
  31. *
  32. * @var \Drupal\payment\Plugin\Payment\MethodConfiguration\PaymentMethodConfigurationManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $paymentStatusManager;
  35. /**
  36. * The payment status storage.
  37. *
  38. * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $paymentStatusStorage;
  41. /**
  42. * The plugin selector manager.
  43. *
  44. * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. protected $pluginSelectorManager;
  47. /**
  48. * The payment status plugin type.
  49. *
  50. * @var \Drupal\plugin\PluginType\PluginTypeInterface|\Prophecy\Prophecy\ObjectProphecy
  51. */
  52. protected $paymentStatusPluginType;
  53. /**
  54. * The string translator.
  55. *
  56. * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. protected $stringTranslation;
  59. /**
  60. * The form under test.
  61. *
  62. * @var \Drupal\payment\Entity\PaymentStatus\PaymentStatusForm
  63. */
  64. protected $sut;
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setUp() {
  69. $this->paymentStatusManager = $this->getMock(PaymentStatusManagerInterface::class);
  70. $this->paymentStatusStorage = $this->getMock(EntityStorageInterface::class);
  71. $this->paymentStatus = $this->getMock(PaymentStatusInterface::class);
  72. $this->pluginSelectorManager = $this->getMock(PluginSelectorManagerInterface::class);
  73. $this->stringTranslation = $this->getStringTranslationStub();
  74. $this->paymentStatusPluginType = $this->prophesize(PluginTypeInterface::class);
  75. $this->sut = new PaymentStatusForm($this->stringTranslation, $this->paymentStatusStorage, $this->pluginSelectorManager, $this->paymentStatusPluginType->reveal());
  76. $this->sut->setEntity($this->paymentStatus);
  77. }
  78. /**
  79. * @covers ::create
  80. * @covers ::__construct
  81. */
  82. function testCreate() {
  83. $entity_manager = $this->getMock(EntityManagerInterface::class);
  84. $entity_manager->expects($this->any())
  85. ->method('getStorage')
  86. ->with('payment_status')
  87. ->willReturn($this->paymentStatusStorage);
  88. $plugin_type_manager = $this->prophesize(PluginTypeManagerInterface::class);
  89. $plugin_type_manager->getPluginType('payment_status')->willReturn($this->paymentStatusPluginType);
  90. $container = $this->getMock(ContainerInterface::class);
  91. $map = array(
  92. array('entity.manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $entity_manager),
  93. array('plugin.manager.plugin.plugin_selector', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->pluginSelectorManager),
  94. array('plugin.plugin_type_manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $plugin_type_manager->reveal()),
  95. array('string_translation', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $this->stringTranslation),
  96. );
  97. $container->expects($this->any())
  98. ->method('get')
  99. ->willReturnMap($map);
  100. $form = PaymentStatusForm::create($container);
  101. $this->assertInstanceOf(PaymentStatusForm::class, $form);
  102. }
  103. /**
  104. * @covers ::form
  105. */
  106. public function testForm() {
  107. $label = $this->randomMachineName();
  108. $id = $this->randomMachineName();
  109. $is_new = FALSE;
  110. $parent_id = $this->randomMachineName();
  111. $description = $this->randomMachineName();
  112. $form_state = $this->getMock(FormStateInterface::class);
  113. $language = $this->getMock(LanguageInterface::class);
  114. $parent_selector_form = [
  115. '#foo' => $this->randomMachineName(),
  116. ];
  117. $parent_selector = $this->getMock(PluginSelectorInterface::class);
  118. $parent_selector->expects($this->atLeastOnce())
  119. ->method('buildSelectorForm')
  120. ->with([], $form_state)
  121. ->willReturn($parent_selector_form);
  122. $this->pluginSelectorManager->expects($this->atLeastOnce())
  123. ->method('createInstance')
  124. ->willReturn($parent_selector);
  125. $this->paymentStatus->expects($this->any())
  126. ->method('id')
  127. ->willReturn($id);
  128. $this->paymentStatus->expects($this->any())
  129. ->method('getDescription')
  130. ->willReturn($description);
  131. $this->paymentStatus->expects($this->any())
  132. ->method('getParentId')
  133. ->willReturn($parent_id);
  134. $this->paymentStatus->expects($this->any())
  135. ->method('isNew')
  136. ->willReturn($is_new);
  137. $this->paymentStatus->expects($this->any())
  138. ->method('label')
  139. ->willReturn($label);
  140. $this->paymentStatus->expects($this->any())
  141. ->method('language')
  142. ->willReturn($language);
  143. $build = $this->sut->form([], $form_state);
  144. unset($build['#process']);
  145. unset($build['langcode']);
  146. $expected_build['label'] = [
  147. '#type' => 'textfield',
  148. '#default_value' => $label,
  149. '#maxlength' => 255,
  150. '#required' => TRUE,
  151. ];
  152. unset($build['label']['#title']);
  153. $expected_build['id'] = [
  154. '#default_value' => $id,
  155. '#disabled' => !$is_new,
  156. '#machine_name' => array(
  157. 'source' => array('label'),
  158. 'exists' => array($this->sut, 'PaymentStatusIdExists'),
  159. ),
  160. '#maxlength' => 255,
  161. '#type' => 'machine_name',
  162. '#required' => TRUE,
  163. ];
  164. unset($build['id']['#title']);
  165. $expected_build['parent_id'] = $parent_selector_form;
  166. $expected_build['description'] = [
  167. '#type' => 'textarea',
  168. '#default_value' => $description,
  169. '#maxlength' => 255,
  170. ];
  171. unset($build['description']['#title']);
  172. $expected_build['#after_build'] = ['::afterBuild'];
  173. $this->assertSame($expected_build, $build);
  174. }
  175. /**
  176. * @covers ::copyFormValuesToEntity
  177. */
  178. public function testCopyFormValuesToEntity() {
  179. $description = $this->randomMachineName();
  180. $id = $this->randomMachineName();
  181. $label = $this->randomMachineName();
  182. $parent_id = $this->randomMachineName();
  183. $this->paymentStatus->expects($this->once())
  184. ->method('setDescription')
  185. ->with($description);
  186. $this->paymentStatus->expects($this->once())
  187. ->method('setId')
  188. ->with($id);
  189. $this->paymentStatus->expects($this->once())
  190. ->method('setLabel')
  191. ->with($label);
  192. $this->paymentStatus->expects($this->once())
  193. ->method('setParentId')
  194. ->with($parent_id);
  195. $parent_status = $this->getMock(PluginSelectorInterface::class);
  196. $parent_status->expects($this->atLeastOnce())
  197. ->method('getPluginId')
  198. ->willReturn($parent_id);
  199. $parent_selector = $this->getMock(PluginSelectorInterface::class);
  200. $parent_selector->expects($this->atLeastOnce())
  201. ->method('getSelectedPlugin')
  202. ->willReturn($parent_status);
  203. $this->pluginSelectorManager->expects($this->atLeastOnce())
  204. ->method('createInstance')
  205. ->willReturn($parent_selector);
  206. $form = [];
  207. $form_state = new FormState();
  208. $form_state->setValue('description', $description);
  209. $form_state->setValue('id', $id);
  210. $form_state->setValue('label', $label);
  211. $form_state->setValue('parent_id', $parent_id);
  212. $method = new \ReflectionMethod($this->sut, 'copyFormValuesToEntity');
  213. $method->setAccessible(TRUE);
  214. $method->invokeArgs($this->sut, array($this->paymentStatus, $form, $form_state));
  215. }
  216. /**
  217. * @covers ::paymentStatusIdExists
  218. */
  219. public function testPaymentStatusIdExists() {
  220. $method = new \ReflectionMethod($this->sut, 'paymentStatusIdExists');
  221. $method->setAccessible(TRUE);
  222. $payment_method_configuration_id = $this->randomMachineName();
  223. $this->paymentStatusStorage->expects($this->at(0))
  224. ->method('load')
  225. ->with($payment_method_configuration_id)
  226. ->willReturn($this->paymentStatus);
  227. $this->paymentStatusStorage->expects($this->at(1))
  228. ->method('load')
  229. ->with($payment_method_configuration_id)
  230. ->willReturn(NULL);
  231. $this->assertTrue($method->invoke($this->sut, $payment_method_configuration_id));
  232. $this->assertFalse($method->invoke($this->sut, $payment_method_configuration_id));
  233. }
  234. /**
  235. * @covers ::save
  236. */
  237. public function testSave() {
  238. $form_state = $this->getMock(FormStateInterface::class);
  239. $form_state->expects($this->once())
  240. ->method('setRedirect')
  241. ->with('entity.payment_status.collection');
  242. /** @var \Drupal\payment\Entity\PaymentStatus\PaymentStatusForm|\PHPUnit_Framework_MockObject_MockObject $form */
  243. $form = $this->getMockBuilder(PaymentStatusForm::class)
  244. ->setConstructorArgs(array($this->stringTranslation, $this->paymentStatusStorage, $this->pluginSelectorManager, $this->paymentStatusPluginType->reveal()))
  245. ->setMethods(array('copyFormValuesToEntity'))
  246. ->getMock();
  247. $form->setEntity($this->paymentStatus);
  248. $this->paymentStatus->expects($this->once())
  249. ->method('save');
  250. $form->save([], $form_state);
  251. }
  252. }
  253. }
  254. namespace {
  255. if (!function_exists('drupal_set_message')) {
  256. function drupal_set_message() {}
  257. }
  258. }