FormStateTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Form\FormStateTest.
  5. */
  6. namespace Drupal\Tests\Core\Form;
  7. use Drupal\Core\Form\FormInterface;
  8. use Drupal\Core\Form\FormState;
  9. use Drupal\Core\Form\FormStateInterface;
  10. use Drupal\Core\Url;
  11. use Drupal\Tests\UnitTestCase;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. /**
  14. * @coversDefaultClass \Drupal\Core\Form\FormState
  15. *
  16. * @group Form
  17. */
  18. class FormStateTest extends UnitTestCase {
  19. /**
  20. * Tests the getRedirect() method.
  21. *
  22. * @covers ::getRedirect
  23. *
  24. * @dataProvider providerTestGetRedirect
  25. */
  26. public function testGetRedirect($form_state_additions, $expected) {
  27. $form_state = (new FormState())->setFormState($form_state_additions);
  28. $redirect = $form_state->getRedirect();
  29. $this->assertEquals($expected, $redirect);
  30. }
  31. /**
  32. * Provides test data for testing the getRedirect() method.
  33. *
  34. * @return array
  35. * Returns some test data.
  36. */
  37. public function providerTestGetRedirect() {
  38. $data = [];
  39. $data[] = [[], NULL];
  40. $redirect = new RedirectResponse('/example');
  41. $data[] = [['redirect' => $redirect], $redirect];
  42. $data[] = [['redirect' => new Url('test_route_b', ['key' => 'value'])], new Url('test_route_b', ['key' => 'value'])];
  43. $data[] = [['programmed' => TRUE], NULL];
  44. $data[] = [['rebuild' => TRUE], NULL];
  45. $data[] = [['no_redirect' => TRUE], NULL];
  46. return $data;
  47. }
  48. /**
  49. * Tests the setError() method.
  50. *
  51. * @covers ::setError
  52. */
  53. public function testSetError() {
  54. $form_state = new FormState();
  55. $element['#parents'] = ['foo', 'bar'];
  56. $form_state->setError($element, 'Fail');
  57. $this->assertSame(['foo][bar' => 'Fail'], $form_state->getErrors());
  58. }
  59. /**
  60. * Tests the getError() method.
  61. *
  62. * @covers ::getError
  63. *
  64. * @dataProvider providerTestGetError
  65. */
  66. public function testGetError($errors, $parents, $error = NULL) {
  67. $element['#parents'] = $parents;
  68. $form_state = (new FormState())->setFormState([
  69. 'errors' => $errors,
  70. ]);
  71. $this->assertSame($error, $form_state->getError($element));
  72. }
  73. public function providerTestGetError() {
  74. return [
  75. [[], ['foo']],
  76. [['foo][bar' => 'Fail'], []],
  77. [['foo][bar' => 'Fail'], ['foo']],
  78. [['foo][bar' => 'Fail'], ['bar']],
  79. [['foo][bar' => 'Fail'], ['baz']],
  80. [['foo][bar' => 'Fail'], ['foo', 'bar'], 'Fail'],
  81. [['foo][bar' => 'Fail'], ['foo', 'bar', 'baz'], 'Fail'],
  82. [['foo][bar' => 'Fail 2'], ['foo']],
  83. [['foo' => 'Fail 1', 'foo][bar' => 'Fail 2'], ['foo'], 'Fail 1'],
  84. [['foo' => 'Fail 1', 'foo][bar' => 'Fail 2'], ['foo', 'bar'], 'Fail 1'],
  85. ];
  86. }
  87. /**
  88. * @covers ::setErrorByName
  89. *
  90. * @dataProvider providerTestSetErrorByName
  91. */
  92. public function testSetErrorByName($limit_validation_errors, $expected_errors) {
  93. $form_state = new FormState();
  94. $form_state->setLimitValidationErrors($limit_validation_errors);
  95. $form_state->clearErrors();
  96. $form_state->setErrorByName('test', 'Fail 1');
  97. $form_state->setErrorByName('test', 'Fail 2');
  98. $form_state->setErrorByName('options');
  99. $this->assertSame(!empty($expected_errors), $form_state::hasAnyErrors());
  100. $this->assertSame($expected_errors, $form_state->getErrors());
  101. }
  102. public function providerTestSetErrorByName() {
  103. return [
  104. // Only validate the 'options' element.
  105. [[['options']], ['options' => '']],
  106. // Do not limit an validation, and, ensuring the first error is returned
  107. // for the 'test' element.
  108. [NULL, ['test' => 'Fail 1', 'options' => '']],
  109. // Limit all validation.
  110. [[], []],
  111. ];
  112. }
  113. /**
  114. * Tests that form errors during submission throw an exception.
  115. *
  116. * @covers ::setErrorByName
  117. */
  118. public function testFormErrorsDuringSubmission() {
  119. $form_state = new FormState();
  120. $form_state->setValidationComplete();
  121. $this->expectException(\LogicException::class);
  122. $this->expectExceptionMessage('Form errors cannot be set after form validation has finished.');
  123. $form_state->setErrorByName('test', 'message');
  124. }
  125. /**
  126. * @covers ::prepareCallback
  127. */
  128. public function testPrepareCallbackValidMethod() {
  129. $form_state = new FormState();
  130. $form_state->setFormObject(new PrepareCallbackTestForm());
  131. $processed_callback = $form_state->prepareCallback('::buildForm');
  132. $this->assertEquals([$form_state->getFormObject(), 'buildForm'], $processed_callback);
  133. }
  134. /**
  135. * @covers ::prepareCallback
  136. */
  137. public function testPrepareCallbackInValidMethod() {
  138. $form_state = new FormState();
  139. $form_state->setFormObject(new PrepareCallbackTestForm());
  140. $processed_callback = $form_state->prepareCallback('not_a_method');
  141. // The callback was not changed as no such method exists.
  142. $this->assertEquals('not_a_method', $processed_callback);
  143. }
  144. /**
  145. * @covers ::prepareCallback
  146. */
  147. public function testPrepareCallbackArray() {
  148. $form_state = new FormState();
  149. $form_state->setFormObject(new PrepareCallbackTestForm());
  150. $callback = [$form_state->getFormObject(), 'buildForm'];
  151. $processed_callback = $form_state->prepareCallback($callback);
  152. $this->assertEquals($callback, $processed_callback);
  153. }
  154. /**
  155. * @covers ::loadInclude
  156. */
  157. public function testLoadInclude() {
  158. $type = 'some_type';
  159. $module = 'some_module';
  160. $name = 'some_name';
  161. $form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
  162. ->setMethods(['moduleLoadInclude'])
  163. ->getMock();
  164. $form_state->expects($this->once())
  165. ->method('moduleLoadInclude')
  166. ->with($module, $type, $name)
  167. ->willReturn(TRUE);
  168. $this->assertTrue($form_state->loadInclude($module, $type, $name));
  169. }
  170. /**
  171. * @covers ::loadInclude
  172. */
  173. public function testLoadIncludeNoName() {
  174. $type = 'some_type';
  175. $module = 'some_module';
  176. $form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
  177. ->setMethods(['moduleLoadInclude'])
  178. ->getMock();
  179. $form_state->expects($this->once())
  180. ->method('moduleLoadInclude')
  181. ->with($module, $type, $module)
  182. ->willReturn(TRUE);
  183. $this->assertTrue($form_state->loadInclude($module, $type));
  184. }
  185. /**
  186. * @covers ::loadInclude
  187. */
  188. public function testLoadIncludeNotFound() {
  189. $type = 'some_type';
  190. $module = 'some_module';
  191. $form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
  192. ->setMethods(['moduleLoadInclude'])
  193. ->getMock();
  194. $form_state->expects($this->once())
  195. ->method('moduleLoadInclude')
  196. ->with($module, $type, $module)
  197. ->willReturn(FALSE);
  198. $this->assertFalse($form_state->loadInclude($module, $type));
  199. }
  200. /**
  201. * @covers ::loadInclude
  202. */
  203. public function testLoadIncludeAlreadyLoaded() {
  204. $type = 'some_type';
  205. $module = 'some_module';
  206. $name = 'some_name';
  207. $form_state = $this->getMockBuilder('Drupal\Core\Form\FormState')
  208. ->setMethods(['moduleLoadInclude'])
  209. ->getMock();
  210. $form_state->addBuildInfo('files', [
  211. 'some_module:some_name.some_type' => [
  212. 'type' => $type,
  213. 'module' => $module,
  214. 'name' => $name,
  215. ],
  216. ]);
  217. $form_state->expects($this->never())
  218. ->method('moduleLoadInclude');
  219. $this->assertFalse($form_state->loadInclude($module, $type, $name));
  220. }
  221. /**
  222. * @covers ::isCached
  223. *
  224. * @dataProvider providerTestIsCached
  225. */
  226. public function testIsCached($cache_key, $no_cache_key, $expected) {
  227. $form_state = (new FormState())->setFormState([
  228. 'cache' => $cache_key,
  229. 'no_cache' => $no_cache_key,
  230. ]);
  231. $form_state->setMethod('POST');
  232. $this->assertSame($expected, $form_state->isCached());
  233. $form_state->setMethod('GET');
  234. $this->assertSame($expected, $form_state->isCached());
  235. }
  236. /**
  237. * Provides test data for testIsCached().
  238. */
  239. public function providerTestIsCached() {
  240. $data = [];
  241. $data[] = [
  242. TRUE,
  243. TRUE,
  244. FALSE,
  245. ];
  246. $data[] = [
  247. FALSE,
  248. TRUE,
  249. FALSE,
  250. ];
  251. $data[] = [
  252. FALSE,
  253. FALSE,
  254. FALSE,
  255. ];
  256. $data[] = [
  257. TRUE,
  258. FALSE,
  259. TRUE,
  260. ];
  261. $data[] = [
  262. TRUE,
  263. NULL,
  264. TRUE,
  265. ];
  266. $data[] = [
  267. FALSE,
  268. NULL,
  269. FALSE,
  270. ];
  271. return $data;
  272. }
  273. /**
  274. * @covers ::setCached
  275. */
  276. public function testSetCachedPost() {
  277. $form_state = new FormState();
  278. $form_state->setRequestMethod('POST');
  279. $form_state->setCached();
  280. $this->assertTrue($form_state->isCached());
  281. }
  282. /**
  283. * @covers ::setCached
  284. */
  285. public function testSetCachedGet() {
  286. $form_state = new FormState();
  287. $form_state->setRequestMethod('GET');
  288. $this->expectException(\LogicException::class);
  289. $this->expectExceptionMessage('Form state caching on GET requests is not allowed.');
  290. $form_state->setCached();
  291. }
  292. /**
  293. * @covers ::isMethodType
  294. * @covers ::setMethod
  295. *
  296. * @dataProvider providerTestIsMethodType
  297. */
  298. public function testIsMethodType($set_method_type, $input, $expected) {
  299. $form_state = (new FormState())
  300. ->setMethod($set_method_type);
  301. $this->assertSame($expected, $form_state->isMethodType($input));
  302. }
  303. /**
  304. * Provides test data for testIsMethodType().
  305. */
  306. public function providerTestIsMethodType() {
  307. $data = [];
  308. $data[] = [
  309. 'get',
  310. 'get',
  311. TRUE,
  312. ];
  313. $data[] = [
  314. 'get',
  315. 'GET',
  316. TRUE,
  317. ];
  318. $data[] = [
  319. 'GET',
  320. 'GET',
  321. TRUE,
  322. ];
  323. $data[] = [
  324. 'post',
  325. 'get',
  326. FALSE,
  327. ];
  328. return $data;
  329. }
  330. /**
  331. * @covers ::getTemporaryValue
  332. * @covers ::hasTemporaryValue
  333. * @covers ::setTemporaryValue
  334. */
  335. public function testTemporaryValue() {
  336. $form_state = new FormState();
  337. $this->assertFalse($form_state->hasTemporaryValue('rainbow_sparkles'));
  338. $form_state->setTemporaryValue('rainbow_sparkles', 'yes please');
  339. $this->assertSame($form_state->getTemporaryValue('rainbow_sparkles'), 'yes please');
  340. $this->assertTrue($form_state->hasTemporaryValue('rainbow_sparkles'), TRUE);
  341. $form_state->setTemporaryValue(['rainbow_sparkles', 'magic_ponies'], 'yes please');
  342. $this->assertSame($form_state->getTemporaryValue(['rainbow_sparkles', 'magic_ponies']), 'yes please');
  343. $this->assertTrue($form_state->hasTemporaryValue(['rainbow_sparkles', 'magic_ponies']), TRUE);
  344. }
  345. /**
  346. * @covers ::getCleanValueKeys
  347. */
  348. public function testGetCleanValueKeys() {
  349. $form_state = new FormState();
  350. $this->assertSame($form_state->getCleanValueKeys(), ['form_id', 'form_token', 'form_build_id', 'op']);
  351. }
  352. /**
  353. * @covers ::setCleanValueKeys
  354. */
  355. public function testSetCleanValueKeys() {
  356. $form_state = new FormState();
  357. $form_state->setCleanValueKeys(['key1', 'key2']);
  358. $this->assertSame($form_state->getCleanValueKeys(), ['key1', 'key2']);
  359. }
  360. /**
  361. * @covers ::addCleanValueKey
  362. */
  363. public function testAddCleanValueKey() {
  364. $form_state = new FormState();
  365. $form_state->setValue('value_to_clean', 'rainbow_sprinkles');
  366. $form_state->addCleanValueKey('value_to_clean');
  367. $this->assertSame($form_state->getCleanValueKeys(), ['form_id', 'form_token', 'form_build_id', 'op', 'value_to_clean']);
  368. return $form_state;
  369. }
  370. /**
  371. * @depends testAddCleanValueKey
  372. *
  373. * @covers ::cleanValues
  374. */
  375. public function testCleanValues($form_state) {
  376. $form_state->setValue('value_to_keep', 'magic_ponies');
  377. $this->assertSame($form_state->cleanValues()->getValues(), ['value_to_keep' => 'magic_ponies']);
  378. }
  379. /**
  380. * @covers ::setValues
  381. * @covers ::getValues
  382. */
  383. public function testGetValues() {
  384. $values = [
  385. 'foo' => 'bar',
  386. ];
  387. $form_state = new FormState();
  388. $form_state->setValues($values);
  389. $this->assertSame($values, $form_state->getValues());
  390. }
  391. }
  392. /**
  393. * A test form used for the prepareCallback() tests.
  394. */
  395. class PrepareCallbackTestForm implements FormInterface {
  396. public function getFormId() {
  397. return 'test_form';
  398. }
  399. public function buildForm(array $form, FormStateInterface $form_state) {}
  400. public function validateForm(array &$form, FormStateInterface $form_state) {}
  401. public function submitForm(array &$form, FormStateInterface $form_state) {}
  402. }