QueueSerializationTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Drupal\KernelTests\Core\Queue;
  3. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  4. use Drupal\Core\Form\FormInterface;
  5. use Drupal\Core\Form\FormState;
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\KernelTests\KernelTestBase;
  8. use Drupal\user\Entity\User;
  9. /**
  10. * Tests serializing a form with an injected DatabaseQueue instance.
  11. *
  12. * @group Queue
  13. */
  14. class QueueSerializationTest extends KernelTestBase implements FormInterface {
  15. use DependencySerializationTrait;
  16. /**
  17. * A queue instance.
  18. *
  19. * @var \Drupal\Core\Queue\DatabaseQueue
  20. */
  21. protected $queue;
  22. /**
  23. * Modules to enable.
  24. *
  25. * @var array
  26. */
  27. public static $modules = ['system', 'user', 'aggregator'];
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getFormId() {
  32. return 'queue_test_injection_form';
  33. }
  34. /**
  35. * Process callback.
  36. *
  37. * @param array $element
  38. * Form element
  39. *
  40. * @return array
  41. * Processed element.
  42. */
  43. public function process($element) {
  44. return $element;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function buildForm(array $form, FormStateInterface $form_state) {
  50. $form['#process'][] = [$this, 'process'];
  51. return $form;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function validateForm(array &$form, FormStateInterface $form_state) {}
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function submitForm(array &$form, FormStateInterface $form_state) {
  61. $form_state->setRebuild();
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function setUp() {
  67. parent::setUp();
  68. $this->installSchema('system', ['key_value_expire', 'sequences']);
  69. $this->installEntitySchema('user');
  70. $this->queue = \Drupal::service('queue.database')->get('aggregator_refresh');
  71. $test_user = User::create([
  72. 'name' => 'foobar',
  73. 'mail' => 'foobar@example.com',
  74. ]);
  75. $test_user->save();
  76. \Drupal::service('current_user')->setAccount($test_user);
  77. }
  78. /**
  79. * Tests queue injection serialization.
  80. */
  81. public function testQueueSerialization() {
  82. $form_state = new FormState();
  83. $form_state->setRequestMethod('POST');
  84. $form_state->setCached();
  85. $form_builder = $this->container->get('form_builder');
  86. $form_id = $form_builder->getFormId($this, $form_state);
  87. $form = $form_builder->retrieveForm($form_id, $form_state);
  88. $form_builder->prepareForm($form_id, $form, $form_state);
  89. $form_builder->processForm($form_id, $form, $form_state);
  90. }
  91. }