1d34f8a4592b868e5eb46f445cff6f6f21f7d363
[yaffs-website] / batch_test / src / Controller / BatchTestController.php
1 <?php
2
3 namespace Drupal\batch_test\Controller;
4
5 use Drupal\Core\Form\FormState;
6
7 /**
8  * Controller routines for batch tests.
9  */
10 class BatchTestController {
11
12   /**
13    * Redirects successfully.
14    *
15    * @return array
16    *   Render array containing success message.
17    */
18   public function testRedirect() {
19     return [
20       'success' => [
21         '#markup' => 'Redirection successful.',
22       ]
23     ];
24   }
25
26   /**
27    * Fires a batch process without a form submission.
28    *
29    * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
30    *   A redirect response if the batch is progressive. No return value otherwise.
31    */
32   public function testLargePercentage() {
33     batch_test_stack(NULL, TRUE);
34
35     batch_set(_batch_test_batch_5());
36     return batch_process('batch-test/redirect');
37   }
38
39   /**
40    * Submits a form within a batch programmatically.
41    *
42    * @param int $value
43    *   Some value passed to a custom batch callback.
44    *
45    * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
46    *   A redirect response if the batch is progressive. No return value otherwise.
47    */
48   public function testNestedDrupalFormSubmit($value = 1) {
49     // Set the batch and process it.
50     $batch['operations'] = [
51       ['_batch_test_nested_drupal_form_submit_callback', [$value]],
52     ];
53     batch_set($batch);
54     return batch_process('batch-test/redirect');
55   }
56
57   /**
58    * Fires a batch process without a form submission.
59    *
60    * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
61    *   A redirect response if the batch is progressive. No return value otherwise.
62    */
63   public function testNoForm() {
64     batch_test_stack(NULL, TRUE);
65
66     batch_set(_batch_test_batch_1());
67     return batch_process('batch-test/redirect');
68
69   }
70
71   /**
72    * Fires a batch process without a form submission and a finish redirect.
73    *
74    * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
75    *   A redirect response if the batch is progressive. No return value otherwise.
76    */
77   public function testFinishRedirect() {
78     batch_test_stack(NULL, TRUE);
79
80     $batch = _batch_test_batch_1();
81     $batch['finished'] = '_batch_test_finished_1_finished';
82     batch_set($batch);
83     return batch_process('batch-test/redirect');
84   }
85
86   /**
87    * Submits the 'Chained' form programmatically.
88    *
89    * Programmatic form: the page submits the 'Chained' form through
90    * \Drupal::formBuilder()->submitForm().
91    *
92    * @param int $value
93    *   Some value passed to a the chained form.
94    *
95    * @return array
96    *   Render array containing markup.
97    */
98   public function testProgrammatic($value = 1) {
99     $form_state = (new FormState())->setValues([
100       'value' => $value,
101     ]);
102     \Drupal::formBuilder()->submitForm('Drupal\batch_test\Form\BatchTestChainedForm', $form_state);
103     return [
104       'success' => [
105         '#markup' => 'Got out of a programmatic batched form.',
106       ]
107     ];
108   }
109
110   /**
111    * Runs a batch for testing theme used on the progress page.
112    *
113    * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
114    *   A redirect response if the batch is progressive. No return value otherwise.
115    */
116   public function testThemeBatch() {
117     batch_test_stack(NULL, TRUE);
118     $batch = [
119       'operations' => [
120         ['_batch_test_theme_callback', []],
121       ],
122     ];
123     batch_set($batch);
124     return batch_process('batch-test/redirect');
125   }
126
127   /**
128    * Runs a batch for testing the title shown on the progress page.
129    *
130    * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
131    *   A redirect response if the batch is progressive. No return value otherwise.
132    */
133   public function testTitleBatch() {
134     batch_test_stack(NULL, TRUE);
135     $batch = [
136       'title' => 'Batch Test',
137       'operations' => [
138         ['_batch_test_title_callback', []],
139       ],
140     ];
141     batch_set($batch);
142     return batch_process('batch-test/redirect');
143   }
144
145 }